Commit 28344bd3 authored by Spiros Koulouzis's avatar Spiros Koulouzis

adding attributes

parent 8403213f
......@@ -85,7 +85,7 @@ def handle_delivery(message):
nodes_pairs = tosca_helper.get_deployment_node_pairs()
deployService = DeployService(semaphore_base_url=semaphore_base_url, semaphore_username=semaphore_username,
semaphore_password=semaphore_password)
semaphore_password=semaphore_password,vms=tosca_helper.get_vms())
for node_pair in nodes_pairs:
deployService.deploy(node_pair)
......
......@@ -39,7 +39,6 @@ class TestDeployer(unittest.TestCase):
sure_tosca_base_url = 'http://127.0.0.1:8081/tosca-sure/1.0.0'
polemarch_base_url='http://127.0.0.1:30001/api/v2'
semaphore_base_url = 'http://127.0.0.1:3000/api'
tosca_service_is_up = ToscaHelper.service_is_up(sure_tosca_base_url)
semaphore_is_up = ToscaHelper.service_is_up(semaphore_base_url)
......
......@@ -30,11 +30,11 @@ class AnsibleService:
def execute(self,nodes_pair):
vms = nodes_pair[0]
def execute(self, nodes_pair, interface_type, vms, env_vars=None):
application = nodes_pair[1]
name = application.name
desired_state = None
tasks_outputs = {}
interfaces = application.node_template.interfaces
if 'current_state' in application.node_template.attributes:
current_state = application.node_template.attributes['current_state']
......@@ -49,25 +49,35 @@ class AnsibleService:
key_id = self.semaphore_helper.create_ssh_key(application.name, project_id, private_key)
inventory_id = self.semaphore_helper.create_inventory(application.name, project_id, key_id,inventory_contents)
if 'RUNNING' == desired_state:
standard = interfaces['Standard']
create = standard['create']
interface = interfaces[interface_type]
create = interface['create']
inputs = create['inputs']
git_url = inputs['repository']
playbook_names = inputs['resources']
for playbook_name in playbook_names:
task_id = self.run_task(name, project_id, key_id, git_url, inventory_id, playbook_name)
if self.semaphore_helper.get_task(project_id, task_id).status != 'success':
break
if self.semaphore_helper.get_task(project_id,task_id).status == 'success':
configure = standard['configure']
# for playbook_name in playbook_names:
# environment_id = None
# if env_vars:
# environment_id = self.semaphore_helper.create_environment(project_id,name,env_vars)
# task_id = self.run_task(name, project_id, key_id, git_url, inventory_id, playbook_name, environment_id= environment_id)
# if self.semaphore_helper.get_task(project_id, task_id).status != 'success':
# break
# tasks_outputs[task_id] = self.semaphore_helper.get_task_outputs(project_id, task_id)
# if self.semaphore_helper.get_task(project_id,task_id).status == 'success':
if True:
configure = interface['configure']
inputs = configure['inputs']
git_url = inputs['repository']
playbook_names = inputs['resources']
for playbook_name in playbook_names:
task_id = self.run_task(name, project_id, key_id, git_url, inventory_id, playbook_name)
environment_id = None
if env_vars:
environment_id = self.semaphore_helper.create_environment(project_id, name, env_vars)
task_id = self.run_task(name, project_id, key_id, git_url, inventory_id, playbook_name,environment_id= environment_id)
if self.semaphore_helper.get_task(project_id, task_id).status != 'success':
break
tasks_outputs[task_id] = self.semaphore_helper.get_task_outputs(project_id, task_id)
return tasks_outputs
def build_yml_inventory(self, vms):
# loader = DataLoader()
......@@ -100,12 +110,12 @@ class AnsibleService:
private_key = vms[0].node_template.attributes['user_key_pair']['keys']['private_key']
return base64.b64decode(private_key).decode('utf-8').replace(r'\n', '\n')
def run_task(self, name, project_id, key_id, git_url, inventory_id, playbook_name):
def run_task(self, name, project_id, key_id, git_url, inventory_id, playbook_name,environment_id=None):
logger.info('project_id: '+str(project_id)+ ' task name: ' + str(name)+ ' git url: '+git_url+' playbook: '+playbook_name)
self.repository_id = self.semaphore_helper.create_repository(name, project_id, key_id, git_url)
template_id = self.semaphore_helper.create_template(project_id, key_id, inventory_id, self.repository_id,
playbook_name)
task_id = self.semaphore_helper.execute_task(project_id, template_id, playbook_name)
task_id = self.semaphore_helper.execute_task(project_id, template_id, playbook_name,environment_id=environment_id)
task = self.semaphore_helper.get_task(project_id, task_id)
last_output = ''
while task.status == 'waiting' or task.status == 'running':
......@@ -116,8 +126,6 @@ class AnsibleService:
if last_output != this_output:
logger.info('task output: ' + str(this_output))
last_output = this_output
# logger.info('task output: ' + str(latask name:st_output))
sleep(3)
return task_id
return task_id
\ No newline at end of file
import json
from service import tosca_helper, ansible_service
from service.ansible_service import AnsibleService
import logging
logger = logging.getLogger(__name__)
if not getattr(logger, 'handler_set', None):
logger.setLevel(logging.INFO)
h = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
h.setFormatter(formatter)
logger.addHandler(h)
logger.handler_set = True
class DeployService:
def __init__(self, polemarch_base_url=None,polemarch_username=None,polemarch_password=None,
semaphore_base_url=None,semaphore_username=None,semaphore_password=None):
semaphore_base_url=None,semaphore_username=None,semaphore_password=None, vms=None):
# self.polemarch_base_url = polemarch_base_url
# self.polemarch_username=polemarch_username
# self.polemarch_password = polemarch_password
self.semaphore_base_url = semaphore_base_url
self.semaphore_username = semaphore_username
self.semaphore_password = semaphore_password
self.vms = vms
def deploy(self,nodes_pair):
target = nodes_pair[0]
source = nodes_pair[1]
logger.info('target: ' + str(target) + ' source: ' + str(source))
interface_types = tosca_helper.get_interface_types(source)
if interface_types:
ansible_service = AnsibleService(self.semaphore_base_url, self.semaphore_username, self.semaphore_password)
env_vars = self.get_env_vars(nodes_pair)
if 'Standard' in interface_types:
ansible_service = AnsibleService(self.semaphore_base_url,self.semaphore_username,self.semaphore_password)
ansible_service.execute(nodes_pair)
else:
print(interface_types)
task_outputs = ansible_service.execute(nodes_pair, 'Standard', self.vms, env_vars=env_vars)
self.set_attributes(task_outputs,nodes_pair)
if 'Kubernetes' in interface_types:
task_outputs = ansible_service.execute(nodes_pair, 'Kubernetes', self.vms, env_vars=env_vars)
self.set_attributes(task_outputs,nodes_pair)
return None
def get_env_vars(self, nodes_pair):
target = nodes_pair[0]
source = nodes_pair[1]
if source.node_template.type == 'tosca.nodes.QC.Container.Application.Docker':
env_vars = {'DOCKER_IMAGE':source.node_template.artifacts['image']['file']}
env_vars['DOCKER_SERVICE_NAME'] = source.name
env_vars['CONTAINER_PORT'] = source.node_template.properties['ports'][0].split(':')[1]
env_vars['K8s_NAMESPACE'] = 'default'
return env_vars
return None
def set_attributes(self, task_outputs,nodes_pair):
target = nodes_pair[0]
source = nodes_pair[1]
if source.node_template.type == 'tosca.nodes.QC.docker.Orchestrator.Kubernetes':
if 'tokens' not in source.node_template.attributes:
tokens = []
source.node_template.attributes['tokens'] = tokens
else:
tokens = source.node_template.attributes['tokens']
k8s_secrets = None
k8s_services = None
for task_output_key in task_outputs:
task_output = task_outputs[task_output_key]
if not k8s_secrets:
k8s_secrets = self.parse_ansible_var('k8s_secrets',task_output)
if not k8s_services:
k8s_services = self.parse_ansible_var('k8s_services', task_output)
if k8s_services and k8s_secrets:
break
credential = {'token_type':'k8s_dashboard_token'}
credential['token'] = self.get_dashboard_token(k8s_secrets)
tokens.append(credential)
def parse_ansible_var(self,var_name, output_array):
index = 0
start_index = -1
end_index = -1
for out in output_array:
index += 1
if 'TASK' in out or 'PLAY RECAP' in out:
if start_index > -1:
end_index = index - 1
break
if '"' + var_name + '":' in out:
start_index = index - 1
if start_index <= -1:
return None
ansible_var = output_array[start_index:end_index]
json_ansible_var = '{'
for item in ansible_var:
json_ansible_var = json_ansible_var + item
return json.loads(json_ansible_var)
def get_dashboard_token(self, k8s_secrets):
print(k8s_secrets)
return None
\ No newline at end of file
......@@ -49,7 +49,7 @@ class ToscaHelper:
# So we explicitly get the VMs
# I don't like this solution but I can't think of something better.
if related_node.node_template.type == 'tosca.nodes.QC.VM.topology':
vms = self.tosca_client.get_node_templates(self.doc_id,type_name='tosca.nodes.QC.VM.Compute')
vms = self.get_vms()
related_node = vms
pair = (related_node, node)
nodes_pairs.append(pair)
......@@ -70,6 +70,9 @@ class ToscaHelper:
return True
def get_vms(self):
return self.tosca_client.get_node_templates(self.doc_id, type_name='tosca.nodes.QC.VM.Compute')
def get_interface_types(node):
interface_type_names = []
......
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