예제 #1
0
    def create_taskconfig(self, app, **kwargs):
        kwargs.setdefault("type", TaskConfigType.deploy)
        kwargs.setdefault("provider", "shell")
        kwargs.setdefault(
            "data",
            {
                "provider_config": {
                    "command": "/bin/echo helloworld"
                },
                "notifiers": [
                    {
                        "type": "slack",
                        "config": {
                            "webhook_url": "https://example.com"
                        }
                    },
                    {
                        "type": "datadog",
                        "config": {
                            "webhook_url": "https://example.com"
                        },
                    },
                ],
            },
        )

        task_config = TaskConfig(app_id=app.id, **kwargs)
        db.session.add(task_config)
        db.session.commit()

        return task_config
예제 #2
0
    def create_taskconfig(self, app, **kwargs):
        kwargs.setdefault('type', TaskConfigType.deploy)
        kwargs.setdefault('provider', 'shell')
        kwargs.setdefault(
            'data', {
                'provider_config': {
                    'command': '/bin/echo helloworld',
                },
                'notifiers': [
                    {
                        'type': 'slack',
                        'config': {
                            'webhook_url': 'https://example.com'
                        },
                    },
                    {
                        'type': 'datadog',
                        'config': {
                            'webhook_url': 'https://example.com'
                        },
                    },
                ],
            })

        task_config = TaskConfig(app_id=app.id, **kwargs)
        db.session.add(task_config)
        db.session.commit()

        return task_config
예제 #3
0
    def post(self):
        """
        Create a new app.
        """
        args = self.post_parser.parse_args()

        provider_config = parse_provider_config(
            args.provider, args.provider_config or {}
        )

        checks_config = parse_checks_config(args.checks or [])

        notifiers_config = parse_notifiers_config(args.notifiers or [])

        environments_config = parse_environments_config(args.environments or {})

        # TODO(dcramer): this needs to be a get_or_create pattern
        repo = Repository.query.filter(Repository.url == args.repository).first()
        if repo is None:
            repo = Repository(url=args.repository, vcs="git")
            db.session.add(repo)
            db.session.flush()

        app = App(
            name=args.name,
            repository_id=repo.id,
            data={"environments": environments_config},
        )
        db.session.add(app)
        db.session.flush()

        # For backwards compatibility, we assume that we need a deploy TaskConfig
        # on the app at all times.
        deploy_config = TaskConfig(
            app_id=app.id,
            type=TaskConfigType.deploy,
            provider=args.provider,
            data={
                "provider_config": provider_config,
                "notifiers": notifiers_config,
                "checks": checks_config,
            },
        )
        db.session.add(deploy_config)
        db.session.commit()

        return self.respond(serialize(app), status_code=201)
예제 #4
0
    def put(self, app):
        """
        Update an app.
        """
        args = self.put_parser.parse_args()

        app = App.query.filter(App.name == app).first()
        if app is None:
            return self.error('Invalid app', name='invalid_resource', status_code=404)

        # For backwards compatibility, we assume that we need a deploy TaskConfig
        # on the app at all times.
        deploy_config = TaskConfig.query.filter(
            TaskConfig.app_id == app.id,
            TaskConfig.type == TaskConfigType.deploy,
        ).first()
        if deploy_config is None:
            deploy_config = TaskConfig(app_id=app.id, type=TaskConfigType.deploy)
            db.session.add(deploy_config)
            db.session.flush()

        if args.provider or args.provider_config:
            if args.provider is not None:
                provider = args.provider
            else:
                provider = deploy_config.provider

            if args.provider_config is not None:
                provider_config = args.provider_config
            else:
                provider_config = deploy_config.provider_config

            deploy_config.provider = provider
            deploy_config.data['provider_config'] = parse_provider_config(
                provider, provider_config
            )

        if args.notifiers is not None:
            deploy_config.data['notifiers'] = parse_notifiers_config(args.notifiers)

        if args.checks is not None:
            deploy_config.data['checks'] = parse_checks_config(args.checks)

        if args.environments is not None:
            app.data['environments'] = parse_environments_config(args.environments)

        if args.name:
            app.name = args.name

        # TODO(dcramer): this needs to be a get_or_create pattern
        if args.repository:
            repo = Repository.query.filter(
                Repository.url == args.repository,
            ).first()
            if repo is None:
                repo = Repository(url=args.repository, vcs='git')
                db.session.add(repo)
                db.session.flush()
            app.repository_id = repo.id

        db.session.add(app)
        db.session.add(deploy_config)
        db.session.commit()

        context = serialize(app)
        context.update({
            'provider': deploy_config.provider,
            'provider_config': deploy_config.provider_config,
            'notifiers': deploy_config.notifiers,
            'checks': deploy_config.checks,
        })

        return self.respond(context)