summaryrefslogtreecommitdiff
path: root/trove.configure
blob: a1abae9773d96850ba2fc070fc68ec626e9a8fa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/bin/sh
#
# Copyright (C) 2013  Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# This is a "morph deploy" configuration extension to fully configure
# a Trove instance at deployment time. It uses the following variables
# from the environment (see MUSTARD for a description of them):
#
#   * TROVE_ID
#   * TROVE_COMPANY
#   * LORRY_SSH_KEY
#   * UPSTREAM_TROVE
#   * UPSTREAM_TROVE_USER
#   * UPSTREAM_TROVE_EMAIL
#   * TROVE_ADMIN_USER
#   * TROVE_ADMIN_EMAIL
#   * TROVE_ADMIN_NAME
#   * TROVE_ADMIN_SSH_PUBKEY
#
# The configuration of a Trove is slightly tricky: part of it has to
# be run on the configured system after it has booted. We accomplish
# this by copying in all the relevant data to the target system
# (in /var/lib/trove-setup), and creating a systemd unit file that
# runs on the first boot. The first boot will be detected by the
# existence of the /var/lib/trove-setup/needed file.

set -e

ROOT="$1"

##########################################################################
# Configuration in /etc, which we need to do on all deployments.
##########################################################################

echo "Set hostname to $TROVE_ID"
echo "$TROVE_ID" > "$ROOT/etc/hostname"

##########################################################################

lua_escape()
{
    echo -n "$1" | perl -pe 's/([-+\(\).%*?^$\[\]])/%$1/g'
}

echo "Creating /etc/trove-setup.sed"

cat <<EOF > "$ROOT"/etc/trove-setup.sed
s/##TROVE_HOSTNAME##/$TROVE_ID/g
s/##MASON_HOST##/$MASON_ID/g
s/##MASON_PORT##/18755/g
s/##TROVE_TITLE##/$TROVE_ID/g
s/##TROVE_COMPANY##/$TROVE_COMPANY/g
s/##TROVE_LOG_PREFIX##/$TROVE_ID/g
s/##ESC_PERSONAL_PREFIX##/people/g
s/##PREFIX##/$TROVE_ID/g
## The same prefix as above, only lua-pattern-escaped
s/##ESC_PREFIX##/$(lua_escape "$TROVE_ID")/g
EOF

##########################################################################

echo "Performing substitutions in /etc"

sed -f "$ROOT"/etc/trove-setup.sed -i \
    "$ROOT"/etc/cgitrc \
    "$ROOT"/etc/gitano-setup.clod \
    "$ROOT"/etc/lorry.conf \
    "$ROOT"/usr/share/gitano/skel/gitano-admin/*/*.lace \
    "$ROOT"/usr/share/gitano/skel/gitano-admin/*/*.lua \
    "$ROOT"/usr/share/gitano/skel/gitano-admin/users/*/user.conf

##########################################################################

# trove-early-setup needs "localhost" to be defined, and there's no
# guarantee it's going to be in DNS, or that external networking is
# up when trove-early-setup runs. We work around this by creating
# /etc/hosts with the right line.
echo "Add localhost to /etc/hosts"
cat <<EOF >> "$ROOT/etc/hosts"
127.0.0.1 localhost
EOF

##########################################################################
# Configuration of trove-early-setup
#
# We configure trove-early-setup so that it runs at first boot of an initial
# deployment, to do the parts of Trove system setup that require running
# commands from the deployed system.
##########################################################################

if [ "$UPGRADE" == "yes" ]; then
    echo "Not configuring trove-early-setup because this is an upgrade."
    exit 0
fi

echo "Create /var/lib/trove-setup"
install -d -o 0 -g 0 -m 0755 "$ROOT/var/lib/trove-setup"

echo "Create /etc/trove-setup.needed"
touch "$ROOT/etc/trove-setup.needed"
chown 0:0 "$ROOT/etc/trove-setup.needed"
chmod 0600 "$ROOT/etc/trove-setup.needed"

##########################################################################

# Put the lorry ssh keys onto the system. The trove-early-setup unit will
# put them into the right place for the lorry user upon first boot.
# We can't do that right now, because the lorry user won't exist until
# trove-early-setup has run.
echo "Copy Lorry ssh key to system"
install -m 0600 "$LORRY_SSH_KEY" "$ROOT/var/lib/trove-setup/lorry.key"
install -m 0644 "${LORRY_SSH_KEY}.pub" \
    "$ROOT/var/lib/trove-setup/lorry.key.pub"

##########################################################################

echo "Copy admin's ssh public key to system"
install -m 0644 "$TROVE_ADMIN_SSH_PUBKEY" \
    "$ROOT/var/lib/trove-setup/admin.key.pub"

##########################################################################

echo "Copy worker's ssh public key to system"
install -m 0644 "$WORKER_SSH_PUBKEY" \
    "$ROOT/var/lib/trove-setup/worker.key.pub"

##########################################################################

echo "Copy mason's ssh public key to system"
install -m 0644 "$MASON_SSH_PUBKEY" \
    "$ROOT/var/lib/trove-setup/mason.key.pub"

##########################################################################

if [ "x$MASON_DEFAULT_CI_HOSTS_FILE" = x ]; then
    echo "No default Mason hosts provided, using '[]'"
    printf '[\n]\n' >"$ROOT/var/lib/trove-setup/hosts.json.txt"
else
    echo "Copy default Mason host configuration to the System"
    install -m 0644 "$MASON_DEFAULT_CI_HOSTS_FILE" \
        "$ROOT/var/lib/trove-setup/hosts.json.txt"
fi

if [ "x$MASON_DEFAULT_CI_SYSTEMS_FILE" = x ]; then
    echo "No default Mason systems provided, using '[]'"
    printf '[\n]\n' >"$ROOT/var/lib/trove-setup/systems.json.txt"
else
    echo "Copy default Mason system configuration to the System"
    install -m 0644 "$MASON_DEFAULT_CI_SYSTEMS_FILE" \
        "$ROOT/var/lib/trove-setup/systems.json.txt"
fi

##########################################################################

echo "Create trove-early-setup unit file"
cat <<EOF > "$ROOT/etc/systemd/system/trove-early-setup.service"
[Unit]
Description=Run trove-early-setup (once)
Requires=network.target
After=network.target
Requires=opensshd.service
After=opensshd.service

# If there's a shared /var subvolume, it must be mounted before this
# unit runs.
Requires=local-fs.target
After=local-fs.target

ConditionPathExists=/etc/trove-setup.needed

# These must wait until we have created the required users on first boot.
# We reboot the machine after this unit completes so these lines are not
# strictly required, but it's nice to have a dependency graph that is true.
Before=lighttpd.service
Before=git-daemon.service


[Service]
Type=oneshot
ExecStart=/bin/sh -c 'ssh-keyscan localhost $UPSTREAM_TROVE> /etc/ssh/ssh_known_hosts'
ExecStart=/usr/bin/trove-early-setup
ExecStart=/usr/bin/install -m 0600 -o lorry -g lorry /var/lib/trove-setup/lorry.key /home/lorry/.ssh/id_rsa
ExecStart=/usr/bin/install -m 0644 -o lorry -g lorry /var/lib/trove-setup/lorry.key.pub /home/lorry/.ssh/id_rsa.pub
ExecStart=/bin/su git -c 'ssh git@localhost as lorry sshkey add configured < /var/lib/trove-setup/lorry.key.pub'
ExecStart=/bin/su git -c 'ssh git@localhost user add $TROVE_ADMIN_USER $TROVE_ADMIN_EMAIL $TROVE_ADMIN_NAME'
ExecStart=/bin/su git -c 'ssh git@localhost group adduser trove-admin $TROVE_ADMIN_USER'
ExecStart=/bin/su git -c 'ssh git@localhost as $TROVE_ADMIN_USER sshkey add default < /var/lib/trove-setup/admin.key.pub'
ExecStart=/bin/su git -c 'ssh git@localhost as distbuild sshkey add default < /var/lib/trove-setup/worker.key.pub'
ExecStart=/bin/su git -c 'ssh git@localhost as mason  sshkey add default < /var/lib/trove-setup/mason.key.pub'
ExecStart=/bin/mkdir -p /var/run/lighttpd/
ExecStart=/bin/chown cache:cache /var/run/lighttpd/
ExecStart=/bin/rm /etc/trove-setup.needed
ExecStart=/sbin/reboot
Restart=no
EOF

##########################################################################

ln -s "/etc/systemd/system/trove-early-setup.service" \
  "$ROOT/etc/systemd/system/multi-user.target.wants/trove-early-setup.service"