-
Notifications
You must be signed in to change notification settings - Fork 4
/
Vm.py
218 lines (204 loc) · 9.83 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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
__author__ = 'Andreas Bader'
__version__ = "0.01"
import threading
import time
import vagrant
import os
import Util
import re
from fabric.api import *
import subprocess
class Vm():
vagrantFolders=None
pathVagrantfile=None
credFiles=None
basicFilesFolder=None
pathFolder=None
tmpFolder=None
name=None
logging=False
logger=None
created=False
vm=None
thread=None
provider=None
ip=None
# vagrantFolders: Folders inside which vagrantfiles+folders are
# tmpFolder: Path to tempfolder
# name: name of the VM
# logger: Logger instance
# logging: if true, vagrant debug logging will be done
def __init__(self, vagrantFolders, credFiles, basicFilesFolder, tmpFolder, name, logger, provider, logging):
self.vagrantFolders = vagrantFolders
self.credFiles=credFiles
self.basicFilesFolder=basicFilesFolder
self.tmpFolder = tmpFolder
self.name = name
self.logger = logger
self.logging = logging
self.provider = provider
def start(self):
self.thread = threading.Thread(target=self.run)
self.thread.start()
def join(self):
self.thread.join()
def run(self):
if not self.created:
self.create_vm()
else:
self.destroy()
def destroy(self):
if self.vm != None:
try:
if self.vm.status()[0].state == vagrant.Vagrant.RUNNING:
self.vm.halt()
startTime = int(time.time())
while self.vm.status()[0].state == "powering-off" and (int(time.time()) - startTime) < 60 :
time.sleep(1)
if self.vm.status()[0].state != vagrant.Vagrant.POWEROFF and self.vm.status()[0].state != vagrant.Vagrant.SHUTOFF:
self.vm.halt(force=True)
return self.vm.destroy()
except subprocess.CalledProcessError:
# ignore warnings that destroy did not work (occurs sometimes)
self.logger.warning("There might be some leftovers from vm '%s'." %(self.name))
return True
return True
def create_vm(self):
# Check if you want to log
log_cm=None
self.logger.info("BEGIN Creating %s." %(self.name))
if self.logging:
try:
filename="%s_%s.log" %(self.name,time.strftime("%Y%m%d%H%M%S", time.localtime()))
log_cm = vagrant.make_file_cm(filename)
self.logger.info("Logging creation of %s to %s." % (self.name,filename))
except Exception, e:
self.logger.error('Failed to open file', exc_info=True)
return False
pathName=self.name.rsplit("_",1)[0] # Name is something like VMNAME_1, split _1 away!
# copy to tmpfolder (already checked that it exist, need to check if vm folder exists)
paths=Util.create_folder_path_list(self.vagrantFolders, [pathName])
pathTmp=os.path.join(self.tmpFolder,self.name)
if not Util.check_folders(paths, self.logger, False, True, True):
return False
if not Util.check_folder(pathTmp, self.logger, True):
return False
# Copy Vagrantfile in place
pathVagrantFileOld=os.path.join(pathTmp,"%s.vagrant"%(self.name))
pathVagrantFileNew=os.path.join(pathTmp,"Vagrantfile")
if not Util.copy_folders(paths,pathTmp,self.logger, True):
return False
self.pathFolder=pathTmp
for credFile in self.credFiles:
credFilesOld = Util.create_folder_path_list(self.vagrantFolders, [credFile])
pathCredFileNew = os.path.join(pathTmp,credFile)
for credFileOld in credFilesOld:
if Util.check_file_exists(credFileOld):
if not Util.copy_file(credFileOld,pathCredFileNew,self.logger):
return False
basicFileFoldersOld=Util.create_folder_path_list(self.vagrantFolders, [self.basicFilesFolder])
pathBasicFileFolderNew=os.path.join(pathTmp,self.basicFilesFolder)
if not Util.copy_folders(basicFileFoldersOld,pathBasicFileFolderNew,self.logger, True):
return False
# digital ocean needs a random name, otherwise we always get problems with multiple measurements
if Util.check_file_exists(pathVagrantFileNew):
self.logger.error("'%s' does already exist." %(pathVagrantFileNew))
return False
try:
file_old = open(pathVagrantFileOld, "r")
file_new = open(pathVagrantFileNew, "w")
for line in file_old:
if re.match(r'^\s*HOSTNAME\s+=\s+("[^"]+"|\'[^\']+\')\s*$',line) != None:
split_res = re.search(r'("[^"]+"|\'[^\']+\')',line)
if split_res != None:
if self.provider == "digital_ocean":
# allowed are numbers, letters, hyphens and dots
random_string = Util.get_random_string(10)
file_new.write("HOSTNAME = \"%s-%s\"\n" %(split_res.group()[1:-1],
random_string))
self.name = "%s-%s" % (split_res.group()[1:-1], random_string)
else:
self.name = "%s" % (split_res.group()[1:-1])
file_new.write(line)
else:
self.logger.warning("Could not parse hostname out of '%s'. "
"Using the default. Errors can occur." % (line))
file_new.write(line)
else:
file_new.write(line)
file_new.flush()
file_new.close()
file_old.close()
except Exception,e:
self.logger.error("An error occured while copying '%s' to '%s'." % (pathVagrantFileOld,
pathVagrantFileNew), exc_info=True)
# else:
# if not Util.copy_file(pathVagrantFileOld,pathVagrantFileNew,self.logger):
# return False
self.pathVagrantfile=pathVagrantFileNew
Util.clear_vagrant_files(pathTmp,self.logger)
# Create VM
try:
vm = vagrant.Vagrant(root=pathTmp, out_cm=log_cm, err_cm=log_cm)
self.vm=vm
vm.up(provider=self.provider)
# with settings(host_string= vm.user_hostname_port(),
# key_filename = vm.keyfile(),
# disable_known_hosts = True):
# run("uname -a")
except Exception, e:
self.logger.error('Failed while creating vm %s.' %(self.name), exc_info=True)
self.logger.info('Since creation failed, trying to destroy vm %s.' %(self.name), exc_info=True)
#if not self.vm.destroy():
# self.logger.error('Can not destroy %s.' %(self.name), exc_info=True)
## vm.destroy() seems to be always returning false at this stage (opentsack), but destroying works fine -> Ignore it.
self.destroy()
return False
self.logger.info("END Creating %s." %(self.name))
self.logger.info("GET IP of %s." %(self.name))
self.ip = self.get_ip()
# the IP Part is needed, because on OpenStack the 'internal' IP differs from the 'external' (SSH) IP
if self.ip == None:
self.logger.error('Failed getting IP while creating vm %s.' %(self.name), exc_info=True)
self.logger.info('Since creation failed, trying to destroy vm %s.' %(self.name), exc_info=True)
self.destroy()
return False
self.created=True
return True
# Return the output
def run_with_output (self,disableKnownHosts,command,warn_only=False,quiet=False):
with hide('output','running', 'warnings', 'stdout', 'stderr'),\
settings(host_string = self.vm.user_hostname_port(),
key_filename = self.vm.keyfile(),
disable_known_hosts = disableKnownHosts,
warn_only = warn_only):
result = run(command,warn_only=warn_only, quiet=quiet)
return result
# Only return true/false
def run_without_output (self,disableKnownHosts,command,warn_only=False,quiet=False,test=False):
result = self.run_with_output (disableKnownHosts,command,warn_only,quiet)
if result.return_code == 0 or (warn_only and result.return_code == 255) or (test and result.return_code == -1):
return True
else:
self.logger.error("Command '%s' on %s returned %s." %(command, self.vm.user_hostname_port(), result.return_code))
return False
def get_ip(self):
head_str = " | head -n1"
if self.provider in ["virtualbox", "digital_ocean"]:
# virtualbox uses second network interface for vm-interconnections
head_str = " | head -n4 | tail -n1"
result = self.run_with_output (True,'sudo ifconfig | grep -E -o "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"%s' % (head_str),True, True)
if result.return_code == 0:
return result.stdout
else:
self.logger.error("Can't get IP on %s, command returned %s." %(self.vm.user_hostname_port(), result.return_code))
return None
def get_hostname(self):
result = self.run_with_output (True,'hostname',True, True)
if result.return_code == 0:
return result.stdout
else:
self.logger.error("Can't get hostname on %s, command returned %s." %(self.vm.user_hostname_port(), result.return_code))
return None