Commit 96924a93 authored by Spiros Koulouzis's avatar Spiros Koulouzis

added sure_tosca python client

parent d32aceb8
......@@ -14,8 +14,8 @@ from time import sleep
from concurrent.futures import thread
from threading import Thread
from service import tosca, k8s_service
from service import ansible_service
import sure_tosca_client
logger = logging.getLogger(__name__)
......@@ -66,6 +66,14 @@ def on_request(ch, method, props, body):
ch.basic_ack(delivery_tag=method.delivery_tag)
def init_sure_tosca_client(sure_tosca_base_path):
configuration = sure_tosca_client.Configuration()
sure_tosca_client.configuration.host = sure_tosca_base_path
api_client = sure_tosca_client.ApiClient(configuration=configuration)
sure_tosca_client_api = sure_tosca_client.api.default_api.DefaultApi(api_client=api_client) # noqa: E501
return sure_tosca_client_api
def handle_delivery(message):
logger.info("Got: " + str(message))
try:
......@@ -77,31 +85,35 @@ def handle_delivery(message):
tosca_file_name = 'tosca_template'
tosca_template_dict = parsed_json_message['toscaTemplate']
tosca_interfaces = tosca.get_interfaces(tosca_template_dict)
tmp_path = tempfile.mkdtemp()
vms = tosca.get_vms(tosca_template_dict)
inventory_path = ansible_service.write_inventory_file(tmp_path, vms)
paths = ansible_service.write_playbooks_from_tosca_interface(tosca_interfaces, tmp_path)
tokens = {}
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_api_tokens(out.decode("utf-8"))
if api_key:
tokens['api_key'] = api_key
if join_token:
tokens['join_token'] = join_token
if discovery_token_ca_cert_hash:
tokens['discovery_token_ca_cert_hash'] = discovery_token_ca_cert_hash
ansible_playbook_path = k8s_service.write_ansible_k8s_files(tosca_template_dict, tmp_path)
out, err = ansible_service.run(inventory_path, ansible_playbook_path)
dashboard_token = ansible_service.parse_dashboard_tokens(out.decode("utf-8"))
tokens['dashboard_token'] = dashboard_token
tosca_template_dict = tosca.add_tokens(tokens, tosca_template_dict)
tosca_template_dict = tosca.add_dashboard_url(k8s_service.get_dashboard_url(vms), tosca_template_dict)
tosca_template_dict = tosca.add_service_url(k8s_service.get_service_urls(vms,tosca_template_dict), tosca_template_dict)
print(yaml.dump(tosca_template_dict))
sure_tosca_client.upload_tosca_template()
# tosca_interfaces = tosca.get_interfaces(tosca_template_dict)
# tmp_path = tempfile.mkdtemp()
# vms = tosca.get_vms(tosca_template_dict)
# inventory_path = ansible_service.write_inventory_file(tmp_path, vms)
# paths = ansible_service.write_playbooks_from_tosca_interface(tosca_interfaces, tmp_path)
# tokens = {}
# 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_api_tokens(out.decode("utf-8"))
# if api_key:
# tokens['api_key'] = api_key
# if join_token:
# tokens['join_token'] = join_token
# if discovery_token_ca_cert_hash:
# tokens['discovery_token_ca_cert_hash'] = discovery_token_ca_cert_hash
#
# ansible_playbook_path = k8s_service.write_ansible_k8s_files(tosca_template_dict, tmp_path)
# out, err = ansible_service.run(inventory_path, ansible_playbook_path)
# dashboard_token = ansible_service.parse_dashboard_tokens(out.decode("utf-8"))
# tokens['dashboard_token'] = dashboard_token
#
# tosca_template_dict = tosca.add_tokens(tokens, tosca_template_dict)
# tosca_template_dict = tosca.add_dashboard_url(k8s_service.get_dashboard_url(vms), tosca_template_dict)
# tosca_template_dict = tosca.add_service_url(k8s_service.get_service_urls(vms, tosca_template_dict),
# tosca_template_dict)
response = {'toscaTemplate': tosca_template_dict}
output_current_milli_time = int(round(time.time() * 1000))
......@@ -127,17 +139,13 @@ if __name__ == "__main__":
# use safe_load instead load
tosca_template_json = yaml.safe_load(f)
interfaces = tosca.get_interfaces(tosca_template_json)
vms = tosca.get_vms(tosca_template_json)
ansible_service.run(interfaces, vms)
k8s_service.write_ansible_k8s_files(tosca_template_json)
else:
logger.info("Input args: " + sys.argv[0] + ' ' + sys.argv[1] + ' ' + sys.argv[2])
global channel, queue_name, connection
global channel, queue_name, connection, sure_tosca_client
channel, connection = init_chanel(sys.argv)
queue_name = sys.argv[2]
sure_tosca_base_url = sys.argv[3] # "http://localhost:8081/tosca-sure/1.0.0/"
sure_tosca_client = init_sure_tosca_client(sure_tosca_base_url)
logger.info("Awaiting RPC requests")
try:
thread = Thread(target=threaded_function, args=(1,))
......
......@@ -5,34 +5,33 @@ import os
import os.path
import tempfile
import time
import yaml
import unittest
from service import k8s_service, tosca, ansible_service
from tower_cli import get_resource
from tower_cli.exceptions import Found
from tower_cli.conf import settings
import sure_tosca_client
class TestDeployer(unittest.TestCase):
def test_parse_token(self):
tosca_path = "../../ansible_playbooks/"
example_ansible_output_file_path = tosca_path + '/example_ansible_output.out'
if not os.path.exists(example_ansible_output_file_path):
tosca_path = "../ansible_playbooks/"
example_ansible_output_file_path = tosca_path + '/example_ansible_output.out'
with open(example_ansible_output_file_path, 'r') as file:
out = file.read()
token = ansible_service.parse_dashboard_tokens(out)
# def test_parse_token(self):
# tosca_path = "../../ansible_playbooks/"
# example_ansible_output_file_path = tosca_path + '/example_ansible_output.out'
# if not os.path.exists(example_ansible_output_file_path):
# tosca_path = "../ansible_playbooks/"
# example_ansible_output_file_path = tosca_path + '/example_ansible_output.out'
#
# with open(example_ansible_output_file_path, 'r') as file:
# out = file.read()
# token = ansible_service.parse_dashboard_tokens(out)
def test(self):
logger = logging.getLogger(__name__)
tosca_path = "../../example_messages/"
input_tosca_file_path = tosca_path + '/message_example_provisioned.json'
input_tosca_file_path = tosca_path + '/message_provision_response.json'
if not os.path.exists(input_tosca_file_path):
tosca_path = "../example_messages/"
input_tosca_file_path = tosca_path + '/message_example_provisioned.json'
input_tosca_file_path = tosca_path + '/message_provision_response.json'
with open(input_tosca_file_path, 'r') as stream:
parsed_json_message = json.load(stream)
......@@ -42,39 +41,56 @@ class TestDeployer(unittest.TestCase):
tosca_file_name = 'tosca_template'
tosca_template_dict = parsed_json_message['toscaTemplate']
tosca_interfaces = tosca.get_interfaces(tosca_template_dict)
tmp_path = tempfile.mkdtemp()
vms = tosca.get_vms(tosca_template_dict)
inventory_path = ansible_service.write_inventory_file(tmp_path, vms)
paths = ansible_service.write_playbooks_from_tosca_interface(tosca_interfaces, tmp_path)
tokens = {}
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_api_tokens(out.decode("utf-8"))
if api_key:
tokens['api_key'] = api_key
if join_token:
tokens['join_token'] = join_token
if discovery_token_ca_cert_hash:
tokens['discovery_token_ca_cert_hash'] = discovery_token_ca_cert_hash
ansible_playbook_path = k8s_service.write_ansible_k8s_files(tosca_template_dict, tmp_path)
out, err = ansible_service.run(inventory_path, ansible_playbook_path)
dashboard_token = ansible_service.parse_dashboard_tokens(out.decode("utf-8"))
tokens['dashboard_token'] = dashboard_token
tosca_template_dict = tosca.add_tokens(tokens, tosca_template_dict)
tosca_template_dict = tosca.add_dashboard_url(k8s_service.get_dashboard_url(vms), tosca_template_dict)
response = {'toscaTemplate': tosca_template_dict}
output_current_milli_time = int(round(time.time() * 1000))
response["creationDate"] = output_current_milli_time
logger.info("Returning Deployment")
logger.info("Output message:" + json.dumps(response))
print(json.dumps(response))
tosca_template_path = tmp_path + os.path.sep + 'toscaTemplate.yml'
with open(tosca_template_path, 'w') as outfile:
yaml.dump(tosca_template_dict, outfile, default_flow_style=False)
tosca_client = init_sure_tosca_client('http://localhost:8081/tosca-sure/1.0.0/')
doc_id = tosca_client.upload_tosca_template(tosca_template_path)
print(doc_id)
# tosca_interfaces = tosca.get_interfaces(tosca_template_dict)
# tmp_path = tempfile.mkdtemp()
# vms = tosca.get_vms(tosca_template_dict)
# inventory_path = ansible_service.write_inventory_file(tmp_path, vms)
# paths = ansible_service.write_playbooks_from_tosca_interface(tosca_interfaces, tmp_path)
#
# tokens = {}
# 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_api_tokens(out.decode("utf-8"))
# if api_key:
# tokens['api_key'] = api_key
# if join_token:
# tokens['join_token'] = join_token
# if discovery_token_ca_cert_hash:
# tokens['discovery_token_ca_cert_hash'] = discovery_token_ca_cert_hash
#
# ansible_playbook_path = k8s_service.write_ansible_k8s_files(tosca_template_dict, tmp_path)
# out, err = ansible_service.run(inventory_path, ansible_playbook_path)
# dashboard_token = ansible_service.parse_dashboard_tokens(out.decode("utf-8"))
#
# tokens['dashboard_token'] = dashboard_token
#
# tosca_template_dict = tosca.add_tokens(tokens, tosca_template_dict)
#
# tosca_template_dict = tosca.add_dashboard_url(k8s_service.get_dashboard_url(vms), tosca_template_dict)
#
# response = {'toscaTemplate': tosca_template_dict}
# output_current_milli_time = int(round(time.time() * 1000))
# response["creationDate"] = output_current_milli_time
# logger.info("Returning Deployment")
# logger.info("Output message:" + json.dumps(response))
# print(json.dumps(response))
def init_sure_tosca_client(sure_tosca_base_path):
configuration = sure_tosca_client.Configuration()
sure_tosca_client.configuration.host = sure_tosca_base_path
api_client = sure_tosca_client.ApiClient(configuration=configuration)
sure_tosca_client_api = sure_tosca_client.api.default_api.DefaultApi(api_client=api_client) # noqa: E501
return sure_tosca_client_api
if __name__ == '__main__':
......
......@@ -9,10 +9,37 @@ services:
- "4369:4369"
- "15671:15671"
semaphore:
mysql:
image: mysql:5.6
environment:
MYSQL_RANDOM_ROOT_PASSWORD: 'yes'
MYSQL_DATABASE: semaphore
MYSQL_USER: semaphore
MYSQL_PASSWORD: semaphore
#volumes:
# - /tmp/mysql_data:/var/lib/mysql
ports:
- "30003:3000"
- "3306:3306"
semaphore:
image: ansiblesemaphore/semaphore
environment:
SEMAPHORE_DB_USER: semaphore
SEMAPHORE_DB_PASS: semaphore
SEMAPHORE_DB_HOST: mysql
SEMAPHORE_DB_PORT: 3306
SEMAPHORE_DB: semaphore
SEMAPHORE_PLAYBOOK_PATH: /etc/semaphore
SEMAPHORE_ADMIN_PASSWORD: password
SEMAPHORE_ADMIN_NAME: "Developer"
SEMAPHORE_ADMIN_EMAIL: admin@localhost
SEMAPHORE_ADMIN: admin
SEMAPHORE_WEB_ROOT: http://0.0.0.0:3000
ports:
- "3000:3000"
depends_on:
- mysql
logspout:
......@@ -58,23 +85,24 @@ services:
environment:
RABBITMQ_HOST: rabbit
#provisioner:
provisioner:
depends_on:
- rabbit
- sure-tosca
image: provisioner:3.0.0
environment:
RABBITMQ_HOST: rabbit
SURE_TOSCA_BASE_PATH: http://sure-tosca:8081/tosca-sure/1.0.0
#deployer:
#depends_on:
#- rabbit
#- sure-tosca
#image: provisioner:3.0.0
#image: deployer:3.0.0
#environment:
#RABBITMQ_HOST: rabbit
#SURE_TOSCA_BASE_PATH: http://sure-tosca:8081/tosca-sure/1.0.0
deployer:
depends_on:
- rabbit
- sure-tosca
image: deployer:3.0.0
environment:
RABBITMQ_HOST: rabbit
#volumes:
#db-data:
......@@ -2,11 +2,11 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="3f84153d-6ed1-4691-94d6-53105266f15e" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/../deployer/requirements.txt" beforeDir="false" afterPath="$PROJECT_DIR$/../deployer/requirements.txt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../deployer/service/__init__.py" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/../deployer/service/k8s_service.py" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/../deployer/service/tosca.py" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/../deployer/__main__.py" beforeDir="false" afterPath="$PROJECT_DIR$/../deployer/__main__.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../deployer/test/test_deployer.py" beforeDir="false" afterPath="$PROJECT_DIR$/../deployer/test/test_deployer.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../docker-compose.yml" beforeDir="false" afterPath="$PROJECT_DIR$/../docker-compose.yml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/setup.py" beforeDir="false" afterPath="$PROJECT_DIR$/setup.py" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
......@@ -67,38 +67,38 @@
<screen x="67" y="34" width="1853" height="1046" />
</state>
<state x="723" y="257" width="530" height="598" key="FileChooserDialogImpl/67.34.1853.1046@67.34.1853.1046" timestamp="1584814061190" />
<state width="1825" height="134" key="GridCell.Tab.0.bottom" timestamp="1584900161062">
<state width="1825" height="342" key="GridCell.Tab.0.bottom" timestamp="1584902089025">
<screen x="67" y="34" width="1853" height="1046" />
</state>
<state width="1825" height="134" key="GridCell.Tab.0.bottom/67.34.1853.1046@67.34.1853.1046" timestamp="1584900161062" />
<state width="1825" height="134" key="GridCell.Tab.0.center" timestamp="1584900161062">
<state width="1825" height="342" key="GridCell.Tab.0.bottom/67.34.1853.1046@67.34.1853.1046" timestamp="1584902089025" />
<state width="1825" height="342" key="GridCell.Tab.0.center" timestamp="1584902089024">
<screen x="67" y="34" width="1853" height="1046" />
</state>
<state width="1825" height="134" key="GridCell.Tab.0.center/67.34.1853.1046@67.34.1853.1046" timestamp="1584900161062" />
<state width="1825" height="134" key="GridCell.Tab.0.left" timestamp="1584900161062">
<state width="1825" height="342" key="GridCell.Tab.0.center/67.34.1853.1046@67.34.1853.1046" timestamp="1584902089024" />
<state width="1825" height="342" key="GridCell.Tab.0.left" timestamp="1584902089023">
<screen x="67" y="34" width="1853" height="1046" />
</state>
<state width="1825" height="134" key="GridCell.Tab.0.left/67.34.1853.1046@67.34.1853.1046" timestamp="1584900161062" />
<state width="1825" height="134" key="GridCell.Tab.0.right" timestamp="1584900161062">
<state width="1825" height="342" key="GridCell.Tab.0.left/67.34.1853.1046@67.34.1853.1046" timestamp="1584902089023" />
<state width="1825" height="342" key="GridCell.Tab.0.right" timestamp="1584902089024">
<screen x="67" y="34" width="1853" height="1046" />
</state>
<state width="1825" height="134" key="GridCell.Tab.0.right/67.34.1853.1046@67.34.1853.1046" timestamp="1584900161062" />
<state width="1825" height="372" key="GridCell.Tab.1.bottom" timestamp="1584815771481">
<state width="1825" height="342" key="GridCell.Tab.0.right/67.34.1853.1046@67.34.1853.1046" timestamp="1584902089024" />
<state width="1825" height="263" key="GridCell.Tab.1.bottom" timestamp="1584902065994">
<screen x="67" y="34" width="1853" height="1046" />
</state>
<state width="1825" height="372" key="GridCell.Tab.1.bottom/67.34.1853.1046@67.34.1853.1046" timestamp="1584815771481" />
<state width="1825" height="372" key="GridCell.Tab.1.center" timestamp="1584815771481">
<state width="1825" height="263" key="GridCell.Tab.1.bottom/67.34.1853.1046@67.34.1853.1046" timestamp="1584902065994" />
<state width="1825" height="263" key="GridCell.Tab.1.center" timestamp="1584902065993">
<screen x="67" y="34" width="1853" height="1046" />
</state>
<state width="1825" height="372" key="GridCell.Tab.1.center/67.34.1853.1046@67.34.1853.1046" timestamp="1584815771481" />
<state width="1825" height="372" key="GridCell.Tab.1.left" timestamp="1584815771481">
<state width="1825" height="263" key="GridCell.Tab.1.center/67.34.1853.1046@67.34.1853.1046" timestamp="1584902065993" />
<state width="1825" height="263" key="GridCell.Tab.1.left" timestamp="1584902065993">
<screen x="67" y="34" width="1853" height="1046" />
</state>
<state width="1825" height="372" key="GridCell.Tab.1.left/67.34.1853.1046@67.34.1853.1046" timestamp="1584815771481" />
<state width="1825" height="372" key="GridCell.Tab.1.right" timestamp="1584815771481">
<state width="1825" height="263" key="GridCell.Tab.1.left/67.34.1853.1046@67.34.1853.1046" timestamp="1584902065993" />
<state width="1825" height="263" key="GridCell.Tab.1.right" timestamp="1584902065994">
<screen x="67" y="34" width="1853" height="1046" />
</state>
<state width="1825" height="372" key="GridCell.Tab.1.right/67.34.1853.1046@67.34.1853.1046" timestamp="1584815771481" />
<state width="1825" height="263" key="GridCell.Tab.1.right/67.34.1853.1046@67.34.1853.1046" timestamp="1584902065994" />
<state x="359" y="103" key="SettingsEditor" timestamp="1584813615342">
<screen x="67" y="34" width="1853" height="1046" />
</state>
......@@ -108,4 +108,15 @@
</state>
<state x="563" y="235" width="1053" height="732" key="find.popup/67.34.1853.1046@67.34.1853.1046" timestamp="1584900160970" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager>
<breakpoints>
<line-breakpoint enabled="true" suspend="THREAD" type="python-line">
<url>file://$PROJECT_DIR$/test/test_default_api.py</url>
<line>159</line>
<option name="timeStamp" value="2" />
</line-breakpoint>
</breakpoints>
</breakpoint-manager>
</component>
</project>
\ No newline at end of file
......@@ -25,7 +25,7 @@ VERSION = "1.0.0"
REQUIRES = [
"certifi==2019.11.28",
"six==1.14.0",
"python_dateutl==2.5.3",
# "python_dateutl==2.5.3",
"setuptools==46",
"urllib3==1.25.8",
"numpy==1.18.2"
......
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