def __init__(self): ''' Wrapper around the python-vagrant module for use with ansible. Note that Vagrant itself is non-thread safe, as is the python-vagrant lib, so we need to lock on basically all operations ... ''' # Get a lock self.lock = None try: self.lock = lockfile.FileLock(VAGRANT_LOCKFILE) self.lock.acquire() except Exception: # fall back to using flock instead ... try: import fcntl self.lock = open(VAGRANT_LOCKFILE, 'w') fcntl.flock(self.lock, fcntl.LOCK_EX) except Exception: print( "failed=True msg='Could not get a lock for using vagrant. Install python module \"lockfile\" to use vagrant on non-POSIX filesytems.'" ) sys.exit(1) # Initialize vagrant and state files log_cm = vagrant.make_file_cm('vagrant.log') self.vg = vagrant.Vagrant(out_cm=log_cm, err_cm=log_cm) # operation will create a default data structure if none present self.vg_data = {"instances": {}, "num_inst": 0}
def __init__(self, username, boxname): self.username = username self.boxname = boxname from pathlib import Path self.vagrantdir_path = os.path.join(settings.TMPSPACE, username, boxname) #env = os.environ.copy() #env['PWD'] = self.vagrantdir_path #v.env = os_env log_cm = vagrant.make_file_cm( os.path.join(self.vagrantdir_path, 'deployment.log')) self.native_obj = vagrant.Vagrant( self.vagrantdir_path, out_cm=log_cm, err_cm=log_cm, quiet_stdout=False, quiet_stderr=False, #env=env ) #logger.debug("error_obj: ", error_obj) logger.debug( "Vagrantfile native object created: {}/{}".format( username, boxname), extra={'box': '{}/{}'.format(self.username, self.boxname)})
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
def inject_arca(self, arca): """ Apart from the usual validation stuff it also creates log file for this instance. """ super().inject_arca(arca) import vagrant self.log_path = Path(self._arca.base_dir) / "logs" / (str(uuid4()) + ".log") self.log_path.parent.mkdir(exist_ok=True, parents=True) logger.info("Storing vagrant log in %s", self.log_path) self.log_cm = vagrant.make_file_cm(self.log_path)
def _run_vagrant(environment): log_cm = vagrant.make_file_cm('vagrant.log') v = vagrant.Vagrant(out_cm=log_cm, err_cm=log_cm) vagrant_config_file = "%s/vagrant.yml" % environment if os.path.isfile(vagrant_config_file): _set_envvar("SETTINGS_FILE", vagrant_config_file) vagrant_config = yaml.load(open(vagrant_config_file, 'r')) else: vagrant_config = yaml.load(open('vagrant.yml', 'r')) vms = vagrant_config['vms'].keys() nothing_up = True log_cm = vagrant.make_file_cm('vagrant.log') for vm in v.status(): if vm.state == "running": nothing_up = False print "View Vagrant status at vagrant.log" if nothing_up: print "Starting %s" % vms v.up() else: for vm in vms: state = v.status(vm_name=vm)[0].state if state == "not_created": print "Starting %s" % vm v.up(vm_name=vm) rc = _vagrant_ssh_config(environment, vms) if rc: return rc print "**************************************************" print "Ursula <3 Vagrant" print "To interact with your environment via Vagrant set:" print "$ export SETTINGS_FILE=%s" % vagrant_config_file print "**************************************************" return 0
def __init__(self, *args, **kwargs): ''' Wrapper around the python-vagrant module for use with ansible. Note that Vagrant itself is non-thread safe, as is the python-vagrant lib, so we need to lock on basically all operations ... ''' log = kwargs.setdefault('log', False) self.config_code = kwargs.setdefault('config_code', "\n;\n;") self.share_folder = kwargs.setdefault('share_folder', ".") self.share_mount = kwargs.setdefault('share_mount', "/vagrant") self.provider = kwargs.setdefault('provider', "virtualbox") self.module = kwargs.setdefault('module', None) # Get a lock self.lock = None if not HAS_VAGRANT_LIBRARY: self.module.fail_json( msg=missing_required_lib('vagrant'), exception=VAGRANT_LIBRARY_IMPORT_ERROR) try: self.lock = lockfile.FileLock(VAGRANT_LOCKFILE) self.lock.acquire() except Exception: # fall back to using flock instead ... try: with open(VAGRANT_LOCKFILE, 'w') as self.lock: fcntl.flock(self.lock, fcntl.LOCK_EX) except Exception: self.module.debug( "failed=True msg='Could not get " + "a lock for using vagrant. Install python " + "module \"lockfile\" to use vagrant on non-POSIX filesytems.'" ) self.module.fail_json() # Initialize vagrant and state files vgargs = [] vgkwargs = dict(root=VAGRANT_ROOT) if log: log_cm = vagrant.make_file_cm(VAGRANT_LOGFILE) vgkwargs['out_cm'] = log_cm vgkwargs['err_cm'] = log_cm self.vg = vagrant.Vagrant(*vgargs, **vgkwargs) # operation will create a default data structure if none present self._deserialize() self._serialize()
def test_make_file_cm(): filename = os.path.join(TD, 'test.log') if os.path.exists(filename): os.remove(filename) # Test writing to the filehandle yielded by cm cm = vagrant.make_file_cm(filename) with cm() as fh: fh.write('one\n') with open(filename) as read_fh: assert read_fh.read() == 'one\n' # Test appending to the file yielded by cm with cm() as fh: fh.write('two\n') with open(filename) as read_fh: assert read_fh.read() == 'one\ntwo\n'
def test_make_file_cm(test_dir): filename = os.path.join(test_dir, "test.log") if os.path.exists(filename): os.remove(filename) # Test writing to the filehandle yielded by cm cm = vagrant.make_file_cm(filename) with cm() as fh: fh.write("one\n") with open(filename, encoding="utf-8") as read_fh: assert read_fh.read() == "one\n" # Test appending to the file yielded by cm with cm() as fh: fh.write("two\n") with open(filename, encoding="utf-8") as read_fh: assert read_fh.read() == "one\ntwo\n"
def run_provision_step(self, step, **kwargs): if self.resume: res = self.check_status(step) if res == True: print("Step {} already performed.. Skipping..".format(step)) return if res == None: self.setup_status_file() self.run_provision_step(step) self.check_log_dir() self.step_log = os.path.join(self.LOG_DIR, step + '-' + 'output.log') log_cm = vagrant.make_file_cm(self.step_log) print('Logging to file: {}'.format(self.step_log)) v = vagrant.Vagrant(out_cm=log_cm, err_cm=log_cm) try: v.up(provision_with=[step], **kwargs) self.update_status(step) except subprocess.CalledProcessError as e: print('Error: The provision step {} failed with error code {}.'. format(step, e.returncode), file=sys.stderr) self.dump_log() sys.exit(1)
def _create_log(self, which, std): log = self._log(which, std) unlink(log) return vagrant.make_file_cm(log)
from __future__ import print_function, with_statement from web_runner import * from my_utils.system import SystemUtils, retry_call import vagrant from fabric.api import * from fabric.network import disconnect_all import logging import os import re import tarfile import ConfigParser import time import subprocess # from my_utils.web import WebAgent log_cm = vagrant.make_file_cm('deployment.log') configuration_log = open('configuration.log', 'a+') installation_agent_log = open('installation_agent.log', 'a') work_path = os.getcwd() + "/" #Returns current directory, where script is run. box_work_path = "/home/vagrant" tar_name = "linux_qa.tar" config = work_path + "/cfg/vagrant_up.ini" config_web = work_path + "config.ini" class VagrantAutomation(SystemUtils, TestRunner): box_distro_name = None os_list = [] __logDir = 'Logs' box_log = '/box.log' box_log_object = None