forked from andsens/bootstrap-vz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
139 lines (116 loc) · 5.02 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from __future__ import print_function
from bootstrapvz.base import Task
from bootstrapvz.common import phases
from bootstrapvz.common.tools import log_check_call
from bootstrapvz.common.tasks import apt
from bootstrapvz.common.tasks import locale
from . import assets
from shutil import copy
import logging
import os
class AddCloudInitPackages(Task):
description = 'Adding cloud-init package and sudo'
phase = phases.preparation
predecessors = [apt.AddBackports]
@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')
class SetUsername(Task):
description = 'Setting username in cloud.cfg'
phase = phases.system_modification
@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)
class SetGroups(Task):
description = 'Setting groups in cloud.cfg'
phase = phases.system_modification
@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']
search = (r'^ groups: \[adm, audio, cdrom, dialout, floppy, video,'
r' plugdev, dip\]$')
replace = (' groups: [adm, audio, cdrom, dialout, floppy, video,'
' plugdev, dip, {groups}]').format(groups=', '.join(groups))
sed_i(cloud_cfg, search, replace)
class SetMetadataSource(Task):
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)
class DisableModules(Task):
description = 'Disabling cloud.cfg modules'
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:
patterns = r"^\s+-\s+(" + pattern
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):
print(line, end='')
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'])
print(line, end='')
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)
os.chmod(cloud_init_dst, 0o644)