Skip to content

Commit d44c516

Browse files
committed
feature: create charm for deployment
1 parent e651126 commit d44c516

7 files changed

Lines changed: 137 additions & 3 deletions

File tree

app.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This file serves as an entry point for the rock image. It is required by the PaaS app charmer.
2+
# The flask application must be defined in this file under the variable name `app`.
3+
# See - https://documentation.ubuntu.com/rockcraft/en/latest/reference/extensions/flask-framework/
4+
import os
5+
import logging
6+
7+
# canonicalwebteam.flask-base requires SECRET_KEY to be set, this must be done before importing the app
8+
os.environ["SECRET_KEY"] = os.environ["FLASK_SECRET_KEY"]
9+
10+
# disable talisker logger, as it is not used in this application and clutters logs
11+
logging.getLogger("talisker.context").disabled = True
12+
13+
from webapp.app import create_app
14+
15+
app = create_app()

charm/.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
venv/
2+
build/
3+
*.charm
4+
.tox/
5+
.coverage
6+
__pycache__/
7+
*.py[cod]
8+
.idea
9+
.vscode/
10+
lib/

charm/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# The Charm for the snapcraft.io website
2+
3+
This charm was created using the [PaaS App Charmer](https://juju.is/docs/sdk/paas-charm)
4+
5+
## Local development
6+
7+
To work on this charm locally, you first need to set up an environment, follow [this section](https://juju.is/docs/sdk/write-your-first-kubernetes-charm-for-a-flask-app#heading--set-things-up) of the tutorial.
8+
9+
Then, you can run the following command to pack and upload the rock:
10+
11+
```bash
12+
rockcraft pack
13+
rockcraft.skopeo --insecure-policy copy --dest-tls-verify=false oci-archive:snapcraft-io*.rock docker://localhost:32000/snapcraft-io:1
14+
```
15+
16+
This will pack the application into a [rock](https://documentation.ubuntu.com/rockcraft/en/latest/explanation/rocks/) (OCI image) and upload it to the local registry.
17+
18+
You can deploy the charm locally with:
19+
20+
```bash
21+
cd charm
22+
charmcraft fetch-libs
23+
charmcraft pack
24+
juju deploy ./*.charm --resource flask-app-image=localhost:32000/snapcraft-io:1
25+
```
26+
27+
This will deploy the charm with the rock image you just uploaded attached as a resource.
28+
29+
once `juju status` reports the charm as `active`, you can test the webserver:
30+
31+
```bash
32+
curl {IP_OF_SNAPCRAFT_IO_UNIT}:8000
33+
```
34+
35+
to connect using a browser, the easiest way is to integrate with `nginx-ingress-integrator`:
36+
37+
```bash
38+
juju deploy nginx-ingress-integrator --trust
39+
juju config nginx-ingress-integrator service-hostname=snapcraft.local path-routes=/
40+
juju integrate nginx-ingress-integrator snapcraft-io
41+
```
42+
43+
You can then add `snapcraft.local` to your `/etc/hosts` file with the IP of the multipass vm:
44+
45+
```bash
46+
multipass ls # Get the IP of the VM
47+
echo "{IP_OF_VM} snapcraft.local" | sudo tee -a /etc/hosts
48+
```
49+
50+
> Note: login will not work using this setup, if you'd like to access publisher pages, change the domain to `staging.snapcraft.io`, but make sure to remove the line from `/etc/hosts/` after you're done.
51+
52+
53+
## Design Decisions:
54+
- To keep the codebase clean and charm libraries updated, they are only fetched before packing the charm in the [Github Actions workflow](https://github.com/canonical/snapcraft.io/blob/main/.github/workflows/publish_charm.yaml#L25).
55+
- As all our work is open source, the charm is publicly available on [snapcraft](https://charmhub.io/snapcraft-io), the rock image is also included as a resource. This significantly simplifies deployment.

charm/charmcraft.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: snapcraft-io
2+
3+
type: charm
4+
5+
bases:
6+
- build-on:
7+
- name: ubuntu
8+
channel: "22.04"
9+
run-on:
10+
- name: ubuntu
11+
channel: "22.04"
12+
13+
summary: The charm for the snapcraft.io website
14+
15+
description: The charm for the snapcraft.io website, built with the PaaS app charmer
16+
17+
extensions:
18+
- flask-framework
19+
20+
requires:
21+
tracing:
22+
interface: tracing
23+
optional: true
24+
limit: 1

charm/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ops ~= 2.17
2+
paas-charm>=1.0,<2

charm/src/charm.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python3
2+
3+
"""Flask Charm entrypoint."""
4+
5+
import logging
6+
import typing
7+
8+
import ops
9+
10+
import paas_charm.flask
11+
12+
logger = logging.getLogger(__name__)
13+
14+
15+
class CharmCharm(paas_charm.flask.Charm):
16+
"""Flask Charm service."""
17+
18+
def __init__(self, *args: typing.Any) -> None:
19+
"""Initialize the instance.
20+
21+
Args:
22+
args: passthrough to CharmBase.
23+
"""
24+
super().__init__(*args)
25+
26+
27+
if __name__ == "__main__":
28+
ops.main(CharmCharm)

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3310,9 +3310,9 @@ camelcase@^6.2.0:
33103310
integrity sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==
33113311

33123312
caniuse-lite@^1.0.30001646, caniuse-lite@^1.0.30001688:
3313-
version "1.0.30001690"
3314-
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001690.tgz#f2d15e3aaf8e18f76b2b8c1481abde063b8104c8"
3315-
integrity sha512-5ExiE3qQN6oF8Clf8ifIDcMRCRE/dMGcETG/XGMD8/XiXm6HXQgQTh1yZYLXXpSOsEUlJm1Xr7kGULZTuGtP/w==
3313+
version "1.0.30001726"
3314+
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001726.tgz"
3315+
integrity sha512-VQAUIUzBiZ/UnlM28fSp2CRF3ivUn1BWEvxMcVTNwpw91Py1pGbPIyIKtd+tzct9C3ouceCVdGAXxZOpZAsgdw==
33163316

33173317
chalk@^3.0.0:
33183318
version "3.0.0"

0 commit comments

Comments
 (0)