Merge ~cjwatson/launchpad:charm-launchpad-admin into launchpad:master

Proposed by Colin Watson
Status: Merged
Approved by: Colin Watson
Approved revision: 43dd651ea33c4cd88cc6c39022b21baa4ec0a86e
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~cjwatson/launchpad:charm-launchpad-admin
Merge into: launchpad:master
Diff against target: 334 lines (+255/-1)
11 files modified
charm/Makefile (+6/-1)
charm/launchpad-admin/README.md (+22/-0)
charm/launchpad-admin/charmcraft.yaml (+58/-0)
charm/launchpad-admin/layer.yaml (+21/-0)
charm/launchpad-admin/metadata.yaml (+21/-0)
charm/launchpad-admin/reactive/launchpad-admin.py (+71/-0)
charm/launchpad-admin/templates/bash_aliases.j2 (+9/-0)
charm/launchpad-admin/templates/db-admin.j2 (+10/-0)
charm/launchpad-admin/templates/db-session.j2 (+10/-0)
charm/launchpad-admin/templates/db.j2 (+10/-0)
charm/launchpad-admin/templates/launchpad-admin-lazr.conf (+17/-0)
Reviewer Review Type Date Requested Status
Guruprasad Approve
Review via email: mp+439044@code.launchpad.net

Commit message

charm: Add a simple launchpad-admin charm

Description of the change

This is useful as a way to get hold of database superuser credentials in a charmed deployment, without having to SSH into a `postgresql` unit directly.

For now this is just for interactive use, but I can think of a couple of natural future extensions: we could do with an action to initialize a new Launchpad database, and it might well make sense to use this charm as a place to run our schema upgrade machinery.

To post a comment you must log in.
Revision history for this message
Guruprasad (lgp171188) wrote :

I don't understand some of the things very well since I haven't seen them in action, but the code changes look good to me. ๐Ÿ‘๐Ÿผ

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/charm/Makefile b/charm/Makefile
index 24ac014..1b97cdd 100644
--- a/charm/Makefile
+++ b/charm/Makefile
@@ -14,7 +14,9 @@ BUILD_LABEL = $(shell git rev-parse HEAD)
14TARBALL = $(APP_NAME).tar.gz14TARBALL = $(APP_NAME).tar.gz
15ASSET = ../build/$(BUILD_LABEL)/$(TARBALL)15ASSET = ../build/$(BUILD_LABEL)/$(TARBALL)
1616
17CHARMS := launchpad-appserver17CHARMS := \
18 launchpad-admin \
19 launchpad-appserver
1820
19all: ## alias to build21all: ## alias to build
20all: build22all: build
@@ -29,6 +31,9 @@ $(BUILDDIR) $(TMPDIR):
29build: ## build all the charms31build: ## build all the charms
30build: $(foreach charm,$(CHARMS),build-$(charm))32build: $(foreach charm,$(CHARMS),build-$(charm))
3133
34build-launchpad-admin: ## build the launchpad-admin charm
35build-launchpad-admin: dist/$(call charm_file,launchpad-admin)
36
32build-launchpad-appserver: ## build the launchpad-appserver charm37build-launchpad-appserver: ## build the launchpad-appserver charm
33build-launchpad-appserver: dist/$(call charm_file,launchpad-appserver)38build-launchpad-appserver: dist/$(call charm_file,launchpad-appserver)
3439
diff --git a/charm/launchpad-admin/README.md b/charm/launchpad-admin/README.md
35new file mode 10064440new file mode 100644
index 0000000..4241d3d
--- /dev/null
+++ b/charm/launchpad-admin/README.md
@@ -0,0 +1,22 @@
1# Launchpad administrative tools
2
3This charm provides administrative tools for use with a Launchpad
4deployment.
5
6You will need the following relations:
7
8 juju relate launchpad-admin:db postgresql:db
9 juju relate launchpad-admin:db-admin postgresql:db
10 juju relate launchpad-admin:session-db postgresql:db
11 juju relate launchpad-admin rabbitmq-server
12
13This will give you an environment you can SSH into for interactive database
14tasks.
15
16The `db` and `db-admin` commands give you `launchpad_main` and
17superuser-level PostgreSQL clients respectively connected to the main
18database, while the `db-session` command gives you a PostgreSQL client
19connected to the session database.
20
21You can run `/srv/launchpad/code/bin/iharness launchpad_main` to get an
22interactive Python harness authenticated as `launchpad_main`.
diff --git a/charm/launchpad-admin/charmcraft.yaml b/charm/launchpad-admin/charmcraft.yaml
0new file mode 10064423new file mode 100644
index 0000000..f831b9f
--- /dev/null
+++ b/charm/launchpad-admin/charmcraft.yaml
@@ -0,0 +1,58 @@
1type: charm
2bases:
3 - build-on:
4 - name: ubuntu
5 channel: "20.04"
6 run-on:
7 - name: ubuntu
8 channel: "20.04"
9parts:
10 charm-wheels:
11 source: https://git.launchpad.net/~ubuntuone-hackers/ols-charm-deps/+git/wheels
12 source-commit: "fe523e25521254c2034eea96e2fde079034b593a"
13 source-submodules: []
14 source-type: git
15 plugin: dump
16 organize:
17 "*": charm-wheels/
18 prime:
19 - "-charm-wheels"
20 ols-layers:
21 source: https://git.launchpad.net/ols-charm-deps
22 source-commit: "c2faacbd6d227b2ae40ee57ab162b28691e88ca6"
23 source-submodules: []
24 source-type: git
25 plugin: dump
26 organize:
27 "*": layers/
28 stage:
29 - layers
30 prime:
31 - "-layers"
32 launchpad-layers:
33 after:
34 - ols-layers
35 source: https://git.launchpad.net/launchpad-layers
36 source-commit: "624df58b5b21a34f8931bba12931e1e3a37ac7b5"
37 source-submodules: []
38 source-type: git
39 plugin: dump
40 organize:
41 launchpad-base: layers/layer/launchpad-base
42 stage:
43 - layers
44 prime:
45 - "-layers"
46 launchpad-admin:
47 after:
48 - charm-wheels
49 - launchpad-layers
50 source: .
51 plugin: reactive
52 build-snaps: [charm/2.x/stable]
53 build-packages: [libpq-dev]
54 build-environment:
55 - CHARM_LAYERS_DIR: $CRAFT_STAGE/layers/layer
56 - CHARM_INTERFACES_DIR: $CRAFT_STAGE/layers/interface
57 - PIP_NO_INDEX: "true"
58 - PIP_FIND_LINKS: $CRAFT_STAGE/charm-wheels
diff --git a/charm/launchpad-admin/layer.yaml b/charm/launchpad-admin/layer.yaml
0new file mode 10064459new file mode 100644
index 0000000..352e6d4
--- /dev/null
+++ b/charm/launchpad-admin/layer.yaml
@@ -0,0 +1,21 @@
1includes:
2 - layer:launchpad-base
3repo: https://git.launchpad.net/launchpad
4options:
5 apt:
6 packages:
7 - postgresql-client
8 ols:
9 # This charm is intended mainly for interactive use, so it's more
10 # convenient to just use the `ubuntu` user.
11 user: ubuntu
12 ols-pg:
13 databases:
14 db:
15 name: launchpad_dev
16 roles: launchpad_main
17 db-admin:
18 name: launchpad_dev
19 session-db:
20 name: session_dev
21 roles: session
diff --git a/charm/launchpad-admin/metadata.yaml b/charm/launchpad-admin/metadata.yaml
0new file mode 10064422new file mode 100644
index 0000000..974315f
--- /dev/null
+++ b/charm/launchpad-admin/metadata.yaml
@@ -0,0 +1,21 @@
1name: launchpad-admin
2display-name: launchpad-admin
3summary: Launchpad administrative tools
4maintainer: Colin Watson <cjwatson@canonical.com>
5description: |
6 Launchpad is an open source suite of tools that help people and teams
7 to work together on software projects.
8
9 This charm provides administrative tools for use with a Launchpad
10 deployment.
11tags:
12 # https://juju.is/docs/charm-metadata#heading--charm-store-fields
13 - network
14series:
15 - focal
16subordinate: false
17requires:
18 db-admin:
19 interface: pgsql
20 session-db:
21 interface: pgsql
diff --git a/charm/launchpad-admin/reactive/launchpad-admin.py b/charm/launchpad-admin/reactive/launchpad-admin.py
0new file mode 10064422new file mode 100644
index 0000000..0a31116
--- /dev/null
+++ b/charm/launchpad-admin/reactive/launchpad-admin.py
@@ -0,0 +1,71 @@
1# Copyright 2023 Canonical Ltd. This software is licensed under the
2# GNU Affero General Public License version 3 (see the file LICENSE).
3
4import os.path
5
6from charmhelpers.core import hookenv, host, templating
7from charms.launchpad.base import (
8 configure_lazr,
9 get_service_config,
10 home_dir,
11 strip_dsn_authentication,
12 update_pgpass,
13)
14from charms.reactive import set_state, when, when_not
15from ols import base, postgres
16from psycopg2.extensions import make_dsn, parse_dsn
17
18
19def strip_password(dsn):
20 parsed_dsn = parse_dsn(dsn)
21 parsed_dsn.pop("password", None)
22 return make_dsn(**parsed_dsn)
23
24
25@when(
26 "launchpad.base.configured",
27 "db.master.available",
28 "db-admin.master.available",
29 "session-db.master.available",
30)
31@when_not("service_configured")
32def configure(db, db_admin, session_db):
33 config = get_service_config()
34 db_primary, _ = postgres.get_db_uris(db)
35 db_admin_primary, _ = postgres.get_db_uris(db_admin)
36 session_db_primary, _ = postgres.get_db_uris(session_db)
37 update_pgpass(db_admin_primary)
38 update_pgpass(session_db_primary)
39 config["db_primary"] = strip_password(db_primary)
40 config["db_admin_primary"] = strip_password(db_admin_primary)
41 config["db_session_primary"] = strip_password(session_db_primary)
42 config["db_session"] = strip_dsn_authentication(session_db_primary)
43 config["db_session_user"] = parse_dsn(session_db_primary)["user"]
44 configure_lazr(
45 config,
46 "launchpad-admin-lazr.conf",
47 "launchpad-admin/launchpad-lazr.conf",
48 )
49 templating.render(
50 "bash_aliases.j2",
51 os.path.join(home_dir(), ".bash_aliases"),
52 config,
53 owner=base.user(),
54 group=base.user(),
55 perms=0o644,
56 )
57 bin_dir = os.path.join(home_dir(), "bin")
58 host.mkdir(bin_dir, owner=base.user(), group=base.user(), perms=0o755)
59 for script in ("db", "db-admin", "db-session"):
60 script_path = os.path.join(bin_dir, script)
61 templating.render(
62 f"{script}.j2",
63 script_path,
64 config,
65 owner=base.user(),
66 group=base.user(),
67 perms=0o755,
68 )
69
70 set_state("service.configured")
71 hookenv.status_set("active", "Ready")
diff --git a/charm/launchpad-admin/templates/bash_aliases.j2 b/charm/launchpad-admin/templates/bash_aliases.j2
0new file mode 10064472new file mode 100644
index 0000000..b5f4133
--- /dev/null
+++ b/charm/launchpad-admin/templates/bash_aliases.j2
@@ -0,0 +1,9 @@
1# Copyright 2023 Canonical Ltd. This software is licensed under the
2# GNU Affero General Public License version 3 (see the file LICENSE).
3
4# Part of the launchpad-admin Juju charm.
5
6# This isn't an alias, but ~/.bash_aliases is conveniently already sourced
7# by the default ~/.bashrc.
8export LPCONFIG=launchpad-admin
9
diff --git a/charm/launchpad-admin/templates/db-admin.j2 b/charm/launchpad-admin/templates/db-admin.j2
0new file mode 10064410new file mode 100644
index 0000000..aa0f73d
--- /dev/null
+++ b/charm/launchpad-admin/templates/db-admin.j2
@@ -0,0 +1,10 @@
1#! /bin/sh
2# Copyright 2023 Canonical Ltd. This software is licensed under the
3# GNU Affero General Public License version 3 (see the file LICENSE).
4
5# Part of the launchpad-admin Juju charm.
6
7set -e
8
9psql '{{ db_admin_primary }}'
10
diff --git a/charm/launchpad-admin/templates/db-session.j2 b/charm/launchpad-admin/templates/db-session.j2
0new file mode 10075511new file mode 100755
index 0000000..4d776ae
--- /dev/null
+++ b/charm/launchpad-admin/templates/db-session.j2
@@ -0,0 +1,10 @@
1#! /bin/sh
2# Copyright 2023 Canonical Ltd. This software is licensed under the
3# GNU Affero General Public License version 3 (see the file LICENSE).
4
5# Part of the launchpad-admin Juju charm.
6
7set -e
8
9psql '{{ db_session_primary }}'
10
diff --git a/charm/launchpad-admin/templates/db.j2 b/charm/launchpad-admin/templates/db.j2
0new file mode 10064411new file mode 100644
index 0000000..f07f7e8
--- /dev/null
+++ b/charm/launchpad-admin/templates/db.j2
@@ -0,0 +1,10 @@
1#! /bin/sh
2# Copyright 2023 Canonical Ltd. This software is licensed under the
3# GNU Affero General Public License version 3 (see the file LICENSE).
4
5# Part of the launchpad-admin Juju charm.
6
7set -e
8
9psql '{{ db_primary }}'
10
diff --git a/charm/launchpad-admin/templates/launchpad-admin-lazr.conf b/charm/launchpad-admin/templates/launchpad-admin-lazr.conf
0new file mode 10064411new file mode 100644
index 0000000..b652e52
--- /dev/null
+++ b/charm/launchpad-admin/templates/launchpad-admin-lazr.conf
@@ -0,0 +1,17 @@
1# Public configuration data. The contents of this file may be freely shared
2# with developers if needed for debugging.
3
4# A schema's sections, keys, and values are automatically inherited, except
5# for '.optional' sections. Update this config to override key values.
6# Values are strings, except for numbers that look like ints. The tokens
7# true, false, and none are treated as True, False, and None.
8
9{% from "macros.j2" import opt -%}
10
11[meta]
12extends: ../launchpad-base-lazr.conf
13
14[launchpad_session]
15database: {{ db_session }}
16dbuser: {{ db_session_user }}
17

Subscribers

People subscribed via source and target branches

to status/vote changes: