def configure_db(hostname, database, username): passwd_file = "/var/lib/mysql/mysql-{}.passwd"\ .format(username) if hostname != local_hostname: remote_ip = socket.gethostbyname(hostname) else: remote_ip = '127.0.0.1' if not os.path.exists(passwd_file): password = pwgen() with open(passwd_file, 'w') as pfile: pfile.write(password) else: with open(passwd_file) as pfile: password = pfile.read().strip() if not database_exists(database): create_database(database) if not grant_exists(database, username, remote_ip): create_grant(database, username, remote_ip, password) return password
def get_allowed_units(database, username): allowed_units = set() for relid in hookenv.relation_ids('shared-db'): for unit in hookenv.related_units(relid): attr = "%s_%s" % (database, 'hostname') hosts = hookenv.relation_get(attribute=attr, unit=unit, rid=relid) if not hosts: hosts = [hookenv.relation_get(attribute='private-address', unit=unit, rid=relid)] else: # hostname can be json-encoded list of hostnames try: hosts = json.loads(hosts) except ValueError: pass if not isinstance(hosts, list): hosts = [hosts] if hosts: for host in hosts: utils.juju_log('INFO', "Checking host '%s' grant" % (host)) if grant_exists(database, username, host): if unit not in allowed_units: allowed_units.add(unit) else: utils.juju_log('INFO', "No hosts found for grant check") return allowed_units
def configure_db(hostname, database, username): passwd_file = "/var/lib/mysql/mysql-{}.passwd".format(username) if hostname != local_hostname: try: remote_ip = socket.gethostbyname(hostname) except Exception: # socket.gethostbyname doesn't support ipv6 remote_ip = hostname else: remote_ip = '127.0.0.1' if not os.path.exists(passwd_file): password = pwgen() with open(passwd_file, 'w') as pfile: pfile.write(password) os.chmod(pfile.name, 0600) else: with open(passwd_file) as pfile: password = pfile.read().strip() if not database_exists(database): create_database(database) if not grant_exists(database, username, remote_ip): create_grant(database, username, remote_ip, password) return password
def get_allowed_units(database, username): allowed_units = set() for relid in hookenv.relation_ids('shared-db'): for unit in hookenv.related_units(relid): if grant_exists(database, username, get_unit_addr(relid, unit)): allowed_units.add(unit) return allowed_units