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