-
Notifications
You must be signed in to change notification settings - Fork 116
Charmed snapcraft.io #5181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
edisile
wants to merge
17
commits into
main
Choose a base branch
from
charmed-snapcraft-io
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Charmed snapcraft.io #5181
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
58fb5be
feature: create a rock for the application
edisile c9304b4
feature: create charm for deployment
edisile d0880ba
feature: github actions for rock test on PR and deploy
edisile 3c508ee
fix: copy static files correctly in rockcraft config
edisile a866916
fix: cleanup before merging into deploy branches
edisile fcc8c67
fix: add deleted.yaml to rock
edisile 0030ae3
fix: disable charm tracing interface
edisile 413a234
fix: rename charm class
edisile a2b44d0
fix: update PaaS charm link
edisile 99d5d0a
fix: added env variables to charmcraft.yaml
edisile 5d93e46
fix: import webapp.config at the top of app module
edisile bc4d179
chore: remove unused env var
edisile f577bb5
chore: env vars descriptions
edisile 45cf186
fix: lining, duplicate import
edisile 5cf5982
fix: reduce token permissions in github workflows
edisile 240e856
force deploy
edisile 9d04538
fix: workflow permissions
edisile File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
name: Pack and Deploy | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
- staging | ||
workflow_dispatch: | ||
inputs: | ||
environment: | ||
description: 'Environment (Production or Staging)' | ||
required: true | ||
type: choice | ||
options: | ||
- Production | ||
- Staging | ||
jobs: | ||
setup: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
environment: ${{ github.event.inputs.environment != '' && github.event.inputs.environment || (github.ref == 'refs/heads/main' && 'Production' || 'Staging') }} | ||
outputs: | ||
charm_name: ${{ steps.setup-vars.outputs.charm_name }} | ||
channel: ${{ steps.setup-vars.outputs.channel }} | ||
juju_controller_name: ${{ steps.setup-vars.outputs.juju_controller_name }} | ||
juju_model_name: ${{ steps.setup-vars.outputs.juju_model_name }} | ||
environment: ${{ steps.setup-vars.outputs.environment }} | ||
steps: | ||
- name: setup vars | ||
id: setup-vars | ||
run: | | ||
echo "charm_name=${{ vars.CHARM_NAME }}" >> $GITHUB_OUTPUT | ||
echo "channel=${{ vars.CHANNEL }}" >> $GITHUB_OUTPUT | ||
echo "juju_controller_name=${{ vars.JUJU_CONTROLLER_NAME }}" >> $GITHUB_OUTPUT | ||
echo "juju_model_name=${{ vars.JUJU_MODEL_NAME }}" >> $GITHUB_OUTPUT | ||
echo "environment=${{ github.event.inputs.environment != '' && github.event.inputs.environment || (github.ref == 'refs/heads/main' && 'Production' || 'Staging') }}" >> $GITHUB_OUTPUT | ||
|
||
deploy: | ||
needs: setup | ||
name: Deploy | ||
uses: canonical/webteam-devops/.github/workflows/deploy.yaml@main | ||
permissions: | ||
contents: read | ||
deployments: write | ||
packages: write | ||
with: | ||
environment: ${{ needs.setup.outputs.environment }} | ||
charm_name: ${{ needs.setup.outputs.charm_name }} | ||
channel: ${{ needs.setup.outputs.channel }} | ||
juju_controller_name: ${{ needs.setup.outputs.juju_controller_name }} | ||
juju_model_name: ${{ needs.setup.outputs.juju_model_name }} | ||
secrets: | ||
VAULT_APPROLE_ROLE_ID: ${{ secrets.VAULT_APPROLE_ROLE_ID }} | ||
VAULT_APPROLE_SECRET_ID: ${{ secrets.VAULT_APPROLE_SECRET_ID }} | ||
CHARMHUB_TOKEN: ${{ secrets.CHARMHUB_TOKEN }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,3 +70,7 @@ coverage/ | |
.webcache_blog/ | ||
.coverage | ||
cypress/screenshots/ | ||
|
||
# Charming artifacts | ||
*.charm | ||
*.rock |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# 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 | ||
import logging | ||
|
||
# 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"] | ||
|
||
# disable talisker logger, as it is not used in this application and clutters logs | ||
logging.getLogger("talisker.context").disabled = True | ||
|
||
from webapp.app import create_app | ||
|
||
app = create_app() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# The Charm for the snapcraft.io website | ||
|
||
This charm was created using the [PaaS App Charmer](https://canonical-12-factor-app-support.readthedocs-hosted.com/latest/) | ||
|
||
## Local development | ||
|
||
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. | ||
|
||
Then, you can run the following command to pack and upload the rock: | ||
|
||
```bash | ||
rockcraft pack | ||
rockcraft.skopeo --insecure-policy copy --dest-tls-verify=false oci-archive:snapcraft-io*.rock docker://localhost:32000/snapcraft-io:1 | ||
``` | ||
|
||
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. | ||
|
||
You can deploy the charm locally with: | ||
|
||
```bash | ||
cd charm | ||
charmcraft fetch-libs | ||
charmcraft pack | ||
juju deploy ./*.charm --resource flask-app-image=localhost:32000/snapcraft-io:1 | ||
``` | ||
|
||
This will deploy the charm with the rock image you just uploaded attached as a resource. | ||
|
||
once `juju status` reports the charm as `active`, you can test the webserver: | ||
|
||
```bash | ||
curl {IP_OF_SNAPCRAFT_IO_UNIT}:8000 | ||
``` | ||
|
||
to connect using a browser, the easiest way is to integrate with `nginx-ingress-integrator`: | ||
|
||
```bash | ||
juju deploy nginx-ingress-integrator --trust | ||
juju config nginx-ingress-integrator service-hostname=snapcraft.local path-routes=/ | ||
juju integrate nginx-ingress-integrator snapcraft-io | ||
``` | ||
|
||
You can then add `snapcraft.local` to your `/etc/hosts` file with the IP of the multipass vm: | ||
|
||
```bash | ||
multipass ls # Get the IP of the VM | ||
echo "{IP_OF_VM} snapcraft.local" | sudo tee -a /etc/hosts | ||
``` | ||
|
||
> 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. | ||
|
||
|
||
## Design Decisions: | ||
- 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). | ||
- 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
name: snapcraft-io | ||
|
||
type: charm | ||
|
||
bases: | ||
- build-on: | ||
- name: ubuntu | ||
channel: "22.04" | ||
run-on: | ||
- name: ubuntu | ||
channel: "22.04" | ||
|
||
summary: The charm for the snapcraft.io website | ||
|
||
description: The charm for the snapcraft.io website, built with the PaaS app charmer | ||
|
||
extensions: | ||
- flask-framework | ||
|
||
config: | ||
options: | ||
sentry-dsn: | ||
description: "Sentry Data Source Name for the project" | ||
type: string | ||
|
||
environment: | ||
description: "" | ||
default: "production" | ||
type: string | ||
|
||
marketo-client-id: | ||
description: "Marketo API client ID" | ||
type: string | ||
|
||
marketo-client-secret: | ||
description: "Marketo API client secret" | ||
type: string | ||
|
||
github-client-id: | ||
description: "GitHub OAuth application ID for prompting users for access to their repositories" | ||
type: string | ||
|
||
github-client-secret: | ||
description: "GitHub OAuth application client secret for prompting users for access to their repositories" | ||
type: string | ||
|
||
github-snapcraft-user-token: | ||
description: "GitHub application token for automated builds" | ||
type: string | ||
|
||
github-snapcraft-bot-user-token: | ||
description: "GitHub application token for CVE data" | ||
type: string | ||
|
||
github-webhook-secret: | ||
description: "Secret salt used for signing automated build webhooks" | ||
type: string | ||
|
||
github-webhook-host-url: | ||
description: "URL of the automated build webhooks' host" | ||
type: string | ||
|
||
lp-api-username: | ||
description: "Launchpad API username" | ||
type: string | ||
|
||
lp-api-token: | ||
description: "Launchpad API token" | ||
type: string | ||
|
||
lp-api-token-secret: | ||
description: "Launchpad API secret" | ||
type: string | ||
|
||
youtube-api-key: | ||
description: "" | ||
type: string | ||
|
||
discourse-api-key: | ||
description: "" | ||
type: string | ||
|
||
discourse-api-username: | ||
description: "" | ||
type: string | ||
|
||
dns-verification-salt: | ||
description: "" | ||
type: string | ||
|
||
login-url: | ||
description: "Base URL for SSO login redirects" | ||
default: "https://login.ubuntu.com" | ||
type: string | ||
|
||
bsi-url: | ||
description: "" | ||
default: "https://build.snapcraft.io" | ||
type: string | ||
|
||
snapstore-dashboard-api-url: | ||
description: "Base URL for SCA backend" | ||
default: "https://dashboard.snapcraft.io/" | ||
type: string | ||
|
||
# requires: | ||
# tracing: | ||
# interface: tracing | ||
# optional: true | ||
# limit: 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ops ~= 2.17 | ||
paas-charm>=1.0,<2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/usr/bin/env python3 | ||
|
||
"""Flask Charm entrypoint.""" | ||
|
||
import logging | ||
import typing | ||
|
||
import ops | ||
|
||
import paas_charm.flask | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class SnapcraftCharm(paas_charm.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(SnapcraftCharm) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
name: snapcraft-io | ||
base: bare | ||
build-base: ubuntu@22.04 | ||
version: "0.1" | ||
summary: Rocked snapcraft.io | ||
description: | | ||
This is the rockcraft for the snapcraft.io website. | ||
platforms: | ||
amd64: | ||
arm64: | ||
|
||
extensions: | ||
- flask-framework | ||
|
||
parts: | ||
build-ui: | ||
plugin: nil | ||
source: . | ||
source-type: local | ||
build-snaps: | ||
- node/22/stable | ||
override-build: | | ||
set -eux | ||
# install dependencies | ||
npm install -g yarn | ||
yarn install | ||
# build the UI | ||
yarn run build | ||
mkdir -p "$CRAFT_PART_INSTALL/flask/app" | ||
cp -r static "$CRAFT_PART_INSTALL/flask/app/" | ||
flask-framework/install-app: | ||
after: | ||
- build-ui | ||
prime: | ||
- flask/app/.env | ||
- flask/app/app.py | ||
- flask/app/webapp | ||
- flask/app/templates | ||
# - flask/app/static # it already gets copied in the build-ui step | ||
- flask/app/deleted.yaml | ||
- flask/app/redirects.yaml | ||
- flask/app/security.txt | ||
- flask/app/robots.txt | ||
edisile marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Contact: mailto:security@ubuntu.com | ||
Expires: 2025-08-01T00:00:00.000Z | ||
Preferred-Languages: en |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.