13
LDT = '/home/instance-manager/lava-deployment-tool/lava-deployment-tool'
17
return salt.client.LocalClient()
20
def info(client, instance):
22
Shows whether an instance of LAVA is running or not on its configured hosts.
24
cmd = 'status lava-instance LAVA_INSTANCE={0}'.format(instance)
25
inst_path = '/srv/lava/instances/{0}'.format(instance)
26
worker_file = '{0}/sbin/mount-masterfs'.format(inst_path)
30
ret = client.cmd('*', 'lava.list_instances', [])
31
for k, v in ret.iteritems():
33
ret = client.cmd(k, 'cmd.run', [cmd])
35
if ret[k] == 'status: Unknown instance: %s' % instance:
37
elif ret[k] == 'lava-instance (%s) start/running' % instance:
40
ret = client.cmd(k, 'file.file_exists', [worker_file])
43
inf[k] = {'running': running, 'master': master}
47
def stop(client, instance, just_workers=False):
49
Issues a command to stop a given instance name on all minions where the
50
LAVA instance appears to be running.
52
cmd = 'stop lava-instance LAVA_INSTANCE={0}'.format(instance)
55
for host, props in info(client, instance).iteritems():
56
if props['running'] != STOPPED:
57
if not just_workers or not props['master']:
61
return client.cmd(hosts, 'cmd.run', [cmd], expr_form='list')
64
def start(client, instance):
66
Issues a command to start a given instance name on all minions where the
67
LAVA instance appears to not be running.
69
cmd = 'start lava-instance LAVA_INSTANCE={0}'.format(instance)
72
for host, props in info(client, instance).iteritems():
73
if props['running'] != RUNNING:
77
return client.cmd(hosts, 'cmd.run', [cmd], expr_form='list')
80
def upgrade(client, instance, dry_run=True):
82
Runs lava-deployment-tool upgrade for a LAVA setup. It first shuts down
83
each worker node instance. Then it runs lava-deployment-tool upgrade on
84
the master. Lastly, it runs lava-deployment-tool upgradeworker on the
87
timeout = 300 # 5 minutes
91
for host, props in info(client, instance).iteritems():
93
assert not master, 'Detected multiple master instances in LAVA deployment'
98
assert master, 'No master instance found in LAVA deployment'
102
w_ret[h] = {'stop': 'dry-run', 'upgrade': 'dry-run', 'start': 'dry-run'}
105
m_ret = {master: 'dry-run: upgrade master'}
108
# first stop workers. This prevents a DB error if the upgrade changes
110
ret = stop(client, instance, True)
112
for host, msg in ret.iteritems():
113
w_ret[host]['stop'] = msg
115
# now upgrade the master node
116
cmd = 'SKIP_ROOT_CHECK=yes {0} upgrade {1}'.format(LDT, instance)
117
m_ret = client.cmd(master, 'cmd.run', [cmd], timeout=timeout)
119
# now upgrade the workers
120
cmd = 'SKIP_ROOT_CHECK=yes {0} upgradeworker {1}'.format(LDT, instance)
122
ret = client.cmd(workers, 'cmd.run', [cmd], timeout=timeout, expr_form='list')
123
for host, msg in ret.iteritems():
124
w_ret[host]['upgrade'] = msg
126
ret = start(client, instance)
128
for host, msg in ret.iteritems():
130
w_ret[host]['start'] = msg
132
# last thing: l-d-t ran as root, lets chmod things
133
cmd = 'chown -R instance-manager:instance-manager /srv/lava/instances/{0}/code/*'.format(instance)
134
client.cmd(workers + [master], 'cmd.run', [cmd], expr_form='list')
139
def _update_props(inifile_content, props):
140
for line in inifile_content.split('\n'):
141
if not line.strip().startswith('#'):
142
key, val = line.split('=')
144
props[key] = val.replace("'", '')
147
def add_worker(client, minion, minion_ip, instance, dry_run=True):
149
Creates a new lava workernode on a salt-minion.
153
'LAVA_SERVER_IP': None,
154
'LAVA_SYS_USER': None,
156
'masterdir': '/srv/lava/instances/{0}'.format(instance),
157
'workerip': minion_ip,
159
'instance': instance,
166
# ensure the instance exists and isn't already installed on the minion
168
for host, props in info(client, instance).iteritems():
170
assert not master, 'Detected multiple master instances in LAVA deployment'
172
assert minion != host, 'LAVA instance already deployed on minion'
174
assert master, 'No master instance found in LAVA deployment'
176
# determine settings needed by looking at master instance
177
cmd = 'cat {0}/instance.conf'.format(args['masterdir'])
178
ret = client.cmd(master, 'cmd.run', [cmd])
179
_update_props(ret[master], args)
181
# get the db information
182
cmd = 'cat {0}/etc/lava-server/default_database.conf'.format(args['masterdir'])
183
ret = client.cmd(master, 'cmd.run', [cmd])
184
_update_props(ret[master], args)
185
if not args['dbserver']:
186
args['dbserver'] = args['LAVA_SERVER_IP']
188
cmd = ('SKIP_ROOT_CHECK=yes '
189
'LAVA_DB_SERVER={dbserver} LAVA_DB_NAME={dbname} '
190
'LAVA_DB_USER={dbuser} LAVA_DB_PASSWORD={dbpass} '
191
'LAVA_REMOTE_FS_HOST={LAVA_SERVER_IP} '
192
'LAVA_REMOTE_FS_USER={LAVA_SYS_USER} LAVA_REMOTE_FS_DIR={masterdir} '
193
'LAVA_PROXY="{LAVA_PROXY}" LAVA_SERVER_IP={workerip} '
194
'{ldt} installworker -n {instance} 2>&1 | tee /tmp/ldt.log'.format(**args))
197
return {minion: 'dry-run: {0}'.format(cmd)}
199
ret = client.cmd(minion, 'cmd.run', [cmd], timeout=600)
201
# l-d-t ran as root, lets chmod things
202
cmd = 'chown -R instance-manager:instance-manager /srv/lava/instances/{0}/code/*'.format(instance)
203
client.cmd(minion, 'cmd.run', [cmd])
205
# now add the pubkey of the minion to the master's list of authorized keys
206
cmd = 'cat /srv/lava/instances/{0}/home/.ssh/id_rsa.pub'.format(instance)
207
pubkey = client.cmd(minion, 'cmd.run', [cmd])
208
pubkey = pubkey[minion].replace('ssh key used by LAVA for sshfs', minion)
209
authorized_keys = '{0}/home/.ssh/authorized_keys'.format(args['masterdir'])
210
client.cmd(master, 'file.append', [authorized_keys, pubkey])