Start of getting autoimage back into shape

This commit is contained in:
Ian Gulliver
2016-05-17 23:05:57 +00:00
parent 862dead4fe
commit 1f406af2ee
3 changed files with 85 additions and 90 deletions

View File

@@ -1,18 +1,20 @@
#!/usr/bin/python3 #!/usr/bin/python3
import argparse import argparse
import fetcher
import os import os
import shutil import shutil
import subprocess import subprocess
import sys import sys
import tempfile import tempfile
import time import time
import update_grub
parser = argparse.ArgumentParser(description='iconograph image') parser = argparse.ArgumentParser(description='iconograph image')
parser.add_argument( parser.add_argument(
'--base-url', '--server',
dest='base_url', dest='server',
action='store', action='store',
required=True) required=True)
parser.add_argument( parser.add_argument(
@@ -23,15 +25,23 @@ parser.add_argument(
parser.add_argument( parser.add_argument(
'--https-ca-cert', '--https-ca-cert',
dest='https_ca_cert', dest='https_ca_cert',
action='store') action='store',
required=True)
parser.add_argument( parser.add_argument(
'--https-client-cert', '--https-client-cert',
dest='https_client_cert', dest='https_client_cert',
action='store') action='store',
required=True)
parser.add_argument( parser.add_argument(
'--https-client-key', '--https-client-key',
dest='https_client_key', dest='https_client_key',
action='store') action='store',
required=True)
parser.add_argument(
'--image-type',
dest='image_type',
action='store',
required=True)
parser.add_argument( parser.add_argument(
'--device', '--device',
dest='device', dest='device',
@@ -48,24 +58,9 @@ FLAGS = parser.parse_args()
class Imager(object): class Imager(object):
def __init__(self, device, persistent_percent, base_url, ca_cert, https_ca_cert, https_client_cert, https_client_key): def __init__(self, device, persistent_percent):
self._device = device self._device = device
self._persistent_percent = persistent_percent self._persistent_percent = persistent_percent
self._fetcher_args = [
'--base-url', base_url,
'--ca-cert', ca_cert,
]
if https_ca_cert:
self._fetcher_args.extend([
'--https-ca-cert', https_ca_cert,
])
if https_client_cert and https_client_key:
self._fetcher_args.extend([
'--https-client-cert', https_client_cert,
'--https-client-key', https_client_key,
])
self._icon_path = os.path.dirname(sys.argv[0]) self._icon_path = os.path.dirname(sys.argv[0])
def _Exec(self, *args, **kwargs): def _Exec(self, *args, **kwargs):
@@ -138,37 +133,38 @@ class Imager(object):
'--boot-directory', root, '--boot-directory', root,
self._device) self._device)
def _GetFetcher(self, image_dir):
return fetcher.Fetcher(
'https://%s/image/%s' % (FLAGS.server, FLAGS.image_type),
FLAGS.ca_cert,
image_dir,
FLAGS.https_ca_cert,
FLAGS.https_client_cert,
FLAGS.https_client_key)
def _UpdateGrub(self, root, image_dir):
boot_dir = os.path.join(root, 'isodevice')
update = update_grub.GrubUpdater(
image_dir,
boot_dir)
update.Update()
def _FetchImages(self, root): def _FetchImages(self, root):
image_path = os.path.join(root, 'iconograph') image_dir = os.path.join(root, 'iconograph')
os.mkdir(image_path) os.mkdir(image_dir)
fetcher = os.path.join(self._icon_path, '..', 'client', 'fetcher.py') fetch = self._GetFetcher(image_dir)
fetch.Fetch()
self._Exec( return image_dir
fetcher,
'--image-dir', image_path,
*self._fetcher_args)
return image_path
def _CreateGrubCfg(self, root, image_path):
grub_cfg_path = os.path.join(root, 'grub', 'grub.cfg')
update_grub = os.path.join(self._icon_path, '..', 'client', 'update_grub.py')
with open(grub_cfg_path, 'w') as fh:
self._Exec(
update_grub,
'--image-dir', image_path,
'--boot-dir', root,
stdout=fh)
def _Image(self): def _Image(self):
self._PartitionAndMkFS() self._PartitionAndMkFS()
root = self._MountBoot() root = self._MountBoot()
self._InstallGrub(root) self._InstallGrub(root)
image_path = self._FetchImages(root) image_dir = self._FetchImages(root)
self._CreateGrubCfg(root, image_path) self._UpdateGrub(root, image_dir)
def Image(self): def Image(self):
self._umount = [] self._umount = []
@@ -185,12 +181,7 @@ class Imager(object):
def main(): def main():
imager = Imager( imager = Imager(
FLAGS.device, FLAGS.device,
FLAGS.persistent_percent, FLAGS.persistent_percent)
FLAGS.base_url,
FLAGS.ca_cert,
FLAGS.https_ca_cert,
FLAGS.https_client_cert,
FLAGS.https_client_key)
imager.Image() imager.Image()

View File

@@ -62,8 +62,7 @@ parser.add_argument(
parser.add_argument( parser.add_argument(
'--volume-id', '--volume-id',
dest='volume_id', dest='volume_id',
action='store', action='store')
required=True)
FLAGS = parser.parse_args() FLAGS = parser.parse_args()
@@ -235,8 +234,8 @@ class ImageBuilder(object):
'image_type': self._image_type, 'image_type': self._image_type,
'timestamp': timestamp, 'timestamp': timestamp,
} }
if FLAGS.volume_id: if self._volume_id:
info['volume_id'] = FLAGS.volume_id info['volume_id'] = self._volume_id
json.dump(info, fh, sort_keys=True, indent=4) json.dump(info, fh, sort_keys=True, indent=4)
fh.write('\n') fh.write('\n')

View File

@@ -8,11 +8,6 @@ from urllib import parse
parser = argparse.ArgumentParser(description='iconograph autoimage') parser = argparse.ArgumentParser(description='iconograph autoimage')
parser.add_argument(
'--base-url',
dest='base_url',
action='store',
required=True)
parser.add_argument( parser.add_argument(
'--ca-cert', '--ca-cert',
dest='ca_cert', dest='ca_cert',
@@ -31,21 +26,34 @@ parser.add_argument(
parser.add_argument( parser.add_argument(
'--https-ca-cert', '--https-ca-cert',
dest='https_ca_cert', dest='https_ca_cert',
action='store') action='store',
required=True)
parser.add_argument( parser.add_argument(
'--https-client-cert', '--https-client-cert',
dest='https_client_cert', dest='https_client_cert',
action='store') action='store',
required=True)
parser.add_argument( parser.add_argument(
'--https-client-key', '--https-client-key',
dest='https_client_key', dest='https_client_key',
action='store') action='store',
required=True)
parser.add_argument(
'--image-type',
dest='image_type',
action='store',
required=True)
parser.add_argument( parser.add_argument(
'--persistent-percent', '--persistent-percent',
dest='persistent_percent', dest='persistent_percent',
action='store', action='store',
type=int, type=int,
default=0) default=0)
parser.add_argument(
'--server',
dest='server',
action='store',
required=True)
FLAGS = parser.parse_args() FLAGS = parser.parse_args()
@@ -80,31 +88,27 @@ def main():
image_flags = [] image_flags = []
if FLAGS.https_ca_cert: https_ca_cert_path = os.path.join('icon', 'config', 'ca.www.cert.pem')
https_ca_cert_path = os.path.join('icon', 'config', 'ca.www.cert.pem') shutil.copyfile(
shutil.copyfile( FLAGS.https_ca_cert,
FLAGS.https_ca_cert, os.path.join(FLAGS.chroot_path, https_ca_cert_path))
os.path.join(FLAGS.chroot_path, https_ca_cert_path)) image_flags.extend([
image_flags.extend([ '--https-ca-cert', os.path.join('/', https_ca_cert_path),
'--https-ca-cert', os.path.join('/', https_ca_cert_path), ])
])
if FLAGS.https_client_cert and FLAGS.https_client_key: https_client_cert_path = os.path.join('icon', 'config', 'client.www.cert.pem')
https_client_cert_path = os.path.join('icon', 'config', 'client.www.cert.pem') shutil.copyfile(
shutil.copyfile( FLAGS.https_client_cert,
FLAGS.https_client_cert, os.path.join(FLAGS.chroot_path, https_client_cert_path))
os.path.join(FLAGS.chroot_path, https_client_cert_path)) https_client_key_path = os.path.join('icon', 'config', 'client.www.key.pem')
https_client_key_path = os.path.join('icon', 'config', 'client.www.key.pem') shutil.copyfile(
shutil.copyfile( FLAGS.https_client_key,
FLAGS.https_client_key, os.path.join(FLAGS.chroot_path, https_client_key_path))
os.path.join(FLAGS.chroot_path, https_client_key_path)) os.chmod(os.path.join(FLAGS.chroot_path, https_client_key_path), 0o400)
os.chmod(os.path.join(FLAGS.chroot_path, https_client_key_path), 0o400) image_flags.extend([
image_flags.extend([ '--https-client-cert', os.path.join('/', https_client_cert_path),
'--https-client-cert', os.path.join('/', https_client_cert_path), '--https-client-key', os.path.join('/', https_client_key_path),
'--https-client-key', os.path.join('/', https_client_key_path), ])
])
parsed = parse.urlparse(FLAGS.base_url)
init = os.path.join(FLAGS.chroot_path, 'etc', 'init', 'autoimage.conf') init = os.path.join(FLAGS.chroot_path, 'etc', 'init', 'autoimage.conf')
with open(init, 'w') as fh: with open(init, 'w') as fh:
@@ -118,7 +122,7 @@ script
chvt 8 chvt 8
/icon/iconograph/client/wait_for_service.py --host=%(host)s --service=%(service)s /icon/iconograph/client/wait_for_service.py --host=%(host)s --service=%(service)s
chvt 8 chvt 8
/icon/iconograph/imager/image.py --device=%(device)s --persistent-percent=%(persistent_percent)d --ca-cert=/icon/config/ca.image.cert.pem --base-url=%(base_url)s %(image_flags)s /icon/iconograph/imager/image.py --device=%(device)s --persistent-percent=%(persistent_percent)d --ca-cert=/icon/config/ca.image.cert.pem --server=%(server)s --image-type=%(image_type)s %(image_flags)s
chvt 8 chvt 8
echo echo
@@ -129,11 +133,12 @@ script
/icon/iconograph/client/alert.py --type=happy /icon/iconograph/client/alert.py --type=happy
end script end script
""" % { """ % {
'host': parsed.hostname, 'host': FLAGS.server,
'service': parsed.port or parsed.scheme, 'service': 'https',
'device': FLAGS.device, 'device': FLAGS.device,
'persistent_percent': FLAGS.persistent_percent, 'persistent_percent': FLAGS.persistent_percent,
'base_url': FLAGS.base_url, 'server': FLAGS.server,
'image_type': FLAGS.image_type,
'image_flags': ' '.join(image_flags), 'image_flags': ' '.join(image_flags),
}) })