From 668b9896c15a66f9673d84eec54e06ee54ac2cf0 Mon Sep 17 00:00:00 2001 From: Anders Ingemann Date: Sun, 1 Dec 2013 14:34:38 +0100 Subject: [PATCH] Modularize init script installation Don't install various init scripts when using cloud-init --- base/bootstrapinfo.py | 2 + common/tasks/initd.py | 73 ++++++++++++++++++-------------- plugins/packages/tasks.py | 4 +- providers/ec2/__init__.py | 4 +- providers/ec2/tasks/initd.py | 2 - providers/virtualbox/__init__.py | 2 +- 6 files changed, 50 insertions(+), 37 deletions(-) diff --git a/base/bootstrapinfo.py b/base/bootstrapinfo.py index ff425b9..404acb5 100644 --- a/base/bootstrapinfo.py +++ b/base/bootstrapinfo.py @@ -11,3 +11,5 @@ class BootstrapInformation(object): import os.path workspace_dirname = '{id:x}'.format(id=self.run_id) self.workspace = os.path.join(manifest.bootstrapper['workspace'], workspace_dirname) + + self.initd = {'install': {}, 'disable': []} diff --git a/common/tasks/initd.py b/common/tasks/initd.py index f002f77..7bd5a14 100644 --- a/common/tasks/initd.py +++ b/common/tasks/initd.py @@ -1,43 +1,13 @@ from base import Task from common import phases +from common.exceptions import TaskError from common.tools import log_check_call import os.path -class ResolveInitScripts(Task): - description = 'Determining which startup scripts to install or disable' - phase = phases.system_modification - - def run(self, info): - init_scripts = {} - init_scripts['expand-root'] = 'expand-root' - - from subprocess import CalledProcessError - try: - log_check_call(['/usr/sbin/chroot', info.root, - '/usr/bin/dpkg-query', '-W', 'openssh-server']) - init_scripts['generate-ssh-hostkeys'] = 'generate-ssh-hostkeys' - if info.manifest.system['release'] == 'squeeze': - init_scripts['generate-ssh-hostkeys'] = 'squeeze/generate-ssh-hostkeys' - except CalledProcessError: - pass - - disable_scripts = ['hwclock.sh'] - if info.manifest.system['release'] == 'squeeze': - disable_scripts.append('hwclockfirst.sh') - - init_scripts_dir = os.path.normpath(os.path.join(os.path.dirname(__file__), '../assets/init.d')) - for name, path in init_scripts.iteritems(): - init_scripts[name] = os.path.join(init_scripts_dir, path) - - info.initd = {'install': init_scripts, - 'disable': disable_scripts} - - class InstallInitScripts(Task): description = 'Installing startup scripts' phase = phases.system_modification - predecessors = [ResolveInitScripts] def run(self, info): import stat @@ -55,6 +25,47 @@ class InstallInitScripts(Task): log_check_call(['/usr/sbin/chroot', info.root, '/sbin/insserv', '--remove', name]) +class AddExpandRoot(Task): + description = 'Adding init script to expand the root volume' + phase = phases.system_modification + successors = [InstallInitScripts] + + def run(self, info): + init_scripts_dir = os.path.normpath(os.path.join(os.path.dirname(__file__), '../assets/init.d')) + info.initd['install']['expand-root'] = os.path.join(init_scripts_dir, 'expand-root') + + +class AddSSHKeyGeneration(Task): + description = 'Adding SSH private key generation init scripts' + phase = phases.system_modification + successors = [InstallInitScripts] + + def run(self, info): + init_scripts_dir = os.path.normpath(os.path.join(os.path.dirname(__file__), '../assets/init.d')) + install = info.initd['install'] + from subprocess import CalledProcessError + try: + log_check_call(['/usr/sbin/chroot', info.root, + '/usr/bin/dpkg-query', '-W', 'openssh-server']) + if info.manifest.system['release'] == 'squeeze': + install['generate-ssh-hostkeys'] = os.path.join(init_scripts_dir, 'squeeze/generate-ssh-hostkeys') + else: + install['generate-ssh-hostkeys'] = os.path.join(init_scripts_dir, 'generate-ssh-hostkeys') + except CalledProcessError: + pass + + +class RemoveHWClock(Task): + description = 'Removing hardware clock init scripts' + phase = phases.system_modification + successors = [InstallInitScripts] + + def run(self, info): + info.initd['disable'].append('hwclock.sh') + if info.manifest.system['release'] == 'squeeze': + info.initd['disable'].append('hwclockfirst.sh') + + class AdjustExpandRootScript(Task): description = 'Adjusting the expand-root script' phase = phases.system_modification diff --git a/plugins/packages/tasks.py b/plugins/packages/tasks.py index 7839217..b372545 100644 --- a/plugins/packages/tasks.py +++ b/plugins/packages/tasks.py @@ -15,10 +15,10 @@ class AptSources(Task): manifest_vars = {'release': info.manifest.system['release'], 'architecture': info.manifest.system['architecture'], 'apt_mirror': 'http://http.debian.net/debian'} - for name in info.manifest.plugins['packages']['sources'].iterkeys(): + for name, lines in info.manifest.plugins['packages']['sources'].iteritems(): list_path = os.path.join(info.root, 'etc/apt/sources.list.d/', name + '.list') with open(list_path, 'a') as source_list: - for line in info.manifest.plugins['packages']['sources'][name]: + for line in lines: source_list.write('{line}\n'.format(line=line.format(**manifest_vars))) diff --git a/providers/ec2/__init__.py b/providers/ec2/__init__.py index 0c14415..f63bb5f 100644 --- a/providers/ec2/__init__.py +++ b/providers/ec2/__init__.py @@ -57,7 +57,9 @@ def tasks(tasklist, manifest): common_network.RemoveDNSInfo, common_network.ConfigureNetworkIF, network.EnableDHCPCDDNS, - common_initd.ResolveInitScripts, + common_initd.AddExpandRoot, + common_initd.AddSSHKeyGeneration, + common_initd.RemoveHWClock, initd.AddEC2InitScripts, common_initd.InstallInitScripts, common_initd.AdjustExpandRootScript, diff --git a/providers/ec2/tasks/initd.py b/providers/ec2/tasks/initd.py index 9539eee..3e17b56 100644 --- a/providers/ec2/tasks/initd.py +++ b/providers/ec2/tasks/initd.py @@ -1,6 +1,5 @@ from base import Task from common import phases -from common.exceptions import TaskError from common.tasks import initd import os.path @@ -8,7 +7,6 @@ import os.path class AddEC2InitScripts(Task): description = 'Adding EC2 startup scripts' phase = phases.system_modification - predecessors = [initd.ResolveInitScripts] successors = [initd.InstallInitScripts] def run(self, info): diff --git a/providers/virtualbox/__init__.py b/providers/virtualbox/__init__.py index dc0dc8f..edf8790 100644 --- a/providers/virtualbox/__init__.py +++ b/providers/virtualbox/__init__.py @@ -45,7 +45,7 @@ def tasks(tasklist, manifest): network.RemoveDNSInfo, network.ConfigureNetworkIF, network.RemoveHostname, - initd.ResolveInitScripts, + initd.AddSSHKeyGeneration, initd.InstallInitScripts, cleanup.ClearMOTD, cleanup.CleanTMP,