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

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
vktrrdk committed May 15, 2020
2 parents 5cd7088 + be7f380 commit 671e0ea
Show file tree
Hide file tree
Showing 14 changed files with 1,413 additions and 236 deletions.
13 changes: 7 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
FROM python:3.8.2-slim
ADD . /code
WORKDIR /code
FROM python:3.8.2-buster
RUN apt-get update -y
RUN apt install -y build-essential
RUN apt-get install -y python3-dev
RUN apt-get install -y build-essential
WORKDIR /code
ADD requirements.txt /code
RUN pip install -r requirements.txt
COPY ansible.cfg /etc/ansible/
ADD requirements.yml /code
ADD ansible.cfg /etc/ansible/
RUN ansible-galaxy install -r requirements.yml
ADD . /code
WORKDIR /code/VirtualMachineService
33 changes: 32 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,43 @@ thrift_py: ## Builds python code from thrift file
dev-build: ## Build and Start the docker-compose.dev.yml
docker-compose -f docker-compose.dev.yml up --build

dev-d: ## Build and Start the docker-compose.dev.yml
docker-compose -f docker-compose.dev.yml up -d

dev-build-d: ## Build and Start the docker-compose.dev.yml
docker-compose -f docker-compose.dev.yml up --build -d

dev: ## Build and Start the docker-compose.dev.yml
docker-compose -f docker-compose.dev.yml up
docker-compose -f docker-compose.dev.yml up

dev-build-bibigrid: ## Build and Start the docker-compose.dev.yml with bibigrid
docker-compose -f docker-compose.dev.bibigrid.yml up --build

dev-bibigrid: ## Build and Start the docker-compose.dev.yml with bibigrid
docker-compose -f docker-compose.dev.bibigrid.yml up

dev-build-bibigrid-d: ## Build and Start the docker-compose.dev.yml with bibigrid
docker-compose -f docker-compose.dev.bibigrid.yml up --build -d

dev-bibigrid-d: ## Build and Start the docker-compose.dev.yml with bibigrid
docker-compose -f docker-compose.dev.bibigrid.yml up -d

production: ## Build Release from .env
docker-compose -f docker-compose.yml up --build -d

production-bibigrid: ## Build Release from .env and with bibigrid
docker-compose -f docker-compose.bibigrid.yml up --build -d

client_logs: ## Logs from Client
docker logs client_portal-client_1

bibigrid_logs: ## Logs from Bibigrid
docker logs client_bibigrid_1

enter_client_container: ## Enter Client container
docker exec -it client_portal-client_1 bash




.PHONY: help lint docs thrift_py
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,5 +250,6 @@ Optional Keys are:
+ env_file
+ secrets_file
+ client_server_pem
+ client_config

**Note:** Default repository is always master. Also by default, the files are taken from the folders as for local start.
162 changes: 133 additions & 29 deletions VirtualMachineService/VirtualMachineHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class VirtualMachineHandler(Iface):
"""Handler which the PortalClient uses."""

global active_playbooks
API_TOKEN = None
API_TOKEN_BUFFER = 15
BUILD = "BUILD"
ACTIVE = "ACTIVE"
ERROR = "ERROR"
Expand Down Expand Up @@ -511,10 +513,8 @@ def get_server(self, openstack_id):
self.logger.exception(
"No Server found {0} | Error {1}".format(openstack_id, e)
)
return VM(status="DELETED")
if server is None:
self.logger.exception("No Server {0}".format(openstack_id))
raise serverNotFoundException(Reason="No Server {0}".format(openstack_id))
return VM(status="NOT FOUND")

serv = server.to_dict()

if serv["attached_volumes"]:
Expand Down Expand Up @@ -676,37 +676,71 @@ def create_mount_init_script(
return init_script

def get_api_token(self):
auth_url = self.conn.endpoint_for("identity")
self.logger.info(auth_url)
auth = {
"auth": {
"identity": {
"methods": ["password"],
"password": {
"user": {
"name": self.USERNAME,
"domain": {"name": self.USER_DOMAIN_NAME},
"password": self.PASSWORD,
self.get_or_refresh_token()
return str(self.API_TOKEN["token"])

def get_or_refresh_token(self):
self.logger.info("Get API Token")
if not self.API_TOKEN:
self.logger.info("Create a new API Token")
auth_url = self.conn.endpoint_for("identity")
self.logger.info(auth_url)
auth = {
"auth": {
"identity": {
"methods": ["password"],
"password": {
"user": {
"name": self.USERNAME,
"domain": {"name": self.USER_DOMAIN_NAME},
"password": self.PASSWORD,
}
},
},
"scope": {
"project": {
"domain": {"id": "default"},
"name": self.PROJECT_NAME,
}
},
}
}
}
res = req.post(url=auth_url + "/auth/tokens?nocatalog", json=auth)
self.logger.info(res.headers)
return res.headers["X-Subject-Token"]
res = req.post(url=auth_url + "/auth/tokens?nocatalog", json=auth)

expires_at = datetime.datetime.strptime(
res.json()["token"]["expires_at"], "%Y-%m-%dT%H:%M:%S.%fZ"
)

self.API_TOKEN = {
"token": res.headers["X-Subject-Token"],
"expires_at": expires_at,
}
self.logger.info("New Token: {}".format(self.API_TOKEN))
else:
self.logger.info("Check existing token")
now = datetime.datetime.now()
# some buffer
now = now - datetime.timedelta(minutes=self.API_TOKEN_BUFFER)
api_token_expires_at = self.API_TOKEN["expires_at"]
if now.time() > api_token_expires_at.time():
expired_since = api_token_expires_at - now
self.logger.info(
"Old token is expired since {} minutes!".format(
expired_since.seconds // 60
)
)
self.API_TOKEN = None
self.get_api_token()
else:
self.logger.info("Token still valid!")

def resize_volume(self, volume_id, size):
token = self.get_api_token()
vol3 = self.conn.endpoint_for("volumev3")
header = {"X-Auth-Token": str(token)}
body = {"os-extend": {"new_size": size}}
url = vol3 + "/volumes/" + volume_id + "/action"
self.logger.info(url)
res = req.post(url=url, json=body, headers=header)
self.logger.info(res.status_code)
self.logger.info(res.content)
return int(res.status_code)
try:
self.conn.block_storage.extend_volume(volume_id, size)
except Exception as e:
self.logger.exception(e)
return 1
return 0

def create_volume(self, volume_name, volume_storage, metadata):
"""
Expand Down Expand Up @@ -1296,6 +1330,76 @@ def delete_backend(self, id):
self.logger.exception(e)
return str(-1)

def add_user_to_backend(self, backend_id, owner_id, user_id):
try:
post_url = "{0}users/{1}".format(self.RE_BACKEND_URL, backend_id)
user_info = {
"owner": owner_id,
"user": user_id,
}
except Exception as e:
self.logger.exception(e)
return {"Error": "Could not create url or json body."}
try:
response = req.post(
post_url,
json=user_info,
timeout=(30, 30),
headers={"X-API-KEY": self.FORC_API_KEY},
verify=self.PRODUCTION,
)
try:
data = response.json()
except Exception as e:
self.logger.exception(e)
return {"Error": "Error in POST."}
return data
except Timeout as e:
self.logger.info(msg="create_backend timed out. {0}".format(e))
return {"Error": "Timeout."}
except Exception as e:
self.logger.exception(e)
return {"Error": "An error occured."}

def get_users_from_backend(self, backend_id):
get_url = "{0}/users/{1}".format(self.RE_BACKEND_URL, backend_id)
try:
response = req.get(
get_url,
timeout=(30, 30),
headers={"X-API-KEY": self.FORC_API_KEY},
verify=self.PRODUCTION,
)
if response.status_code == 401:
return ["Error: 401"]
else:
return response.json()
except Timeout as e:
self.logger.info(msg="Get users for backend timed out. {0}".format(e))
return []

def delete_user_from_backend(self, backend_id, owner_id, user_id):
delete_url = "{0}/users/{1}".format(self.RE_BACKEND_URL, backend_id)
user_info = {
"owner": owner_id,
"user": user_id,
}
try:
response = req.delete(
delete_url,
json=user_info,
timeout=(30, 30),
headers={"X-API-KEY": self.FORC_API_KEY},
verify=self.PRODUCTION,
)
return response.json()
except Timeout as e:
self.logger.info(msg="Delete user from backend timed out. {0}".format(e))
return {"Error": "Timeout."}
except Exception as e:
self.logger.exception(e)
return {"Error": "An Exception occured."}

def exist_server(self, name):
if self.conn.compute.find_server(name) is not None:
return True
Expand Down
21 changes: 21 additions & 0 deletions VirtualMachineService/VirtualMachineService-remote
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ if len(sys.argv) <= 1 or sys.argv[1] == '--help':
print(' get_backends_by_template(string template)')
print(' Backend get_backend_by_id(i64 id)')
print(' string delete_backend(i64 id)')
print(' add_user_to_backend(i64 backend_id, string owner_id, string user_id)')
print(' get_users_from_backend(i64 backend_id)')
print(' delete_user_from_backend(i64 backend_id, string owner_id, string user_id)')
print(' get_templates()')
print(' get_allowed_templates()')
print(' get_templates_by_template(string template_name)')
Expand Down Expand Up @@ -373,6 +376,24 @@ elif cmd == 'delete_backend':
sys.exit(1)
pp.pprint(client.delete_backend(eval(args[0]),))

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

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

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

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

0 comments on commit 671e0ea

Please sign in to comment.