def PrepareSandboxInfra(self, context, request, cancellation_context):
        """
        Called in the beginning of the orchestration flow (preparation stage), even before Deploy is called.

        Prepares all of the required infrastructure needed for a sandbox operating with L3 connectivity.
        For example, creating networking infrastructure like VPC, subnets or routing tables in AWS, storage entities such as S3 buckets, or
        keyPair objects for authentication.
        In general, any other entities needed on the sandbox level should be created here.

        Note:
        PrepareSandboxInfra can be called multiple times in a sandbox.
        Setup can be called multiple times in the sandbox, and every time setup is called, the PrepareSandboxInfra method will be called again.
        Implementation should support this use case and take under consideration that the cloud resource might already exist.
        It's recommended to follow the "get or create" pattern when implementing this method.

        When an error is raised or method returns action result with success false
        Cloudshell will fail sandbox creation, so bear that in mind when doing so.

        :param ResourceCommandContext context:
        :param str request:
        :param CancellationContext cancellation_context:
        :return:
        :rtype: str
        """
        '''
        # parse the json strings into action objects
        actions = self.request_parser.convert_driver_request_to_actions(request)
        
        action_results = _my_prepare_connectivity(context, actions, cancellation_context)
        
        return DriverResponse(action_results).to_driver_response_json()    
        '''
        dump_context("prepare-sandbox-infra-context", context,
                     r"C:\temp\context")
        dump_context("prepare-sandbox-infra-request",
                     request,
                     r"C:\temp\context",
                     obj=False)

        with LoggingSessionContext(context) as logger:
            # parse the json strings into action objects
            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,
                CancellationContextManager(cancellation_context))
            tag_service = TagsService(context)

            request_actions = PrepareSandboxInfraRequestActions.from_request(
                request)

            flow = PrepareSandboxInfraFlow(logger, resource_config,
                                           service_provider, tag_service)
            return flow.prepare(request_actions)
    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
            )
Example #4
0
    def Deploy(self, context, request, cancellation_context=None):
        """Called when reserving a sandbox during setup, a call for each app in the sandbox.

        Method creates the compute resource in the cloud provider - VM instance or container.
        If App deployment fails, return a "success false" action result.
        :param ResourceCommandContext context:
        :param str request: A JSON string with the list of requested deployment actions
        :param CancellationContext cancellation_context:
        :return:
        :rtype: str
        """
        with LoggingSessionContext(context) as logger:
            logger.info("Starting Deploy command...")
            logger.debug(f"Request: {request}")
            api = CloudShellSessionContext(context).get_api()
            resource_config = AzureResourceConfig.from_context(
                shell_name=self.SHELL_NAME, context=context, api=api)

            cancellation_manager = CancellationContextManager(
                cancellation_context)
            reservation_info = AzureReservationInfo.from_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 (AzureVMFromMarketplaceDeployApp,
                                   AzureVMFromCustomImageDeployApp):
                DeployVMRequestActions.register_deployment_path(deploy_app_cls)

            request_actions = DeployVMRequestActions.from_request(
                request=request, cs_api=api)

            if isinstance(request_actions.deploy_app,
                          AzureVMFromMarketplaceDeployApp):
                deploy_flow_class = AzureDeployMarketplaceVMFlow
            else:
                deploy_flow_class = AzureDeployCustomVMFlow

            deploy_flow = deploy_flow_class(
                resource_config=resource_config,
                azure_client=azure_client,
                cs_api=api,
                reservation_info=reservation_info,
                cancellation_manager=cancellation_manager,
                cs_ip_pool_manager=cs_ip_pool_manager,
                lock_manager=self.lock_manager,
                logger=logger)

            return deploy_flow.deploy(request_actions=request_actions)
    def PrepareSandboxInfra(self, context, request, cancellation_context):
        """Called in the beginning of the orchestration flow (preparation stage).

        Prepares all of the required infrastructure needed for a sandbox operating
        with L3 connectivity. For example, creating networking infrastructure
        like VPC, subnets or routing tables in AWS, storage entities  such as
        S3 buckets, or keyPair objects for authentication. In general, any other
        entities needed on the sandbox level should be created here.

        Note:
        PrepareSandboxInfra can be called multiple times in a sandbox.
        Setup can be called multiple times in the sandbox, and every time
        setup is called, the PrepareSandboxInfra method will be called again.
        Implementation should support this use case and take under consideration that
        the cloud resource might already exist. It's recommended to follow the
        "get or create" pattern when implementing this method.

        When an error is raised or method returns action result with success false
        Cloudshell will fail sandbox creation, so bear that in mind when doing so.
        :param ResourceCommandContext context:
        :param str request:
        :param CancellationContext cancellation_context:
        :return:
        :rtype: str
        """
        with LoggingSessionContext(context) as logger:
            logger.info("Starting Prepare Sandbox Infra command...")
            logger.debug(f"Request: {request}")
            api = CloudShellSessionContext(context).get_api()
            resource_config = AzureResourceConfig.from_context(
                shell_name=self.SHELL_NAME, context=context, api=api
            )

            request_actions = PrepareSandboxInfraRequestActions.from_request(request)
            reservation_info = AzureReservationInfo.from_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,
            )

            prepare_sandbox_flow = AzurePrepareSandboxInfraFlow(
                resource_config=resource_config,
                azure_client=azure_client,
                reservation_info=reservation_info,
                cancellation_manager=cancellation_manager,
                logger=logger,
            )

            return prepare_sandbox_flow.prepare(request_actions=request_actions)
    def GetVmDetails(self, context, requests, 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 VM Details pane. Method queries
        cloud provider for instance operating system, specifications and networking
        information and returns that as a json serialized driver response
        containing a list of VmDetailsData. If the operation fails,
        method should raise an exception.
        :param ResourceCommandContext context:
        :param str requests:
        :param CancellationContext cancellation_context:
        :return:
        """
        with LoggingSessionContext(context) as logger:
            logger.info("Starting Get VM Details command...")
            logger.debug(f"Requests: {requests}")
            api = CloudShellSessionContext(context).get_api()
            resource_config = AzureResourceConfig.from_context(
                shell_name=self.SHELL_NAME, context=context, api=api
            )

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

            request_actions = GetVMDetailsRequestActions.from_request(
                request=requests, cs_api=api
            )

            cancellation_manager = CancellationContextManager(cancellation_context)
            reservation_info = AzureReservationInfo.from_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,
            )

            vm_details_flow = AzureGetVMDetailsFlow(
                resource_config=resource_config,
                azure_client=azure_client,
                reservation_info=reservation_info,
                cancellation_manager=cancellation_manager,
                logger=logger,
            )

            return vm_details_flow.get_vm_details(request_actions=request_actions)
    def Deploy(self, context, request, cancellation_context=None):
        """
        Called when reserving a sandbox during setup, a call for each app in the sandbox.

        Method creates the compute resource in the cloud provider - VM instance or container.

        If App deployment fails, return a "success false" action result.

        :param ResourceCommandContext context:
        :param str request: A JSON string with the list of requested deployment actions
        :param CancellationContext cancellation_context:
        :return:
        :rtype: str
        """
        '''
        # parse the json strings into action objects
        actions = self.request_parser.convert_driver_request_to_actions(request)
        
        # extract DeployApp action
        deploy_action = single(actions, lambda x: isinstance(x, DeployApp))
        
        # if we have multiple supported deployment options use the 'deploymentPath' property 
        # to decide which deployment option to use. 
        deployment_name = deploy_action.actionParams.deployment.deploymentPath
                
        deploy_result = _my_deploy_method(context, actions, cancellation_context)
        return DriverResponse(deploy_result).to_driver_response_json()
        '''
        dump_context("deploy-context", context, r"C:\temp\context")
        dump_context("deploy-request", request, r"C:\temp\context", obj=False)

        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)

            DeployVMRequestActions.register_deployment_path(
                KubernetesDeployApp)
            request_actions = DeployVMRequestActions.from_request(request, api)
            service_provider = ServiceProvider(
                api_clients, logger,
                CancellationContextManager(cancellation_context))
            tag_service = TagsService(context)
            return DeployFlow(logger, resource_config, service_provider,
                              VmDetailsProvider(),
                              tag_service).deploy(request_actions)
 def Deploy(
     self,
     context: ResourceCommandContext,
     request: str,
     cancellation_context: CancellationContext,
 ) -> str:
     """Deploy image."""
     with LoggingSessionContext(context) as logger:
         logger.info("Starting Deploy command")
         logger.debug(f"Request: {request}")
         api = CloudShellSessionContext(context).get_api()
         conf = OSResourceConfig.from_context(self.SHELL_NAME, context, api)
         cancellation_manager = CancellationContextManager(cancellation_context)
         DeployVMRequestActions.register_deployment_path(OSNovaImgDeployApp)
         request_actions = DeployVMRequestActions.from_request(request, api)
         os_api = OSApi(conf, logger)
         return DeployAppFromNovaImgFlow(
             conf, cancellation_manager, os_api, logger
         ).deploy(request_actions)
 def GetVmDetails(
     self,
     context: ResourceCommandContext,
     requests: str,
     cancellation_context: CancellationContext,
 ) -> str:
     """Get instance operating system, specifications and networking information."""
     with LoggingSessionContext(context) as logger:
         logger.info("Starting Get VM Details command")
         logger.debug(f"Requests: {requests}")
         api = CloudShellSessionContext(context).get_api()
         conf = OSResourceConfig.from_context(self.SHELL_NAME, context, api)
         cancellation_manager = CancellationContextManager(cancellation_context)
         GetVMDetailsRequestActions.register_deployment_path(OSNovaImgDeployedApp)
         actions = GetVMDetailsRequestActions.from_request(requests, api)
         os_api = OSApi(conf, logger)
         return GetVMDetailsFlow(
             conf, cancellation_manager, os_api, logger
         ).get_vm_details(actions)
    def GetVmDetails(self, context, requests, 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 VM Details pane

        Method queries cloud provider for instance operating system, specifications and networking information and
        returns that as a json serialized driver response containing a list of VmDetailsData.

        If the operation fails, method should raise an exception.

        :param ResourceCommandContext context:
        :param str requests:
        :param CancellationContext cancellation_context:
        :return:
        """
        dump_context("vmditails-context", context, r"C:\temp\context")
        dump_context("vmditails-request",
                     requests,
                     r"C:\temp\context",
                     obj=False)
        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)
            GetVMDetailsRequestActions.register_deployment_path(
                KubernetesDeployedApp)
            request_actions = GetVMDetailsRequestActions.from_request(
                requests, api)
            service_provider = ServiceProvider(
                api_clients, logger,
                CancellationContextManager(cancellation_context))
            return VmDetialsFlow(
                logger, resource_config, service_provider,
                VmDetailsProvider()).get_vm_details(request_actions)
Example #11
0
def cancellation_context_manager():
    context = Mock(name="cancellation context", is_cancelled=False)
    return CancellationContextManager(context)