111 |
111 |
class SynnefoCI(object):
|
112 |
112 |
"""SynnefoCI python class"""
|
113 |
113 |
|
114 |
|
def __init__(self, config_file=None, cleanup_config=False, cloud=None):
|
|
114 |
def __init__(self, config_file=None, build_id=None, cloud=None):
|
115 |
115 |
""" Initialize SynnefoCI python class
|
116 |
116 |
|
117 |
117 |
Setup logger, local_dir, config and kamaki
|
... | ... | |
140 |
140 |
config_file = DEFAULT_CONFIG_FILE
|
141 |
141 |
if not os.path.isabs(config_file):
|
142 |
142 |
config_file = os.path.join(self.ci_dir, config_file)
|
143 |
|
|
144 |
143 |
self.config = ConfigParser()
|
145 |
144 |
self.config.optionxform = str
|
146 |
145 |
self.config.read(config_file)
|
|
146 |
|
|
147 |
# Read temporary_config file
|
147 |
148 |
temp_config = self.config.get('Global', 'temporary_config')
|
148 |
|
if cleanup_config:
|
149 |
|
try:
|
150 |
|
os.remove(temp_config)
|
151 |
|
except OSError:
|
152 |
|
pass
|
|
149 |
self.temp_config = ConfigParser()
|
|
150 |
self.temp_config.optionxform = str
|
|
151 |
self.temp_config.read(temp_config)
|
|
152 |
if build_id is not None:
|
|
153 |
self.build_id = build_id
|
153 |
154 |
else:
|
154 |
|
self.config.read(self.config.get('Global', 'temporary_config'))
|
|
155 |
# Find a uniq build_id to use
|
|
156 |
ids = self.temp_config.sections()
|
|
157 |
if ids:
|
|
158 |
max_id = int(max(self.temp_config.sections(), key=int))
|
|
159 |
self.build_id = max_id + 1
|
|
160 |
else:
|
|
161 |
self.build_id = 1
|
|
162 |
self.logger.info("Will use %s as build id" % _green(self.build_id))
|
|
163 |
# If build_id doesn't exist create a new one
|
|
164 |
try:
|
|
165 |
self.temp_config.add_section(str(self.build_id))
|
|
166 |
creation_time = time.strftime("%a, %d %b %Y %X", time.localtime())
|
|
167 |
self.write_config("created", creation_time)
|
|
168 |
except DuplicateSectionError:
|
|
169 |
pass
|
155 |
170 |
|
156 |
171 |
# Set kamaki cloud
|
157 |
172 |
if cloud is not None:
|
... | ... | |
234 |
249 |
@_check_kamaki
|
235 |
250 |
def destroy_server(self, wait=True):
|
236 |
251 |
"""Destroy slave server"""
|
237 |
|
server_id = self.config.getint('Temporary Options', 'server_id')
|
|
252 |
server_id = self.temp_config.getint(str(self.build_id), 'server_id')
|
238 |
253 |
self.logger.info("Destoying server with id %s " % server_id)
|
239 |
254 |
self.cyclades_client.delete_server(server_id)
|
240 |
255 |
if wait:
|
... | ... | |
369 |
384 |
else:
|
370 |
385 |
self.logger.debug("No ssh keys found")
|
371 |
386 |
|
372 |
|
def write_config(self, option, value, section="Temporary Options"):
|
|
387 |
def write_config(self, option, value):
|
373 |
388 |
"""Write changes back to config file"""
|
374 |
|
try:
|
375 |
|
self.config.add_section(section)
|
376 |
|
except DuplicateSectionError:
|
377 |
|
pass
|
378 |
|
self.config.set(section, option, str(value))
|
|
389 |
self.temp_config.set(str(self.build_id), option, str(value))
|
|
390 |
curr_time = time.strftime("%a, %d %b %Y %X", time.localtime())
|
|
391 |
self.temp_config.set(str(self.build_id), "modified", curr_time)
|
379 |
392 |
temp_conf_file = self.config.get('Global', 'temporary_config')
|
380 |
393 |
with open(temp_conf_file, 'wb') as tcf:
|
381 |
|
self.config.write(tcf)
|
|
394 |
self.temp_config.write(tcf)
|
382 |
395 |
|
383 |
396 |
def setup_fabric(self):
|
384 |
397 |
"""Setup fabric environment"""
|
385 |
398 |
self.logger.info("Setup fabric parameters..")
|
386 |
|
fabric.env.user = self.config.get('Temporary Options', 'server_user')
|
|
399 |
fabric.env.user = self.temp_config.get(str(self.build_id),
|
|
400 |
'server_user')
|
387 |
401 |
fabric.env.host_string = \
|
388 |
|
self.config.get('Temporary Options', 'server_ip')
|
389 |
|
fabric.env.port = self.config.getint('Temporary Options',
|
390 |
|
'server_port')
|
391 |
|
fabric.env.password = self.config.get('Temporary Options',
|
392 |
|
'server_passwd')
|
|
402 |
self.temp_config.get(str(self.build_id), 'server_ip')
|
|
403 |
fabric.env.port = self.temp_config.getint(str(self.build_id),
|
|
404 |
'server_port')
|
|
405 |
fabric.env.password = self.temp_config.get(str(self.build_id),
|
|
406 |
'server_passwd')
|
393 |
407 |
fabric.env.connection_attempts = 10
|
394 |
408 |
fabric.env.shell = "/bin/bash -c"
|
395 |
409 |
fabric.env.disable_known_hosts = True
|