Merge ~cjwatson/turnip:publish-tarball into turnip:master

Proposed by Colin Watson
Status: Merged
Approved by: Colin Watson
Approved revision: db46bbf6d8241aa96537416c5202b2ac86d38eb5
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~cjwatson/turnip:publish-tarball
Merge into: turnip:master
Diff against target: 178 lines (+138/-2)
4 files modified
Makefile (+13/-1)
dependencies-devel.txt (+1/-0)
ols-vms.conf (+1/-1)
publish-to-swift (+123/-0)
Reviewer Review Type Date Requested Status
Ioana Lasc (community) Approve
Review via email: mp+385657@code.launchpad.net

Commit message

Add a "make publish-tarball" target

Description of the change

This will help us to write a Jenkins job that builds a deployment artifact and publishes it to Swift for use by the deployment machinery.

This is mostly copied from lp-signing.

To post a comment you must log in.
Revision history for this message
Ioana Lasc (ilasc) wrote :

looks good

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/Makefile b/Makefile
2index 9e6bb12..ad76c8f 100644
3--- a/Makefile
4+++ b/Makefile
5@@ -27,6 +27,11 @@ TARBALL_BUILDS_DIR ?= build
6 TARBALL_BUILD_DIR = $(TARBALL_BUILDS_DIR)/$(TARBALL_BUILD_LABEL)
7 TARBALL_BUILD_PATH = $(TARBALL_BUILD_DIR)/$(TARBALL_FILE_NAME)
8
9+SWIFT_CONTAINER_NAME ?= turnip-builds
10+# This must match the object path used by install_payload in the turnip-base
11+# charm layer.
12+SWIFT_OBJECT_PATH = turnip-builds/$(TARBALL_BUILD_LABEL)/$(TARBALL_FILE_NAME)
13+
14 build: $(ENV)
15
16 bootstrap:
17@@ -113,4 +118,11 @@ build-tarball:
18 --exclude env \
19 ./
20
21-.PHONY: build check clean dist lint run-api run-pack test build-tarball
22+publish-tarball: build-tarball
23+ [ ! -e ~/.config/swift/turnip ] || . ~/.config/swift/turnip; \
24+ ./publish-to-swift --debug \
25+ $(SWIFT_CONTAINER_NAME) $(SWIFT_OBJECT_PATH) \
26+ $(TARBALL_BUILD_PATH)
27+
28+.PHONY: build check clean dist lint run-api run-pack test
29+.PHONY: build-tarball publish-tarball
30diff --git a/dependencies-devel.txt b/dependencies-devel.txt
31new file mode 100644
32index 0000000..e3bee0e
33--- /dev/null
34+++ b/dependencies-devel.txt
35@@ -0,0 +1 @@
36+python3-swiftclient
37diff --git a/ols-vms.conf b/ols-vms.conf
38index 8d1324f..d7f8d17 100644
39--- a/ols-vms.conf
40+++ b/ols-vms.conf
41@@ -4,7 +4,7 @@ vm.release = bionic
42
43 # pygit2
44 apt.sources = ppa:launchpad/ppa
45-vm.packages = @system-dependencies.txt, @charm/packages.txt
46+vm.packages = @system-dependencies.txt, @dependencies-devel.txt, @charm/packages.txt
47
48 [turnip]
49 vm.class = lxd
50diff --git a/publish-to-swift b/publish-to-swift
51new file mode 100755
52index 0000000..e5cfd59
53--- /dev/null
54+++ b/publish-to-swift
55@@ -0,0 +1,123 @@
56+#! /usr/bin/python3
57+
58+"""Publish a built tarball to Swift for deployment."""
59+
60+from argparse import ArgumentParser
61+import os
62+import re
63+import subprocess
64+import sys
65+
66+
67+def ensure_container_privs(container_name):
68+ """Ensure that the container exists and is world-readable.
69+
70+ This allows us to give services suitable credentials for getting the
71+ built code from a container.
72+ """
73+ subprocess.run(["swift", "post", container_name, "--read-acl", ".r:*"])
74+
75+
76+def get_swift_storage_url():
77+ # This is a bit cumbersome, but probably still easier than bothering
78+ # with swiftclient.
79+ auth = subprocess.run(
80+ ["swift", "auth"],
81+ stdout=subprocess.PIPE, check=True,
82+ universal_newlines=True).stdout.splitlines()
83+ return [
84+ line.split("=", 1)[1] for line in auth
85+ if line.startswith("export OS_STORAGE_URL=")][0]
86+
87+
88+def publish_file_to_swift(container_name, object_path, local_path,
89+ overwrite=True):
90+ """Publish a file to a Swift container."""
91+ storage_url = get_swift_storage_url()
92+
93+ already_published = False
94+ # Some swift versions unhelpfully exit 0 regardless of whether the
95+ # object exists.
96+ try:
97+ stats = subprocess.run(
98+ ["swift", "stat", container_name, object_path],
99+ stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, check=True,
100+ universal_newlines=True).stdout
101+ if re.search(
102+ r"Object: %s$" % re.escape(object_path), stats, flags=re.M):
103+ already_published = True
104+ except subprocess.CalledProcessError:
105+ pass
106+
107+ if already_published:
108+ print("Object {} already published to {}.".format(
109+ object_path, container_name))
110+ if not overwrite:
111+ return
112+
113+ print("Publishing {} to {} as {}.".format(
114+ local_path, container_name, object_path))
115+ try:
116+ subprocess.run(
117+ ["swift", "upload", "--object-name", object_path,
118+ container_name, local_path])
119+ except subprocess.CalledProcessError:
120+ sys.exit("Failed to upload {} to {} as {}".format(
121+ local_path, container_name, object_path))
122+
123+ print("Published file: {}/{}/{}".format(
124+ storage_url, container_name, object_path))
125+
126+
127+def main():
128+ parser = ArgumentParser()
129+ parser.add_argument("--debug", action="store_true", default=False)
130+ parser.add_argument("container_name")
131+ parser.add_argument("swift_object_path")
132+ parser.add_argument("local_path")
133+ args = parser.parse_args()
134+
135+ if args.debug:
136+ # Print OpenStack-related environment variables for ease of
137+ # debugging. Only OS_AUTH_TOKEN and OS_PASSWORD currently seem to
138+ # be secret, but for safety we only show unredacted contents of
139+ # variables specifically known to be safe. See "swift --os-help"
140+ # for most of these.
141+ safe_keys = {
142+ "OS_AUTH_URL",
143+ "OS_AUTH_VERSION",
144+ "OS_CACERT",
145+ "OS_CERT",
146+ "OS_ENDPOINT_TYPE",
147+ "OS_IDENTITY_API_VERSION",
148+ "OS_INTERFACE",
149+ "OS_KEY",
150+ "OS_PROJECT_DOMAIN_ID",
151+ "OS_PROJECT_DOMAIN_NAME",
152+ "OS_PROJECT_ID",
153+ "OS_PROJECT_NAME",
154+ "OS_REGION_NAME",
155+ "OS_SERVICE_TYPE",
156+ "OS_STORAGE_URL",
157+ "OS_TENANT_ID",
158+ "OS_TENANT_NAME",
159+ "OS_USERNAME",
160+ "OS_USER_DOMAIN_ID",
161+ "OS_USER_DOMAIN_NAME",
162+ "OS_USER_ID",
163+ }
164+ for key, value in sorted(os.environ.items()):
165+ if key.startswith("OS_"):
166+ if key not in safe_keys:
167+ value = "<redacted>"
168+ print("{}: {}".format(key, value))
169+
170+ overwrite = "FORCE_REBUILD" in os.environ
171+ ensure_container_privs(args.container_name)
172+ publish_file_to_swift(
173+ args.container_name, args.swift_object_path, args.local_path,
174+ overwrite=overwrite)
175+
176+
177+if __name__ == "__main__":
178+ main()

Subscribers

People subscribed via source and target branches