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

Commit

Permalink
Merge pull request #198 from deNBI/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
dweinholz authored Jan 9, 2020
2 parents f9fbf00 + a1a7bd9 commit ea475ad
Show file tree
Hide file tree
Showing 8 changed files with 1,331 additions and 89 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@

## (2020-01-09)


#### Features

* **Cluster:**
* cluster model ([6e1d45b3](6e1d45b3))
* terminate and create ([738e2b65](738e2b65))

## (2020-01-06)


Expand Down
89 changes: 82 additions & 7 deletions VirtualMachineService/VirtualMachineHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from ttypes import otherException
from ttypes import flavorNotFoundException
from ttypes import ressourceException
from ttypes import Flavor, Image, VM, PlaybookResult, Backend
from ttypes import Flavor, Image, VM, PlaybookResult, Backend, ClusterInfo
from constants import VERSION
from ancon.Playbook import Playbook, THEIA, GUACAMOLE

Expand All @@ -26,7 +26,7 @@
from .ttypes import otherException
from .ttypes import flavorNotFoundException
from .ttypes import ressourceException
from .ttypes import Flavor, Image, VM, PlaybookResult, Backend
from .ttypes import Flavor, Image, VM, PlaybookResult, Backend, ClusterInfo
from .constants import VERSION
from .ancon.Playbook import Playbook, THEIA, GUACAMOLE

Expand Down Expand Up @@ -148,6 +148,16 @@ def __init__(self, config):
]
self.AVAIALABILITY_ZONE = cfg["openstack_connection"]["availability_zone"]
# try to initialize forc connection
try:
self.BIBIGRID_URL = cfg["bibigrid"]["bibigrid_url"]
self.SUB_NETWORK = cfg["bibigrid"]["sub_network"]
self.logger.info(msg="Bibigrd url loaded: {0}".format(self.BIBIGRID_URL))
except Exception as e:
self.logger.exception(e)
self.logger.info("Bibigrid not loaded.")
self.BIBIGRID_URL = None
self.SUB_NETWORK = None

try:
self.RE_BACKEND_URL = cfg["forc"]["forc_url"]
self.FORC_API_KEY = os.environ["FORC_API_KEY"]
Expand Down Expand Up @@ -749,7 +759,8 @@ def start_server_with_custom_key(self, flavor, image, servername, metadata, disk
).name)
if GUACAMOLE in resenv:
custom_security_groups.append(self.create_security_group(
name=servername + "_guacamole", resenv=resenv, description="Guacamole", ssh=False
name=servername + "_guacamole", resenv=resenv, description="Guacamole",
ssh=False
).name)

try:
Expand All @@ -763,7 +774,6 @@ def start_server_with_custom_key(self, flavor, image, servername, metadata, disk
server_name=servername, metadata=metadata)
init_script = self.create_mount_init_script(volume_id=volume_id)


server = self.conn.create_server(
name=servername,
image=image.id,
Expand Down Expand Up @@ -855,7 +865,8 @@ def create_backend(self, elixir_id, user_key_url, template, template_version, up
self.logger.exception(e)
return {}
try:
response = req.post(post_url, json=backend_info, timeout=(30, 30), headers={"X-API-KEY": self.FORC_API_KEY})
response = req.post(post_url, json=backend_info, timeout=(30, 30),
headers={"X-API-KEY": self.FORC_API_KEY})
try:
data = response.json()
except Exception as e:
Expand Down Expand Up @@ -947,7 +958,8 @@ def get_backend_by_id(self, id):
def delete_backend(self, id):
delete_url = "{0}/backends/{1}".format(self.RE_BACKEND_URL, id)
try:
response = req.delete(delete_url, timeout=(30, 30), headers={"X-API-KEY": self.FORC_API_KEY})
response = req.delete(delete_url, timeout=(30, 30),
headers={"X-API-KEY": self.FORC_API_KEY})
if response.status_code != 200:
return str(response.json())
elif response.status_code == 200:
Expand Down Expand Up @@ -990,7 +1002,8 @@ def get_templates_by_template(self, template_name):
self.logger.info(msg="create_backend timed out. {0}".format(e))

def check_template(self, template_name, template_version):
get_url = "{0}/templates/{1}/{2}".format(self.RE_BACKEND_URL, template_name, template_version)
get_url = "{0}/templates/{1}/{2}".format(self.RE_BACKEND_URL, template_name,
template_version)
try:
response = req.get(get_url, timeout=(30, 30), headers={"X-API-KEY": self.FORC_API_KEY})
if response.status_code == 401:
Expand Down Expand Up @@ -1252,6 +1265,14 @@ def add_security_group_to_server(self, http, https, udp, server_id):

return True

def get_servers_by_bibigrid_id(self, bibigrid_id):
filters = {"bibigrid_id": bibigrid_id, "name": bibigrid_id}
servers = self.conn.list_servers(filters=filters)
thrift_servers = []
for server in servers:
thrift_servers.append(self.openstack_server_to_thrift_server(server))
return thrift_servers

def get_ip_ports(self, openstack_id):
"""
Get Ip and Port of the sever.
Expand Down Expand Up @@ -1281,6 +1302,60 @@ def get_ip_ports(self, openstack_id):
)
return {}

def get_cluster_info(self, cluster_id):
headers = {"content-Type": "application/json"}
body = {"mode": "openstack"}
request_url = self.BIBIGRID_URL + 'list'
self.logger.info(request_url)

response = req.get(url=request_url, json=body, headers=headers,
verify=False)
self.logger.info(response.json())
infos = response.json()["info"]
for info in infos:
self.logger.info(cluster_id)
self.logger.info(info)
self.logger.info(info["cluster-id"])
self.logger.info(cluster_id == info["cluster-id"])
if info["cluster-id"] == cluster_id:
cluster_info = ClusterInfo(launch_date=info["launch date"],
group_id=info["group-id"],
network_id=info["network-id"],
public_ip=info["public-ip"],
subnet_id=info["subnet-id"],
user=info["user"],
inst_counter=info["# inst"],
cluster_id=info["cluster-id"],
key_name=info["key name"])
self.logger.info("CLuster info : {}".format(cluster_info))
return cluster_info

return None


def start_cluster(self, public_key, master_instance, worker_instances, user):
master_instance = master_instance.__dict__
del master_instance['count']
wI = []
for wk in worker_instances:
self.logger.info(wk)
wI.append(wk.__dict__)
headers = {"content-Type": "application/json"}
body = {"mode": "openstack", "subnet": self.SUB_NETWORK, "user": user, "sshUser": "ubuntu",
"availabilityZone": self.AVAIALABILITY_ZONE, "masterInstance": master_instance,
"workerInstances": wI}
request_url = self.BIBIGRID_URL + 'create'
response = req.post(url=request_url, json=body, headers=headers,
verify=False)
self.logger.info(response.json())
return response.json()


def terminate_cluster(self, cluster_id):
response = req.delete(url="{}terminate/{}".format(self.BIBIGRID_URL, cluster_id))
self.logger.info(response.json())
return response.json()

def create_snapshot(self, openstack_id, name, elixir_id, base_tag, description):
"""
Create an Snapshot from an server.
Expand Down
3 changes: 2 additions & 1 deletion VirtualMachineService/VirtualMachineServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

try:
from VirtualMachineService.VirtualMachineHandler import VirtualMachineHandler
except Exception:
except Exception as e:
print(e)
from VirtualMachineHandler import VirtualMachineHandler

from thrift.transport import TSSLSocket
Expand Down
28 changes: 28 additions & 0 deletions VirtualMachineService/VirtualMachineService-remote
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,14 @@ if len(sys.argv) <= 1 or sys.argv[1] == '--help':
print(' bool add_security_group_to_server(bool http, bool https, bool udp, string server_id)')
print(' get_servers()')
print(' get_servers_by_ids( server_ids)')
print(' get_servers_by_bibigrid_id(string bibigrid_id)')
print(' ClusterInfo get_cluster_info(string cluster_id)')
print(' VM get_server(string openstack_id)')
print(' bool stop_server(string openstack_id)')
print(' string create_snapshot(string openstack_id, string name, string elixir_id, string base_tag, string description)')
print(' get_limits()')
print(' start_cluster(string public_key, ClusterInstance master_instance, worker_instance, string user)')
print(' terminate_cluster(string cluster_id)')
print(' bool delete_image(string image_id)')
print(' bool delete_volume_attachment(string volume_id, string server_id)')
print(' bool delete_volume(string volume_id)')
Expand Down Expand Up @@ -340,6 +344,18 @@ elif cmd == 'get_servers_by_ids':
sys.exit(1)
pp.pprint(client.get_servers_by_ids(eval(args[0]),))

elif cmd == 'get_servers_by_bibigrid_id':
if len(args) != 1:
print('get_servers_by_bibigrid_id requires 1 args')
sys.exit(1)
pp.pprint(client.get_servers_by_bibigrid_id(args[0],))

elif cmd == 'get_cluster_info':
if len(args) != 1:
print('get_cluster_info requires 1 args')
sys.exit(1)
pp.pprint(client.get_cluster_info(args[0],))

elif cmd == 'get_server':
if len(args) != 1:
print('get_server requires 1 args')
Expand All @@ -364,6 +380,18 @@ elif cmd == 'get_limits':
sys.exit(1)
pp.pprint(client.get_limits())

elif cmd == 'start_cluster':
if len(args) != 4:
print('start_cluster requires 4 args')
sys.exit(1)
pp.pprint(client.start_cluster(args[0], eval(args[1]), eval(args[2]), args[3],))

elif cmd == 'terminate_cluster':
if len(args) != 1:
print('terminate_cluster requires 1 args')
sys.exit(1)
pp.pprint(client.terminate_cluster(args[0],))

elif cmd == 'delete_image':
if len(args) != 1:
print('delete_image requires 1 args')
Expand Down
Loading

0 comments on commit ea475ad

Please sign in to comment.