예제 #1
0
def do_get_kubeconfig(secrets):
    endpoint = re.search("(?:http.*://)?(?P<host>[^:/ ]+)", secrets['OS_AUTH_URL']).group('host')
    token, catalog, project_id = qbert.get_token_v3(endpoint, secrets['OS_USERNAME'], secrets['OS_PASSWORD'],
                                                    secrets['PACKET_PROJECT_ID'])
    qbert_url = "{0}/{1}".format(qbert.get_service_url('qbert', catalog, secrets['OS_REGION_NAME']), project_id)
    dir_path = "{}/{}".format(os.path.dirname(os.path.realpath(__file__)), "terraform")
    state_path = "{}/states/{}/{}".format(dir_path, secrets['PACKET_PROJECT_ID'], secrets['CLUSTER_ID'])
    conn = openstack.connect(cloud='cloud')
    os_user = conn.get_user(secrets['user_id'])
    if not os_user:
        return {"Error": "User id: {} not found.".format(secrets['user_id'])}
    username = os_user['name']
    if username == 'admin@{}.{}.tikube'.format(secrets['CLUSTER_ID'], secrets['PACKET_PROJECT_ID']):
        with open("{}/admin_creds.json".format(state_path)) as f:
            user_creds = json.load(f)
        password = user_creds['password']
        authorize_cluster.delay(qbert_url, token, secrets['CLUSTER_ID'], username)
    else:
        conn = openstack.connect(cloud='cloud')
        os_project = conn.get_project(secrets['PACKET_PROJECT_ID'])
        os_user, password = create_user(conn, os_project, username)

    kubeconfig = qbert.get_kube_config(qbert_url, token, endpoint, secrets['CLUSTER_ID'], secrets['PACKET_PROJECT_ID'],
                                       username, password)

    return kubeconfig
예제 #2
0
def pf9_auth(endpoint, user, pw, tenant, region):
    token, catalog, project_id = qbert.get_token_v3(endpoint, user, pw, tenant)
    qbert_url = "{0}/{1}".format(
        qbert.get_service_url('qbert', catalog, region), project_id)
    output = {
        "token": token,
        "catalog": catalog,
        "project_id": project_id,
        "qbert_url": qbert_url
    }
    return output
예제 #3
0
def create_cluster(endpoint, user, pw, tenant, region, cluster_name, dnz_zone_name, privileged_mode_enabled=True,
                   app_catalog_enabled=False, runtime_config='', allow_workloads_on_master=False,
                   networkPlugin='calico', container_cidr='172.30.0.0/16', services_cidr='172.31.0.0/16',
                   debug_flag=True):

    token, catalog, project_id = qbert.get_token_v3(endpoint, user, pw, tenant)
    qbert_url = "{0}/{1}".format(qbert.get_service_url('qbert', catalog, region), project_id)
    node_pool_uuid = qbert.get_node_pool(qbert_url, token)
    new_cluster = qbert.create_cluster(qbert_url, token, cluster_name, container_cidr, services_cidr,
                                       "", privileged_mode_enabled, app_catalog_enabled,
                                       allow_workloads_on_master, runtime_config, node_pool_uuid,
                                       networkPlugin, debug_flag)
    put_body = {"externalDnsName": "{}-api.{}".format(new_cluster, dnz_zone_name)}
    qbert.put_request(qbert_url, token, "clusters/{}".format(new_cluster), put_body)

    return new_cluster, node_pool_uuid
예제 #4
0
def get_clusters(project_name, SECRETS):
    endpoint = re.search("(?:http.*://)?(?P<host>[^:/ ]+)",
                         SECRETS['OS_AUTH_URL']).group('host')

    try:
        token, catalog, project_id = qbert.get_token_v3(
            endpoint, SECRETS['OS_USERNAME'], SECRETS['OS_PASSWORD'],
            project_name)
        qbert_url = "{0}/{1}".format(
            qbert.get_service_url('qbert', catalog, SECRETS['OS_REGION_NAME']),
            project_id)
        clusters = qbert.get_request(qbert_url, token, "clusters")
        status_code = 200
    except:
        print("Something went wrong.")
        clusters = []
        status_code = 200
    return clusters, status_code
예제 #5
0
def get_cluster(project_name, cluster_id, SECRETS):
    endpoint = re.search("(?:http.*://)?(?P<host>[^:/ ]+)",
                         SECRETS['OS_AUTH_URL']).group('host')

    try:
        token, catalog, project_id = qbert.get_token_v3(
            endpoint, SECRETS['OS_USERNAME'], SECRETS['OS_PASSWORD'],
            project_name)
        qbert_url = "{0}/{1}".format(
            qbert.get_service_url('qbert', catalog, SECRETS['OS_REGION_NAME']),
            project_id)

        cluster = qbert.get_request(qbert_url, token,
                                    "clusters/{}".format(cluster_id))
        status_code = 200
    except:
        print("Something went wrong.")
        cluster = {
            'error': {
                'message':
                "Error: table clusters does not have object {}".format(
                    cluster_id),
                'code':
                400
            }
        }
        status_code = 400

    if not isinstance(cluster, dict):
        print("Something went wrong.")
        cluster = {
            'error': {
                'message':
                "Error: table clusters does not have object {}".format(
                    cluster_id),
                'code':
                400
            }
        }
        status_code = 400

    return cluster, status_code
예제 #6
0
def delete_project(conn, endpoint, user, pw, tenant, region):
    os_admin = conn.get_user(user)
    os_project = conn.get_project(tenant)
    if os_project:
        params = dict(project=os_project)
        role_mappings = conn.list_role_assignments(filters=params)
        for mapping in role_mappings:
            if mapping['user'] != os_admin['id']:
                print("Project still has users. Skipping project deletion...")
                return False
        token, catalog, project_id = qbert.get_token_v3(endpoint, user, pw, tenant)
        qbert_url = "{0}/{1}".format(qbert.get_service_url('qbert', catalog, region), project_id)
        clusters = qbert.get_request(qbert_url, token, "clusters")
        if len(clusters) != 0:
            print("Cluster(s) still exist. Skipping project deletion...")
            return False
        conn.delete_project(os_project, domain_id="default")
        return True
    else:
        print("Project doesn't exists. Skipping deletion...")
        return False
예제 #7
0
def delete_cluster(endpoint, user, pw, tenant, region, cluster_id):
    token, catalog, project_id = qbert.get_token_v3(endpoint, user, pw, tenant)
    qbert_url = "{0}/{1}".format(qbert.get_service_url('qbert', catalog, region), project_id)
    delete_cluster = qbert.delete_request(qbert_url, token, "clusters/{}".format(cluster_id))

    return delete_cluster