2018-04-09 22:13:21 -07:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2014-03-23 23:12:07 +01:00
|
|
|
from bootstrapvz.base import Task
|
|
|
|
from bootstrapvz.common import phases
|
|
|
|
from bootstrapvz.common.tools import log_check_call
|
|
|
|
from bootstrapvz.common.tasks import apt
|
2014-04-26 16:48:09 -03:00
|
|
|
from bootstrapvz.common.tasks import locale
|
2017-11-09 20:16:19 +01:00
|
|
|
from . import assets
|
|
|
|
from shutil import copy
|
2014-04-26 16:48:09 -03:00
|
|
|
import logging
|
2017-11-09 20:16:19 +01:00
|
|
|
import os
|
2013-12-29 17:45:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
class AddCloudInitPackages(Task):
|
2016-06-04 11:35:59 +02:00
|
|
|
description = 'Adding cloud-init package and sudo'
|
|
|
|
phase = phases.preparation
|
|
|
|
predecessors = [apt.AddBackports]
|
2013-12-29 17:45:23 +01:00
|
|
|
|
2016-06-04 11:35:59 +02:00
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
|
|
|
target = None
|
|
|
|
from bootstrapvz.common.releases import wheezy
|
|
|
|
if info.manifest.release == wheezy:
|
|
|
|
target = '{system.release}-backports'
|
|
|
|
info.packages.add('cloud-init', target)
|
|
|
|
info.packages.add('sudo')
|
2013-12-01 12:26:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
class SetUsername(Task):
|
2016-06-04 11:35:59 +02:00
|
|
|
description = 'Setting username in cloud.cfg'
|
|
|
|
phase = phases.system_modification
|
2013-12-01 12:26:09 +01:00
|
|
|
|
2016-06-04 11:35:59 +02:00
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
|
|
|
from bootstrapvz.common.tools import sed_i
|
|
|
|
cloud_cfg = os.path.join(info.root, 'etc/cloud/cloud.cfg')
|
|
|
|
username = info.manifest.plugins['cloud_init']['username']
|
|
|
|
search = '^ name: debian$'
|
|
|
|
replace = (' name: {username}\n'
|
|
|
|
' sudo: ALL=(ALL) NOPASSWD:ALL\n'
|
|
|
|
' shell: /bin/bash').format(username=username)
|
|
|
|
sed_i(cloud_cfg, search, replace)
|
2013-12-17 15:51:03 +00:00
|
|
|
|
2013-12-21 12:26:37 +00:00
|
|
|
|
2016-04-16 08:45:59 -04:00
|
|
|
class SetGroups(Task):
|
2016-06-04 11:35:59 +02:00
|
|
|
description = 'Setting groups in cloud.cfg'
|
|
|
|
phase = phases.system_modification
|
2016-04-16 08:45:59 -04:00
|
|
|
|
2016-06-04 11:35:59 +02:00
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
|
|
|
from bootstrapvz.common.tools import sed_i
|
|
|
|
cloud_cfg = os.path.join(info.root, 'etc/cloud/cloud.cfg')
|
|
|
|
groups = info.manifest.plugins['cloud_init']['groups']
|
2019-03-05 19:01:05 +00:00
|
|
|
search = (r'^ groups: \[adm, audio, cdrom, dialout, floppy, video,'
|
|
|
|
r' plugdev, dip\]$')
|
2016-06-04 11:35:59 +02:00
|
|
|
replace = (' groups: [adm, audio, cdrom, dialout, floppy, video,'
|
|
|
|
' plugdev, dip, {groups}]').format(groups=', '.join(groups))
|
|
|
|
sed_i(cloud_cfg, search, replace)
|
2016-04-16 08:45:59 -04:00
|
|
|
|
|
|
|
|
2013-12-17 15:51:03 +00:00
|
|
|
class SetMetadataSource(Task):
|
2016-06-04 11:35:59 +02:00
|
|
|
description = 'Setting metadata source'
|
|
|
|
phase = phases.package_installation
|
|
|
|
predecessors = [locale.GenerateLocale]
|
|
|
|
successors = [apt.AptUpdate]
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
|
|
|
if 'metadata_sources' in info.manifest.plugins['cloud_init']:
|
|
|
|
sources = info.manifest.plugins['cloud_init']['metadata_sources']
|
|
|
|
else:
|
|
|
|
source_mapping = {'ec2': 'Ec2'}
|
|
|
|
sources = source_mapping.get(info.manifest.provider['name'], None)
|
|
|
|
if sources is None:
|
|
|
|
msg = ('No cloud-init metadata source mapping found for provider `{provider}\', '
|
|
|
|
'skipping selections setting.').format(provider=info.manifest.provider['name'])
|
|
|
|
logging.getLogger(__name__).warn(msg)
|
|
|
|
return
|
|
|
|
sources = "cloud-init cloud-init/datasources multiselect " + sources
|
|
|
|
log_check_call(['chroot', info.root, 'debconf-set-selections'], sources)
|
2013-12-21 12:26:37 +00:00
|
|
|
|
2013-12-19 11:44:34 +00:00
|
|
|
|
|
|
|
class DisableModules(Task):
|
2016-06-04 22:04:09 +02:00
|
|
|
description = 'Disabling cloud.cfg modules'
|
2016-06-04 11:35:59 +02:00
|
|
|
phase = phases.system_modification
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
|
|
|
import re
|
|
|
|
patterns = ""
|
|
|
|
for pattern in info.manifest.plugins['cloud_init']['disable_modules']:
|
|
|
|
if patterns != "":
|
|
|
|
patterns = patterns + "|" + pattern
|
|
|
|
else:
|
2019-03-05 19:01:05 +00:00
|
|
|
patterns = r"^\s+-\s+(" + pattern
|
2016-06-04 11:35:59 +02:00
|
|
|
patterns = patterns + ")$"
|
|
|
|
regex = re.compile(patterns)
|
|
|
|
|
|
|
|
cloud_cfg = os.path.join(info.root, 'etc/cloud/cloud.cfg')
|
|
|
|
import fileinput
|
|
|
|
for line in fileinput.input(files=cloud_cfg, inplace=True):
|
|
|
|
if not regex.match(line):
|
2018-06-13 13:07:14 +03:00
|
|
|
print(line, end='')
|
2016-06-04 22:04:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
class EnableModules(Task):
|
|
|
|
description = 'Enabling cloud.cfg modules'
|
|
|
|
phase = phases.system_modification
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
|
|
|
import fileinput
|
|
|
|
import re
|
|
|
|
cloud_cfg = os.path.join(info.root, 'etc/cloud/cloud.cfg')
|
|
|
|
for section in info.manifest.plugins['cloud_init']['enable_modules']:
|
|
|
|
regex = re.compile("^%s:" % section)
|
|
|
|
for entry in info.manifest.plugins['cloud_init']['enable_modules'][section]:
|
|
|
|
count = 0
|
|
|
|
counting = 0
|
|
|
|
for line in fileinput.input(files=cloud_cfg, inplace=True):
|
|
|
|
if regex.match(line) and not counting:
|
|
|
|
counting = True
|
|
|
|
if counting:
|
|
|
|
count = count + 1
|
|
|
|
if int(entry['position']) == int(count):
|
|
|
|
print(" - %s" % entry['module'])
|
2018-06-13 13:07:14 +03:00
|
|
|
print(line, end='')
|
2017-11-09 20:16:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
class SetCloudInitMountOptions(Task):
|
|
|
|
description = 'Setting cloud-init default mount options'
|
|
|
|
phase = phases.system_modification
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
|
|
|
cloud_init_src = os.path.join(assets, 'cloud-init/debian_cloud.cfg')
|
|
|
|
cloud_init_dst = os.path.join(info.root, 'etc/cloud/cloud.cfg.d/01_debian_cloud.cfg')
|
|
|
|
copy(cloud_init_src, cloud_init_dst)
|
2018-04-09 22:25:58 -07:00
|
|
|
os.chmod(cloud_init_dst, 0o644)
|