Commit 3b41ac2a authored by Spiros Koulouzis's avatar Spiros Koulouzis

check if input is tosca create warning and parse as yml

parent 0e20afdd
This diff is collapsed.
...@@ -10,6 +10,7 @@ import tempfile ...@@ -10,6 +10,7 @@ import tempfile
import time import time
import json import json
from transformer.docker_compose_transformer import * from transformer.docker_compose_transformer import *
from os.path import expanduser
...@@ -69,12 +70,14 @@ def handle_delivery(message): ...@@ -69,12 +70,14 @@ def handle_delivery(message):
return "response" return "response"
if __name__ == "__main__": if __name__ == "__main__":
print sys.argv home = expanduser("~")
channel = init_chanel(sys.argv) transformer = DockerComposeTransformer(home+"/workspace/DRIP/docs/input_tosca_files/MOG_cardif.yml")
global queue_name transformer.getnerate_compose()
queue_name = sys.argv[2] # print sys.argv
# channel = init_chanel(sys.argv)
start(channel) # global queue_name
# queue_name = sys.argv[2]
# start(channel)
# try: # try:
## for node in tosca.nodetemplates: ## for node in tosca.nodetemplates:
## print "Name %s Type: %s " %(node.name,node.type) ## print "Name %s Type: %s " %(node.name,node.type)
......
...@@ -6,55 +6,104 @@ import toscaparser.utils.yamlparser ...@@ -6,55 +6,104 @@ import toscaparser.utils.yamlparser
class DockerComposeTransformer: class DockerComposeTransformer:
def __init__(self, tosca_file_path): def __init__(self, tosca_file_path):
yaml_dict_tpl = toscaparser.utils.yamlparser.load_yaml(tosca_file_path) self.yaml_dict_tpl = toscaparser.utils.yamlparser.load_yaml(tosca_file_path)
self.tt = ToscaTemplate(path=None, yaml_dict_tpl=yaml_dict_tpl) self.errors =[]
self.wornings = []
self.tt = None
try:
self.tt = ToscaTemplate(path=None, yaml_dict_tpl=self.yaml_dict_tpl)
except:
self.wornings.append("Not a valid tosca file")
self.DOCKER_TYPE = 'Switch.nodes.Application.Container.Docker' self.DOCKER_TYPE = 'Switch.nodes.Application.Container.Docker'
def getnerate_compose(self): def getnerate_compose(self):
dockers = [] if self.tt:
print dir(self.tt.topology_template) analize_tosca()
print dir(self.tt.outputs) else:
print dir(self.tt.nested_tosca_tpls_with_topology) self.analyze_yaml()
print dir(self.tt.nested_tosca_templates_with_topology)
print dir(self.tt.inputs)
print dir(self.tt.input_path)
print dir(self.tt.graph)
for node in self.tt.nodetemplates:
if node.parent_type.type == self.DOCKER_TYPE:
dockers.append(node)
print "Name %s Type: %s Parent: %s" %(node.name,node.type,node.parent_type.type)
def analyze_yaml(self):
node_types = self.yaml_dict_tpl['node_types']
docker_types = []
for node_type_key in node_types:
if node_types[node_type_key] and 'derived_from' in node_types[node_type_key].keys():
if node_types[node_type_key]['derived_from'] == self.DOCKER_TYPE:
docker_types.append(node_type_key)
node_templates = self.yaml_dict_tpl['topology_template']['node_templates']
services = {}
for node_template_key in node_templates:
# print node_templates[node_template_key]
for docker_type in docker_types:
if docker_type in node_templates[node_template_key]['type']:
if 'artifacts' in node_templates[node_template_key]:
artifacts = node_templates[node_template_key]['artifacts']
key = next(iter(artifacts))
docker_file = artifacts[key]['file']
services['name'] = docker_file
services['image'] = docker_file
services['id'] = node_template_key
if 'properties' in node_templates[node_template_key]:
properties = node_templates[node_template_key]['properties']
environment = []
for prop in properties:
if not isinstance(properties[prop],dict):
environment.append(prop+"="+str(properties[prop]))
services['environment'] = environment
if 'ports_mapping' in properties:
ports_mappings = properties['ports_mapping']
key = next(iter(ports_mappings))
host_port = ports_mappings[key]['host_port']
if not isinstance(host_port, (int, long, float, complex)):
host_port_var = host_port.replace('${','').replace('}','')
host_port = properties[host_port_var]
container_port = ports_mappings[key]['container_port']
if not isinstance(container_port, (int, long, float, complex)):
container_port_var = container_port.replace('${','').replace('}','')
container_port = properties[container_port_var]
ports = []
ports.append(str(host_port)+':'+str(container_port))
services['ports'] = ports
print services
def analize_tosca():
dockers = []
# print dir(self.tt.topology_template)
# print dir(self.tt.outputs)
# print dir(self.tt.nested_tosca_tpls_with_topology)
# print dir(self.tt.nested_tosca_templates_with_topology)
# print dir(self.tt.inputs)
# print dir(self.tt.input_path)
# print dir(self.tt.graph)
# topology_template = parsed_json_value['topology_template'] for node in self.tt.nodetemplates:
# node_templates = topology_template["node_templates"] if node.parent_type.type == self.DOCKER_TYPE:
# # dockers.append(node)
# response = {} # print dir(node)
# current_milli_time = lambda: int(round(time.time() * 1000)) print "Name %s Type: %s" %(node.name,node.type)
# response["creationDate"] = current_milli_time() service = {}
# response["parameters"] = [] service['name'] = node.type
# # print dir(node.get_properties_objects())
# for nodes in node_templates: # for prop_obj in node.get_properties_objects():
# if "Switch.nodes.Application.Container.Docker." in node_templates[nodes]['type']: # print dir(prop_obj)
# node_keys = node_templates[nodes].keys() # print "Name %s Type: %s Val: %s" %(prop_obj.name,prop_obj.type,prop_obj.value)
# if 'artifacts' in node_keys: # print (node.templates.keys())
# artifact_key = next(iter(node_templates[nodes]['artifacts'])) docker_file = ""
# artifact_keys = node_templates[nodes]['artifacts'][artifact_key].keys() for temp in node.templates:
# if 'file' in artifact_keys: print "\t template: %s" %(temp)
# docker = node_templates[nodes]['artifacts'][artifact_key]['file'] if 'artifacts' in node.templates[temp]:
# elif 'docker_image' in artifact_keys: key = next(iter(node.templates[temp]['artifacts']))
# docker = node_templates[nodes]['artifacts']['docker_image']['file'] if 'file' in node.templates[temp]['artifacts'][key]:
# result = {} docker_file = node.templates[temp]['artifacts'][key]['file']
# parameter = {} print "\t\tdocker_file: %s"%(docker_file)
# result['name'] = nodes
# result['size'] = 'Medium' if docker_file:
# result['docker'] = docker container_name = docker_file.split("/")[1]
# parameter['value'] = str(json.dumps(result)) if ':' in container_name:
# parameter['attributes'] = 'null' container_name = container_name.split(':')[0]
# parameter["url"] = "null" # print container_name
# parameter["encoding"] = "UTF-8" service ['container_name'] = container_name
# response["parameters"].append(parameter) # print "Name %s Type: %s Val: %s" %(prop_obj.name,prop_obj.type,prop_obj.value)
# print ("Output message: %s" % json.dumps(response)) # service ['container_name'] =
# return json.dumps(response)
\ No newline at end of file
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