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 #169 from deNBI/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
dweinholz authored Nov 19, 2019
2 parents dc28fa7 + e694f96 commit b41f052
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 52 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CLOUD_CLIENT_TAG=0.1.0-beta.0.7.4
111 changes: 62 additions & 49 deletions VirtualMachineService/VirtualMachineHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,9 @@ def get_Image_with_Tag(self, id):
self.logger.info("Get Image {0} with tags".format(id))
try:
images = self.conn.list_images()
img = list(filter(lambda image: image["id"] == id, images))[0]
img = list(filter(lambda image: image["id"] == id, images))
self.logger.info(img)
img = img[0]
metadata = img["metadata"]
description = metadata.get("description")
tags = img.get("tags")
Expand Down Expand Up @@ -360,6 +362,37 @@ def import_keypair(self, keyname, public_key):
self.logger.exception("Import Keypair {0} error:{1}".format(keyname, e))
return

def openstack_flav_to_thrift_flav(self, flavor):
try:
if "id" in flavor:
flav = self.conn.compute.get_flavor(flavor["id"]).to_dict()
name = flav["name"]
openstack_id = flavor["id"]
else:
# Giessen
flav = flavor['flavor']
name = flav["original_name"]
openstack_id = None

flav = Flavor(
vcpus=flav["vcpus"],
ram=flav["ram"],
disk=flav["disk"],
name=name,
openstack_id=openstack_id,
)
return flav
except Exception as e:
self.logger.exception(e)
flav = Flavor(
vcpus=None,
ram=None,
disk=None,
name=None,
openstack_id=None,
)
return flav

def get_server(self, openstack_id):
"""
Get a server.
Expand Down Expand Up @@ -393,11 +426,9 @@ def get_server(self, openstack_id):
timestamp = time.mktime(dt.timetuple())
else:
timestamp = None
try:
flav = self.conn.compute.get_flavor(serv["flavor"]["id"]).to_dict()
except Exception as e:
self.logger.exception(e)
flav = None

flav = self.openstack_flav_to_thrift_flav(serv["flavor"])

try:
img = self.get_Image_with_Tag(serv["image"]["id"])
except Exception as e:
Expand All @@ -413,13 +444,7 @@ def get_server(self, openstack_id):

if floating_ip:
server = VM(
flav=Flavor(
vcpus=flav["vcpus"],
ram=flav["ram"],
disk=flav["disk"],
name=flav["name"],
openstack_id=flav["id"],
),
flav=flav,
img=img,
status=serv["status"],
metadata=serv["metadata"],
Expand All @@ -434,13 +459,7 @@ def get_server(self, openstack_id):
)
else:
server = VM(
flav=Flavor(
vcpus=flav["vcpus"],
ram=flav["ram"],
disk=flav["disk"],
name=flav["name"],
openstack_id=flav["id"],
),
flav=flav,
img=img,
status=serv["status"],
metadata=serv["metadata"],
Expand Down Expand Up @@ -601,6 +620,7 @@ def start_server(
network=[network.id],
key_name=key_pair.name,
meta=metadata,
availability_zone=self.AVAIALABILITY_ZONE,
security_groups=self.DEFAULT_SECURITY_GROUPS
)

Expand Down Expand Up @@ -632,38 +652,45 @@ def start_server_with_custom_key(self, flavor, image, servername, metadata, disk
image = self.get_image(image=image)
flavor = self.get_flavor(flavor=flavor)
network = self.get_network()
private_key = self.conn.create_keypair(name=servername).__dict__['private_key']
key_creation = self.conn.create_keypair(name=servername)
try:
private_key = key_creation["private_key"]
except Exception:
private_key = key_creation.__dict__["private_key"]

if int(diskspace) > 0:
volume_id = self.create_volume_by_start(volume_storage=diskspace,
volume_name=volumename,
server_name=servername, metadata=metadata)
init_script = self.create_mount_init_script(volume_id=volume_id)

server = self.conn.compute.create_server(
server = self.conn.create_server(
name=servername,
image_id=image.id,
flavor_id=flavor.id,
networks=[{"uuid": network.id}],
image=image.id,
flavor=flavor.id,
network=[network.id],
key_name=servername,
metadata=metadata,
user_data=init_script,
meta=metadata,
userdata=init_script,
availability_zone=self.AVAIALABILITY_ZONE,
security_groups=self.DEFAULT_SECURITY_GROUPS

)
else:
server = self.conn.compute.create_server(
server = self.conn.create_server(
name=servername,
image_id=image.id,
flavor_id=flavor.id,
networks=[{"uuid": network.id}],
image=image.id,
flavor=flavor.id,
network=[network.id],
key_name=servername,
metadata=metadata,
meta=metadata,
availability_zone=self.AVAIALABILITY_ZONE,
security_groups=self.DEFAULT_SECURITY_GROUPS

)

openstack_id = server.to_dict()["id"]
openstack_id = server['id']

self.redis.hmset(openstack_id, dict(key=private_key, name=servername,
status=self.PREPARE_PLAYBOOK_BUILD))
return {"openstackid": openstack_id, "volumeId": volume_id, 'private_key': private_key}
Expand Down Expand Up @@ -861,16 +888,8 @@ def openstack_server_to_thrift_server(self, server):
timestamp = time.mktime(dt.timetuple())
else:
timestamp = None
try:
flav = self.conn.compute.get_flavor(serv["flavor"]["id"]).to_dict()
except Exception as e:
self.logger.exception(e)
flav = self.openstack_flav_to_thrift_flav(serv["flavor"])

try:
flav = serv['flavor']
except Exception as e:
self.logger.exception(e)
flav = None
try:
img = self.get_Image_with_Tag(serv["image"]["id"])
except Exception as e:
Expand All @@ -885,13 +904,7 @@ def openstack_server_to_thrift_server(self, server):
fixed_ip = address["addr"]

server = VM(
flav=Flavor(
vcpus=flav["vcpus"] if flav else None,
ram=flav["ram"] if flav else None,
disk=flav["disk"] if flav else None,
name=flav["name"] if flav else None,
openstack_id=flav["id"] if flav else None,
),
flav=flav,
img=img,
status=serv["status"],
metadata=serv["metadata"],
Expand Down
43 changes: 43 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
version: '3.5'
services:
portal-client:
build:
context: .
dockerfile: Dockerfile
ports:
- "9090:9090"

environment:
- OS_AUTH_URL
- OS_PROJECT_ID
- OS_PROJECT_NAME
- OS_USERNAME
- OS_PASSWORD
- OS_USER_DOMAIN_NAME
- OS_PROJECT_DOMAIN_ID

volumes:
- ./VirtualMachineService/config/config.yml:/code/VirtualMachineService/config.yml
- ./VirtualMachineService/keys/localhost/server.pem:/code/VirtualMachineService/keys/server.pem
command: python3 VirtualMachineServer.py /code/VirtualMachineService/config.yml
networks:
- portal

redis:
image: redis:5.0.5
expose:
- "6379"
networks:
- portal


networks:
portal:
name: portal_default
driver: bridge
driver_opts:
com.docker.network.driver.mtu: 1440
default:
driver: bridge
driver_opts:
com.docker.network.driver.mtu: 1440
4 changes: 1 addition & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
version: '3.5'
services:
portal-client:
build:
context: .
dockerfile: Dockerfile
image: denbicloud/cloud-portal-client:${CLOUD_CLIENT_TAG}
ports:
- "9090:9090"

Expand Down

0 comments on commit b41f052

Please sign in to comment.