142
148
print("Creating guest {}".format(self.guest))
151
def wait_for_maas_completion(self):
153
Polls the virsh console to wait for the MAAS installation to
154
complete. Note: an alternative implementation could be to open a
155
small webserver/socket and let the MAAS cloud-init scripts do a
156
get/put request on this URI to indicate it is done. For now we'll
157
just 'keep it simple' and poll the output of the console.
162
Waiting for MAAS installation to complete.
163
This may take a few minutes, current cloud-init progress
164
is being saved in the maas-install.log file.
168
# Note: the virsh console screws with the current tty settings, so
169
# save those off in order to restore this.
170
stty_settings = subprocess.check_output(['stty', '-g']).strip()
173
with open('maas-install.log', 'w') as f:
174
cmd = ['virsh', 'console', self.guest]
175
process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
176
for c in iter(lambda: process.stdout.readline(), ''):
180
if c.find('MAAS controller is now configured') >= 0:
181
print("MAAS setup complete, terminating virsh console")
186
# Give some progress with some dots across the screen for
187
# every 20 lines of output, that way there's some visual
188
# indicator things are still working.
189
if (count % 20) == 0:
190
sys.stdout.write('.')
191
printed = printed + 1
193
if (printed % 80) == 0:
194
sys.stdout.write('\n')
195
except subprocess.CalledProcessError as e:
196
# An exception when process.terminate() is invoked because
197
# virsh console will return -1. A pty is required to
198
# gracefully exit, so we work around it
202
if stty_settings is not None:
204
subprocess.call(['stty', stty_settings])
209
def create_juju_guest(self):
211
Creates the Juju guest within Libvirt.
213
print("Creating the Juju Bootstrap virtual node...")
214
fname = 'juju-root.img'
215
virsh('vol-create-as', self.pool.name(), fname, '20G',
218
disk_tmpl = 'vol={pool}/{image},format={format},bus=virtio,io=native'
223
'--ram', str(self.memory),
224
'--vcpus', str(self.vcpus),
225
'--network', self.networks[0],
226
'--disk', disk_tmpl.format(pool=self.pool.name(), image=fname,
228
'--boot', 'network,hd,menu=off',
232
print("Creating juju guest")
145
235
def __call__(self):
146
236
self.create_cloud_image()
147
237
self.create_seed_image()
148
238
self.create_maas_guest()
240
# Wait for the cloud-init MAAS scripts to finish
241
self.wait_for_maas_completion()
243
# Create the juju guest
244
self.create_juju_guest()
151
247
if __name__ == '__main__':
152
248
parser = argparse.ArgumentParser(description="Creates a virtual MAAS deployment")
154
parser.add_argument('-n', '--network', type=str, action='append')
250
parser.add_argument('-n', '--network', type=str, action='append', required=True)
155
251
parser.add_argument('--disk-size', type=str, default='60G')
156
252
parser.add_argument('--vcpus', type=int, default=2)
157
253
parser.add_argument('--memory', type=str, default='2048')
254
parser.add_argument('--noprompt', type=bool, default=False)
158
255
args = parser.parse_args()
160
run('editor', 'config/meta-data')
257
if not args.noprompt:
258
run('editor', 'config/meta-data')
162
260
install = Installer(**args.__dict__)
b'\\ No newline at end of file'