Skip to content

Commit

Permalink
feat(12-factor-app): add configurations to deploy as 12 factor app
Browse files Browse the repository at this point in the history
WD-11371
  • Loading branch information
lorumic committed Aug 28, 2024
1 parent 1e97bd1 commit 3c2ae11
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
PORT=8043
FLASK_DEBUG=true
SECRET_KEY=secret_key
FLASK_SECRET_KEY=secret_key
DEVEL=true
FLASK_GOOGLE_SEARCH_API_KEY=insecure_development_key
93 changes: 93 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
name: Deploy site

on:
push:
branches:
- main

env:
CHARMCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS: true
ROCKCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS: true

jobs:
pack-charm:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3

- name: Setup LXD
uses: canonical/setup-lxd@main

- name: Setup Charmcraft
run: sudo snap install charmcraft --classic --channel=latest/edge

- name: Fetch libs
run: |
cd ./charm
charmcraft fetch-libs
- name: Pack charm
run: charmcraft pack -v --project-dir ./charm

- name: Upload charm
uses: actions/upload-artifact@v3
with:
name: anbox-cloud-io-charm
path: ./*.charm

pack-rock:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3

- name: Use Node.js
uses: actions/setup-node@v3

- name: Build Assets
run: |
yarn install
yarn run build
- name: Setup LXD
uses: canonical/setup-lxd@main

- name: Setup Rockcraft
run: sudo snap install rockcraft --classic --channel=latest/edge

- name: Pack Rock
run: rockcraft pack

- name: Upload Rock
uses: actions/upload-artifact@v3
with:
name: anbox-cloud-io-rock
path: ./*.rock

publish-image:
runs-on: ubuntu-latest
needs: pack-rock
outputs:
image_url: ${{ steps.set_image_url.outputs.image_url }}
steps:
- name: Get Rock
uses: actions/download-artifact@v3
with:
name: anbox-cloud-io-rock

- name: Set image URL
id: set_image_url
run: echo "image_url=ghcr.io/canonical/anbox-cloud.io:$(date +%s)-${GITHUB_SHA:0:7}" >> $GITHUB_OUTPUT

- name: Push to GHCR
run: skopeo --insecure-policy copy oci-archive:$(ls *.rock) docker://${{ steps.set_image_url.outputs.image_url }} --dest-creds "canonical:${{ secrets.GITHUB_TOKEN }}"

deploy:
runs-on: ubuntu-latest
needs: [pack-charm, publish-image]
steps:
- name: Workflow run ID
run : echo ${{ github.run_id }}
- name: Image URL
run : echo ${{ needs.publish-image.outputs.image_url }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ package-lock.json
*.snap
_site/
*.*.map
*.rock

# [env] Local environment settings
.docker-project
Expand Down
9 changes: 9 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This file serves as an entry point for the rock image. It is required by the PaaS app charmer.
# The flask application must be defined in this file under the variable name `app`.
# See - https://documentation.ubuntu.com/rockcraft/en/latest/reference/extensions/flask-framework/
import os

# canonicalwebteam.flask-base requires SECRET_KEY to be set, this must be done before importing the app
os.environ["SECRET_KEY"] = os.environ["FLASK_SECRET_KEY"]

from webapp.app import app
10 changes: 10 additions & 0 deletions charm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
venv/
build/
*.charm
.tox/
.coverage
__pycache__/
*.py[cod]
.idea
.vscode/
lib/
26 changes: 26 additions & 0 deletions charm/charmcraft.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: anbox-cloud-io

type: charm

bases:
- build-on:
- name: ubuntu
channel: "22.04"
run-on:
- name: ubuntu
channel: "22.04"

summary: This is the charm for the anbox-cloud.io website.

description: This is the charm for the anbox-cloud.io website.

extensions:
- flask-framework

config:
options:
google-search-api-key:
description: Google search API key
type: string
required: true
default: ""
1 change: 1 addition & 0 deletions charm/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
paas-app-charmer==1.0.4
29 changes: 29 additions & 0 deletions charm/src/charm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python3
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

"""Flask Charm entrypoint."""

import logging
import typing

import ops
import paas_app_charmer.flask

logger = logging.getLogger(__name__)


class FlaskCharm(paas_app_charmer.flask.Charm):
"""Flask Charm service."""

def __init__(self, *args: typing.Any) -> None:
"""Initialize the instance.
Args:
args: passthrough to CharmBase.
"""
super().__init__(*args)


if __name__ == "__main__":
ops.main.main(FlaskCharm)
7 changes: 6 additions & 1 deletion konf/site.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ env:
- name: SENTRY_DSN
value: https://[email protected]//23

- name: SEARCH_API_KEY
- name: FLASK_GOOGLE_SEARCH_API_KEY
secretKeyRef:
key: google-custom-search-key
name: google-api

- name: FLASK_SECRET_KEY
secretKeyRef:
key: anbox-cloud-io
name: secret-keys

production:
replicas: 5
nginxConfigurationSnippet: |
Expand Down
24 changes: 24 additions & 0 deletions rockcraft.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: anbox-cloud-io
base: [email protected]
version: "0.1"
summary: Scalable Android in the cloud
description: |
Anbox Cloud lets you stream mobile apps securely, at any scale, to any
device letting you focus on your apps. Run Android in system containers,
not emulators, on AWS, OCI, Azure, GCP or your private cloud with ultra
low streaming latency.
platforms:
amd64:

extensions:
- flask-framework

parts:
flask-framework/install-app:
prime:
- flask/app/.env
- flask/app/app.py
- flask/app/webapp
- flask/app/templates
- flask/app/static
- flask/app/redirects.yaml

0 comments on commit 3c2ae11

Please sign in to comment.