def host_config(self): """ Ensure the host configuration file exists """ if platform.system() == 'Darwin': default_file_dir = join(expanduser('~'), 'vent_files') else: default_file_dir = '/opt/vent_files' status = self.ensure_dir(default_file_dir) if not isfile(self.cfg_file): config = Template(template=self.cfg_file) sections = { 'main': { 'files': default_file_dir }, 'network-mapping': {}, 'nvidia-docker-plugin': { 'port': '3476' } } for s in sections: if sections[s]: for option in sections[s]: config.add_option(s, option, sections[s][option]) else: config.add_section(s) config.write_config() return status
def test_del_option(): """ Test the del_option function """ instance = Template() instance.del_option('foo', 'bar') instance.add_option('foo', 'bar2') instance.del_option('foo', 'bar2') instance.del_option('foo', 'bar2')
def host_config(self): """ Ensure the host configuration file exists """ if platform.system() == 'Darwin': default_file_dir = join(expanduser('~'), 'vent_files') else: default_file_dir = '/opt/vent_files' status = self.ensure_dir(default_file_dir) if not isfile(self.cfg_file): config = Template(template=self.cfg_file) sections = {'main': {'files': default_file_dir}, 'network-mapping': {}, 'nvidia-docker-plugin': {'port': '3476'}} for s in sections: if sections[s]: for option in sections[s]: config.add_option(s, option, sections[s][option]) else: config.add_section(s) config.write_config() return status
def _startup(self): """ Automatically detect if a startup file is specified and stand up a vent host with all necessary tools based on the specifications in that file """ status = (True, None) try: s_dict = {} # rewrite the yml file to exclusively lowercase with open(self.startup_file, 'r') as sup: vent_startup = sup.read() with open(self.startup_file, 'w') as sup: for line in vent_startup: sup.write(line.lower()) with open(self.startup_file, 'r') as sup: s_dict = yaml.safe_load(sup.read()) if 'vent.cfg' in s_dict: v_cfg = Template(self.vent_config) for section in s_dict['vent.cfg']: for option in s_dict['vent.cfg'][section]: val = ('no', 'yes')[ s_dict['vent.cfg'][section][option]] v_status = v_cfg.add_option(section, option, value=val) if not v_status[0]: v_cfg.set_option(section, option, val) v_cfg.write_config() del s_dict['vent.cfg'] tool_d = {} extra_options = ['info', 'service', 'settings', 'docker', 'gpu'] s_dict_c = copy.deepcopy(s_dict) # TODO check for repo or image type for repo in s_dict_c: repository = Repository(System().manifest) repository.repo = repo repository._clone() repo_path, org, r_name = self.path_dirs.get_path(repo) get_tools = [] for tool in s_dict_c[repo]: t_branch = 'master' t_version = 'HEAD' if 'branch' in s_dict[repo][tool]: t_branch = s_dict[repo][tool]['branch'] if 'version' in s_dict[repo][tool]: t_version = s_dict[repo][tool]['version'] get_tools.append((tool, t_branch, t_version)) available_tools = AvailableTools(repo_path, tools=get_tools) for tool in s_dict_c[repo]: # if we can't find the tool in that repo, skip over this # tool and notify in the logs t_path, t_path_cased = PathDirs.rel_path( tool, available_tools) if t_path is None: self.logger.error("Couldn't find tool " + tool + ' in' ' repo ' + repo) continue # ensure no NoneType iteration errors if s_dict_c[repo][tool] is None: s_dict[repo][tool] = {} # check if we need to configure instances along the way instances = 1 if 'settings' in s_dict[repo][tool]: if 'instances' in s_dict[repo][tool]['settings']: instances = int(s_dict[repo][tool] ['settings']['instances']) # add the tool t_branch = 'master' t_version = 'HEAD' t_image = None add_tools = None add_tools = [(t_path_cased, '')] if 'branch' in s_dict[repo][tool]: t_branch = s_dict[repo][tool]['branch'] if 'version' in s_dict[repo][tool]: t_version = s_dict[repo][tool]['version'] if 'image' in s_dict[repo][tool]: t_image = s_dict[repo][tool]['image'] repository.add( repo, tools=add_tools, branch=t_branch, version=t_version, image_name=t_image) manifest = Template(self.manifest) # update the manifest with extra defined runtime settings base_section = ':'.join([org, r_name, t_path, t_branch, t_version]) for option in extra_options: if option in s_dict[repo][tool]: opt_dict = manifest.option(base_section, option) # add new values defined into default options for # that tool, don't overwrite them if opt_dict[0]: opt_dict = json.loads(opt_dict[1]) else: opt_dict = {} # stringify values for vent for v in s_dict[repo][tool][option]: pval = s_dict[repo][tool][option][v] s_dict[repo][tool][option][v] = json.dumps( pval) opt_dict.update(s_dict[repo][tool][option]) manifest.set_option(base_section, option, json.dumps(opt_dict)) # copy manifest info into new sections if necessary if instances > 1: for i in range(2, instances + 1): i_section = base_section.rsplit(':', 2) i_section[0] += str(i) i_section = ':'.join(i_section) manifest.add_section(i_section) for opt_val in manifest.section(base_section)[1]: if opt_val[0] == 'name': manifest.set_option(i_section, opt_val[0], opt_val[1] + str(i)) else: manifest.set_option(i_section, opt_val[0], opt_val[1]) manifest.write_config() tool_d = {} tools = Tools() # start tools, if necessary for repo in s_dict: for tool in s_dict[repo]: if 'start' in s_dict[repo][tool]: if s_dict[repo][tool]['start']: local_instances = 1 if 'settings' in s_dict[repo][tool] and 'instances' in s_dict[repo][tool]['settings']: local_instances = int( s_dict[repo][tool]['settings']['instances']) t_branch = 'master' t_version = 'HEAD' if 'branch' in s_dict[repo][tool]: t_branch = s_dict[repo][tool]['branch'] if 'version' in s_dict[repo][tool]: t_version = s_dict[repo][tool]['version'] for i in range(1, local_instances + 1): i_name = tool + str(i) if i != 1 else tool i_name = i_name.replace('@', '') tool_d.update( tools._prep_start(repo, i_name)[1]) if tool_d: tools.start(tool_d, None, is_tool_d=True) except Exception as e: # pragma: no cover self.logger.error('Startup failed because: {0}'.format(str(e))) status = (False, str(e)) return status
def _startup(self): """ Automatically detect if a startup file is specified and stand up a vent host with all necessary tools based on the specifications in that file """ status = (True, None) try: s_dict = {} # rewrite the yml file to exclusively lowercase with open(self.startup_file, 'r') as sup: vent_startup = sup.read() with open(self.startup_file, 'w') as sup: for line in vent_startup: sup.write(line.lower()) with open(self.startup_file, 'r') as sup: s_dict = yaml.safe_load(sup.read()) if 'vent.cfg' in s_dict: v_cfg = Template(self.vent_config) for section in s_dict['vent.cfg']: for option in s_dict['vent.cfg'][section]: val = ('no', 'yes')[ s_dict['vent.cfg'][section][option]] v_status = v_cfg.add_option(section, option, value=val) if not v_status[0]: v_cfg.set_option(section, option, val) v_cfg.write_config() del s_dict['vent.cfg'] tool_d = {} extra_options = ['info', 'service', 'settings', 'docker', 'gpu'] s_dict_c = copy.deepcopy(s_dict) # TODO check for repo or image type for repo in s_dict_c: repository = Repository(System().manifest) repository.repo = repo repository._clone() repo_path, org, r_name = self.path_dirs.get_path(repo) get_tools = [] for tool in s_dict_c[repo]: t_branch = 'master' t_version = 'HEAD' if 'branch' in s_dict[repo][tool]: t_branch = s_dict[repo][tool]['branch'] if 'version' in s_dict[repo][tool]: t_version = s_dict[repo][tool]['version'] get_tools.append((tool, t_branch, t_version)) available_tools = AvailableTools(repo_path, tools=get_tools) for tool in s_dict_c[repo]: # if we can't find the tool in that repo, skip over this # tool and notify in the logs t_path, t_path_cased = PathDirs.rel_path( tool, available_tools) if t_path is None: self.logger.error("Couldn't find tool " + tool + ' in' ' repo ' + repo) continue # ensure no NoneType iteration errors if s_dict_c[repo][tool] is None: s_dict[repo][tool] = {} # check if we need to configure instances along the way instances = 1 if 'settings' in s_dict[repo][tool]: if 'instances' in s_dict[repo][tool]['settings']: instances = int(s_dict[repo][tool] ['settings']['instances']) # add the tool t_branch = 'master' t_version = 'HEAD' t_image = None add_tools = None add_tools = [(t_path_cased, '')] if 'branch' in s_dict[repo][tool]: t_branch = s_dict[repo][tool]['branch'] if 'version' in s_dict[repo][tool]: t_version = s_dict[repo][tool]['version'] if 'image' in s_dict[repo][tool]: t_image = s_dict[repo][tool]['image'] repository.add( repo, add_tools, branch=t_branch, version=t_version, image_name=t_image) manifest = Template(self.manifest) # update the manifest with extra defined runtime settings base_section = ':'.join([org, r_name, t_path, t_branch, t_version]) for option in extra_options: if option in s_dict[repo][tool]: opt_dict = manifest.option(base_section, option) # add new values defined into default options for # that tool, don't overwrite them if opt_dict[0]: opt_dict = json.loads(opt_dict[1]) else: opt_dict = {} # stringify values for vent for v in s_dict[repo][tool][option]: pval = s_dict[repo][tool][option][v] s_dict[repo][tool][option][v] = json.dumps( pval) opt_dict.update(s_dict[repo][tool][option]) manifest.set_option(base_section, option, json.dumps(opt_dict)) # copy manifest info into new sections if necessary if instances > 1: for i in range(2, instances + 1): i_section = base_section.rsplit(':', 2) i_section[0] += str(i) i_section = ':'.join(i_section) manifest.add_section(i_section) for opt_val in manifest.section(base_section)[1]: if opt_val[0] == 'name': manifest.set_option(i_section, opt_val[0], opt_val[1] + str(i)) else: manifest.set_option(i_section, opt_val[0], opt_val[1]) manifest.write_config() tool_d = {} tools = Tools() # start tools, if necessary for repo in s_dict: for tool in s_dict[repo]: if 'start' in s_dict[repo][tool]: if s_dict[repo][tool]['start']: local_instances = 1 if 'settings' in s_dict[repo][tool] and 'instances' in s_dict[repo][tool]['settings']: local_instances = int( s_dict[repo][tool]['settings']['instances']) t_branch = 'master' t_version = 'HEAD' if 'branch' in s_dict[repo][tool]: t_branch = s_dict[repo][tool]['branch'] if 'version' in s_dict[repo][tool]: t_version = s_dict[repo][tool]['version'] for i in range(1, local_instances + 1): i_name = tool + str(i) if i != 1 else tool i_name = i_name.replace('@', '') tool_d.update( tools._prep_start(repo, i_name)[1]) if tool_d: tools.start(tool_d, None, is_tool_d=True) except Exception as e: # pragma: no cover self.logger.error('Startup failed because: {0}'.format(str(e))) status = (False, str(e)) return status
def test_add_option(): """ Test the add_option function """ instance = Template() instance.add_option('foo', 'bar') instance.add_option('foo', 'bar') instance.add_option('bad', 'x')