Commit 8ed128bc authored by Spiros Koulouzis's avatar Spiros Koulouzis

added k8s playboks

parent ac710981
......@@ -69,6 +69,16 @@ node_types:
tosca.nodes.ARTICONF.Orchestrator.Kubernetes:
derived_from: tosca.nodes.ARTICONF.Orchestrator
description: Kubernetes orchestrator
attributes:
api_key:
type: string
required: false
join_token:
type: string
required: false
discovery_token_ca_cert_hash:
type: string
required: false
interfaces:
Kubernetes:
type: tosca.interfaces.ARTICONF.Kubernetes
......
......@@ -70,9 +70,16 @@ def handle_delivery(message):
tosca_template_json = parsed_json_message['toscaTemplate']
interfaces = tosca.get_interfaces(tosca_template_json)
tmp_path = tempfile.mkdtemp()
vms = tosca.get_vms(tosca_template_json)
ansible_service.run(interfaces, vms)
k8s_service.run(tosca_template_json)
inventory_path = ansible_service.write_inventory_file(tmp_path, vms)
paths = ansible_service.write_playbooks_from_tosca_interface(interfaces, tmp_path)
for playbook_path in paths:
out,err = ansible_service.run(inventory_path,playbook_path)
api_key, join_token, discovery_token_ca_cert_hash = ansible_service.parse_tokens(out)
ansible_playbook_path = k8s_service.write_ansible_k8s_files(tosca_template_json, tmp_path)
out, err = ansible_service.run(inventory_path, playbook_path)
template_dict = {}
......@@ -100,7 +107,9 @@ if __name__ == "__main__":
interfaces = tosca.get_interfaces(tosca_template_json)
vms = tosca.get_vms(tosca_template_json)
ansible_service.run(interfaces, vms)
k8s_service.run(tosca_template_json)
k8s_service.write_ansible_k8s_files(tosca_template_json)
......
......@@ -13,6 +13,8 @@ from ansible.executor.playbook_executor import PlaybookExecutor
from ansible.parsing.dataloader import DataLoader
from ansible.vars.manager import VariableManager
import re
logger = logging.getLogger(__name__)
if not getattr(logger, 'handler_set', None):
logger.setLevel(logging.INFO)
......@@ -23,7 +25,7 @@ if not getattr(logger, 'handler_set', None):
logger.handler_set = True
def write_ansible_files(vms, interfaces, tmp_path):
def write_inventory_file(tmp_path, vms):
workers = []
k8_master = None
ansible_ssh_private_key_file_path = None
......@@ -47,6 +49,7 @@ def write_ansible_files(vms, interfaces, tmp_path):
if ansible_ssh_user is None:
ansible_ssh_user = vms[vm_name]['properties']['user_name']
k8s_hosts_path = tmp_path + "/k8s_hosts"
with open(k8s_hosts_path, "w") as k8s_hosts_file:
print('[k8-master]', file=k8s_hosts_file)
print(k8_master, file=k8s_hosts_file)
......@@ -63,41 +66,63 @@ def write_ansible_files(vms, interfaces, tmp_path):
print('ansible_ssh_private_key_file=' + ansible_ssh_private_key_file_path, file=k8s_hosts_file)
print('ansible_ssh_common_args=\'-o StrictHostKeyChecking=no\'', file=k8s_hosts_file)
print('ansible_ssh_user=' + ansible_ssh_user, file=k8s_hosts_file)
return k8s_hosts_path
image_url = interfaces['Kubernetes']['install']['inputs']['playbook']
r = requests.get(image_url)
with open(tmp_path + "/install.yml", 'wb') as f:
f.write(r.content)
def write_playbooks(tmp_path, interface):
playbook_paths = []
interface_stage_list = ['install','create']
# for interface_stage in interface:
for interface_stage in interface_stage_list:
playbook_url = interface[interface_stage]['inputs']['playbook']
r = requests.get(playbook_url)
playbook_path = tmp_path + "/" + interface_stage + '.yaml'
with open(playbook_path, 'wb') as f:
f.write(r.content)
playbook_paths.append(playbook_path)
return playbook_paths
image_url = interfaces['Kubernetes']['create']['inputs']['playbook']
r = requests.get(image_url)
with open(tmp_path + "/create.yml", 'wb') as f:
f.write(r.content)
return tmp_path
def write_playbooks_from_tosca_interface(interfaces, tmp_path):
playbook_paths = []
for interface_name in interfaces:
playbook_paths = playbook_paths + write_playbooks(tmp_path, interfaces[interface_name])
return playbook_paths
def run(interfaces, vms):
tmp_path = tempfile.mkdtemp()
write_ansible_files(vms, interfaces, tmp_path)
p = Popen(["ansible-playbook", "-i", tmp_path + "/k8s_hosts", tmp_path + "/install.yml"], stdin=PIPE, stdout=PIPE,
stderr=PIPE)
def run(inventory_path, playbook_path):
p = Popen(["ansible-playbook", "-i", inventory_path, playbook_path], stdin=PIPE, stdout=PIPE,
stderr=PIPE)
output, err = p.communicate()
print(output.decode('utf-8'))
print(err.decode('utf-8'))
rc = p.returncode
return output, err
p = Popen(["ansible-playbook", "-i", tmp_path + "/k8s_hosts", tmp_path + "/create.yml"], stdin=PIPE, stdout=PIPE,
stderr=PIPE)
output, err = p.communicate()
out = output.decode('utf-8')
err = err.decode('utf-8')
print(out)
print(err)
rc = p.returncode
api_key = out
return api_key
def parse_tokens(out):
api_key = re.search("^msg.*", out)
join_token = re.search("^\"stdout\": \"$", out)
m = re.search('Join command is kubeadm join(.+?)\"', out)
if m:
found = m.group(1)
m = re.search('--token (.+?) --discovery-token-ca-cert-hash', found)
if m:
join_token = m.group(1)
m = re.search('--discovery-token-ca-cert-hash (.+?) "}', out)
if m:
discovery_token_ca_cert_hash = m.group(1)
m = re.search('--token (.+?) --discovery-token-ca-cert-hash', found)
if m:
join_token = m.group(1)
m = re.search('"stdout": " (.+?)",', out)
if m:
api_key = m.group(1)
return api_key, join_token, discovery_token_ca_cert_hash
def execute_playbook(hosts, playbook_path, user, ssh_key_file, extra_vars, passwords):
......
import yaml
yaml.Dumper.ignore_aliases = lambda *args: True
def get_dockers(tosca_template_json):
dockers = []
node_templates = tosca_template_json['topology_template']['node_templates']
for node_name in node_templates:
if node_templates[node_name]['type'] == 'tosca.nodes.ARTICONF.Container.Application.Docker':
dockers.append(node_templates[node_name])
docker = {node_name: node_templates[node_name]}
dockers.append(docker)
return dockers
def write_k8s_files(dockers):
def get_k8s_definitions(dockers):
k8s_services = []
k8s_deployments = []
definitions = {}
for docker in dockers:
docker_name = next(iter(docker))
k8s_service = {'apiVersion': 'v1', 'kind': 'Service'}
labels = {'app': docker_name}
metadata = {'labels': labels, 'name': docker_name}
k8s_service['metadata'] = metadata
spec = {'type': 'NodePort'}
docker_ports = docker[docker_name]['properties']['ports'][0].split(':')
ports = {'port': docker_ports[1], 'nodePort': docker_ports[0]}
spec['ports'] = ports
app = {'app': docker_name}
spec['selector'] = app
k8s_service['spec'] = spec
# print(yaml.safe_dump(k8s_service))
k8s_services.append(k8s_service)
# -----------------------------------------------------------
deployment = {'apiVersion': 'apps/v1', 'kind': 'Deployment', 'metadata': metadata}
match_labels = {'app': docker_name}
selector = {'matchLabels': match_labels}
spec = {'selector': selector, 'replicas': 1}
labels['app'] = docker_name
metadata = {'labels': labels}
template = {'metadata': metadata}
pass
containers = []
container = {'image': docker[docker_name]['artifacts']['image']['file'], 'name': docker_name}
ports = {'containerPort': docker_ports[1]}
container['ports'] = ports
containers.append(container)
template_spec = {'containers': containers}
template['spec'] = template_spec
spec['template'] = template
def run(tosca_template_json):
deployment['spec'] = spec
k8s_deployments.append(deployment)
# print(yaml.dump(deployment))
definitions['services'] = k8s_services
definitions['deployments'] = k8s_deployments
return definitions
def write_ansible_k8s_files(tosca_template_json, tmp_path):
dockers = get_dockers(tosca_template_json)
tmp_dir = write_k8s_files(dockers)
return tmp_dir
\ No newline at end of file
k8s_definitions = get_k8s_definitions(dockers)
services = k8s_definitions['services']
deployments = k8s_definitions['deployments']
i = 0
tasks = []
for services_def in services:
k8s = {'state': 'present', 'definition': services_def}
task = {'name': 'Create a Service object' + str(i), 'become': 'yes', 'k8s': k8s}
i += 1
tasks.append(task)
i = 0
tasks = []
for deployments_def in deployments:
k8s = {'state': 'present', 'definition': services_def}
task = {'name': 'Create a Service object' + str(i), 'become': 'yes', 'k8s': k8s}
i += 1
tasks.append(task)
ansible_playbook = {'hosts': 'k8-master', 'tasks': tasks}
print(yaml.safe_dump(ansible_playbook))
ansible_playbook_path = tmp_path+'/'+'k8s_playbook.yml'
f = open(ansible_playbook_path, "w")
yaml.dump(yaml.safe_dump(ansible_playbook), f)
f.close()
return ansible_playbook_path
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment