def get_container_settings(name, status=None): """ returns a dict of all utils settings for a container status is optional and should be set to RUNNING to retrieve ipv4 config (if unset) """ filename = '{}/{}/config'.format(lxcdir(), name) if not file_exist(filename): return False config = ConfigParser.SafeConfigParser() config.readfp(FakeSection(open(filename))) cfg = {} # for each key in cgroup_ext add value to cfg dict and initialize values for options in cgroup_ext.keys(): if config.has_option('DEFAULT', cgroup_ext[options][0]): cfg[options] = config.get('DEFAULT', cgroup_ext[options][0]) else: cfg[options] = '' # add the key in dictionary anyway to match form # if ipv4 is unset try to determinate it if cfg['ipv4'] == '' and status == 'RUNNING': cmd = ['lxc-ls --fancy --fancy-format name,ipv4|grep -w \'%s\\s\' | awk \'{ print $2 }\'' % name] try: cfg['ipv4'] = subprocess.check_output(cmd, shell=True) except subprocess.CalledProcessError: cfg['ipv4'] = '' # parse memlimits to int cfg['memlimit'] = re.sub(r'[a-zA-Z]', '', cfg['memlimit']) cfg['swlimit'] = re.sub(r'[a-zA-Z]', '', cfg['swlimit']) return cfg
def push_config_value(key, value, container=None): """ replace a var in a container config file """ from lwp.utils import cgroup_ext def save_cgroup_devices(filename=None): """ returns multiple values (lxc.cgroup.devices.deny and lxc.cgroup.devices.allow) in a list. because ConfigParser cannot make this... """ if filename: values = [] i = 0 with open(filename, 'r') as load: read = load.readlines() while i < len(read): if not read[i].startswith('#'): if not (read[i] in values): if re.match( 'lxc.cgroup.devices.deny|lxc.cgroup.devices.allow|lxc.mount.entry|lxc.cap.drop', read[i]): values.append(read[i]) i += 1 return values if container: filename = '{}/{}/config'.format(lxcdir(), container) save = save_cgroup_devices(filename=filename) normalized = NormalizeConfig(filename).read() config = ConfigParser() config.readfp(normalized) if not value: config.remove_option('DEFAULT', key) elif key == cgroup_ext['memlimit'][ 0] or key == cgroup_ext['swlimit'][0] and value is not False: config.set('DEFAULT', key, '%sM' % value) else: config.set('DEFAULT', key, '%s' % value) # Bugfix (can't duplicate keys with config parser) if config.has_option('DEFAULT', cgroup_ext['deny'][0]): config.remove_option('DEFAULT', cgroup_ext['deny'][0]) if config.has_option('DEFAULT', cgroup_ext['allow'][0]): config.remove_option('DEFAULT', cgroup_ext['allow'][0]) if config.has_option('DEFAULT', 'lxc.cap.drop'): config.remove_option('DEFAULT', 'lxc.cap.drop') if config.has_option('DEFAULT', 'lxc.mount.entry'): config.remove_option('DEFAULT', 'lxc.mount.entry') with open(filename, 'w') as configfile: config.write(configfile) del_section(filename=filename) with open(filename, "a") as configfile: configfile.writelines(save)
def push_config_value(key, value, container=None): """ replace a var in a container config file """ def save_cgroup_devices(filename=None): """ returns multiple values (lxc.cgroup.devices.deny and lxc.cgroup.devices.allow) in a list. because ConfigParser cannot make this... """ if filename: values = [] i = 0 with open(filename, 'r') as load: read = load.readlines() while i < len(read): if not read[i].startswith('#'): if not (read[i] in values): if re.match('lxc.cgroup.devices.deny|lxc.cgroup.devices.allow|lxc.mount.entry|lxc.cap.drop', read[i]): values.append(read[i]) i += 1 return values if container: filename = '{}/{}/config'.format(lxcdir(), container) save = save_cgroup_devices(filename=filename) config = ConfigParser.RawConfigParser() config.readfp(FakeSection(open(filename))) if not value: config.remove_option('DEFAULT', key) elif key == cgroup_ext['memlimit'][0] or key == cgroup_ext['swlimit'][0] and value is not False: config.set('DEFAULT', key, '%sM' % value) else: config.set('DEFAULT', key, value) # Bugfix (can't duplicate keys with config parser) if config.has_option('DEFAULT', cgroup_ext['deny'][0]): config.remove_option('DEFAULT', cgroup_ext['deny'][0]) if config.has_option('DEFAULT', cgroup_ext['allow'][0]): config.remove_option('DEFAULT', cgroup_ext['allow'][0]) if config.has_option('DEFAULT', 'lxc.cap.drop'): config.remove_option('DEFAULT', 'lxc.cap.drop') if config.has_option('DEFAULT', 'lxc.mount.entry'): config.remove_option('DEFAULT', 'lxc.mount.entry') with open(filename, 'wb') as configfile: config.write(configfile) del_section(filename=filename) with open(filename, "a") as configfile: configfile.writelines(save)
def host_disk_usage(partition=None): """ returns a dict of disk usage values {'total': usage[1], 'used': usage[2], 'free': usage[3], 'percent': usage[4]} """ partition = lxcdir() usage = subprocess.check_output(['df -h %s' % partition], shell=True).split('\n')[1].split() return {'total': usage[1], 'used': usage[2], 'free': usage[3], 'percent': usage[4]}
def get_container_settings(name): """ returns a dict of all utils settings for a container """ filename = '{}/{}/config'.format(lxc.lxcdir(), name) if not file_exist(filename): return False config = ConfigParser.SafeConfigParser() config.readfp(FakeSection(open(filename))) # start with bucket token, as this is not from the config file cfg = {'bucket': get_bucket_token(name)} # for each key in CGROUP_EXT add value to cfg dict and initialize values for options in CGROUP_EXT.keys(): if config.has_option('DEFAULT', CGROUP_EXT[options][0]): cfg[options] = config.get('DEFAULT', CGROUP_EXT[options][0]) else: # add the key in dictionary anyway to match form if options == 'loglevel': cfg[options] = '5' # default loglevel is 5 (error) else: cfg[options] = '' # default for all other missing fields # if ipv4 is unset try to determine it if cfg['ipv4'] == '': cmd = ['lxc-ls --fancy --fancy-format name,ipv4|grep \'%s\' |egrep -o \'[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\'' % name] try: cfg['ipv4'] = subprocess.check_output(cmd, shell=True) except subprocess.CalledProcessError: cfg['ipv4'] = '' # parse memlimits to ints cfg['memlimit'] = re.sub(r'[a-zA-Z]', '', cfg['memlimit']) cfg['swlimit'] = re.sub(r'[a-zA-Z]', '', cfg['swlimit']) # the form requires these to be actual ints if cfg['memlimit'] != '': cfg['memlimit'] = int(cfg['memlimit']) if cfg['swlimit'] != '': cfg['swlimit'] = int(cfg['swlimit']) # convert internal lxc values to boolean cfg['start_auto'] = True if cfg['start_auto'] is '1' else False cfg['flags'] = True if cfg['flags'] == 'up' else False return cfg