def get_fabric_interface(): ''' Returns the fabric interface. ''' fabric_interfaces = config('fabric-interfaces') if fabric_interfaces == 'MANAGEMENT': return get_mgmt_interface() else: try: all_fabric_interfaces = json.loads(fabric_interfaces) except ValueError: raise ValueError('Invalid json provided for fabric interfaces') hostname = get_unit_hostname() if hostname in all_fabric_interfaces: node_fabric_interface = all_fabric_interfaces[hostname] elif 'DEFAULT' in all_fabric_interfaces: node_fabric_interface = all_fabric_interfaces['DEFAULT'] else: raise ValueError('No fabric interface provided for node') if interface_exists(node_fabric_interface): return node_fabric_interface else: log('Provided fabric interface %s does not exist' % node_fabric_interface) raise ValueError('Provided fabric interface does not exist') return node_fabric_interface
def setup_apache2(self): os.makedirs('/etc/apache2/ssl', exist_ok=True) ssl.generate_selfsigned(keyfile='/etc/apache2/ssl/private.key', certfile='/etc/apache2/ssl/cert.crt', keysize=2048, cn=get_unit_hostname()) ctxt_gens = [{ 'template': 'apache2/ports.conf.j2', 'output': '/etc/apache2/ports.conf', 'context': { 'http_port': self.config['http-port'], 'https_port': self.config['https-port'] } }, { 'template': 'apache2/simplesamlphp.conf.j2', 'output': '/etc/apache2/sites-available/simplesamlphp.conf', 'context': { 'http_port': self.config['http-port'], 'https_port': self.config['https-port'] } }] render_configs(ctxt_gens) subprocess.check_call(['a2enmod', 'ssl']) subprocess.check_call( ['a2dissite', '000-default.conf', 'default-ssl.conf']) subprocess.check_call(['a2ensite', 'simplesamlphp.conf']) service_restart('apache2')
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), ]) 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_crm_leader(resource, retry=False): """ Returns True if the charm calling this is the elected corosync leader, as returned by calling the external "crm" command. We allow this operation to be retried to avoid the possibility of getting a false negative. See LP #1396246 for more info. """ if resource == DC_RESOURCE_NAME: return is_crm_dc() cmd = ['crm', 'resource', 'show', resource] try: status = subprocess.check_output(cmd, stderr=subprocess.STDOUT) if not isinstance(status, six.text_type): status = six.text_type(status, "utf-8") except subprocess.CalledProcessError: status = None if status and get_unit_hostname() in status: return True if status and "resource %s is NOT running" % (resource) in status: raise CRMResourceNotFound("CRM resource %s not found" % (resource)) return False
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 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 is_crm_leader(resource): """ Returns True if the charm calling this is the elected corosync leader, as returned by calling the external "crm" command. """ cmd = ["crm", "resource", "show", resource] try: status = subprocess.check_output(cmd).decode('UTF-8') except subprocess.CalledProcessError: return False else: if get_unit_hostname() in status: return True else: return False
def is_crm_leader(resource): """ Returns True if the charm calling this is the elected corosync leader, as returned by calling the external "crm" command. """ 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 is_crm_dc(): """ Determine leadership by querying the pacemaker Designated Controller """ cmd = ['crm', 'status'] try: status = subprocess.check_output(cmd, stderr=subprocess.STDOUT) if not isinstance(status, six.text_type): status = six.text_type(status, "utf-8") except subprocess.CalledProcessError: return False current_dc = '' for line in status.split('\n'): if line.startswith('Current DC'): # Current DC: juju-lytrusty-machine-2 (168108163) - partition with quorum current_dc = line.split(':')[1].split()[0] if current_dc == get_unit_hostname(): return True return False
def get_gw_interfaces(): ''' Gateway node can have multiple interfaces. This function parses json provided in config to get all gateway interfaces for this node. ''' node_interfaces = [] try: all_interfaces = json.loads(config('external-interfaces')) except ValueError: raise ValueError("Invalid json provided for gateway interfaces") hostname = get_unit_hostname() if hostname in all_interfaces: node_interfaces = all_interfaces[hostname].split(',') elif 'DEFAULT' in all_interfaces: node_interfaces = all_interfaces['DEFAULT'].split(',') for interface in node_interfaces: if not interface_exists(interface): log('Provided gateway interface %s does not exist' % interface) raise ValueError('Provided gateway interface does not exist') return node_interfaces
def is_leader(): asok = "/var/run/ceph/ceph-mon.{}.asok".format(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'] == LEADER: return True else: return False else: return False
def setup_simplesamlphp(self): if os.path.exists(self.DEST_DIR): os.rmdir(self.DEST_DIR) version = self.config.get('simple-saml-php-version') archive_handler = ArchiveUrlFetchHandler() retry_on_error()(archive_handler.install)( source='{0}/v{1}/simplesamlphp-{1}.tar.gz'.format( self.BASE_DOWNLOAD_URL, version), dest=os.path.dirname(self.DEST_DIR)) os.rename('{0}-{1}'.format(self.DEST_DIR, version), self.DEST_DIR) key_file = '{0}/cert/server.pem'.format(self.DEST_DIR) cert_file = '{0}/cert/server.crt'.format(self.DEST_DIR) ssl.generate_selfsigned(keyfile=key_file, certfile=cert_file, keysize=2048, cn=get_unit_hostname()) uid = pwd.getpwnam(self.APACHE_USER).pw_uid gid = grp.getgrnam(self.APACHE_GROUP).gr_gid os.chown(key_file, uid, gid) os.chown(cert_file, uid, gid)
def is_crm_dc(): """ Determine leadership by querying the pacemaker Designated Controller """ cmd = ['crm', 'status'] try: status = subprocess.check_output( cmd, stderr=subprocess.STDOUT).decode('utf-8') except subprocess.CalledProcessError as ex: raise CRMDCNotFound(str(ex)) current_dc = '' for line in status.split('\n'): if line.startswith('Current DC'): # Current DC: juju-lytrusty-machine-2 (168108163) # - partition with quorum current_dc = line.split(':')[1].split()[0] if current_dc == get_unit_hostname(): return True elif current_dc == 'NONE': raise CRMDCNotFound('Current DC: NONE') return False
def is_crm_dc(): """ Determine leadership by querying the pacemaker Designated Controller """ cmd = ['crm', 'status'] try: status = subprocess.check_output(cmd, stderr=subprocess.STDOUT) if not isinstance(status, six.text_type): status = six.text_type(status, "utf-8") except subprocess.CalledProcessError as ex: raise CRMDCNotFound(str(ex)) current_dc = '' for line in status.split('\n'): if line.startswith('Current DC'): # Current DC: juju-lytrusty-machine-2 (168108163) - partition with quorum current_dc = line.split(':')[1].split()[0] if current_dc == get_unit_hostname(): return True elif current_dc == 'NONE': raise CRMDCNotFound('Current DC: NONE') return False
def node_name(self): return get_unit_hostname()