Skip to content
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

Snap ratings #12

Merged
merged 15 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .github/workflows/build-charm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,43 @@ jobs:
run: |
cd vm_operator
tox -e unit

functional-test:
name: Functional tests
runs-on: ubuntu-latest
needs:
- unit-test
- lint
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install dependencies
run: python -m pip install tox
- name: Setup LXD
uses: canonical/setup-lxd@4e959f8e0d9c5feb27d44c5e4d9a330a782edee0
- name: Run tests
run: |
cd vm_operator
tox -e functional

integration-test:
name: Integration tests
runs-on: ubuntu-22.04
needs:
- lint
- unit-test
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup hosts for tests
run: |
echo "10.64.140.43 testing-ratings.foo.bar" | sudo tee -a /etc/hosts
- name: Setup operator environment
uses: charmed-kubernetes/actions-operator@main
with:
provider: lxd
juju-channel: 3.2/stable
- name: Run integration tests
run: |
cd vm_operator
tox -e integration -- --model=testing
35 changes: 35 additions & 0 deletions snap/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# App Center Ratings Snap

This directory contains files used to build the App Center Ratings snap.

## Installation

The snap provides a wrapper used to collect environment variables via `snapctl` in order for them to be configurable post the snap being built.

You can install the App Center Ratings snap manually like so:

```bash
# Install the snap
$ sudo snap install ratings --channel stable

# Update an environment variable
sudo snap set ratings app-log-level=debug

# Restart the service to reload environment variables into the service
sudo snap restart ratings
```

## Configuration

This Snap is intended to run within a charm where certain configuration settings are unknown until the charm installs the snap and establishes relations to a database.

The list of config options are:

| Name | Options | Default | Description |
|:----------------------------|:--------------------------------|:---------------------------------------------------------------|:------------------------|
| `app-jwt-secret` | Any string | `deadbeaf` | JWT secret |
matthew-hagemann marked this conversation as resolved.
Show resolved Hide resolved
| `app-log-level` | `error`, `warn`, `info`, `debug` | `info` | Log level |
| `app-postgres-uri` | Any string | `postgresql://migration_user:strongpassword@localhost:5433/ratings` | Service connection |
matthew-hagemann marked this conversation as resolved.
Show resolved Hide resolved
| `app-migration-postgres-uri`| Any string | `postgresql://migration_user:strongpassword@localhost:5433/ratings` | Migrator connection |
matthew-hagemann marked this conversation as resolved.
Show resolved Hide resolved

Config options can be set using: `sudo snap set ratings <option>=<value>`
28 changes: 28 additions & 0 deletions snap/hooks/configure
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/sh

set -eu

app_jwt_secret="$(snapctl get app-jwt-secret)"
if [ -z "$app_jwt_secret" ]; then
snapctl set app-jwt-secret=deadbeef
matthew-hagemann marked this conversation as resolved.
Show resolved Hide resolved
fi

app_log_level="$(snapctl get app-log-level)"
if [ -z "$app_log_level" ]; then
snapctl set app-log-level=info
fi

app_postgres_uri="$(snapctl get app-postgres-uri)"
if [ -z "$app_postgres_uri" ]; then
snapctl set app-postgres-uri=postgresql://migration_user:strongpassword@localhost:5433/ratings
fi
matthew-hagemann marked this conversation as resolved.
Show resolved Hide resolved

app_migration_postgres_uri="$(snapctl get app-migration-postgres-uri)"
if [ -z "$app_migration_postgres_uri" ]; then
snapctl set app-migration-postgres-uri=postgresql://migration_user:strongpassword@localhost:5433/ratings
fi
matthew-hagemann marked this conversation as resolved.
Show resolved Hide resolved

app_env="$(snapctl get app-env)"
if [ -z "$app_env" ]; then
snapctl set app-env=dev
fi
9 changes: 9 additions & 0 deletions snap/local/ratings_wrapper
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh

export APP_JWT_SECRET=$(snapctl get app-jwt-secret)
matthew-hagemann marked this conversation as resolved.
Show resolved Hide resolved
export APP_LOG_LEVEL=$(snapctl get app-log-level)
export APP_POSTGRES_URI=$(snapctl get app-postgres-uri)
export APP_MIGRATION_POSTGRES_URI=$(snapctl get app-migration-postgres-uri)
export APP_ENV=$(snapctl get app-env)

exec $SNAP/bin/ratings
53 changes: 53 additions & 0 deletions snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: ratings
base: core22
version: '1.4'
license: GPL-3.0
summary: Ubuntu App Ratings Service
description: |
Backend service to support application ratings in the new Ubuntu Software Centre.

grade: stable
confinement: strict

apps:
ratings-svc:
command: ratings_wrapper
matthew-hagemann marked this conversation as resolved.
Show resolved Hide resolved
daemon: simple
matthew-hagemann marked this conversation as resolved.
Show resolved Hide resolved
plugs:
- network
- network-bind

environment:
APP_HOST: 0.0.0.0
APP_NAME: ratings
APP_PORT: 443

parts:
rust-deps:
plugin: nil
override-pull: |
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --profile minimal

ratings:
after: [ rust-deps ]
plugin: rust
build-packages:
- libssl-dev
- pkg-config
build-snaps:
- protobuf
source: .

migrations:
plugin: dump
source: ./sql

wrapper:
plugin: dump
source: ./snap/local
source-type: local
override-build: |
# Copy the wrapper into place
cp $SNAPCRAFT_PROJECT_DIR/snap/local/ratings_wrapper $SNAPCRAFT_PART_INSTALL/

chmod 0755 $SNAPCRAFT_PART_INSTALL/ratings_wrapper
matthew-hagemann marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 9 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use std::env;

use tracing::info;

mod app;
mod features;
mod utils;
Expand All @@ -11,8 +15,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.with_span_events(tracing_subscriber::fmt::format::FmtSpan::CLOSE)
.init();

tracing::info!("Starting Ubuntu App Rating Service");
tracing::info!("Starting THE Ubuntu App Rating Service");
matthew-hagemann marked this conversation as resolved.
Show resolved Hide resolved

match env::current_dir() {
Ok(cur_dir) => info!("Current directory: {}", cur_dir.display()),
Err(e) => info!("Error retrieving current directory: {:?}", e),
}
app::run(config).await?;

Ok(())
Expand Down
14 changes: 13 additions & 1 deletion src/utils/migrator.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::env;
use std::error::Error;
use std::fmt::{Debug, Formatter};
use std::sync::Arc;

use sqlx::{postgres::PgPoolOptions, PgPool};
use tracing::info;

const MIGRATIONS_PATH: &str = "./sql/migrations";

Expand All @@ -19,8 +21,18 @@ impl Migrator {
Ok(Migrator { pool })
}

fn migrations_path() -> String {
let snap_path = std::env::var("SNAP").unwrap_or("./sql".to_string());
format!("{}/migrations", snap_path)
}

pub async fn run(&self) -> Result<(), sqlx::Error> {
let m = sqlx::migrate::Migrator::new(std::path::Path::new(MIGRATIONS_PATH)).await?;
match env::current_dir() {
Ok(cur_dir) => info!("Current directory: {}", cur_dir.display()),
Err(e) => info!("Error retrieving current directory: {:?}", e),
}
let m =
sqlx::migrate::Migrator::new(std::path::Path::new(&Self::migrations_path())).await?;

m.run(&mut self.pool.acquire().await?).await?;
Ok(())
Expand Down
1 change: 1 addition & 0 deletions vm_operator/charmcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ parts:
charm:
charm-binary-python-packages:
- psycopg[binary]
- PyYAML
9 changes: 3 additions & 6 deletions vm_operator/config.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
options:
app-env:
env:
type: string
default: "dev"
description: "Application environment."
app-log-level:
log-level:
type: string
default: "info"
matthew-hagemann marked this conversation as resolved.
Show resolved Hide resolved
app-repo:
type: string
default: "https://github.com/matthew-hagemann/app-center-ratings"
squid-proxy-url:
type: string
default: "http://proxy.example.com"
default: ""
matthew-hagemann marked this conversation as resolved.
Show resolved Hide resolved
Loading
Loading