def get_template_data(): rels = hookenv.relations() template_data = hookenv.Config() template_data.CONFIG_FILE_NAME = '.unit-state' overlay_type = get_scoped_rel_attr('network', rels, 'overlay_type') etcd_servers = get_rel_hosts('etcd', rels, ('hostname', 'port')) api_servers = get_rel_hosts('api', rels, ('hostname', 'port')) # kubernetes master isn't ha yet. if api_servers: api_info = api_servers.pop() api_servers = 'http://%s:%s' % (api_info[0], api_info[1]) template_data['overlay_type'] = overlay_type template_data['kubelet_bind_addr'] = _bind_addr( hookenv.unit_private_ip()) template_data['proxy_bind_addr'] = _bind_addr( hookenv.unit_get('public-address')) template_data['kubeapi_server'] = api_servers template_data['etcd_servers'] = ','.join([ 'http://%s:%s' % (s[0], s[1]) for s in sorted(etcd_servers)]) template_data['identifier'] = os.environ['JUJU_UNIT_NAME'].replace( '/', '-') return _encode(template_data)
def get_template_data(): rels = hookenv.relations() config = hookenv.config() version = config['version'] template_data = {} template_data['etcd_servers'] = ",".join([ "http://%s:%s" % (s[0], s[1]) for s in sorted( get_rel_hosts('etcd', rels, ('hostname', 'port')))]) template_data['minions'] = ",".join(get_rel_hosts('minions-api', rels)) template_data['api_bind_address'] = _bind_addr(hookenv.unit_private_ip()) template_data['bind_address'] = "127.0.0.1" template_data['api_server_address'] = "http://%s:%s" % ( hookenv.unit_private_ip(), 8080) arch = subprocess.check_output(['dpkg', '--print-architecture']).strip() template_data['web_uri'] = "/qingyuan/%s/local/bin/linux/%s/" % (version, arch) if version == 'local': template_data['alias'] = hookenv.charm_dir() + '/files/output/' else: directory = '/opt/qingyuan/_output/local/bin/linux/%s/' % arch template_data['alias'] = directory _encode(template_data) return template_data
def config_changed(): rel_ids = list(hookenv.relation_ids('database')) if len(rel_ids) == 0: log("Database not ready yet... skipping it for now") return # Get the database settings db_id = rel_ids[0] relations = hookenv.relations()['database'][db_id] database = None for key, value in relations.items(): if key.startswith('postgresql'): database = value if not database: log("Database not ready yet... skipping it for now") return # Open the relevant port for the service open_port() # Update the config file with the service_type and database settings update_config(database) # Restart the service restart_service() hookenv.status_set('active', '') set_state(ACTIVE)
def get_template_data(): rels = hookenv.relations() config = hookenv.config() version = config['version'] template_data = {} template_data['etcd_servers'] = ','.join([ 'http://%s:%s' % (s[0], s[1]) for s in sorted( get_rel_hosts('etcd', rels, ('hostname', 'port')))]) template_data['minions'] = ','.join(get_rel_hosts('minions-api', rels)) private_ip = hookenv.unit_private_ip() public_ip = hookenv.unit_public_ip() template_data['api_public_address'] = _bind_addr(public_ip) template_data['api_private_address'] = _bind_addr(private_ip) template_data['bind_address'] = '127.0.0.1' template_data['api_http_uri'] = 'http://%s:%s' % (private_ip, 8080) template_data['api_https_uri'] = 'https://%s:%s' % (private_ip, 6443) arch = subprocess.check_output(['dpkg', '--print-architecture']).strip() template_data['web_uri'] = '/kubernetes/%s/local/bin/linux/%s/' % (version, arch) if version == 'local': template_data['alias'] = hookenv.charm_dir() + '/files/output/' else: directory = '/opt/kubernetes/_output/local/bin/linux/%s/' % arch template_data['alias'] = directory _encode(template_data) return template_data
def get_template_data(): rels = hookenv.relations() config = hookenv.config() version = config['version'] template_data = {} template_data['etcd_servers'] = ','.join([ 'http://%s:%s' % (s[0], s[1]) for s in sorted(get_rel_hosts('etcd', rels, ('hostname', 'port'))) ]) template_data['minions'] = ','.join(get_rel_hosts('minions-api', rels)) private_ip = hookenv.unit_private_ip() public_ip = hookenv.unit_public_ip() template_data['api_public_address'] = _bind_addr(public_ip) template_data['api_private_address'] = _bind_addr(private_ip) template_data['bind_address'] = '127.0.0.1' template_data['api_http_uri'] = 'http://%s:%s' % (private_ip, 8080) template_data['api_https_uri'] = 'https://%s:%s' % (private_ip, 6443) arch = subprocess.check_output(['dpkg', '--print-architecture']).strip() template_data['web_uri'] = '/kubernetes/%s/local/bin/linux/%s/' % (version, arch) if version == 'local': template_data['alias'] = hookenv.charm_dir() + '/files/output/' else: directory = '/opt/kubernetes/_output/local/bin/linux/%s/' % arch template_data['alias'] = directory _encode(template_data) return template_data
def setCluster_config(): relation = relations() idKubeMaster = relation_ids()[0] remoteUnit = remote_unit() kubeMasterIP = relation["kubemaster"][idKubeMaster][remoteUnit][ "private-address"] call([ "kubectl", "config", "set-cluster", "juju", "--insecure-skip-tls-verify=true", "--server=http://" + kubeMasterIP ]) # Get Kubemaster IP address.
def get_template_data(): rels = hookenv.relations() template_data = {} template_data['etcd_servers'] = ",".join([ "http://%s:%s" % (s[0], s[1]) for s in sorted( get_rel_hosts('etcd', rels, ('hostname', 'port')))]) template_data['minions'] = ",".join(get_rel_hosts('minions-api', rels)) template_data['api_bind_address'] = _bind_addr(hookenv.unit_private_ip()) template_data['bind_address'] = "127.0.0.1" template_data['api_server_address'] = "http://%s:%s" % ( hookenv.unit_private_ip(), 8080) _encode(template_data) return template_data
def update_port(): if hookenv.hook_name() == "install": hookenv.log("Not udpating port during install hook", "DEBUG") return hookenv.close_port(sab.charm_config.previous('port'), 'TCP') hookenv.open_port(sab.charm_config['port'], 'TCP') sab.set_defaults() host.service_restart('sabnzbdplus') relations = hookenv.relations() for relation in relations.keys(): if relations[relation]: hookenv.log('Relations should be departed before updating port', 'WARNING') hookenv.log( 'Port change not reflected in relation: {}'.format(relation), 'WARNING')
def mongodb_relation(): """ Setup relation to a mongodb database (will replace any other database relations) """ log('Hook function: mongodb_relation') host = relation_get("hostname") if 'mongodb' in relations() and host: link_database( scheme='mongodb', database_host=host, port=relation_get("port"), variable_name='MONGO_URL' )
def get_template_data(): rels = hookenv.relations() config = hookenv.config() template_data = {} template_data['etcd_servers'] = ",".join([ "http://%s:%s" % (s[0], s[1]) for s in sorted( get_rel_hosts('etcd', rels, ('hostname', 'port')))]) template_data['minions'] = ",".join(get_rel_hosts('minions-api', rels)) template_data['api_bind_address'] = _bind_addr(hookenv.unit_private_ip()) template_data['bind_address'] = "127.0.0.1" template_data['api_server_address'] = "http://%s:%s" % ( hookenv.unit_private_ip(), 8080) arch = subprocess.check_output(['dpkg', '--print-architecture']).strip() template_data['web_uri'] = "/kubernetes/%s/local/bin/linux/%s/" % ( config['version'], arch) _encode(template_data) return template_data
def pgsql_relation(): """ Setup relation to a postgresql database (will replace any other database relations) """ log('Hook function: pgsql_relation') host = relation_get("host") if 'pgsql' in relations() and host: link_database( scheme='postgresql', database_host=host, port=relation_get("port"), username=relation_get("user"), password=relation_get("password"), database_name=relation_get("database") )
def update_relations(context, namespace_separator=':'): """Update the context with the relation data.""" # Add any relation data prefixed with the relation type. relation_type = hookenv.relation_type() relations = [] context['current_relation'] = {} if relation_type is not None: relation_data = hookenv.relation_get() context['current_relation'] = relation_data # Deprecated: the following use of relation data as keys # directly in the context will be removed. relation_data = dict( ("{relation_type}{namespace_separator}{key}".format( relation_type=relation_type, key=key, namespace_separator=namespace_separator), val) for key, val in relation_data.items()) relation_data = dict_keys_without_hyphens(relation_data) context.update(relation_data) relations = hookenv.relations_of_type(relation_type) relations = [dict_keys_without_hyphens(rel) for rel in relations] context['relations_full'] = hookenv.relations() # the hookenv.relations() data structure is effectively unusable in # templates and other contexts when trying to access relation data other # than the current relation. So provide a more useful structure that works # with any hook. local_unit = hookenv.local_unit() relations = {} for rname, rids in context['relations_full'].items(): relations[rname] = [] for rid, rdata in rids.items(): data = rdata.copy() if local_unit in rdata: data.pop(local_unit) for unit_name, rel_data in data.items(): new_data = {'__relid__': rid, '__unit__': unit_name} new_data.update(rel_data) relations[rname].append(new_data) context['relations'] = relations
def get_template_data(): rels = hookenv.relations() template_data = hookenv.Config() template_data.CONFIG_FILE_NAME = ".unit-state" overlay_type = get_scoped_rel_attr("network", rels, "overlay_type") etcd_servers = get_rel_hosts("etcd", rels, ("hostname", "port")) api_servers = get_rel_hosts("api", rels, ("hostname", "port")) # kubernetes master isn't ha yet. if api_servers: api_info = api_servers.pop() api_servers = "http://%s:%s" % (api_info[0], api_info[1]) template_data["overlay_type"] = overlay_type template_data["kubelet_bind_addr"] = _bind_addr(hookenv.unit_private_ip()) template_data["proxy_bind_addr"] = _bind_addr(hookenv.unit_get("public-address")) template_data["kubeapi_server"] = api_servers template_data["etcd_servers"] = ",".join(["http://%s:%s" % (s[0], s[1]) for s in sorted(etcd_servers)]) template_data["identifier"] = os.environ["JUJU_UNIT_NAME"].replace("/", "-") return _encode(template_data)
def test_gets_relations(self, relation_get, related_units, relation_ids, relation_types, local_unit): local_unit.return_value = 'u0' relation_types.return_value = ['t1', 't2'] relation_ids.return_value = ['i1'] related_units.return_value = ['u1', 'u2'] relation_get.return_value = {'key': 'val'} result = hookenv.relations() self.assertEqual( result, { 't1': { 'i1': { 'u0': { 'key': 'val' }, 'u1': { 'key': 'val' }, 'u2': { 'key': 'val' }, }, }, 't2': { 'i1': { 'u0': { 'key': 'val' }, 'u1': { 'key': 'val' }, 'u2': { 'key': 'val' }, }, }, })
def get_template_data(): rels = hookenv.relations() config = hookenv.config() version = config["version"] template_data = {} template_data["etcd_servers"] = ",".join( ["http://%s:%s" % (s[0], s[1]) for s in sorted(get_rel_hosts("etcd", rels, ("hostname", "port")))] ) template_data["minions"] = ",".join(get_rel_hosts("minions-api", rels)) template_data["api_bind_address"] = _bind_addr(hookenv.unit_private_ip()) template_data["bind_address"] = "127.0.0.1" template_data["api_server_address"] = "http://%s:%s" % (hookenv.unit_private_ip(), 8080) arch = subprocess.check_output(["dpkg", "--print-architecture"]).strip() template_data["web_uri"] = "/kubernetes/%s/local/bin/linux/%s/" % (version, arch) if version == "local": template_data["alias"] = hookenv.charm_dir() + "/files/output/" else: directory = "/opt/kubernetes/_output/local/bin/linux/%s/" % arch template_data["alias"] = directory _encode(template_data) return template_data
def assess_status(): '''Assess status of current unit''' application_version_set(get_upstream_version(VERSION_PACKAGE)) if not config('permit-insecure-cmr'): units = [ unit for rtype in relations() for relid in relation_ids(reltype=rtype) for unit in related_units(relid=relid) if is_cmr_unit(unit) ] if units: status_set("blocked", "Unsupported CMR relation") return if is_unit_upgrading_set(): status_set( "blocked", "Ready for do-release-upgrade and reboot. " "Set complete when finished.") return # Check that the no-bootstrap config option is set in conjunction with # having the bootstrap-source relation established if not config('no-bootstrap') and is_relation_made('bootstrap-source'): status_set( 'blocked', 'Cannot join the bootstrap-source relation when ' 'no-bootstrap is False') return moncount = int(config('monitor-count')) units = get_peer_units() # not enough peers and mon_count > 1 if len(units.keys()) < moncount: status_set( 'blocked', 'Insufficient peer units to bootstrap' ' cluster (require {})'.format(moncount)) return # mon_count > 1, peers, but no ceph-public-address ready = sum(1 for unit_ready in units.values() if unit_ready) if ready < moncount: status_set('waiting', 'Peer units detected, waiting for addresses') return configured_rbd_features = config('default-rbd-features') if has_rbd_mirrors() and configured_rbd_features: if add_rbd_mirror_features( configured_rbd_features) != configured_rbd_features: # The configured RBD features bitmap does not contain the features # required for RBD Mirroring status_set( 'blocked', 'Configuration mismatch: RBD Mirroring ' 'enabled but incorrect value set for ' '``default-rbd-features``') return try: get_osd_settings('client') except OSD_SETTING_EXCEPTIONS as e: status_set('blocked', str(e)) return # active - bootstrapped + quorum status check if ceph.is_bootstrapped() and ceph.is_quorum(): expected_osd_count = config('expected-osd-count') or 3 if sufficient_osds(expected_osd_count): status_set('active', 'Unit is ready and clustered') else: status_set( 'waiting', 'Monitor bootstrapped but waiting for number of' ' OSDs to reach expected-osd-count ({})'.format( expected_osd_count)) else: # Unit should be running and clustered, but no quorum # TODO: should this be blocked or waiting? status_set('blocked', 'Unit not clustered (no quorum)')