-
Notifications
You must be signed in to change notification settings - Fork 9
/
vm.py
304 lines (237 loc) · 10.3 KB
/
vm.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
from printer import show
from paramiko import SSHClient, WarningPolicy
import warnings
from time import sleep
import locals
import util
# Ignore incoming warnings from SSHClient about SSH keys mismatch
warnings.filterwarnings("ignore", category=UserWarning)
class VM():
def __init__(self, name, backend, hostname, domain, ip):
"Creates a connection to the client"
self.name = name
self.backend = backend
self.hostname = hostname
self.domain = domain
self.ip = ip
self.fqdn = '%s.%s' % (self.hostname, self.domain)
self.locals = dict()
show.debug("New VM object!")
show.debug("Name: %s" % self.name)
show.debug("Backend: %s" % self.backend)
show.debug("Hostname: %s" % self.hostname)
show.debug("Domain: %s" % self.domain)
show.debug("IP: %s" % self.ip)
def set_sudoers(self):
show('Configuring sudo commands execution in sudoers')
show('Using root login')
self.connect(user='root')
self.cmd('sed -i.bak "s/Defaults requiretty'
'/# Defaults requiretty/g" /etc/sudoers')
self.close()
def start(self):
self.backend.start(self.name)
def connect(self, user=locals.USER, pkey_path=locals.PRIVATE_KEY):
# show('Connecting to %s' % self.fqdn)
success = False
timeout = 0
show('Connecting..')
while not (success or timeout > 60):
try:
self.client = SSHClient()
self.client.set_missing_host_key_policy(WarningPolicy())
self.client.connect(self.ip, username=user,
key_filename=pkey_path)
success = True
except UserWarning:
show.debug('UserWarning ignored')
except Exception, e:
show.debug('Caught exception: %s' % e)
sleep(2)
timeout += 2
if timeout > 60:
raise RuntimeError("Could not connect to the %s" % self.ip)
show('Connection successful!')
def get_connection(self):
return self.client
def cmd(self, command, allow_failure=False, silent=False):
i, o, e = self.client.exec_command('set -o pipefail; ' + command)
def print_out(line):
if silent:
show(line.strip())
else:
show.debug(line.strip())
# It is important to print output here at least at the debug level
# to empty the buffer
for line in o.readlines():
print_out(line)
for line in e.readlines():
print_out(line)
return_code = o.channel.recv_exit_status()
if return_code == 0 or allow_failure:
return return_code
else:
raise RuntimeError("The following command failed: %s" % command)
def close(self):
self.client.close()
# show('Connection to %s closed' % self.fqdn)
def clean_log(self):
show('Removing previous log file for this VM')
self.cmd("rm -f %s" % self.locals['log_file'])
def add_nameserver(self, hostname):
show('Adding ' + self.ip + ' as nameserver')
self.cmd('sudo sed -i.bak "2a\\nameserver '
+ self.ip + '" /etc/resolv.conf')
def update_workspace(self):
show('Updating workspace - pulling updates for FreeIPA and LabTool')
self.cmd("bash labtool/ipa-fun-update-workspace.sh"
" {log}".format(**self.locals))
def setup_logging_path(self):
log_file = getattr(locals, 'LOGFILE', None) or '/vmlog'
log_path = "2>&1 | sudo tee -a {f}".format(f=log_file)
self.set_format(log=log_path, log_file=log_file)
def build(self, build_args):
show('Building:')
show.tab()
if build_args[0] == 'patch':
show('Syncing the patches:')
# TODO: make this generic
#util.run(['rsync', '-rc', '--delete', '/home/tbabej/Work/patches',
# '%s:dev/' % self.ip])
for patch_id in build_args[1:]:
show('Applying patch {patch}'.format(patch=patch_id))
self.cmd("bash labtool/ipa-fun-apply-patch.sh"
" {patch} {log}".format(patch=patch_id,
**self.locals))
elif build_args[0] == 'branch':
show('Checking out to {branch}'.format(branch=build_args[1]))
self.cmd("bash labtool/ipa-fun-checkout.sh"
" {branch} {log}".format(branch=build_args[1],
**self.locals))
else:
raise RuntimeError("Unknown action: %s" % build_args[0])
show('Installing build dependencies')
self.install_build_dependencies()
show('Applying build workarounds')
self.apply_build_workarounds()
show('Building sources')
self.cmd("bash labtool/ipa-fun-build.sh"
" {log}".format(**self.locals))
show.untab()
def create_workspace(self):
show('Creating workspace. You should really do this in the template!')
self.cmd("bash labtool/ipa-fun-create-workspace.sh"
" {log}".format(**self.locals))
def detect_workspace(self):
# Detect workspace returns 0 if the workspace was detected, and 1 otherwise
return not self.cmd("bash labtool/ipa-fun-detect-workspace.sh"
" {log}".format(**self.locals), allow_failure=True)
def install_devel_packages(self, packages=[]):
if packages:
show('Installing packages from devel repo: {pckgs}'
.format(pckgs=', '.join(packages)))
self.cmd('sudo dnf install -y --enablerepo=ipa-devel {pckgs}'
.format(pckgs=' '.join(packages)))
def install_build_dependencies(self):
self.cmd("bash labtool/ipa-fun-install-build-dependencies.sh"
" {log}".format(**self.locals))
def apply_build_workarounds(self):
pass
def install_packages(self, action):
if action[1] == 'local':
show('Installing local rpm packages')
self.cmd("bash labtool/ipa-fun-install-rpms.sh"
" {log}".format(**self.locals))
elif action[1] == 'repo':
show('Installing packages from repositories')
self.cmd("bash labtool/ipa-fun-install-repo.sh"
" {log}".format(**self.locals))
def set_hostname(self, trust=False, subdomain='',
domain=locals.DOMAIN):
last_ip_segment = self.ip.split('.')[-1]
hostname = self.hostname
if trust:
hostname += '.dom{ip_id}.tbad'.format(
ip_id=util.normalize_ip_suffix(last_ip_segment))
hostname += '.' + self.domain
self.hostname = hostname
show('Setting hostname to %s' % hostname)
self.cmd("bash labtool/ipa-set-hostname.sh"
" {hostname} {log}".format(hostname=hostname,
**self.locals))
show('Creating DNS record for the VM')
self.backend.create_record(hostname=self.hostname, ip=self.ip)
def prepare_install(self, firewall, selinux, trust, subdomain=''):
# Set firewall
if firewall:
switch = "on"
else:
switch = "off"
show('Setting firewall {setting}'.format(setting=switch))
self.cmd("bash labtool/ipa-set-firewall.sh"
" {setting} {log}".format(setting=switch, **self.locals))
# set SELinux
if selinux:
switch = "on"
else:
switch = "off"
show('Setting SELinux {setting}'.format(setting=switch))
self.cmd("bash labtool/ipa-set-selinux.sh"
" {setting} {log}".format(setting=switch, **self.locals))
# Apply current workarounds
show('Applying workarounds for IPA install to work')
self.cmd("bash labtool/ipa-fun-current-workarounds.sh"
" {log}".format(**self.locals))
def install_ipa(self):
# install IPA
show('Installing IPA')
self.cmd("bash labtool/ipa-fun-install-ipa.sh"
" {log}".format(**self.locals))
def run_tests(self):
show('Testing:')
show.tab()
# run the test suite
show('Configuring VM for tests')
self.cmd("bash labtool/ipa-fun-setup-tests.sh"
" {log}".format(**self.locals))
show('Running whole test suite')
ret = self.cmd("bash labtool/ipa-fun-run-tests.sh"
" {log}".format(**self.locals),
allow_failure=True)
if ret == 0:
show('PASSED. See {log} for the logs.'.format(**self.locals))
else:
show('FAILED. See {log} for the logs.'.format(**self.locals))
show.untab()
def setup_trust(self, hostname=''):
show('Setting up AD Trust..')
self.cmd("bash labtool/ipa-fun-setup-trust.sh"
" {hostname} {log}".format(hostname=hostname, **self.locals))
show('Completed!')
def set_format(self, **kwargs):
self.locals.update(kwargs)
def prepare_replica(self, hostname):
show('Creating replica file for {replica}'.format(replica=hostname))
self.cmd("bash labtool/ipa-fun-prepare-replica.sh"
" {replica} {log}".format(replica=hostname, **self.locals))
def install_replica(self, master):
# TODO: support for replica install settings
show('Installing replica')
ret = self.cmd("bash labtool/ipa-fun-install-replica.sh"
" {master} {log}".format(master=master, **self.locals),
allow_failure=True)
if ret:
show('Replica installation failed.')
def check_services(self):
ret = self.cmd("bash labtool/ipa-fun-check-services.sh",
allow_failure=True)
if ret:
show('Service check failed.')
def install_client(self, master):
# TODO: support for replica install settings
show('Installing client')
ret = self.cmd("bash labtool/ipa-fun-install-client.sh"
" {master} {log}".format(master=master, **self.locals),
allow_failure=True)
if ret:
show('Client installation failed.')