-
Notifications
You must be signed in to change notification settings - Fork 5
feat: convert the container image to a rock #45
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
Merged
Merged
Changes from all commits
Commits
Show all changes
57 commits
Select commit
Hold shift + click to select a range
cb380f8
switch to uv-build
dwilding e63f2ca
replace Dockerfile by rockcraft.yaml
dwilding 919bf5a
update integration test
dwilding 973314b
install LXD in CI
dwilding 3320cff
prepare LXD (no need to install)
dwilding f38ea8b
use craft-actions instead of manually configuring the runner
dwilding d0d3840
restructure integration test
dwilding 1c08c7d
revert to single test file
dwilding c8266df
add spread config
dwilding 180078b
reduce changes
dwilding b09c8ec
make is not installed in the spread runner
dwilding 38ea980
rewrite README
dwilding 3031db7
add testing details to README
dwilding 38d321d
update dependabot config
dwilding 0d6ff25
bump uvicorn
dwilding 6bff3d1
bump fastapi and python-multipart
dwilding 3540de0
try build workflow on PRs
dwilding 60e30cc
remove ppc64el
dwilding bbb978a
fix branch name
dwilding 02b8ff2
add dispatch to workflow
dwilding 3a287f4
add workflows from template
dwilding 887b13e
remove old build workflow
dwilding 52db383
main --> master
dwilding 06f1b1c
fix directory in CI
dwilding fd11b1a
simplify rock CI
dwilding b65ea1e
override OpenSSL from base
dwilding 9da1803
check then format in Makefile
dwilding 25c0967
overlay instead of stage
dwilding 6f62047
ignore vulnerability instead
dwilding af7abe6
fix build workflow linting
dwilding 9296f07
switch build workflow back to tag push
dwilding a3fe3ef
improve JSON building in workflow
dwilding bd01d5c
switch to bare base
dwilding 76e55bf
fix symlink handling
dwilding 2a7b6f0
remove ignored CVE
dwilding 8cc1708
add dry run build workflow
dwilding 44c727c
adjust parts after feedback
dwilding 6398906
adjust service and check names
dwilding c932cd5
remove dry-run workflow
dwilding e1803f7
bump release version
dwilding 0c436ef
remove redundant permissions block
dwilding a4adb0c
rename 'build' workflow to 'publish'
dwilding cbc429a
remove flit config
dwilding 0373f86
remove ghcr conditional in publish workflow
dwilding bc1e086
switch to tag for rocks-security-manifest
dwilding 5ffc420
add note about Docker needing sudo
dwilding 768ee22
Merge remote-tracking branch 'upstream/master' into convert-to-rock
dwilding b4d807a
remove Ubuntu base from tag
dwilding abd1ec8
remove image from Docker after testing
dwilding 89c2193
add comment about runner selection
dwilding e637312
bump deps and uv action
dwilding 9d0dc83
always use GitHub runners
dwilding c51789c
add comment about runners
dwilding 8cf1eef
for Spread setup, list images instead of running an image
dwilding 6b8cf13
Merge remote-tracking branch 'upstream/master' into convert-to-rock
dwilding e231991
Merge remote-tracking branch 'upstream/master' into convert-to-rock
dwilding 67db955
bump fastapi to 0.138.1
dwilding 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
This file was deleted.
Oops, something went wrong.
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,80 @@ | ||
| name: Build and publish image | ||
|
|
||
| on: | ||
| push: | ||
| tags: | ||
| - '*' | ||
|
|
||
| permissions: {} | ||
|
|
||
| jobs: | ||
| prepare: | ||
| runs-on: ubuntu-latest | ||
| outputs: | ||
| build-matrix: ${{ steps.set-matrix.outputs.build-matrix }} | ||
| steps: | ||
| - name: Set Build Matrix | ||
| id: set-matrix | ||
| env: | ||
| TAG: ${{ github.ref_name }} | ||
| run: | | ||
| matrix=$(jq --null-input --compact-output \ | ||
| --arg tag "${TAG}" \ | ||
| '{ | ||
| include: [{ | ||
| name: "api-demo-server", | ||
| tag: $tag, | ||
| directory: ".", | ||
| "artifact-name": "api-demo-server", | ||
| "run-tests": true, | ||
| lfs: false, | ||
| "lfs-include": "" | ||
| }] | ||
| }') | ||
| echo "build-matrix=${matrix}" >> $GITHUB_OUTPUT | ||
| fi | ||
|
|
||
| build: | ||
| needs: [prepare] | ||
| strategy: | ||
| matrix: ${{ fromJSON(needs.prepare.outputs.build-matrix) }} | ||
| uses: canonical/oci-factory/.github/workflows/Build-Rock.yaml@43f2710f69c29e43fd90f9e600896a7e32de96de | ||
| with: | ||
| rock-repo: ${{ github.event.pull_request.head.repo.full_name || github.repository }} | ||
| rock-repo-commit: ${{ github.head_ref || github.ref_name }} | ||
| rockfile-directory: ${{ matrix.directory }} | ||
| oci-archive-name: ${{ matrix.artifact-name }} | ||
| # We don't have any special architecture or performance requirements, | ||
| # so use GitHub's runners instead of Canonical's self-hosted runners. | ||
| arch-map: '{"amd64":["ubuntu-24.04"],"arm64":["ubuntu-24.04-arm"]}' | ||
| rockcraft-test: ${{ matrix.run-tests }} | ||
| secrets: | ||
| source-github-token: ${{ secrets.REPO_CLONER_TOKEN }} | ||
|
|
||
| test: | ||
| needs: [prepare, build] | ||
| strategy: | ||
| fail-fast: false | ||
| matrix: ${{ fromJSON(needs.prepare.outputs.build-matrix) }} | ||
| uses: canonical/oci-factory/.github/workflows/Test-Rock.yaml@43f2710f69c29e43fd90f9e600896a7e32de96de | ||
| with: | ||
| oci-archive-name: ${{ matrix.artifact-name }} | ||
|
|
||
| upload-ghcr: | ||
| needs: [prepare, test] | ||
| runs-on: ubuntu-latest | ||
| strategy: | ||
| matrix: ${{ fromJSON(needs.prepare.outputs.build-matrix) }} | ||
| fail-fast: false | ||
| permissions: | ||
| packages: write | ||
| steps: | ||
| - name: Upload Rock to GHCR | ||
| uses: canonical/oci-factory/.github/actions/upload-rock@43f2710f69c29e43fd90f9e600896a7e32de96de | ||
| with: | ||
| artifact_name: ${{ matrix.artifact-name }} | ||
| tags: ${{ matrix.tag }} | ||
| name: ${{ matrix.name }} | ||
| registry: ghcr.io/${{ github.repository }} | ||
| username: ${{ github.actor }} | ||
| password: ${{ secrets.GITHUB_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
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 @@ | ||
| *.rock | ||
| .craft-spread-* | ||
| .spread-reuse.* |
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 was deleted.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1,45 +1,70 @@ | ||
| # Description | ||
| This is a demo server based on Python FastAPI. | ||
| Server is used to show connections to PostgreSQL and Prometheus. | ||
| This is a demo Python server written with FastAPI. The server connects to PostgreSQL and exposes Prometheus metrics. | ||
|
|
||
| To see API reference start the server and open: http://127.0.0.1:8000/docs | ||
| To get prometheus metrics: http://127.0.0.1:8000/metrics | ||
| The server is packaged as a "rock" using Canonical's OCI-compliant format for container images. | ||
|
|
||
| # Usage | ||
| Download and start PostgreSQL container: | ||
| ``` | ||
| docker run --name postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres | ||
| ``` | ||
| # Build and run the container image | ||
|
|
||
| Get psql container IP: | ||
| ``` | ||
| docker ps # get postgres container ID | ||
| docker inspect <postgres container ID> | grep IPAddress | ||
| ``` | ||
| You'll need [Rockcraft](https://documentation.ubuntu.com/rockcraft/stable/) and [Docker](https://docs.docker.com/). | ||
|
|
||
| Build a docker container via: | ||
| ``` | ||
| docker build -t api_demo_server . | ||
| ``` | ||
| By default, Docker requires `sudo`. To use Docker as a regular user, run `sudo usermod -aG docker $USER` then log out and log back in again. | ||
|
|
||
| Start demo server: | ||
| ``` | ||
| docker run --rm -e DEMO_SERVER_DB_HOST=<postgres container IP> -p 8000:8000 api_demo_server | ||
| ``` | ||
| 1. Build the container image from source: | ||
|
|
||
| # Configuration via environment variables | ||
| You can configure application by applying following environment variables: | ||
| ```text | ||
| rockcraft pack | ||
| ``` | ||
|
|
||
| | Environment Variable | Value | Description | | ||
| |------------------------- |------------------- |------------------------------------------------- | | ||
| | DEMO_SERVER_LOGFILE | \<path/to/log.log> | Path to the file where logs should be written | | ||
| | DEMO_SERVER_DB_HOST | \<Host IP> | IP address of the host where Database is hosted | | ||
| | DEMO_SERVER_DB_PORT | \<Host Port> | Port of the host where Database is hosted | | ||
| | DEMO_SERVER_DB_USER | \<Username> | Username that has access to `names` Database | | ||
| | DEMO_SERVER_DB_PASSWORD | \<Password> | Password to the `DEMO_SERVER_DB_USER` user | | ||
| This creates a `.rock` file. The name of the `.rock` file depends on your system architecture. | ||
|
|
||
| # Publish to registry | ||
| 2. Make the container image available to Docker: | ||
|
|
||
| ```text | ||
| rockcraft.skopeo --insecure-policy \ | ||
| copy "oci-archive:<rock>" "docker-daemon:api-demo-server:integration" | ||
| ``` | ||
|
dwilding marked this conversation as resolved.
|
||
|
|
||
| Where `<rock>` is the path to the `.rock` file. | ||
|
|
||
| 3. Run the container image alongside PostgreSQL: | ||
|
|
||
| ```text | ||
| docker compose up | ||
| ``` | ||
|
|
||
| 4. In a separate terminal, check that the server is available: | ||
|
|
||
| ```text | ||
| curl http://localhost:8000/version | ||
| ``` | ||
|
|
||
| This returns a JSON object that contains the server's version number. | ||
|
|
||
| The server has several other API endpoints, including: | ||
|
|
||
| - API docs - http://localhost:8000/docs | ||
| - Prometheus metrics - http://localhost:8000/metrics | ||
|
|
||
| # Run automated tests | ||
|
|
||
| If you modify the server, rebuild the container image and run the PostgreSQL integration tests: | ||
|
|
||
| ```text | ||
| rockcraft pack | ||
| make integration | ||
| ``` | ||
| docker buildx build -t ghcr.io/canonical/api_demo_server:1.0.4 --platform linux/amd64,linux/arm64,linux/ppc64le --push . | ||
| ``` | ||
|
|
||
| The integration tests require Docker. | ||
|
|
||
| Alternatively, run `rockcraft test`. This builds the container image and runs the integration tests inside a LXD container. It's slower, but you don't need Docker on your system. | ||
|
|
||
| # Server environment variables | ||
|
|
||
| - `DEMO_SERVER_LOGFILE` - Path to the file where logs should be written | ||
| - `DEMO_SERVER_DB_HOST` - IP address of the database host | ||
| - `DEMO_SERVER_DB_PORT` - Port of the database host | ||
| - `DEMO_SERVER_DB_USER` - Username that has access to the database | ||
| - `DEMO_SERVER_DB_PASSWORD` - Password for the `DEMO_SERVER_DB_USER` user | ||
|
|
||
| # Deploy the container image as a Juju charm | ||
|
|
||
| See [From zero to hero: Write your first Kubernetes charm](https://documentation.ubuntu.com/ops/latest/tutorial/from-zero-to-hero-write-your-first-kubernetes-charm/) | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.