Commit f1c6b279 authored by Spiros Koulouzis's avatar Spiros Koulouzis

Added environment for getting environment vars

parent 67f39af4
This diff is collapsed.
...@@ -82,7 +82,7 @@ def handle_delivery(message): ...@@ -82,7 +82,7 @@ def handle_delivery(message):
def test_local(): def test_local():
home = expanduser("~") home = expanduser("~")
transformer = DockerComposeTransformer(home+"/workspace/DRIP/docs/input_tosca_files/BEIA.yml") transformer = DockerComposeTransformer(home+"/workspace/DRIP/docs/input_tosca_files/Matej.BEIA.yml")
compose = transformer.getnerate_compose() compose = transformer.getnerate_compose()
print yaml.dump(compose) print yaml.dump(compose)
response = {} response = {}
......
...@@ -3,6 +3,17 @@ from toscaparser import * ...@@ -3,6 +3,17 @@ from toscaparser import *
from toscaparser.tosca_template import ToscaTemplate from toscaparser.tosca_template import ToscaTemplate
import toscaparser.utils.yamlparser import toscaparser.utils.yamlparser
import yaml import yaml
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 DockerComposeTransformer: class DockerComposeTransformer:
...@@ -50,7 +61,7 @@ class DockerComposeTransformer: ...@@ -50,7 +61,7 @@ class DockerComposeTransformer:
def get_enviroment_vars(self,properties): def get_enviroment_vars(self,properties):
environments = [] environments = []
for prop in properties: for prop in properties:
if prop == 'Environment_variables' or prop == 'Live_variables': if prop == 'Environment_variables' or prop == 'Live_variables' or prop =='Environment':
for var in properties[prop]: for var in properties[prop]:
environment ={} environment ={}
environment[var] = properties[prop][var] environment[var] = properties[prop][var]
...@@ -68,12 +79,12 @@ class DockerComposeTransformer: ...@@ -68,12 +79,12 @@ class DockerComposeTransformer:
for port_map_key in ports_mappings: for port_map_key in ports_mappings:
port_map = {} port_map = {}
host_port = ports_mappings[port_map_key]['host_port'] host_port = ports_mappings[port_map_key]['host_port']
if not isinstance(host_port, (int, long, float, complex)): if not isinstance(host_port, (int, long, float, complex)) and '$' in host_port:
host_port_var = host_port.replace('${','').replace('}','') host_port_var = host_port.replace('${','').replace('}','')
host_port = properties[host_port_var] host_port = properties[host_port_var]
container_port = ports_mappings[port_map_key]['container_port'] container_port = ports_mappings[port_map_key]['container_port']
if not isinstance(container_port, (int, long, float, complex)): if not isinstance(container_port, (int, long, float, complex)) and '$' in container_port:
container_port_var = container_port.replace('${','').replace('}','') container_port_var = container_port.replace('${','').replace('}','')
container_port = properties[container_port_var] container_port = properties[container_port_var]
port_map[host_port] = container_port port_map[host_port] = container_port
...@@ -82,14 +93,18 @@ class DockerComposeTransformer: ...@@ -82,14 +93,18 @@ class DockerComposeTransformer:
ports_mappings = properties['in_ports'] ports_mappings = properties['in_ports']
for port_map_key in ports_mappings: for port_map_key in ports_mappings:
port_map = {} port_map = {}
host_port = ports_mappings[port_map_key]['host_port'] if 'host_port' in ports_mappings[port_map_key]:
container_port = ports_mappings[port_map_key]['container_port'] host_port = ports_mappings[port_map_key]['host_port']
container_port = None
if 'container_port' in ports_mappings[port_map_key]:
container_port = ports_mappings[port_map_key]['container_port']
if 'protocol' in ports_mappings[port_map_key]: if 'protocol' in ports_mappings[port_map_key]:
protocol = ports_mappings[port_map_key]['protocol'] protocol = ports_mappings[port_map_key]['protocol']
if protocol: if protocol:
container_port=container_port+'/'+protocol container_port=container_port+'/'+protocol
port_map[host_port] = container_port if container_port:
port_maps.append(port_map) port_map[host_port] = container_port
port_maps.append(port_map)
if 'out_ports' in properties: if 'out_ports' in properties:
ports_mappings = properties['out_ports'] ports_mappings = properties['out_ports']
for port_map_key in ports_mappings: for port_map_key in ports_mappings:
...@@ -110,15 +125,16 @@ class DockerComposeTransformer: ...@@ -110,15 +125,16 @@ class DockerComposeTransformer:
return node['requirements'] return node['requirements']
def get_volumes(self,requirements): def get_volumes(self,requirements):
volumes = [] if requirements:
for req in requirements: volumes = []
if 'volume' in req: for req in requirements:
vol = {} if 'volume' in req:
name = req['volume']['name'] vol = {}
path = req['volume']['link'] name = req['volume']['name']
vol[name]=path path = req['volume']['link']
volumes.append(vol) vol[name]=path
return volumes volumes.append(vol)
return volumes
def analyze_yaml(self): def analyze_yaml(self):
docker_types = self.get_docker_types() docker_types = self.get_docker_types()
...@@ -137,15 +153,16 @@ class DockerComposeTransformer: ...@@ -137,15 +153,16 @@ class DockerComposeTransformer:
docker_file = artifacts[key]['file'] docker_file = artifacts[key]['file']
if docker_file: if docker_file:
service['image'] = docker_file service['image'] = docker_file
if docker_file and docker_file is not None and '/' in docker_file: if docker_file and docker_file is not None and '/' in docker_file:
container_name = docker_file.split("/")[1] container_name = docker_file.split("/")[1]
if container_name and ':' in container_name: if container_name and ':' in container_name:
container_name = container_name.split(':')[0] container_name = container_name.split(':')[0]
service ['container_name'] = container_name+"_"+node_template_key service ['container_name'] = container_name+"_"+node_template_key
properties = self.get_properties(node_templates[node_template_key]) properties = self.get_properties(node_templates[node_template_key])
environment = self.get_enviroment_vars(properties) environment = self.get_enviroment_vars(properties)
service['environment'] = environment if environment:
service['environment'] = environment
port_maps = self.get_port_map(properties) port_maps = self.get_port_map(properties)
if port_maps: if port_maps:
......
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