def remote_refresh_ip(self, context, ports, cancellation_context):
        """

        Called when reserving a sandbox during setup, a call for each app in the sandbox can also be run manually by the sandbox end-user from the deployed App's commands pane

        Method retrieves the VM's updated IP address from the cloud provider and sets it on the deployed App resource
        Both private and public IPs are retrieved, as appropriate.

        If the operation fails, method should raise an exception.

        :param ResourceRemoteCommandContext context:
        :param ports:
        :param CancellationContext cancellation_context:
        :return:
        """
        dump_context("refresh-ip-context", context, r"C:\temp\context")
        with LoggingSessionContext(context) as logger:
            api = CloudShellSessionContext(context).get_api()
            resource_config = KubernetesResourceConfig.from_context(
                self.SHELL_NAME, context, api)
            api_clients = ApiClientsProvider(logger).get_api_clients(
                resource_config)
            service_provider = ServiceProvider(api_clients, logger, None)
            DeployedVMActions.register_deployment_path(KubernetesDeployedApp)
            deployed_app = DeployedVMActions.from_remote_resource(
                context.remote_endpoints[0], api).deployed_app
            RefreshIpFlow(logger, resource_config,
                          service_provider).refresh_ip(deployed_app)
    def DeleteInstance(self, context, ports):
        """
        Called during sandbox's teardown or when removing a deployed App from the sandbox

        Method deletes the VM from the cloud provider.

        If the operation fails, method should raise an exception.

        :param ResourceRemoteCommandContext context:
        :param ports:
        """
        dump_context("delete-context", context, r"C:\temp\context")
        with LoggingSessionContext(context) as logger:
            api = CloudShellSessionContext(context).get_api()
            resource_config = KubernetesResourceConfig.from_context(
                self.SHELL_NAME, context, api)
            api_clients = ApiClientsProvider(logger).get_api_clients(
                resource_config)
            service_provider = ServiceProvider(api_clients, logger, None)
            DeployedVMActions.register_deployment_path(KubernetesDeployedApp)
            deployed_app = DeployedVMActions.from_remote_resource(
                context.remote_endpoints[0], api).deployed_app
            DeleteInstanceFlow(logger, resource_config,
                               service_provider).delete_instance(
                                   deployed_app.kubernetes_name,
                                   deployed_app.name, deployed_app.namespace)
    def PowerOn(self, context, ports):
        """
        Called when reserving a sandbox during setup, a call for each app in the sandbox can also be run manually by the sandbox end-user from the deployed App's commands pane

        Method spins up the VM

        If the operation fails, method should raise an exception.

        :param ResourceRemoteCommandContext context:
        :param ports:
        """

        dump_context("poweron-context", context, r"C:\temp\context")
        with LoggingSessionContext(context) as logger:
            api = CloudShellSessionContext(context).get_api()
            resource_config = KubernetesResourceConfig.from_context(
                self.SHELL_NAME, context, api)
            api_clients = ApiClientsProvider(logger).get_api_clients(
                resource_config)
            service_provider = ServiceProvider(api_clients, logger, None)
            DeployedVMActions.register_deployment_path(KubernetesDeployedApp)
            deployed_app = DeployedVMActions.from_remote_resource(
                context.remote_endpoints[0], api).deployed_app
            PowerFlow(logger, resource_config,
                      service_provider).power_on(deployed_app)
    def reconfigure_vm(
        self,
        context,
        ports,
        cancellation_context,
        vm_size,
        os_disk_size,
        os_disk_type,
        data_disks,
    ):
        """Reconfigure VM Size and Data Disks."""
        with LoggingSessionContext(context) as logger:
            logger.info("Starting Reconfigure VM command...")
            api = CloudShellSessionContext(context).get_api()
            resource_config = AzureResourceConfig.from_context(
                shell_name=self.SHELL_NAME, context=context, api=api
            )

            reservation_info = AzureReservationInfo.from_remote_resource_context(
                context
            )
            cancellation_manager = CancellationContextManager(cancellation_context)

            azure_client = AzureAPIClient(
                azure_subscription_id=resource_config.azure_subscription_id,
                azure_tenant_id=resource_config.azure_tenant_id,
                azure_application_id=resource_config.azure_application_id,
                azure_application_key=resource_config.azure_application_key,
                logger=logger,
            )

            for deployed_app_cls in (
                AzureVMFromMarketplaceDeployedApp,
                AzureVMFromCustomImageDeployedApp,
                AzureVMFromSharedGalleryImageDeployedApp,
            ):
                DeployedVMActions.register_deployment_path(deployed_app_cls)

            resource = context.remote_endpoints[0]
            deployed_vm_actions = DeployedVMActions.from_remote_resource(
                resource=resource, cs_api=api
            )

            reconfigure_vm_flow = AzureReconfigureVMFlow(
                resource_config=resource_config,
                azure_client=azure_client,
                cs_api=api,
                reservation_info=reservation_info,
                cancellation_manager=cancellation_manager,
                logger=logger,
            )

            return reconfigure_vm_flow.reconfigure(
                deployed_app=deployed_vm_actions.deployed_app,
                vm_size=vm_size,
                os_disk_size=os_disk_size,
                os_disk_type=os_disk_type,
                data_disks=data_disks,
            )
    def remote_refresh_ip(self, context, ports, cancellation_context):
        """Called when reserving a sandbox during setup.

        Call for each app in the sandbox can also be run manually by the
        sandbox end-user from the deployed App's commands pane.
        Method retrieves the VM's updated IP address from the cloud provider
        and sets it on the deployed App resource. Both private and public IPs
        are retrieved, as appropriate. If the operation fails, method should
        raise an exception.
        :param ResourceRemoteCommandContext context:
        :param ports:
        :param CancellationContext cancellation_context:
        :return:
        """
        with LoggingSessionContext(context) as logger:
            logger.info("Starting Remote Refresh IP command...")
            api = CloudShellSessionContext(context).get_api()
            resource_config = AzureResourceConfig.from_context(
                shell_name=self.SHELL_NAME, context=context, api=api
            )

            reservation_info = AzureReservationInfo.from_remote_resource_context(
                context
            )
            cancellation_manager = CancellationContextManager(cancellation_context)

            azure_client = AzureAPIClient(
                azure_subscription_id=resource_config.azure_subscription_id,
                azure_tenant_id=resource_config.azure_tenant_id,
                azure_application_id=resource_config.azure_application_id,
                azure_application_key=resource_config.azure_application_key,
                logger=logger,
            )

            for deploy_app_cls in (
                AzureVMFromMarketplaceDeployedApp,
                AzureVMFromCustomImageDeployedApp,
                AzureVMFromSharedGalleryImageDeployedApp,
            ):
                DeployedVMActions.register_deployment_path(deploy_app_cls)

            resource = context.remote_endpoints[0]
            deployed_vm_actions = DeployedVMActions.from_remote_resource(
                resource=resource, cs_api=api
            )

            refresh_ip_flow = AzureRefreshIPFlow(
                resource_config=resource_config,
                azure_client=azure_client,
                cs_api=api,
                reservation_info=reservation_info,
                cancellation_manager=cancellation_manager,
                logger=logger,
            )

            return refresh_ip_flow.refresh_ip(
                deployed_app=deployed_vm_actions.deployed_app
            )
 def PowerOn(self, context: ResourceRemoteCommandContext, ports: List[str]):
     """Method spins up the VM."""
     with LoggingSessionContext(context) as logger:
         logger.info("Starting PowerOn command")
         api = CloudShellSessionContext(context).get_api()
         conf = OSResourceConfig.from_context(self.SHELL_NAME, context, api)
         DeployedVMActions.register_deployment_path(OSNovaImgDeployedApp)
         resource = context.remote_endpoints[0]
         actions = DeployedVMActions.from_remote_resource(resource, api)
         os_api = OSApi(conf, logger)
         return PowerFlow(os_api, actions.deployed_app, logger).power_on()
 def DeleteInstance(self, context: ResourceRemoteCommandContext, ports: List[str]):
     """Deletes the VM from the OpenStack."""
     with LoggingSessionContext(context) as logger:
         logger.info("Starting Delete Instance command")
         api = CloudShellSessionContext(context).get_api()
         conf = OSResourceConfig.from_context(self.SHELL_NAME, context, api)
         DeployedVMActions.register_deployment_path(OSNovaImgDeployedApp)
         resource = context.remote_endpoints[0]
         actions = DeployedVMActions.from_remote_resource(resource, api)
         os_api = OSApi(conf, logger)
         delete_instance(os_api, actions.deployed_app)
    def PowerOff(self, context, ports):
        """Called during sandbox's teardown.

        Can also be run manually by the sandbox end-user from the deployed
        App's commands pane. Method shuts down (or powers off) the VM instance.
        If the operation fails, method should raise an exception.
        :param ResourceRemoteCommandContext context:
        :param ports:
        """
        with LoggingSessionContext(context) as logger:
            logger.info("Starting Power Off command...")
            api = CloudShellSessionContext(context).get_api()
            resource_config = AzureResourceConfig.from_context(
                shell_name=self.SHELL_NAME, context=context, api=api
            )

            reservation_info = AzureReservationInfo.from_remote_resource_context(
                context
            )

            azure_client = AzureAPIClient(
                azure_subscription_id=resource_config.azure_subscription_id,
                azure_tenant_id=resource_config.azure_tenant_id,
                azure_application_id=resource_config.azure_application_id,
                azure_application_key=resource_config.azure_application_key,
                logger=logger,
            )

            for deploy_app_cls in (
                AzureVMFromMarketplaceDeployedApp,
                AzureVMFromCustomImageDeployedApp,
                AzureVMFromSharedGalleryImageDeployedApp,
            ):
                DeployedVMActions.register_deployment_path(deploy_app_cls)

            resource = context.remote_endpoints[0]
            deployed_vm_actions = DeployedVMActions.from_remote_resource(
                resource=resource, cs_api=api
            )

            power_mgmt_flow = AzurePowerManagementFlow(
                resource_config=resource_config,
                azure_client=azure_client,
                reservation_info=reservation_info,
                logger=logger,
            )

            return power_mgmt_flow.power_off(
                deployed_app=deployed_vm_actions.deployed_app
            )
 def remote_refresh_ip(
     self,
     context: ResourceRemoteCommandContext,
     ports: List[str],
     cancellation_context: CancellationContext,
 ):
     """Retrieves the VM's updated IP address from the OpenStack."""
     with LoggingSessionContext(context) as logger:
         logger.info("Starting Refresh IP command")
         api = CloudShellSessionContext(context).get_api()
         conf = OSResourceConfig.from_context(self.SHELL_NAME, context, api)
         DeployedVMActions.register_deployment_path(OSNovaImgDeployedApp)
         resource = context.remote_endpoints[0]
         actions = DeployedVMActions.from_remote_resource(resource, api)
         os_api = OSApi(conf, logger)
         refresh_ip(os_api, actions.deployed_app, conf)
 def console(
     self,
     context: ResourceRemoteCommandContext,
     console_type: str,
     ports: List[str],
 ) -> str:
     with LoggingSessionContext(context) as logger:
         logger.info("Starting Get Console command")
         validate_console_type(console_type)
         api = CloudShellSessionContext(context).get_api()
         conf = OSResourceConfig.from_context(self.SHELL_NAME, context, api)
         DeployedVMActions.register_deployment_path(OSNovaImgDeployedApp)
         resource = context.remote_endpoints[0]
         actions = DeployedVMActions.from_remote_resource(resource, api)
         os_api = OSApi(conf, logger)
         return get_console(os_api, actions.deployed_app, console_type)
    def GetApplicationPorts(self, context, ports):
        with LoggingSessionContext(context) as logger:
            logger.info("Starting Get Application Ports command...")
            api = CloudShellSessionContext(context).get_api()
            resource_config = AzureResourceConfig.from_context(
                shell_name=self.SHELL_NAME, context=context, api=api
            )

            reservation_info = AzureReservationInfo.from_remote_resource_context(
                context
            )

            azure_client = AzureAPIClient(
                azure_subscription_id=resource_config.azure_subscription_id,
                azure_tenant_id=resource_config.azure_tenant_id,
                azure_application_id=resource_config.azure_application_id,
                azure_application_key=resource_config.azure_application_key,
                logger=logger,
            )

            for deploy_app_cls in (
                AzureVMFromMarketplaceDeployedApp,
                AzureVMFromCustomImageDeployedApp,
                AzureVMFromSharedGalleryImageDeployedApp,
            ):
                DeployedVMActions.register_deployment_path(deploy_app_cls)

            resource = context.remote_endpoints[0]
            deployed_vm_actions = DeployedVMActions.from_remote_resource(
                resource=resource, cs_api=api
            )

            application_ports_flow = AzureGetApplicationPortsFlow(
                resource_config=resource_config,
                azure_client=azure_client,
                reservation_info=reservation_info,
                logger=logger,
            )

            return application_ports_flow.get_application_ports(
                deployed_app=deployed_vm_actions.deployed_app
            )
    def DeleteInstance(self, context, ports):
        """Called when removing a deployed App from the sandbox.

        Method deletes the VM from the cloud provider. If the operation fails,
        method should raise an exception.
        :param ResourceRemoteCommandContext context:
        :param ports:
        """
        with LoggingSessionContext(context) as logger:
            logger.info("Starting Delete Instance command...")
            api = CloudShellSessionContext(context).get_api()
            resource_config = AzureResourceConfig.from_context(
                shell_name=self.SHELL_NAME, context=context, api=api
            )

            reservation_info = AzureReservationInfo.from_remote_resource_context(
                context
            )
            cs_ip_pool_manager = CSIPPoolManager(cs_api=api, logger=logger)

            azure_client = AzureAPIClient(
                azure_subscription_id=resource_config.azure_subscription_id,
                azure_tenant_id=resource_config.azure_tenant_id,
                azure_application_id=resource_config.azure_application_id,
                azure_application_key=resource_config.azure_application_key,
                logger=logger,
            )

            for deploy_app_cls in (
                AzureVMFromMarketplaceDeployedApp,
                AzureVMFromCustomImageDeployedApp,
                AzureVMFromSharedGalleryImageDeployedApp,
            ):
                GetVMDetailsRequestActions.register_deployment_path(deploy_app_cls)

            resource = context.remote_endpoints[0]
            deployed_vm_actions = DeployedVMActions.from_remote_resource(
                resource=resource, cs_api=api
            )

            delete_flow = AzureDeleteInstanceFlow(
                resource_config=resource_config,
                azure_client=azure_client,
                reservation_info=reservation_info,
                cs_ip_pool_manager=cs_ip_pool_manager,
                lock_manager=self.lock_manager,
                logger=logger,
            )

            delete_flow.delete_instance(deployed_app=deployed_vm_actions.deployed_app)
def test_static_deployed_app():
    app_name = "win-test"
    address = "192.168.26.43"
    uuid = "42282856-0637-216a-511d-ccd88aa07e8f"
    vm_name = "static-vms/win-test"
    app_context = AppContext(
        app_request_json="",
        deployed_app_json=json.dumps({
            "name":
            app_name,
            "family":
            "CS_GenericAppFamily",
            "model":
            StaticApp.DEPLOYMENT_PATH,
            "address":
            address,
            "attributes": [
                {
                    "name": f"{StaticApp.DEPLOYMENT_PATH}.VM Name",
                    "value": "static-vms/win-test",
                },
                {
                    "name":
                    f"{StaticApp.DEPLOYMENT_PATH}.vCenter Resource Name",
                    "value": "vcenter",
                },
                {
                    "name": f"{StaticApp.DEPLOYMENT_PATH}.User",
                    "value": ""
                },
                {
                    "name": f"{StaticApp.DEPLOYMENT_PATH}.Password",
                    "value": ""
                },
                {
                    "name": f"{StaticApp.DEPLOYMENT_PATH}.Public IP",
                    "value": ""
                },
                {
                    "name": "Execution Server Selector",
                    "value": ""
                },
            ],
            "vmdetails": {
                "id": "8b6c4c4d-e2c9-47c9-b260-9a33688bf78a",
                "cloudProviderId": "d4d679c6-3049-4e55-9e64-8692a3400b6a",
                "uid": uuid,
                "vmCustomParams": [],
            },
        }),
    )
    resource = ResourceContextDetails(
        id="0917eb75-92ad-4291-9623-4235c81be76b",
        name=app_name,
        fullname=app_name,
        type="Resource",
        address=address,
        model=StaticApp.DEPLOYMENT_PATH,
        family="CS_GenericAppFamily",
        description=None,
        attributes={
            "Generic Static vCenter VM 2G.VM Name": vm_name,
            "Generic Static vCenter VM 2G.vCenter Resource Name": "vcenter",
            "Generic Static vCenter VM 2G.User": "",
            "Generic Static vCenter VM 2G.Password":
            "******",
            "Generic Static vCenter VM 2G.Public IP": "",
            "Execution Server Selector": "",
        },
        app_context=app_context,
        networks_info=None,
        shell_standard=None,
        shell_standard_version=None,
    )

    DeployedVMActions.register_deployment_path(StaticApp)
    actions = DeployedVMActions.from_remote_resource(resource, Mock())

    app = actions.deployed_app
    assert isinstance(app, StaticApp)
    assert app.name == app_name
    assert app.model == app.deployment_service_model == StaticApp.DEPLOYMENT_PATH
    assert app.private_ip == address
    assert app.vmdetails.uid == uuid
    assert app.attributes[f"{StaticApp.DEPLOYMENT_PATH}.VM Name"] == vm_name
def test_deployed_app():
    app_name = "Azureubuntusimple"
    address = "10.0.1.3"
    deployed_model = "Generic App Model"
    uuid = "3d750874-09f1-4243-a598-6700bb648655"

    app_context = AppContext(
        app_request_json=json.dumps({
            "name": app_name,
            "description": None,
            "logicalResource": {
                "family":
                "GenericAppFamily",
                "model":
                "GenericAppModel",
                "driver":
                None,
                "description":
                "",
                "attributes": [
                    {
                        "name": "Password",
                        "value": ""
                    },
                    {
                        "name": "PublicIP",
                        "value": ""
                    },
                    {
                        "name": "User",
                        "value": ""
                    },
                ],
            },
            "deploymentService": {
                "cloudProviderName":
                "Azure2Goldcode",
                "name":
                "Microsoft Azure 2nd Gen.Azure VM From Marketplace 2nd Gen",
                "model":
                MyDeployedApp.DEPLOYMENT_PATH,
                "driver":
                None,
                "attributes": [
                    {
                        "name":
                        "MicrosoftAzure2G.AzureVMFromMarketplace2G.Disk",
                        "value": "HDD",
                    },
                ],
            },
        }),
        deployed_app_json=json.dumps({
            "name":
            app_name,
            "family":
            "Generic App Family",
            "model":
            "Generic App Model",
            "address":
            address,
            "attributes": [
                {
                    "name": "Password",
                    "value": "3M3u7nkDzxWb0aJ/IZYeWw=="
                },
                {
                    "name": "User",
                    "value": "adminuser"
                },
                {
                    "name": "Public IP",
                    "value": ""
                },
            ],
            "vmdetails": {
                "id": "4da74d28-50d9-4271-b2e4-b49eed1bb0fe",
                "cloudProviderId": "129b8fac-fd8d-4c37-bb1a-fdceba2f38d7",
                "uid": uuid,
                "vmCustomParams": [],
            },
        }),
    )
    resource = ResourceContextDetails(
        id="0917eb75-92ad-4291-9623-4235c81be76b",
        name=app_name,
        fullname=app_name,
        type="Resource",
        address=address,
        model="Generic App Model",
        family="Generic App Family",
        description=None,
        attributes={
            "Password": "******",
            "User": "******",
            "Public IP": "",
        },
        app_context=app_context,
        networks_info=None,
        shell_standard=None,
        shell_standard_version=None,
    )

    DeployedVMActions.register_deployment_path(MyDeployedApp)
    actions = DeployedVMActions.from_remote_resource(resource, Mock())

    app = actions.deployed_app
    assert isinstance(app, MyDeployedApp)
    assert app.name == app_name
    assert app.model == deployed_model
    assert app.deployment_service_model == MyDeployedApp.DEPLOYMENT_PATH
    assert app.private_ip == address
    assert app.vmdetails.uid == uuid
    assert app.attributes[f"{MyDeployedApp.DEPLOYMENT_PATH}.Disk"] == "HDD"
    assert app.attributes["User"] == "adminuser"
Exemplo n.º 15
0
def deployed_app(resource_remote_context, cs_api):
    DeployedVMActions.register_deployment_path(OSNovaImgDeployedApp)
    resource = resource_remote_context.remote_endpoints[0]
    actions = DeployedVMActions.from_remote_resource(resource, cs_api)
    return actions.deployed_app