def override(args, overrides): """override optional arguments with arguments from the config file""" args_dict = vars(args) for key, value in overrides['args'].items(): if key == 'config': raise utils.FatalException( 'argument "%s" specified in the config ' 'override could not be used because it needs to be specified ' 'on the command line' % key) if key == 'variables': if type(value) == dict: value = [(name, data) for name, data in value.items()] if key not in args_dict: raise utils.FatalException('argument "%s" specified in the config ' 'override is not a valid argument' % key) elif not value: pass elif type(args_dict[key]) == list: value = args_dict[key] + value setattr(args, key, value) elif getattr(args, key): raise utils.FatalException( 'argument "%s" has been defined on the ' 'command line and in the config override, omit one of those' % key) else: setattr(args, key, value) return args
def script(self, dst, overlay, prefix): """generate the permissions script which will be applied before the end of an installation""" path = os.path.join(self.cfg['paths']['overlays'], overlay) perm_file = path + '.permissions' perm_script = os.path.join(dst, 'fix_perms.sh') if os.path.isfile(perm_file): data = utils.file_read(perm_file).strip() lines = data.split('\n') lines = [line for line in lines if not line.startswith('#')] script = [] for line in lines: try: mode, uid, gid, real_path = line.split('\t') except ValueError: err = '%s is corrupt, delete or regenerate it with '\ 'the "seedbank manage --overlay" command, or fix the '\ 'file manually, line "%s" contains errors' % \ (perm_file, line) raise utils.FatalException(err) else: if prefix: real_path = os.path.join(prefix, real_path[1:]) if real_path.endswith('.sb_template'): real_path = os.path.splitext(real_path)[0] script.append('chown %s:%s %s' % (uid, gid, real_path)) script.append('chmod %s %s' % (mode, real_path)) utils.file_write(perm_script, '\n'.join(script)) else: logging.warning( 'overlay "%s" has been selected but permission ' 'file "%s" does not exist, so all files will be owned by root ' 'and will keep the current permissons which could lead to ' 'problems', overlay, perm_file) utils.file_write(perm_script, '')
def __init__(self, cfg, overlay, fqdn): """set the configuration variable, source and destination paths""" self.cfg = cfg self.dst = os.path.join(self.cfg['paths']['temp'], 'seedbank', fqdn, 'overlay') self.path = os.path.join(self.cfg['paths']['overlays'], overlay) if not os.path.isdir(self.path): err = 'overlay directory "%s" does not exist' % self.path raise utils.FatalException(err)
def parse_cfg(): """validate the configuration""" settings = None override_files = ['./settings.yaml', '~/.seedbank/settings.yaml'] settings_file_name = '/etc/seedbank/settings.yaml' files = override_files + [settings_file_name] for file_name in files: if os.path.isfile(file_name): settings_file = file_name break settings = utils.yaml_read(settings_file) if not settings: err = 'can not find a settings.yaml file (%s)' % ', '.join(files) raise utils.FatalException(err) path = settings['settings']['configuration_path'] if not os.path.isdir(path): raise utils.FatalException('directory "%s" does not exist' % path) if settings_file in override_files: logging.info( 'found settings file "%s", will use this settings file ' 'instead of the default (%s)', settings_file, settings_file_name) files = os.listdir(path) files = [file_name for file_name in files if file_name.endswith('.yaml')] files = [os.path.join(path, file_name) for file_name in files] cfg = utils.yaml_read(files) cfg.update(settings) distributions = ['debian', 'ubuntu'] isos = [] netboots = [] for distribution in distributions: if 'isos' in cfg[distribution]: isos = list_isos(cfg, distribution, isos) if 'netboots' in cfg[distribution]: netboots = list_netboots(cfg, distribution, netboots) cfg['netboots'] = netboots cfg['isos'] = isos return cfg
def _extract_debs(self, directory): """extract files from all debian packages in a directory""" pattern = self.cfg['debian']['firmware_filter'] os.chdir(directory) for file_name in os.listdir(directory): if fnmatch.fnmatch(file_name, pattern): result = utils.call(['dpkg', '-x', file_name, 'temp']) if result: err = 'failed to extract package "%s"' % file_name raise utils.FatalException(err) logging.info('extracted "%s"', file_name)
def generate(self): """generate the pxe boot file""" self.pxe_variables.update({ 'config': self.config, 'seeds': self.seeds, 'seed_host': cfg['settings']['seed_host'], 'seed_port': cfg['settings']['bottle_port'], 'address': self.address, 'overlay': self.overlay, 'puppet_manifests': self.puppet, 'host_name': self.host_name, 'dns_domain': self.dns_domain, 'fqdn': self.fqdn, 'query': urllib.urlencode([('address', self.address)]), 'date_generated': utils.date_time(), 'date_disabled': '', 'kernel': '%s/%s/%s' % ('seedbank', self.release, 'linux'), 'initrd': '%s/%s/%s' % ('seedbank', self.release, 'initrd.gz') }) if self.config: yaml_file = os.path.join(cfg['paths']['configs'], self.config) yaml_file = yaml_file + '.yaml' overrides = utils.yaml_read(yaml_file) if 'pxe' in overrides: cfg['pxe'].update(overrides['pxe']) values = cfg['pxe'] self.pxe_variables.update(values) distribution = self.release.split('-')[0] file_name = cfg[distribution]['template_pxe'] file_name = os.path.join(cfg['paths']['templates'], file_name) if not os.path.isfile(file_name): err = 'file "%s" does not exist (hint: check the templates '\ 'section in your settings)' % file_name raise utils.FatalException(err) pxe_variables_custom = [] for variable in self.variables: key, value = variable pxe_variables_custom.append('# %s = %s' % (key, value)) self.pxe_variables[key] = value pxe_variables_custom = '\n'.join(pxe_variables_custom) data = utils.file_read(file_name) data = utils.apply_template(data, self.pxe_variables, file_name) if pxe_variables_custom: data = re.sub('(#\n# \*\*\* end - seedBank pxe variables \*\*\*)', pxe_variables_custom + '\n\\1', data) return data