コード例 #1
0
    def create(self, validated_data):
        """
        Fire off a new task for the supplied action.

        Called automatically by the DRF following a POST request.

        :type validated_data: ``dict``
        :param validated_data: Dict containing action the task should perform.
                               Valid actions are `HEALTH_CHECK`, `DELETE`.
        """
        print("deployment task data: %s" % validated_data)
        action = getattr(models.ApplicationDeploymentTask,
                         validated_data.get(
                            'action',
                            models.ApplicationDeploymentTask.HEALTH_CHECK))
        request = self.context.get('view').request
        dpk = self.context['view'].kwargs.get('deployment_pk')
        dpl = models.ApplicationDeployment.objects.get(id=dpk)
        credentials = view_helpers.get_credentials(dpl.target_cloud, request)
        try:
            if action == models.ApplicationDeploymentTask.HEALTH_CHECK:
                async_result = tasks.health_check.delay(dpl, credentials)
            elif action == models.ApplicationDeploymentTask.RESTART:
                async_result = tasks.manage_appliance.delay('restart', dpl,
                                                            credentials)
            elif action == models.ApplicationDeploymentTask.DELETE:
                async_result = tasks.manage_appliance.delay('delete', dpl,
                                                            credentials)
            return models.ApplicationDeploymentTask.objects.create(
                action=action, deployment=dpl, celery_id=async_result.task_id)
        except serializers.ValidationError as ve:
            raise ve
        except Exception as e:
            raise serializers.ValidationError({"error": str(e)})
コード例 #2
0
    def create(self, validated_data):
        name = validated_data.get("name")
        cloud = validated_data.get("target_cloud")
        version = validated_data.get("application_version")
        cloud_version_config = models.ApplicationVersionCloudConfig.objects.get(
            application_version=version.id, cloud=cloud.slug)
        default_config = json.loads(cloud_version_config.default_launch_config)
        request = self.context.get('view').request
        credentials = view_helpers.get_credentials(cloud, request)
        try:
            handler = util.import_class(version.backend_component_name)()
            app_config = validated_data.get("config_app", {})
            merged_config = jsonmerge.merge(default_config, app_config)
            final_ud_config = handler.process_app_config(
                name, cloud_version_config, credentials, merged_config)
            async_result = tasks.launch_appliance.delay(
                name, cloud_version_config, credentials, merged_config,
                final_ud_config)

            del validated_data['application']
            del validated_data['config_app']
            validated_data['owner_id'] = request.user.id
            validated_data['application_config'] = json.dumps(merged_config)
            validated_data['celery_task_id'] = async_result.task_id
            return super(DeploymentSerializer, self).create(validated_data)
        except serializers.ValidationError as ve:
            raise ve
        except Exception as e:
            raise serializers.ValidationError({"error": str(e)})
コード例 #3
0
    def create(self, validated_data):
        """
        Create a new ApplicationDeployment object.

        Called automatically by the DRF following a POST request.
        """
        name = validated_data.get("name")
        cloud = validated_data.get("target_cloud")
        version = validated_data.get("application_version")
        cloud_version_config = models.ApplicationVersionCloudConfig.objects.get(
            application_version=version.id, cloud=cloud.slug)
        default_combined_config = cloud_version_config.compute_merged_config()
        request = self.context.get('view').request
        credentials = view_helpers.get_credentials(cloud, request)
        try:
            handler = util.import_class(version.backend_component_name)()

            app_config = validated_data.get("config_app", {})

            merged_config = jsonmerge.merge(default_combined_config,
                                            app_config)

            final_ud_config = handler.process_app_config(
                name, cloud_version_config, credentials, merged_config)

            sanitised_app_config = handler.sanitise_app_config(merged_config)

            async_result = tasks.launch_appliance.delay(
                name, cloud_version_config, credentials, merged_config,
                final_ud_config)

            del validated_data['application']
            del validated_data['config_app']
            validated_data['owner_id'] = request.user.id
            validated_data['application_config'] = json.dumps(merged_config)
            validated_data['celery_task_id'] = async_result.task_id
            app_deployment = super(DeploymentSerializer,
                                   self).create(validated_data)
            self.log_usage(cloud_version_config, app_deployment,
                           sanitised_app_config, request.user)
            return app_deployment
        except serializers.ValidationError as ve:
            raise ve
        except Exception as e:
            raise serializers.ValidationError({"error": str(e)})