diff --git a/playbooks/files/create_cce_cluster.py b/playbooks/files/create_cce_cluster.py new file mode 100755 index 0000000..b456b73 --- /dev/null +++ b/playbooks/files/create_cce_cluster.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +import sys + +import openstack +from otcextensions import sdk + +# openstack.enable_logging(True, http_debug=True) + +# An 'otc' is a cloud connection with name 'otc' configured in the clouds.yaml +conn = openstack.connect() + +# Register OTC Extensions +sdk.register_otc_extensions(conn) + +cluster = conn.cce.find_cluster(sys.argv[1]) +dict = { + 'metadata': { + 'name': sys.argv[1] + }, + 'spec': { + 'type': 'VirtualMachine', + 'version': 'v1.11.7-r2', + 'hostNetwork': { + 'vpc': sys.argv[2], + 'subnet': sys.argv[3] + }, + 'flavor': 'cce.s1.small', + 'containerNetwork': { + 'mode': 'overlay_l2', + 'cidr': '172.16.0.0/16' + } + } +} + +if (cluster is None): + cluster = conn.cce.create_cluster(**dict) + job = conn.cce.get_job(cluster.job_id) + conn.cce.wait_for_job(job) diff --git a/playbooks/files/create_cce_cluster_node.py b/playbooks/files/create_cce_cluster_node.py new file mode 100644 index 0000000..8d7250b --- /dev/null +++ b/playbooks/files/create_cce_cluster_node.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 + +import sys + +import openstack +from otcextensions import sdk + +# openstack.enable_logging(True, http_debug=True) + +# An 'otc' is a cloud connection with name 'otc' configured in the clouds.yaml +conn = openstack.connect() + +# Register OTC Extensions +sdk.register_otc_extensions(conn) + +cluster = conn.cce.find_cluster(sys.argv[1]) +node = conn.cce.find_cluster_node(cluster, sys.argv[2]) +dict = { + "metadata": { + "name": sys.argv[2] + }, + "spec": { + "flavor": "s2.large.4", + "az": "eu-de-01", + "login": { + "sshKey": sys.argv[3] + }, + "rootVolume": { + "size": 40, + "volumetype": "SATA" + }, + "dataVolumes": [ + { + "size": 100, + "volumetype": "SATA" + } + ], + "count": 1 + } +} + +if cluster and (node is None): + node = conn.cce.create_cluster_node(cluster, **dict) + job = conn.cce.get_job(node.job_id) + conn.cce.wait_for_job(job) diff --git a/playbooks/files/delete_cce_cluster.py b/playbooks/files/delete_cce_cluster.py new file mode 100644 index 0000000..39d1141 --- /dev/null +++ b/playbooks/files/delete_cce_cluster.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 + +import sys +import time + +import openstack +from otcextensions import sdk + +openstack.enable_logging(True, http_debug=True) + +# An 'otc' is a cloud connection with name 'otc' configured in the clouds.yaml +conn = openstack.connect() + +# Register OTC Extensions +sdk.register_otc_extensions(conn) + +cluster = conn.cce.find_cluster(sys.argv[1]) + +timer = 0 + +if cluster: + conn.cce.delete_cluster(cluster) + time.sleep(100) diff --git a/playbooks/files/delete_cce_cluster_node.py b/playbooks/files/delete_cce_cluster_node.py new file mode 100644 index 0000000..9a5efab --- /dev/null +++ b/playbooks/files/delete_cce_cluster_node.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +import sys + +import openstack +from otcextensions import sdk + +openstack.enable_logging(True, http_debug=True) + +# An 'otc' is a cloud connection with name 'otc' configured in the clouds.yaml +conn = openstack.connect() + +# Register OTC Extensions +sdk.register_otc_extensions(conn) + +cluster = conn.cce.find_cluster(sys.argv[1]) +node = conn.cce.find_cluster_node(cluster, sys.argv[2]) + +if cluster and node: + node = conn.cce.delete_cluster_node(cluster, node) + # job = conn.cce.get_job(node.job_id) + # conn.cce.wait_for_job(job) diff --git a/playbooks/files/find_cce_cluster.py b/playbooks/files/find_cce_cluster.py new file mode 100755 index 0000000..aa1b6ef --- /dev/null +++ b/playbooks/files/find_cce_cluster.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 + +import openstack +import sys + +from otcextensions import sdk + +# openstack.enable_logging(True, http_debug=True) + +# An 'otc' is a cloud connection with name 'otc' configured in the clouds.yaml +conn = openstack.connect() + +# Register OTC Extensions +sdk.register_otc_extensions(conn) + +cluster = conn.cce.find_cluster(sys.argv[1]) \ No newline at end of file diff --git a/playbooks/files/find_cce_cluster_node.py b/playbooks/files/find_cce_cluster_node.py new file mode 100644 index 0000000..0205019 --- /dev/null +++ b/playbooks/files/find_cce_cluster_node.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python3 + +import openstack +import sys + +from otcextensions import sdk + +# openstack.enable_logging(True, http_debug=True) + +# An 'otc' is a cloud connection with name 'otc' configured in the clouds.yaml +conn = openstack.connect() + +# Register OTC Extensions +sdk.register_otc_extensions(conn) + +cluster = conn.cce.find_cluster(sys.argv[1]) +node = conn.cce.find_cluster_node(cluster, sys.argv[2]) diff --git a/playbooks/files/get_cce_cluster.py b/playbooks/files/get_cce_cluster.py new file mode 100755 index 0000000..2392a3b --- /dev/null +++ b/playbooks/files/get_cce_cluster.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python3 + +import openstack +from otcextensions import sdk + +# openstack.enable_logging(True, http_debug=True) + +# An 'otc' is a cloud connection with name 'otc' configured in the clouds.yaml +conn = openstack.connect() + +# Register OTC Extensions +sdk.register_otc_extensions(conn) + + +cluster = conn.cce.get_cluster('e9d8539e-c894-11e9-a4c3-0255ac101618') diff --git a/playbooks/files/get_cce_cluster_certificates.py b/playbooks/files/get_cce_cluster_certificates.py new file mode 100644 index 0000000..c19ddb3 --- /dev/null +++ b/playbooks/files/get_cce_cluster_certificates.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python3 + +import re +import sys + +import openstack +from otcextensions import sdk + +# openstack.enable_logging(True, http_debug=True) + +# An 'otc' is a cloud connection with name 'otc' configured in the clouds.yaml +conn = openstack.connect() + +# Register OTC Extensions +sdk.register_otc_extensions(conn) + +# Search for corresponding cluster +cluster = conn.cce.find_cluster(sys.argv[1]) + +# Query Certificate information +certificates = conn.cce.get_cluster_certificates(cluster) + +# Use RegEx to create kubectl conform configuration information +ca_regex = r"ca=(\w*\={,2})," +client_certificate_regex = r"client_certificate=(\w*\={,2})," +client_key_regex = r"client_key=(\w*\={,2})," +internal_ip_regex = r"context={'name': 'internal', 'cluster': 'https://((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]):[0-9]{,5})" + +ca_data = re.search(ca_regex, str(certificates)) +client_certificate_data = re.search(client_certificate_regex, + str(certificates)) +client_key_data = re.search(client_key_regex, str(certificates)) +internal_ip_data = re.search(internal_ip_regex, str(certificates)) + +ca_data = ca_data.group(1) +client_certificate_data = client_certificate_data.group(1) +client_key_data = client_key_data.group(1) +internal_ip_data = "https://" + internal_ip_data.group(1) + +# print("CA-data: " + ca_data) +# print("Client Cert Data: " + client_certificate_data) +# print("Client Key Data: " + client_key_data) +# print("IP Data: " + internal_ip_data) + +kube_config = { + "kind": "Config", + "apiVersion": "v1", + "preferences": { + }, + "clusters": [ + { + "name": "internalCluster", + "cluster": { + "server": internal_ip_data, + "certificate-authority-data": ca_data + } + } + ], + "users": [ + { + "name": "user", + "user": { + "client-certificate-data": client_certificate_data, + "client-key-data": client_key_data + } + } + ], + "contexts": [ + { + "name": "internal", + "context": { + "cluster": "internalCluster", + "user": "user" + } + } + ], + "current-context": "internal" +} + +print(str(kube_config)) diff --git a/playbooks/files/list_cce_clusters.py b/playbooks/files/list_cce_clusters.py new file mode 100755 index 0000000..d8447cf --- /dev/null +++ b/playbooks/files/list_cce_clusters.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python3 + +import openstack +from otcextensions import sdk + +# openstack.enable_logging(True, http_debug=True) + +# An 'otc' is a cloud connection with name 'otc' configured in the clouds.yaml +conn = openstack.connect() + +# Register OTC Extensions +sdk.register_otc_extensions(conn) + +for cluster in conn.cce.clusters(): + print(cluster) diff --git a/playbooks/files/wait_for_cce_cluster.py b/playbooks/files/wait_for_cce_cluster.py new file mode 100755 index 0000000..2db7311 --- /dev/null +++ b/playbooks/files/wait_for_cce_cluster.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 + +import openstack +from otcextensions import sdk + +import sys + +# openstack.enable_logging(True, http_debug=True) + +# An 'otc' is a cloud connection with name 'otc' configured in the clouds.yaml +conn = openstack.connect() + +# Register OTC Extensions +sdk.register_otc_extensions(conn) + + +cluster = conn.cce.find_cluster(sys.argv[1]) +conn.cce.wait_for_cluster(cluster, status='Available', + failures=None, interval=10, wait=960) diff --git a/playbooks/scenario18_create_cce.yaml b/playbooks/scenario18_create_cce.yaml new file mode 100644 index 0000000..faef4c0 --- /dev/null +++ b/playbooks/scenario18_create_cce.yaml @@ -0,0 +1,105 @@ +--- +- name: Scenario 18 - CCE Lifecycle + hosts: localhost + vars: + prefix: scenario18- + tasks: + + - name: Set facts + set_fact: + test_keypair_name: "{{ (prefix + 'keypair') }}" + router_name: "{{ ( prefix + 'router' ) }}" + network_name: "{{ ( prefix + 'network' ) }}" + subnet_name: "{{ ( prefix + 'subnet' ) }}" + cce_name: "{{ ( prefix + 'cluster' ) }}" + node_name: "{{ ( prefix + 'node' ) }}" + + - name: List Keypairs + script: list_keypairs.py + + - name: Ensure .ssh exists + file: + path: "~/.ssh" + state: directory + + - name: Create Keypair + include_role: + name: opentelekomcloud.keypair + vars: + keypair_name: "{{ test_keypair_name }}" + + - block: + - name: Create VPC (Router + Net + Subnet) + include_role: + name: opentelekomcloud.vpc + vars: + router_name: "{{ router_name }}" + network_name: "{{ network_name }}" + subnet_name: "{{ subnet_name }}" + + - name: Debug Router-ID + debug: + var: net_router['id'] + when: net_router is defined + + - name: List all Networks + os_networks_facts: + + - name: Debug Network-ID + debug: + var: net_network['id'] + when: net_network is defined + + - name: List all Subnets + os_subnets_facts: + + - name: Debug Subnet-ID + debug: + var: net_subnet['id'] + when: net_subnet is defined + + - name: List all CCE clusters + script: list_cce_clusters.py + + - name: Create CCE cluster + script: "create_cce_cluster.py {{ cce_name }} {{ net_router['id'] }} {{ net_network['id'] }}" + when: net_router is defined and net_network is defined + + - name: Find CCE cluster + script: "find_cce_cluster.py {{ cce_name }}" + when: net_router is defined and net_network is defined + + - name: Add CCE node + script: "create_cce_cluster_node.py {{ cce_name }} {{ node_name }} {{ test_keypair_name }}" + when: net_router is defined and net_network is defined + + - name: Find CCE node + script: "find_cce_cluster_node.py {{ cce_name }} {{ node_name }}" + when: net_router is defined and net_network is defined + + - name: Get CCE credentials + script: "get_cce_cluster_certificates.py {{ cce_name }}" + when: net_router is defined and net_network is defined + + always: + - name: Delete CCE node + script: "delete_cce_cluster_node.py {{ cce_name }} {{ node_name }}" + + - name: Delete CCE cluster + script: "delete_cce_cluster.py {{ cce_name }}" + + - name: Delete VPC + include_role: + name: opentelekomcloud.vpc + vars: + router_name: "{{ router_name }}" + network_name: "{{ network_name }}" + subnet_name: "{{ subnet_name }}" + state: absent + + - name: Delete Keypair + include_role: + name: opentelekomcloud.keypair + vars: + keypair_name: "{{ test_keypair_name }}" + state: absent