Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
from cloudshell.workflow.orchestration.sandbox import Sandbox
from cloudshell.helpers.scripts import cloudshell_scripts_helpers as helpers
from cloudshell.helpers.save_workflow.save_app_utility import SaveAppUtility

import os

NEW_APP_PARAM = "NEWAPPNAME"
IMAGE_URL_PARAM = "DISPLAYIMAGEURL"


def save_app():
sandbox = Sandbox()
connectivity = helpers.get_connectivity_context_details()
resource = helpers.get_resource_context_details()
reservation_details = helpers.get_reservation_context_details()
sandbox_id = reservation_details.id
api = helpers.get_api_session()

if 'NEWAPPNAME' in os.environ:
new_app_name = os.environ['NEWAPPNAME']
else:
new_app_name = ''

if 'DISPLAYIMAGEURL' in os.environ:
image_url = os.environ['DISPLAYIMAGEURL']
else:
image_url = ''
new_app_name = os.environ.get(NEW_APP_PARAM, "")
image_url = os.environ.get(IMAGE_URL_PARAM, "")

apputility = SaveAppUtility(sandbox, resource.name, connectivity.server_address, connectivity.admin_user,
connectivity.admin_pass, image_url, new_app_name, save_as=True)
apputility.save_flow()
app_utility = SaveAppUtility(api, sandbox_id, resource.name, connectivity.server_address, connectivity.admin_user,
connectivity.admin_pass, image_url, new_app_name, save_as=True)
saved_app_info = app_utility.save_flow()
print(saved_app_info)


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
cloudshell-orch-core>=1.7.0.0,<1.8.0.0
cloudshell-automation-api>=9.3
# cloudshell-orch-core>=1.7.0.0,<1.8.0.0
# cloudshell-automation-api>=9.3
# requests==2.23.0
# why do we need to list dependencies that are included in app helper package
cloudshell-app-helper>=2.0.0,<3.0.0
requests==2.23.0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0.0
6 changes: 5 additions & 1 deletion Orchestration Scripts/Setup Scripts/AppSaveSetup/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
from cloudshell.helpers.save_workflow.save_app_info_orch import save_app_deployment_info


def save_deployment_info(sandbox, components=None):
save_app_deployment_info(sandbox.automation_api, sandbox.id)


sandbox = Sandbox()
DefaultSetupWorkflow().register(sandbox)
sandbox.workflow.add_to_preparation(save_app_deployment_info, None)
sandbox.workflow.add_to_preparation(save_deployment_info, None)
sandbox.execute_setup()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__author__ = 'quali'
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
def attribute(name, value, overwrite=False):
xml ="""
xml = """
<Attribute Name="{Name}" Value="{Value}" {overwrite}/>
"""
return xml.format(Name=name, Value=value, overwrite='Override="true"' if overwrite else "")
Expand All @@ -15,8 +15,9 @@ def deployment_path(deploy_path, cloud_provider):
</DeploymentService>
</DeploymentPath>
"""
return xml.format(DeployAttributes="\n".join([attribute(x, y, True) for x, y in deploy_path['attributes'].iteritems()]),
cp=cloud_provider, dn=deploy_path['name'], dsn=deploy_path['service_name'])
return xml.format(
DeployAttributes="\n".join([attribute(x, y, True) for x, y in deploy_path['attributes'].items()]),
cp=cloud_provider, dn=deploy_path['name'], dsn=deploy_path['service_name'])


def deployment_paths(deploy_paths, cloud_provider):
Expand All @@ -37,10 +38,11 @@ def app_resource(attributes, model, driver):
</AppResources>

"""
return xml.format(Attributes="\n".join(attribute(x, y, True) for x, y in attributes.iteritems()), model=model, driver=driver)
return xml.format(Attributes="\n".join(attribute(x, y, True) for x, y in attributes.items()), model=model,
driver=driver)


def app_resource_info(app_name, deploy_paths, app_attributes, model, driver, cloud_provider, image_name):
def app_resource_info(app_name: str, deploy_paths, app_attributes, model, driver, cloud_provider, image_name):
xml = """
<AppResourceInfo Name="{AppName}" ImagePath="{img}">
{AppResource}
Expand All @@ -50,7 +52,8 @@ def app_resource_info(app_name, deploy_paths, app_attributes, model, driver, clo
</DeploymentPaths>
</AppResourceInfo>
"""
return xml.format(AppName=app_name, img=image_name, AppResource=app_resource(app_attributes, model, driver), DeploymentPath=deployment_paths(deploy_paths, cloud_provider))
return xml.format(AppName=app_name, img=image_name, AppResource=app_resource(app_attributes, model, driver),
DeploymentPath=deployment_paths(deploy_paths, cloud_provider))


def add_category(category):
Expand All @@ -72,10 +75,9 @@ def categories_info(categories):

def app_template(app_name, deploy_paths, categories, app_attributes, model, driver, cloud_provider, image_name):
"""

:return:
"""
xml ="""<?xml version="1.0" encoding="utf-8"?>
xml = """<?xml version="1.0" encoding="utf-8"?>
<AppTemplateInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
{AppResourceInfo}

Expand All @@ -86,4 +88,3 @@ def app_template(app_name, deploy_paths, categories, app_attributes, model, driv
categories = categories_info(categories)

return xml.format(AppResourceInfo=app_resource, Categories=categories if categories else "")

Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import zipfile
import requests
import shutil
import base64

from images import vm_image
from cloudshell.helpers.app_import.images import vm_image


def upload_app_to_cloudshell(cs_api, reservation_id, app_name, app_xml_content, server, user="admin", password="admin",
display_image_result=None, display_image_name='vm.png'):
def upload_app_to_cloudshell(app_name, app_xml_content, server, user="admin", password="admin",
display_image_result="", display_image_name='vm.png'):
"""
:param CloudShellAPISession cs_api:
:param string reservation_id:
Expand Down Expand Up @@ -46,10 +47,10 @@ def upload_app_to_cloudshell(cs_api, reservation_id, app_name, app_xml_content,
os.remove(display_image_file)

fh = open(display_image_file, "wb")
if display_image_result is not None:
fh.write(display_image_result.decode('base64'))
if not display_image_result:
fh.write(base64.b64decode(display_image_result))
else:
fh.write(vm_image.decode('base64'))
fh.write(base64.b64decode(vm_image))
fh.close()

zip_file = zipfile.ZipFile(blueprint_zip_file, "w")
Expand All @@ -62,15 +63,22 @@ def upload_app_to_cloudshell(cs_api, reservation_id, app_name, app_xml_content,
zip_file.close()
zip_content = open(blueprint_zip_file, "rb").read()
shutil.rmtree(working_dir)
authentication_code = requests.put("http://{}:9000/Api/Auth/Login".format(server),
{"username": user, "password": password, "domain": "Global"}).content

result = requests.post("http://{}:9000/API/Package/ImportPackage".format(server),
headers={"Authorization": "Basic {}".format(authentication_code[1:-1])},
files={"QualiPackage": zip_content})
if 'false' in result.content:
raise Exception('Issue importing App XML into Cloudshell: ' + result.content)
if result.status_code >= 300:
return result.content
else:
return None
server_url = "http://{}:9000/Api/Auth/Login".format(server)

login_response = requests.put(server_url,
data={"username": user, "password": password, "domain": "Global"})
if not login_response.ok:
raise Exception(f"Failed login to Quali API trying to import App Package.\n"
f"Status Code: {login_response.status_code}. Reason: {login_response.reason}")
token = login_response.text[1:-1]
upload_response = requests.post("http://{}:9000/API/Package/ImportPackage".format(server),
headers={"Authorization": f"Basic {token}"},
files={"QualiPackage": zip_content})
if not upload_response.ok:
raise Exception("Failed upload of app XML into cloudshell.\n"
f"Status Code: {upload_response.status_code}, Reason: {upload_response.reason}")
if 'false' in upload_response.text.lower():
raise Exception('Issue importing App XML into Cloudshell: ' + upload_response.text)

return upload_response.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__author__ = 'quali'
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import json

from cloudshell.api.cloudshell_api import SandboxDataKeyValue
from cloudshell.api.cloudshell_api import SandboxDataKeyValue, CloudShellAPISession
from cloudshell.helpers.save_workflow.deploy_info import DeployInfo


def save_app_deployment_info(sandbox, components=None):
for app in sandbox.automation_api.GetReservationDetails(sandbox.id).ReservationDescription.Apps:
def save_app_deployment_info(api: CloudShellAPISession, sandbox_id: str):
for app in api.GetReservationDetails(sandbox_id).ReservationDescription.Apps:
serialized_deployment_info = json.dumps(DeployInfo(app.DeploymentPaths))

key_value = SandboxDataKeyValue(app.Name, serialized_deployment_info)

sandbox.automation_api.SetSandboxData(sandbox.id, [key_value])
api.SetSandboxData(sandbox_id, [key_value])
Loading