From 8f6d766383b2b46bcb0bb4f4cd866a42d862447a Mon Sep 17 00:00:00 2001 From: notplants Date: Wed, 3 Mar 2021 18:54:11 +0100 Subject: [PATCH] Add script to build peach-go-sbot debian package --- .gitignore | 3 +- README.md | 7 ++ conf/templates/peach_go_sbot/DEBIAN/control | 5 + conf/templates/peach_go_sbot/DEBIAN/postinst | 39 +++++++ conf/templates/peach_go_sbot/DEBIAN/postrm | 21 ++++ conf/templates/peach_go_sbot/DEBIAN/prerm | 7 ++ .../peach_go_sbot/peach-go-sbot.service | 11 ++ scripts/build_peach_go_sbot.py | 107 ++++++++++++++++++ scripts/constants.py | 3 + 9 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 conf/templates/peach_go_sbot/DEBIAN/control create mode 100755 conf/templates/peach_go_sbot/DEBIAN/postinst create mode 100755 conf/templates/peach_go_sbot/DEBIAN/postrm create mode 100755 conf/templates/peach_go_sbot/DEBIAN/prerm create mode 100644 conf/templates/peach_go_sbot/peach-go-sbot.service create mode 100644 scripts/build_peach_go_sbot.py diff --git a/.gitignore b/.gitignore index fbe14c3..d454fa6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ deploy.sh secret* secret_files* ssh.sh -notes.txt \ No newline at end of file +notes.txt +*__pycache__* \ No newline at end of file diff --git a/README.md b/README.md index a0f08f0..d63c12c 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,13 @@ python3 -u scripts/build_packages.py Freight supports the ability to have multiple versions of a package in a single Debian package archive. If a particular version of a package already exists in the Freight library, it will not be readded or overwritten. +## Build peach-go-sbot debian package + +`sudo python3 scripts/build_peach_go_sbot.py -v ` + +This build the peach-go-sbot package using the latest code from go-ssb, along with a systemd unit file, +and adds the debian package to the freight library. + ## Install Packages from Debian Package Archive To add the PeachCloud Debian package archive as an apt source, run the following commands from your Pi: diff --git a/conf/templates/peach_go_sbot/DEBIAN/control b/conf/templates/peach_go_sbot/DEBIAN/control new file mode 100644 index 0000000..0a54d94 --- /dev/null +++ b/conf/templates/peach_go_sbot/DEBIAN/control @@ -0,0 +1,5 @@ +Package: peach-go-sbot +Version: {{version}} +Architecture: all +Maintainer: Andrew Reid +Description: debian package for go-ssb-server on arm64 diff --git a/conf/templates/peach_go_sbot/DEBIAN/postinst b/conf/templates/peach_go_sbot/DEBIAN/postinst new file mode 100755 index 0000000..23c9dcf --- /dev/null +++ b/conf/templates/peach_go_sbot/DEBIAN/postinst @@ -0,0 +1,39 @@ +#!/bin/sh +set -e + +# create user which go-sbot runs as +adduser --quiet --system peach-go-sbot + +# set permissions +chown peach-go-sbot /usr/bin/go-sbot + +# Automatically added by cargo-deb +if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then + # This will only remove masks created by d-s-h on package removal. + deb-systemd-helper unmask peach-go-sbot.service >/dev/null || true + + # was-enabled defaults to true, so new installations run enable. + if deb-systemd-helper --quiet was-enabled peach-go-sbot.service; then + # Enables the unit on first installation, creates new + # symlinks on upgrades if the unit file has changed. + deb-systemd-helper enable peach-go-sbot.service >/dev/null || true + else + # Update the statefile to add new symlinks (if any), which need to be + # cleaned up on purge. Also remove old symlinks. + deb-systemd-helper update-state peach-go-sbot.service >/dev/null || true + fi +fi +# End automatically added section +# Automatically added by cargo-deb +if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then + if [ -d /run/systemd/system ]; then + systemctl --system daemon-reload >/dev/null || true + if [ -n "$2" ]; then + _dh_action=restart + else + _dh_action=start + fi + deb-systemd-invoke $_dh_action peach-go-sbot.service >/dev/null || true + fi +fi +# End automatically added section \ No newline at end of file diff --git a/conf/templates/peach_go_sbot/DEBIAN/postrm b/conf/templates/peach_go_sbot/DEBIAN/postrm new file mode 100755 index 0000000..b747e03 --- /dev/null +++ b/conf/templates/peach_go_sbot/DEBIAN/postrm @@ -0,0 +1,21 @@ +#!/bin/sh +set -e +# Automatically added by cargo-deb +if [ -d /run/systemd/system ]; then + systemctl --system daemon-reload >/dev/null || true +fi +# End automatically added section +# Automatically added by cargo-deb +if [ "$1" = "remove" ]; then + if [ -x "/usr/bin/deb-systemd-helper" ]; then + deb-systemd-helper mask peach-go-sbot.service >/dev/null || true + fi +fi + +if [ "$1" = "purge" ]; then + if [ -x "/usr/bin/deb-systemd-helper" ]; then + deb-systemd-helper purge peach-go-sbot.service >/dev/null || true + deb-systemd-helper unmask peach-go-sbot.service >/dev/null || true + fi +fi +# End automatically added section \ No newline at end of file diff --git a/conf/templates/peach_go_sbot/DEBIAN/prerm b/conf/templates/peach_go_sbot/DEBIAN/prerm new file mode 100755 index 0000000..7dad053 --- /dev/null +++ b/conf/templates/peach_go_sbot/DEBIAN/prerm @@ -0,0 +1,7 @@ +#!/bin/sh +set -e +# Automatically added by cargo-deb +if [ -d /run/systemd/system ] && [ "$1" = remove ]; then + deb-systemd-invoke stop peach-go-sbot.service >/dev/null || true +fi +# End automatically added section \ No newline at end of file diff --git a/conf/templates/peach_go_sbot/peach-go-sbot.service b/conf/templates/peach_go_sbot/peach-go-sbot.service new file mode 100644 index 0000000..ad396f1 --- /dev/null +++ b/conf/templates/peach_go_sbot/peach-go-sbot.service @@ -0,0 +1,11 @@ +[Unit] +Description=peachs go-sbot + +[Service] +Type=simple +User=peach-go-sbot +ExecStart=/usr/bin/go-sbot +Restart=always + +[Install] +WantedBy=multi-user.target diff --git a/scripts/build_peach_go_sbot.py b/scripts/build_peach_go_sbot.py new file mode 100644 index 0000000..9660553 --- /dev/null +++ b/scripts/build_peach_go_sbot.py @@ -0,0 +1,107 @@ +#!/usr/bin/env python3 +""" +script to create debian packages for cross-compiled go binaries for go-sbot +based off of this post +https://unix.stackexchange.com/questions/627689/how-to-create-a-debian-package-from-a-bash-script-and-a-systemd-service +""" + + +from constants import * +from utils import render_template + +import subprocess +import argparse +import sys +import os +import shutil + +DEB_CONF_DIR = os.path.join(PROJECT_PATH, 'conf/templates/peach_go_sbot') +DEB_BUILD_DIR = "/tmp/peach_go_sbot" +GO_SSB_DIR = "/srv/peachcloud/automation/go-ssb" + + +def crosscompile_peach_go_sbot(): + subprocess.check_call(["git", "pull"], cwd=GO_SSB_DIR) + # TODO: confirm that version number in the repo matches the version number we set for the package we are building + print("[CROSS-COMPILING sbotcli]") + subprocess.check_call(["env", "GOOS=linux", "GOARCH=arm64", "go", "build", "./cmd/sbotcli"], cwd=GO_SSB_DIR) + print("[CROSS-COMPILING go-sbot]") + subprocess.check_call(["env", "GOOS=linux", "GOARCH=arm64", "go", "build", "./cmd/go-sbot"], cwd=GO_SSB_DIR) + +def package_peach_go_sbot(version): + + print("[ PACKAGING peach-go-sbot ]") + # copy debian conf files into correct locations in package build directory + DEBIAN_SRC_DIR = os.path.join(DEB_CONF_DIR, 'DEBIAN') + DEBIAN_DEST_DIR = os.path.join(DEB_BUILD_DIR, 'DEBIAN') + os.makedirs(DEBIAN_DEST_DIR) + maintainer_scripts = ['postinst', 'postrm', 'prerm'] + for script in maintainer_scripts: + src = os.path.join(DEBIAN_SRC_DIR, script) + dest = os.path.join(DEBIAN_DEST_DIR, script) + shutil.copyfile(src, dest) + subprocess.check_call(["chmod", "775", dest]) + # copy control file putting in correct version number + src = os.path.join("peach_go_sbot/DEBIAN/control") + dest = os.path.join(DEBIAN_DEST_DIR, "control") + render_template(src=src, dest=dest, template_vars={"version": version}) + + # copy systemd service file + SERVICE_DIR = os.path.join(DEB_BUILD_DIR, 'lib/systemd/system') + os.makedirs(SERVICE_DIR) + shutil.copyfile( + os.path.join(DEB_CONF_DIR, 'peach-go-sbot.service'), + os.path.join(SERVICE_DIR, 'peach-go-sbot.service') + ) + + # copy cross-compiled binaries + GO_BINARIES = ['go-sbot', 'sbotcli'] + BIN_DIR = os.path.join(DEB_BUILD_DIR, 'usr/bin') + os.makedirs(BIN_DIR) + for go_binary in GO_BINARIES: + destination = os.path.join(BIN_DIR, go_binary) + shutil.copyfile( + os.path.join(os.path.join(GO_SSB_DIR), go_binary), + destination + ) + subprocess.check_call(["chmod", "770", destination]) + + # create deb package + deb_file_name = "peach-go-sbot_{}_arm64.deb".format(version) + print("[ CREATING {}]".format(deb_file_name)) + subprocess.check_call(["dpkg-deb", "-b", ".", deb_file_name], cwd=DEB_BUILD_DIR) + + # add deb package to freight + print("[ ADDING PACKAGE TO FREIGHT ]") + deb_path = os.path.join(DEB_BUILD_DIR, deb_file_name) + subprocess.check_call(["freight", "add", "-c", FREIGHT_CONF, deb_path, "apt/buster"]) + print("[ ADDING PACKAGE TO FREIGHT CACHE ]") + subprocess.call(["sudo", "freight", "cache", "-g", + GPG_KEY_EMAIL, "-p", GPG_KEY_PASS_FILE]) + + +def build_peach_go_sbot(): + + parser = argparse.ArgumentParser() + parser.add_argument( + "-v", + "--version", + help="Set version number for go-sbot", + ) + args = parser.parse_args() + + print("[ BUILDING PEACH-GO-SBOT VERSION {}]".format(args.version)) + + # delete build directory if it already exists or create it + subprocess.check_call(["rm", "-rf", DEB_BUILD_DIR]) + if not os.path.exists(DEB_BUILD_DIR): + os.makedirs(DEB_BUILD_DIR) + + crosscompile_peach_go_sbot() + package_peach_go_sbot(version=args.version) + + + +if __name__ == '__main__': + build_peach_go_sbot() + diff --git a/scripts/constants.py b/scripts/constants.py index 4978b3d..e822feb 100644 --- a/scripts/constants.py +++ b/scripts/constants.py @@ -1,6 +1,9 @@ # constants used by build and setup scripts import os +# path to project directory +PROJECT_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + # before running this script run `gpg --gen-key` on the server # assign the email address of the key id here: GPG_KEY_EMAIL = "andrew@mycelial.technology"