mirror of
https://github.com/kevingruesser/bootstrap-vz.git
synced 2025-10-07 17:40:30 +00:00
Use new package handling in the cloud-init plugin
This saves quite a few "package" lines in the manifest
This commit is contained in:
parent
50f45d31d4
commit
4ffb533625
6 changed files with 84 additions and 118 deletions
|
@ -30,18 +30,6 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"packages": {
|
|
||||||
"sources": {
|
|
||||||
"backports": [
|
|
||||||
"deb {apt_mirror} {system.release}-backports main",
|
|
||||||
"deb-src {apt_mirror} {system.release}-backports main"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"remote": [
|
|
||||||
"sudo",
|
|
||||||
{ "name": "cloud-init", "target": "{system.release}-backports" }
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"plugins": {
|
"plugins": {
|
||||||
"cloud_init": {
|
"cloud_init": {
|
||||||
"username": "admin",
|
"username": "admin",
|
||||||
|
|
|
@ -30,18 +30,6 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"packages": {
|
|
||||||
"sources": {
|
|
||||||
"backports": [
|
|
||||||
"deb {apt_mirror} {system.release}-backports main",
|
|
||||||
"deb-src {apt_mirror} {system.release}-backports main"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"remote": [
|
|
||||||
"sudo",
|
|
||||||
{ "name": "cloud-init", "target": "{system.release}-backports" }
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"plugins": {
|
"plugins": {
|
||||||
"cloud_init": {
|
"cloud_init": {
|
||||||
"username": "admin",
|
"username": "admin",
|
||||||
|
|
|
@ -30,18 +30,6 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"packages": {
|
|
||||||
"sources": {
|
|
||||||
"backports": [
|
|
||||||
"deb {apt_mirror} {system.release}-backports main",
|
|
||||||
"deb-src {apt_mirror} {system.release}-backports main"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"remote": [
|
|
||||||
"sudo",
|
|
||||||
{ "name": "cloud-init", "target": "{system.release}-backports" }
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"plugins": {
|
"plugins": {
|
||||||
"cloud_init": {
|
"cloud_init": {
|
||||||
"username": "admin",
|
"username": "admin",
|
||||||
|
|
|
@ -4,40 +4,26 @@ def validate_manifest(data, schema_validate):
|
||||||
from os import path
|
from os import path
|
||||||
schema_path = path.normpath(path.join(path.dirname(__file__), 'manifest-schema.json'))
|
schema_path = path.normpath(path.join(path.dirname(__file__), 'manifest-schema.json'))
|
||||||
schema_validate(data, schema_path)
|
schema_validate(data, schema_path)
|
||||||
packages = data['plugins']['packages']
|
|
||||||
cloud_init_installed = False
|
|
||||||
if 'remote' in packages:
|
|
||||||
for package in packages['remote']:
|
|
||||||
if isinstance(package, basestring):
|
|
||||||
name = package
|
|
||||||
else:
|
|
||||||
name = package['name']
|
|
||||||
if name == 'cloud-init':
|
|
||||||
cloud_init_installed = True
|
|
||||||
break
|
|
||||||
if not cloud_init_installed:
|
|
||||||
from common.exceptions import ManifestError
|
|
||||||
raise ManifestError('The cloud-init package must be installed for the cloud_init plugin to work')
|
|
||||||
|
|
||||||
|
|
||||||
def resolve_tasks(tasklist, manifest):
|
def resolve_tasks(tasklist, manifest):
|
||||||
from tasks import SetUsername
|
import tasks
|
||||||
from tasks import SetMetadataSource
|
import providers.ec2.tasks.initd as initd_ec2
|
||||||
from tasks import AutoSetMetadataSource
|
|
||||||
from tasks import DisableModules
|
|
||||||
from providers.ec2.tasks.initd import AddEC2InitScripts
|
|
||||||
from common.tasks import initd
|
from common.tasks import initd
|
||||||
|
|
||||||
options = manifest.plugins['cloud_init']
|
if manifest.system['release'] in ['wheezy', 'stable']:
|
||||||
tasklist.add(AutoSetMetadataSource)
|
tasklist.add(tasks.AddBackports)
|
||||||
if 'username' in options:
|
|
||||||
tasklist.add(SetUsername)
|
|
||||||
if 'disable_modules' in options:
|
|
||||||
tasklist.add(DisableModules)
|
|
||||||
if 'metadata_sources' in options:
|
|
||||||
tasklist.add(SetMetadataSource)
|
|
||||||
|
|
||||||
tasklist.remove(AddEC2InitScripts,
|
tasklist.add(tasks.AddCloudInitPackages,
|
||||||
|
tasks.SetMetadataSource)
|
||||||
|
|
||||||
|
options = manifest.plugins['cloud_init']
|
||||||
|
if 'username' in options:
|
||||||
|
tasklist.add(tasks.SetUsername)
|
||||||
|
if 'disable_modules' in options:
|
||||||
|
tasklist.add(tasks.DisableModules)
|
||||||
|
|
||||||
|
tasklist.remove(initd_ec2.AddEC2InitScripts,
|
||||||
initd.AddExpandRoot,
|
initd.AddExpandRoot,
|
||||||
initd.AdjustExpandRootScript,
|
initd.AdjustExpandRootScript,
|
||||||
initd.AddSSHKeyGeneration)
|
initd.AddSSHKeyGeneration)
|
||||||
|
|
|
@ -3,6 +3,17 @@
|
||||||
"title": "cloud-init plugin manifest",
|
"title": "cloud-init plugin manifest",
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
|
"system": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"release": {
|
||||||
|
"type": "string",
|
||||||
|
"enum": ["wheezy", "stable",
|
||||||
|
"jessie", "testing",
|
||||||
|
"sid", "unstable"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"plugins": {
|
"plugins": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
|
@ -27,7 +38,7 @@
|
||||||
},
|
},
|
||||||
"packages": {"type": "object"}
|
"packages": {"type": "object"}
|
||||||
},
|
},
|
||||||
"required": ["cloud_init", "packages"]
|
"required": ["cloud_init"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,42 @@
|
||||||
from base import Task
|
from base import Task
|
||||||
from common import phases
|
from common import phases
|
||||||
from plugins.packages.tasks import InstallRemotePackages
|
|
||||||
from common.tasks import apt
|
|
||||||
from common.tools import log_check_call
|
from common.tools import log_check_call
|
||||||
import re
|
import os.path
|
||||||
|
|
||||||
|
|
||||||
|
class AddBackports(Task):
|
||||||
|
description = 'Adding backports to the apt sources'
|
||||||
|
phase = phases.preparation
|
||||||
|
|
||||||
|
def run(self, info):
|
||||||
|
if info.source_lists.target_exists('{system.release}-backports'):
|
||||||
|
import logging
|
||||||
|
msg = ('{system.release}-backports target already exists').format(**info.manifest_vars)
|
||||||
|
logging.getLogger(__name__).info(msg)
|
||||||
|
else:
|
||||||
|
info.source_lists.add('backports', 'deb {apt_mirror} {system.release}-backports main')
|
||||||
|
info.source_lists.add('backports', 'deb-src {apt_mirror} {system.release}-backports main')
|
||||||
|
|
||||||
|
|
||||||
|
class AddCloudInitPackages(Task):
|
||||||
|
description = 'Adding cloud-init package and sudo'
|
||||||
|
phase = phases.preparation
|
||||||
|
predecessors = [AddBackports]
|
||||||
|
|
||||||
|
def run(self, info):
|
||||||
|
target = None
|
||||||
|
if info.manifest.system['release'] in ['wheezy', 'stable']:
|
||||||
|
target = '{system.release}-backports'
|
||||||
|
info.packages.add('cloud-init', target)
|
||||||
|
info.packages.add('sudo')
|
||||||
|
|
||||||
|
|
||||||
class SetUsername(Task):
|
class SetUsername(Task):
|
||||||
description = 'Setting username in cloud.cfg'
|
description = 'Setting username in cloud.cfg'
|
||||||
phase = phases.system_modification
|
phase = phases.system_modification
|
||||||
predecessors = [InstallRemotePackages]
|
|
||||||
|
|
||||||
def run(self, info):
|
def run(self, info):
|
||||||
from common.tools import sed_i
|
from common.tools import sed_i
|
||||||
import os.path
|
|
||||||
cloud_cfg = os.path.join(info.root, 'etc/cloud/cloud.cfg')
|
cloud_cfg = os.path.join(info.root, 'etc/cloud/cloud.cfg')
|
||||||
username = info.manifest.plugins['cloud_init']['username']
|
username = info.manifest.plugins['cloud_init']['username']
|
||||||
search = '^ name: debian$'
|
search = '^ name: debian$'
|
||||||
|
@ -24,60 +47,42 @@ class SetUsername(Task):
|
||||||
|
|
||||||
|
|
||||||
class SetMetadataSource(Task):
|
class SetMetadataSource(Task):
|
||||||
description = 'Setting metadata source'
|
description = 'Setting metadata source'
|
||||||
phase = phases.system_modification
|
phase = phases.system_modification
|
||||||
predecessors = [apt.AptSources]
|
|
||||||
successors = [apt.AptUpdate]
|
|
||||||
|
|
||||||
def run(self, info):
|
|
||||||
if "metadata_sources" in info.manifest.plugins['cloud_init']:
|
|
||||||
sources = "cloud-init cloud-init/datasources multiselect " + info.manifest.plugins['cloud_init']['metadata_sources']
|
|
||||||
log_check_call(['/usr/sbin/chroot', info.root, '/usr/bin/debconf-set-selections'], sources)
|
|
||||||
|
|
||||||
|
|
||||||
class AutoSetMetadataSource(Task):
|
|
||||||
description = 'Auto-setting metadata source'
|
|
||||||
phase = phases.system_modification
|
|
||||||
predecessors = [apt.AptSources]
|
|
||||||
successors = [SetMetadataSource]
|
|
||||||
|
|
||||||
def run(self, info):
|
def run(self, info):
|
||||||
sources = ""
|
if 'metadata_sources' in info.manifest.plugins['cloud_init']:
|
||||||
if info.manifest.provider == "ec2":
|
sources = info.manifest.plugins['cloud_init']['metadata_sources']
|
||||||
sources = "Ec2"
|
else:
|
||||||
|
source_mapping = {'ec2': 'Ec2'}
|
||||||
if sources:
|
sources = source_mapping.get(info.manifest.provider, None)
|
||||||
print ("Setting metadata source to " + sources)
|
if sources is None:
|
||||||
sources = "cloud-init cloud-init/datasources multiselect " + sources
|
import logging
|
||||||
log_check_call(['/usr/sbin/chroot', info.root, '/usr/bin/debconf-set-selections'], sources)
|
msg = ('No cloud-init metadata source mapping found for provider `{provider}\', '
|
||||||
|
'skipping selections setting.').format(info.manifest.provider)
|
||||||
|
logging.getLogger(__name__).warn(msg)
|
||||||
|
return
|
||||||
|
sources = "cloud-init cloud-init/datasources multiselect " + sources
|
||||||
|
log_check_call(['/usr/sbin/chroot', info.root, '/usr/bin/debconf-set-selections'], sources)
|
||||||
|
|
||||||
|
|
||||||
class DisableModules(Task):
|
class DisableModules(Task):
|
||||||
description = 'Setting cloud.cfg modules'
|
description = 'Setting cloud.cfg modules'
|
||||||
phase = phases.system_modification
|
phase = phases.system_modification
|
||||||
predecessors = [apt.AptUpgrade]
|
|
||||||
|
|
||||||
def run(self, info):
|
def run(self, info):
|
||||||
if 'disable_modules' in info.manifest.plugins['cloud_init']:
|
import re
|
||||||
patterns = ""
|
patterns = ""
|
||||||
for pattern in info.manifest.plugins['cloud_init']['disable_modules']:
|
for pattern in info.manifest.plugins['cloud_init']['disable_modules']:
|
||||||
if patterns != "":
|
if patterns != "":
|
||||||
patterns = patterns + "|" + pattern
|
patterns = patterns + "|" + pattern
|
||||||
else:
|
else:
|
||||||
patterns = "^\s+-\s+(" + pattern
|
patterns = "^\s+-\s+(" + pattern
|
||||||
patterns = patterns + ")$"
|
patterns = patterns + ")$"
|
||||||
regex = re.compile(patterns)
|
regex = re.compile(patterns)
|
||||||
|
|
||||||
try:
|
cloud_cfg = os.path.join(info.root, 'etc/cloud/cloud.cfg')
|
||||||
f = open(info.root + "/etc/cloud/cloud.cfg")
|
import fileinput
|
||||||
lines = f.readlines()
|
for line in fileinput.input(files=cloud_cfg, inplace=True):
|
||||||
f.close()
|
if not regex.match(line):
|
||||||
except:
|
print line,
|
||||||
print "Cannot read cloud.cfg"
|
|
||||||
return -1
|
|
||||||
|
|
||||||
f = open(info.root + "/etc/cloud/cloud.cfg", "w")
|
|
||||||
for line in lines:
|
|
||||||
if not regex.match(line):
|
|
||||||
f.write(line)
|
|
||||||
f.close
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue