def emit_cephconf(): cephcontext = { 'mon_hosts': config('monitor-hosts'), 'fsid': config('fsid'), 'use_syslog': str(config('use-syslog')).lower(), 'loglevel': config('loglevel'), } # Install ceph.conf as an alternative to support # co-existence with other charms that write this file charm_ceph_conf = "/var/lib/charm/{}/ceph.conf".format(service_name()) mkdir(os.path.dirname(charm_ceph_conf), owner=ceph.ceph_user(), group=ceph.ceph_user()) render('ceph.conf', charm_ceph_conf, cephcontext, perms=0o644) install_alternative('ceph.conf', '/etc/ceph/ceph.conf', charm_ceph_conf, 100) keyring = 'ceph.client.admin.keyring' keyring_path = '/etc/ceph/' + keyring render(keyring, keyring_path, {'admin_key': config('admin-key')}, owner=ceph.ceph_user(), perms=0o600) keyring = 'keyring' keyring_path = '/var/lib/ceph/mon/ceph-' + get_unit_hostname()+ '/' + keyring render('mon.keyring', keyring_path, {'admin_key': config('admin-key')}, owner=ceph.ceph_user(), perms=0o600) notify_radosgws() notify_client()
def bootstrap_monitor_cluster(secret): hostname = get_unit_hostname() path = '/var/lib/ceph/mon/ceph-{}'.format(hostname) done = '{}/done'.format(path) upstart = '{}/upstart'.format(path) keyring = '/var/lib/ceph/tmp/{}.mon.keyring'.format(hostname) if os.path.exists(done): log('bootstrap_monitor_cluster: mon already initialized.') else: # Ceph >= 0.61.3 needs this for ceph-mon fs creation mkdir('/var/run/ceph', perms=0o755) mkdir(path) # end changes for Ceph >= 0.61.3 try: subprocess.check_call(['ceph-authtool', keyring, '--create-keyring', '--name=mon.', '--add-key={}'.format(secret), '--cap', 'mon', 'allow *']) subprocess.check_call(['ceph-mon', '--mkfs', '-i', hostname, '--keyring', keyring]) with open(done, 'w'): pass with open(upstart, 'w'): pass service_restart('ceph-mon-all') except: raise finally: os.unlink(keyring)
def get_availability_zones(show_all=False): """Get information about the availability zones. Returns dictionary contains the unit as the current unit and other_units (if the action was executed with the parameter show-all) that provide information about other units. :param show_all: define whether the result should contain AZ information for all units :type show_all: bool :returns: {"unit": <current-unit-AZ>, "all-units": {<unit-hostname>: <unit-AZ>}} :rtype: Dict[str, Dict[str, str]] """ results = {"unit": {}, "all-units": {}} osd_tree = get_osd_tree(service="osd-upgrade") this_unit_host = get_unit_hostname() for crush_location in osd_tree: crush_map = _get_crush_map(crush_location) if this_unit_host == crush_location.name: results["unit"] = crush_map results["all-units"][crush_location.name] = crush_map if not show_all: results.pop("all-units") return results
def bootstrap_monitor_cluster(): hostname = utils.get_unit_hostname() done = '/var/lib/ceph/mon/ceph-{}/done'.format(hostname) secret = utils.config_get('monitor-secret') keyring = '/var/lib/ceph/tmp/{}.mon.keyring'.format(hostname) if os.path.exists(done): utils.juju_log('INFO', 'bootstrap_monitor_cluster: mon already initialized.') else: try: subprocess.check_call(['ceph-authtool', keyring, '--create-keyring', '--name=mon.', '--add-key={}'.format(secret), '--cap', 'mon', 'allow *']) subprocess.check_call(['ceph-mon', '--mkfs', '-i', hostname, '--keyring', keyring]) with open(done, 'w'): pass subprocess.check_call(['start', 'ceph-mon-all-starter']) except: raise finally: os.unlink(keyring)
def get_named_key(name, caps=None): caps = caps or _default_caps cmd = [ 'ceph', '--name', 'mon.', '--keyring', '/var/lib/ceph/mon/ceph-{}/keyring'.format(utils.get_unit_hostname()), 'auth', 'get-or-create', 'client.{}'.format(name), ] # Add capabilities for subsystem, subcaps in caps.iteritems(): cmd.extend([ subsystem, '; '.join(subcaps), ]) output = subprocess.check_output(cmd).strip() # IGNORE:E1103 # get-or-create appears to have different output depending # on whether its 'get' or 'create' # 'create' just returns the key, 'get' is more verbose and # needs parsing key = None if len(output.splitlines()) == 1: key = output else: for element in output.splitlines(): if 'key' in element: key = element.split(' = ')[1].strip() # IGNORE:E1103 return key
def is_quorum(): asok = "/var/run/ceph/ceph-mon.{}.asok".format(get_unit_hostname()) cmd = [ "sudo", "-u", ceph_user(), "ceph", "--admin-daemon", asok, "mon_status" ] if os.path.exists(asok): try: result = json.loads(subprocess.check_output(cmd)) except subprocess.CalledProcessError: return False except ValueError: # Non JSON response from mon_status return False if result['state'] in QUORUM: return True else: return False else: return False
def get_named_key(name, caps=None): config_user_key = _config_user_key(name) if config_user_key: return config_user_key caps = caps or _default_caps cmd = [ "sudo", "-u", ceph_user(), 'ceph', '--name', config('admin-user'), '--keyring', '/var/lib/ceph/mon/ceph-{}/keyring'.format( get_unit_hostname() ), 'auth', 'get-or-create', 'client.{}'.format(name), ] # Add capabilities for subsystem, subcaps in caps.items(): cmd.extend([ subsystem, '; '.join(subcaps), ]) return parse_key(subprocess .check_output(cmd) .decode('utf-8') .strip()) # IGNORE:E1103
def get_named_key(name, caps=None): config_user_key = _config_user_key(name) if config_user_key: return config_user_key caps = caps or _default_caps cmd = [ "sudo", "-u", ceph_user(), 'ceph', '--name', config('admin-user'), '--keyring', '/var/lib/ceph/mon/ceph-{}/keyring'.format(get_unit_hostname()), 'auth', 'get-or-create', 'client.{}'.format(name), ] # Add capabilities for subsystem, subcaps in caps.items(): cmd.extend([ subsystem, '; '.join(subcaps), ]) return parse_key( subprocess.check_output(cmd).decode('utf-8').strip()) # IGNORE:E1103
def is_leader(): asok = "/var/run/ceph/ceph-mon.{}.asok".format(get_unit_hostname()) cmd = [ "sudo", "-u", ceph_user(), "ceph", "--admin-daemon", asok, "mon_status" ] if os.path.exists(asok): try: result = json.loads(subprocess.check_output(cmd)) except subprocess.CalledProcessError: return False except ValueError: # Non JSON response from mon_status return False if result['state'] == LEADER: return True else: return False else: return False
def get_named_key(name, caps=None): caps = caps or _default_caps cmd = [ 'ceph', '--name', 'mon.', '--keyring', '/var/lib/ceph/mon/ceph-{}/keyring'.format( utils.get_unit_hostname() ), 'auth', 'get-or-create', 'client.{}'.format(name), ] # Add capabilities for subsystem, subcaps in caps.iteritems(): cmd.extend([ subsystem, '; '.join(subcaps), ]) output = subprocess.check_output(cmd).strip() # IGNORE:E1103 # get-or-create appears to have different output depending # on whether its 'get' or 'create' # 'create' just returns the key, 'get' is more verbose and # needs parsing key = None if len(output.splitlines()) == 1: key = output else: for element in output.splitlines(): if 'key' in element: key = element.split(' = ')[1].strip() # IGNORE:E1103 return key
def emit_cephconf(): cephcontext = { 'auth_supported': config('auth-supported'), 'mon_hosts': config('monitor-hosts'), 'fsid': config('fsid'), 'use_syslog': str(config('use-syslog')).lower(), 'loglevel': config('loglevel'), } # Install ceph.conf as an alternative to support # co-existence with other charms that write this file charm_ceph_conf = "/var/lib/charm/{}/ceph.conf".format(service_name()) mkdir(os.path.dirname(charm_ceph_conf), owner=ceph.ceph_user(), group=ceph.ceph_user()) render('ceph.conf', charm_ceph_conf, cephcontext, perms=0o644) install_alternative('ceph.conf', '/etc/ceph/ceph.conf', charm_ceph_conf, 100) keyring = 'ceph.client.admin.keyring' keyring_path = '/etc/ceph/' + keyring ctx = {'admin_key': config('admin-key')} user = ceph.ceph_user() render(keyring, keyring_path, ctx, owner=user, perms=0o600) keyring = 'keyring' keyring_path = ( '/var/lib/ceph/mon/ceph-' + get_unit_hostname() + '/' + keyring) render('mon.keyring', keyring_path, ctx, owner=user, perms=0o600) notify_radosgws() notify_client()
def update_monfs(): hostname = get_unit_hostname() monfs = '/var/lib/ceph/mon/ceph-{}'.format(hostname) upstart = '{}/upstart'.format(monfs) if os.path.exists(monfs) and not os.path.exists(upstart): # Mark mon as managed by upstart so that # it gets start correctly on reboots with open(upstart, 'w'): pass
def add_bootstrap_hint(peer): asok = "/var/run/ceph/ceph-mon.{}.asok".format(get_unit_hostname()) cmd = [ "sudo", "-u", ceph_user(), "ceph", "--admin-daemon", asok, "add_bootstrap_peer_hint", peer ] if os.path.exists(asok): # Ignore any errors for this call subprocess.call(cmd)
def is_leader(resource): cmd = ["crm", "resource", "show", resource] try: status = subprocess.check_output(cmd) except subprocess.CalledProcessError: return False else: if get_unit_hostname() in status: return True else: return False
def update_monfs(): hostname = get_unit_hostname() monfs = '/var/lib/ceph/mon/ceph-{}'.format(hostname) if systemd(): init_marker = '{}/systemd'.format(monfs) else: init_marker = '{}/upstart'.format(monfs) if os.path.exists(monfs) and not os.path.exists(init_marker): # Mark mon as managed by upstart so that # it gets start correctly on reboots with open(init_marker, 'w'): pass
def add_bootstrap_hint(peer): asok = "/var/run/ceph/ceph-mon.{}.asok".format(get_unit_hostname()) cmd = [ "ceph", "--admin-daemon", asok, "add_bootstrap_peer_hint", peer ] if os.path.exists(asok): # Ignore any errors for this call subprocess.call(cmd)
def is_leader(resource): cmd = [ "crm", "resource", "show", resource ] try: status = subprocess.check_output(cmd) except subprocess.CalledProcessError: return False else: if get_unit_hostname() in status: return True else: return False
def _get_human_readable(availability_zones): """Get human readable table format. :param availability_zones: information about the availability zone :type availability_zones: Dict[str, Dict[str, str]] :returns: formatted data as table :rtype: str """ data = availability_zones.get( "all-units", {get_unit_hostname(): availability_zones["unit"]} ) data = [[unit, *crush_map.values()] for unit, crush_map in data.items()] return tabulate( data, tablefmt="grid", headers=["unit", *CRUSH_MAP_HIERARCHY] )
def get_named_key(name, caps=None): caps = caps or _default_caps cmd = [ 'ceph', '--name', 'mon.', '--keyring', '/var/lib/ceph/mon/ceph-{}/keyring'.format( get_unit_hostname() ), 'auth', 'get-or-create', 'client.{}'.format(name), ] # Add capabilities for subsystem, subcaps in caps.iteritems(): cmd.extend([ subsystem, '; '.join(subcaps), ]) return parse_key(subprocess.check_output(cmd).strip()) # IGNORE:E1103
def bootstrap_monitor_cluster(secret): hostname = get_unit_hostname() path = '/var/lib/ceph/mon/ceph-{}'.format(hostname) done = '{}/done'.format(path) if systemd(): init_marker = '{}/systemd'.format(path) else: init_marker = '{}/upstart'.format(path) keyring = '/var/lib/ceph/tmp/{}.mon.keyring'.format(hostname) if os.path.exists(done): log('bootstrap_monitor_cluster: mon already initialized.') else: # Ceph >= 0.61.3 needs this for ceph-mon fs creation mkdir('/var/run/ceph', owner=ceph_user(), group=ceph_user(), perms=0o755) mkdir(path, owner=ceph_user(), group=ceph_user()) # end changes for Ceph >= 0.61.3 try: subprocess.check_call([ 'ceph-authtool', keyring, '--create-keyring', '--name=mon.', '--add-key={}'.format(secret), '--cap', 'mon', 'allow *' ]) subprocess.check_call( ['ceph-mon', '--mkfs', '-i', hostname, '--keyring', keyring]) chownr(path, ceph_user(), ceph_user()) with open(done, 'w'): pass with open(init_marker, 'w'): pass if systemd(): subprocess.check_call(['systemctl', 'enable', 'ceph-mon']) service_restart('ceph-mon') else: service_restart('ceph-mon-all') except: raise finally: os.unlink(keyring)
def is_quorum(): asok = "/var/run/ceph/ceph-mon.{}.asok".format(utils.get_unit_hostname()) cmd = [ "ceph", "--admin-daemon", asok, "mon_status" ] if os.path.exists(asok): try: result = json.loads(subprocess.check_output(cmd)) except subprocess.CalledProcessError: return False except ValueError: # Non JSON response from mon_status return False if result['state'] in QUORUM: return True else: return False else: return False
def emit_cephconf(): # Ensure ceph directory actually exists if not os.path.exists('/etc/ceph'): os.makedirs('/etc/ceph') cephcontext = { 'auth_supported': get_auth() or 'none', 'mon_hosts': ' '.join(get_mon_hosts()), 'hostname': utils.get_unit_hostname(), 'version': ceph.get_ceph_version('radosgw') } # Check to ensure that correct version of ceph is # in use if ceph.get_ceph_version('radosgw') >= "0.55": # Add keystone configuration if found ks_conf = get_keystone_conf() if ks_conf: cephcontext.update(ks_conf) with open('/etc/ceph/ceph.conf', 'w') as cephconf: cephconf.write(utils.render_template('ceph.conf', cephcontext))