def prepare_vm(battery_dest,fields=None,vm_repo=None,vm_type="vagrant"): '''prepare_vm Prepare virtual machine to run local with vagrant, or with vagrant-aws :param battery_dest: the battery destination folder :param fields: if specified, will be added to the config.txt :param vm_repo: the expfactory-vm repo to use for templates. If not provided, one will be downloaded to a temporary directory for use. :param vm_type: one of "vagrant" or "aws" Default is "vagrant" meaning a localvirtual machine with vagrant. ''' # Download vm_repo if it hasn't been specified if not vm_repo: download_repo("vm","%s/vm" %battery_dest) # Grab the appropriate vagrantfile template if vm_type == "aws": template = get_template("%s/vm/VagrantfileAWS" %(battery_dest)) else: template = get_template("%s/vm/VagrantfileLocal" %(battery_dest)) # If the user has custom config fields, write to file if fields != None: fields = clean_fields(fields) template = sub_template(template,"[SUB_CONFIG_SUB]",str(fields)) else: template = sub_template(template,"[SUB_CONFIG_SUB]","None") # Change file to be a custom install template = sub_template(template,'CUSTOM_INSTALL="False"','CUSTOM_INSTALL="True"') output_file = "%s/Vagrantfile" %(battery_dest) save_template(output_file,template) return template
def prepare_vm(battery_dest, fields=None, vm_repo=None, vm_type="vagrant"): '''prepare_vm Prepare virtual machine to run local with vagrant, or with vagrant-aws :param battery_dest: the battery destination folder :param fields: if specified, will be added to the config.txt :param vm_repo: the expfactory-vm repo to use for templates. If not provided, one will be downloaded to a temporary directory for use. :param vm_type: one of "vagrant" or "aws" Default is "vagrant" meaning a localvirtual machine with vagrant. ''' # Download vm_repo if it hasn't been specified if not vm_repo: download_repo("vm", "%s/vm" % battery_dest) # Grab the appropriate vagrantfile template if vm_type == "aws": template = get_template("%s/vm/VagrantfileAWS" % (battery_dest)) else: template = get_template("%s/vm/VagrantfileLocal" % (battery_dest)) # If the user has custom config fields, write to file if fields != None: fields = clean_fields(fields) template = sub_template(template, "[SUB_CONFIG_SUB]", str(fields)) else: template = sub_template(template, "[SUB_CONFIG_SUB]", "None") # Change file to be a custom install template = sub_template(template, 'CUSTOM_INSTALL="False"', 'CUSTOM_INSTALL="True"') output_file = "%s/Vagrantfile" % (battery_dest) save_template(output_file, template) return template
def specify_experiments(battery_dest,experiments): '''specify_experiments Specify experiments for a Vagrantfile in an output folder :param battery_dest: destination folder for battery :param experiments: a list of experiment tags to include ''' experiments = [e.encode("utf-8") for e in experiments] vagrantfile = "%s/Vagrantfile" %(battery_dest) if not os.path.exists(vagrantfile): prepare_vm(battery_dest) template = get_template(vagrantfile) template = sub_template(template,"[SUB_EXPERIMENTS_SUB]",str(experiments)) save_template(vagrantfile,template) return template
def specify_experiments(battery_dest, experiments): '''specify_experiments Specify experiments for a Vagrantfile in an output folder :param battery_dest: destination folder for battery :param experiments: a list of experiment tags to include ''' experiments = [e.encode("utf-8") for e in experiments] vagrantfile = "%s/Vagrantfile" % (battery_dest) if not os.path.exists(vagrantfile): prepare_vm(battery_dest) template = get_template(vagrantfile) template = sub_template(template, "[SUB_EXPERIMENTS_SUB]", str(experiments)) save_template(vagrantfile, template) return template
def generate_config(battery_dest, fields): '''generate_config takes a dictionary, and for matching fields, substitues and prints to "config.txt" in a specified battery directory :param battery_dest: should be the copied, skeleton battery folder in generation :param fields: should be a dictionary with fields that match those in the config non matching fields will be ignored. ''' config = get_config() # Convert dictionaries back to string for l in range(len(config)): line = config[l] if isinstance(line, dict): linekey = line.keys()[0] if linekey in fields.keys(): config[l][linekey] = fields[linekey] config[l] = "%s = %s" % (linekey, config[l][linekey]) config = "\n".join(config) save_template("%s/config.txt" % battery_dest, config) return config
def generate_config(battery_dest,fields): '''generate_config takes a dictionary, and for matching fields, substitues and prints to "config.txt" in a specified battery directory :param battery_dest: should be the copied, skeleton battery folder in generation :param fields: should be a dictionary with fields that match those in the config non matching fields will be ignored. ''' config = get_config() # Convert dictionaries back to string for l in range(len(config)): line = config[l] if isinstance(line,dict): linekey = line.keys()[0] if linekey in fields.keys(): config[l][linekey] = fields[linekey] config[l] = "%s = %s" %(linekey,config[l][linekey]) config = "\n".join(config) save_template("%s/config.txt" %battery_dest,config) return config
def main(args, parser, subparser): folder = args.folder if folder is None: folder = os.getcwd() source = args.src[0] if source is None: bot.error('Please provide a Github https address to install.') sys.exit(1) # Is the experiment valid? cli = ExperimentValidator() valid = cli.validate(source, cleanup=False) exp_id = os.path.basename(source).replace('.git', '') if valid is True: # Local Install if os.path.exists(source): config = load_experiment(source) source = os.path.abspath(source) else: config = load_experiment("%s/%s" % (cli.tmpdir, exp_id)) source = "%s/%s" % (cli.tmpdir, exp_id) exp_id = config['exp_id'] python_module = exp_id.replace('-', '_').lower() else: bot.error('%s is not valid.' % exp_id) sys.exit(1) # Move static files to output folder dest = "%s/%s" % (folder, exp_id) bot.log("Installing %s to %s" % (exp_id, dest)) # Building container in_container = False if os.environ.get('SINGULARITY_IMAGE') is not None: in_container = True # Running, live container elif os.environ.get('EXPFACTORY_CONTAINER') is not None: in_container = True if in_container is True: # if in container, we always force args.force = True bot.log("Preparing experiment routes...") template = get_template('experiments/template.py') template = sub_template(template, '{{ exp_id }}', exp_id) template = sub_template(template, '{{ exp_id_python }}', python_module) # 1. Python blueprint views = get_viewsdir(base=args.base) view_output = "%s/%s.py" % (views, python_module) save_template(view_output, template, base=views) # 2. append to __init__ init = "%s/__init__.py" % views with open(init, 'a') as filey: filey.writelines('from .%s import *\n' % python_module) # 3. Instructions if "instructions" in config: instruct = "%s/%s.help" % (views, python_module) with open(instruct, 'w') as filey: filey.writelines(config['instructions']) if not os.path.exists(dest): os.system('mkdir -p %s' % dest) else: if args.force is False: bot.error('%s is not empty! Use --force to delete and re-create.' % folder) sys.exit(1) # We don't need to copy if experiment already there if source != dest: os.system('cp -R %s/* %s' % (source, dest))
def main(args,parser,subparser): folder = args.folder if folder is None: folder = os.getcwd() source = args.src[0] if source is None: bot.error('Please provide a Github https address to install.') sys.exit(1) # Is the experiment valid? cli = ExperimentValidator() valid = cli.validate(source, cleanup=False) exp_id = os.path.basename(source).replace('.git','') if valid is True: # Local Install if os.path.exists(source): config = load_experiment(source) source = os.path.abspath(source) else: config = load_experiment("%s/%s" %(cli.tmpdir,exp_id)) source = "%s/%s" %(cli.tmpdir,exp_id) exp_id = config['exp_id'] python_module = exp_id.replace('-','_').lower() else: bot.error('%s is not valid.' % exp_id) sys.exit(1) # Move static files to output folder dest = "%s/%s" %(folder,exp_id) bot.log("Installing %s to %s" %(exp_id, dest)) # Building container in_container = False if os.environ.get('SINGULARITY_IMAGE') is not None: in_container = True # Running, live container elif os.environ.get('EXPFACTORY_CONTAINER') is not None: in_container = True if in_container is True: # if in container, we always force args.force = True bot.log("Preparing experiment routes...") template = get_template('experiments/template.py') template = sub_template(template, '{{ exp_id }}', exp_id) template = sub_template(template, '{{ exp_id_python }}', python_module) # 1. Python blueprint views = get_viewsdir(base=args.base) view_output = "%s/%s.py" %(views, python_module) save_template(view_output, template, base=views) # 2. append to __init__ init = "%s/__init__.py" % views with open(init,'a') as filey: filey.writelines('from .%s import *\n' %python_module) # 3. Instructions if "instructions" in config: instruct = "%s/%s.help" %(views, python_module) with open(instruct,'w') as filey: filey.writelines(config['instructions']) if not os.path.exists(dest): os.system('mkdir -p %s' %dest) else: if args.force is False: bot.error('%s is not empty! Use --force to delete and re-create.' %folder) sys.exit(1) # We don't need to copy if experiment already there if source != dest: os.system('cp -R %s/* %s' %(source, dest))