Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
feat(Config):added port calc in config
Browse files Browse the repository at this point in the history
  • Loading branch information
dweinholz committed Oct 11, 2019
1 parent 9a05c1a commit 86b74c0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
25 changes: 13 additions & 12 deletions VirtualMachineService/VirtualMachineHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import base64
from oslo_utils import encodeutils
import redis
import parser

active_playbooks = dict()

Expand Down Expand Up @@ -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"]:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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))
Expand Down
13 changes: 7 additions & 6 deletions VirtualMachineService/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 86b74c0

Please sign in to comment.