From 86b74c0e470b0e4a8381c43a8680a612e876d442 Mon Sep 17 00:00:00 2001 From: David Weinholz Date: Fri, 11 Oct 2019 15:06:21 +0200 Subject: [PATCH] feat(Config):added port calc in config --- .../VirtualMachineHandler.py | 25 ++++++++++--------- VirtualMachineService/config/config.yml | 13 +++++----- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/VirtualMachineService/VirtualMachineHandler.py b/VirtualMachineService/VirtualMachineHandler.py index 6abc5613..3c0fa49f 100644 --- a/VirtualMachineService/VirtualMachineHandler.py +++ b/VirtualMachineService/VirtualMachineHandler.py @@ -47,6 +47,7 @@ import base64 from oslo_utils import encodeutils import redis +import parser active_playbooks = dict() @@ -143,12 +144,12 @@ def __init__(self, config): ] self.AVAIALABILITY_ZONE = cfg["openstack_connection"]["availability_zone"] self.DEFAULT_SECURITY_GROUP = cfg['openstack_connection']['default_security_group'] - # self.SET_PASSWORD = cfg['openstack_connection']['set_password'] if self.USE_GATEWAY: - self.GATEWAY_BASE = cfg["openstack_connection"]["gateway_base"] self.GATEWAY_IP = cfg["openstack_connection"]["gateway_ip"] - self.UDP_BASE = cfg["openstack_connection"]["udp_base"] - + self.SSH_FORMULAR = cfg["openstack_connection"]["ssh_port_calc_formular"] + self.UDP_FORMULAR = cfg["openstack_connection"]["udp_port_calc_formular"] + self.SSH_PORT_CALCULATION = parser.expr(self.SSH_FORMULAR).compile() + self.UDP_PORT_CALCULATION = parser.expr(self.UDP_FORMULAR).compile() self.logger.info("Gateway IP is {}".format(self.GATEWAY_IP)) if cfg["openstack_connection"]["openstack_default_security_group"]: @@ -778,8 +779,9 @@ def check_server_status(self, openstack_id, diskspace, volume_id): if self.USE_GATEWAY: serv_cop = self.get_server(openstack_id) server_base = serv_cop.fixed_ip.split(".")[-1] + x=int(server_base) host = str(self.GATEWAY_IP) - port = int(self.GATEWAY_BASE) + int(server_base) + port =eval(self.SSH_PORT_CALCULATION) elif self.get_server(openstack_id).floating_ip is None: host = self.add_floating_ip_to_server( openstack_id, self.FLOATING_IP_NETWORK @@ -923,10 +925,9 @@ def add_security_group_to_server(self, http, https, udp, server_id): ) ip_base = \ - list(self.conn.compute.server_ips(server=server_id))[0].to_dict()['address'].split(".")[ - -1] - - udp_port_start = int(ip_base) * 10 + int(self.UDP_BASE) + list(self.conn.compute.server_ips(server=server_id))[0].to_dict()['address'].split(".")[-1] + x=int(ip_base) + udp_port_start = eval(self.UDP_PORT_CALCULATION) security_group = self.conn.network.find_security_group(name_or_id=server_id) if security_group: @@ -962,8 +963,9 @@ def get_ip_ports(self, openstack_id): if self.USE_GATEWAY: server = self.get_server(openstack_id) server_base = server.fixed_ip.split(".")[-1] - port = int(self.GATEWAY_BASE) + int(server_base) - udp_port_start = int(server_base) * 10 + int(self.UDP_BASE) + x = int(server_base) + port = eval(self.SSH_PORT_CALCULATION) + udp_port_start = eval(self.UDP_PORT_CALCULATION) return {"IP": str(self.GATEWAY_IP), "PORT": str(port), "UDP": str(udp_port_start)} else: @@ -1153,7 +1155,6 @@ def delete_server(self, openstack_id): else: return False - return True except Exception as e: self.logger.exception("Delete Server {0} error: {1}".format(openstack_id, e)) diff --git a/VirtualMachineService/config/config.yml b/VirtualMachineService/config/config.yml index cd0139d0..7c69ef47 100644 --- a/VirtualMachineService/config/config.yml +++ b/VirtualMachineService/config/config.yml @@ -2,20 +2,17 @@ openstack_connection: host: 0.0.0.0 # Client Port port: 9090 - # Gateway Port and IP - gateway_base: 30000 - gateway_ip: 129.70.51.75 + # Gateway IP + gateway_ip: 129.70.51.75 # If set to True the client will use a Gateway instead of providing floating IPs for each instance. use_gateway: True - + # Id of the default simple vm security group default_security_group: ec824d8f-0e45-4eb1-8f49-089867fef366 # id of default security from the openstack project (will be removed from vm after vm is created) openstack_default_security_group: 26713dd3-26e9-41cf-b7cd-931a447923fb - udp_base: 30000 - set_password: False # network where the project is located network: portalexternalnetwork @@ -24,3 +21,7 @@ openstack_connection: availability_zone: default # If you use docker-compose this path needs to be the path you mount the server.pem into certfile: /code/VirtualMachineService/keys/server.pem + + # Used for gateway port calculation , x is always the last octet of the fixed ip (example : (x + 3) *3 ) ) + ssh_port_calc_formular: 30000 + x + udp_port_calc_formular: x * 10 + 30000