From 62d791f29ab0da5bb989540659911d2d47952ed3 Mon Sep 17 00:00:00 2001 From: notplants Date: Fri, 18 Jun 2021 13:30:31 -0400 Subject: [PATCH] Cross compile go-ssb-room --- .../peach_go_ssb_room/DEBIAN/control | 5 + .../peach_go_ssb_room/DEBIAN/postinst | 39 +++++ .../templates/peach_go_ssb_room/DEBIAN/postrm | 21 +++ conf/templates/peach_go_ssb_room/DEBIAN/prerm | 7 + .../peach-go-ssb-room.service | 12 ++ .../build_peach_go_packages.py | 150 ++++++++++++++++++ peach_package_builder/build_peach_go_sbot.py | 104 ------------ peach_package_builder/build_rust_packages.py | 5 + 8 files changed, 239 insertions(+), 104 deletions(-) create mode 100644 conf/templates/peach_go_ssb_room/DEBIAN/control create mode 100755 conf/templates/peach_go_ssb_room/DEBIAN/postinst create mode 100755 conf/templates/peach_go_ssb_room/DEBIAN/postrm create mode 100755 conf/templates/peach_go_ssb_room/DEBIAN/prerm create mode 100644 conf/templates/peach_go_ssb_room/peach-go-ssb-room.service create mode 100644 peach_package_builder/build_peach_go_packages.py delete mode 100644 peach_package_builder/build_peach_go_sbot.py diff --git a/conf/templates/peach_go_ssb_room/DEBIAN/control b/conf/templates/peach_go_ssb_room/DEBIAN/control new file mode 100644 index 0000000..10767a2 --- /dev/null +++ b/conf/templates/peach_go_ssb_room/DEBIAN/control @@ -0,0 +1,5 @@ +Package: peach-go-ssb-room +Version: {{version}} +Architecture: all +Maintainer: Andrew Reid +Description: debian package for go-ssb-room on arm64 diff --git a/conf/templates/peach_go_ssb_room/DEBIAN/postinst b/conf/templates/peach_go_ssb_room/DEBIAN/postinst new file mode 100755 index 0000000..439b116 --- /dev/null +++ b/conf/templates/peach_go_ssb_room/DEBIAN/postinst @@ -0,0 +1,39 @@ +#!/bin/sh +set -e + +# create user which go-ssb-room runs as +adduser --quiet --system peach-go-ssb-room + +# set permissions +chown peach-go-ssb-room /usr/bin/go-ssb-room-server + +# 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-ssb-room.service >/dev/null || true + + # was-enabled defaults to true, so new installations run enable. + if deb-systemd-helper --quiet was-enabled peach-go-ssb-room.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-ssb-room.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-ssb-room.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-ssb-room.service >/dev/null || true + fi +fi +# End automatically added section \ No newline at end of file diff --git a/conf/templates/peach_go_ssb_room/DEBIAN/postrm b/conf/templates/peach_go_ssb_room/DEBIAN/postrm new file mode 100755 index 0000000..b747e03 --- /dev/null +++ b/conf/templates/peach_go_ssb_room/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_ssb_room/DEBIAN/prerm b/conf/templates/peach_go_ssb_room/DEBIAN/prerm new file mode 100755 index 0000000..7dad053 --- /dev/null +++ b/conf/templates/peach_go_ssb_room/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_ssb_room/peach-go-ssb-room.service b/conf/templates/peach_go_ssb_room/peach-go-ssb-room.service new file mode 100644 index 0000000..66f310c --- /dev/null +++ b/conf/templates/peach_go_ssb_room/peach-go-ssb-room.service @@ -0,0 +1,12 @@ +[Unit] +Description=peachs go-sbot + +[Service] +Type=simple +User=peach-go-ssb-room +Environment="GO_HTTPS_DOMAIN=littleorchard.dyn.peachcloud.org" +ExecStart=/usr/bin/go-ssb-room-server -https-domain "$GO_HTTPS_DOMAIN" -lishttp ":3001" -lismux ":8009" +Restart=always + +[Install] +WantedBy=multi-user.target diff --git a/peach_package_builder/build_peach_go_packages.py b/peach_package_builder/build_peach_go_packages.py new file mode 100644 index 0000000..04bd1a4 --- /dev/null +++ b/peach_package_builder/build_peach_go_packages.py @@ -0,0 +1,150 @@ +""" +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 +""" +import subprocess +import argparse +import re +import shutil +import sys +from packaging import version as pversion + +from peach_package_builder.constants import * +from peach_package_builder.utils import render_template, add_deb_to_freight, update_freight_cache + +# manually update this version when we want to build a new peach-go-sbot package +PEACH_GO_SBOT_VERSION = '0.1.5' +PEACH_GO_SSB_ROOM_VERSION = '0.1.7' + + +def crosscompile_peach_go_sbot(src_dir): + subprocess.check_call(["git", "pull"], cwd=src_dir) + print("[CROSS-COMPILING sbotcli]") + subprocess.check_call(["env", "GOOS=linux", "GOARCH=arm64", "go", "build", "./cmd/sbotcli"], cwd=src_dir) + print("[CROSS-COMPILING go-sbot]") + subprocess.check_call(["env", "GOOS=linux", "GOARCH=arm64", "go", "build", "./cmd/go-sbot"], cwd=src_dir) + + +def crosscompile_peach_go_ssb_room(src_dir): + subprocess.check_call(["git", "pull"], cwd=src_dir) + print("[CROSS-COMPILING go-ssb-room/server]") + subprocess.check_call(["env", "CGO_ENABLED=1", "CC=aarch64-linux-gnu-gcc", + "CC_FOR_TARGET=gcc-aarch64-linux-gnu", "GOOS=linux", + "GOARCH=arm64", "go", "build", "./cmd/server"], cwd=src_dir) + + +def package_go_package(src_dir, package_name, deb_conf_dir, build_dir, go_binary_names, version): + print("[ PACKAGING {} ]".format(package_name)) + # 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(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(package_name.replace("-", "_"), "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(build_dir, 'lib/systemd/system') + os.makedirs(SERVICE_DIR) + shutil.copyfile( + os.path.join(deb_conf_dir, '{}.service'.format(package_name)), + os.path.join(SERVICE_DIR, '{}.service'.format(package_name)) + ) + + # copy cross-compiled binaries + BIN_DIR = os.path.join(build_dir, 'usr/bin') + os.makedirs(BIN_DIR) + for go_binary_src, go_binary_dest in go_binary_names: + destination = os.path.join(BIN_DIR, go_binary_dest) + shutil.copyfile( + os.path.join(os.path.join(src_dir), go_binary_src), + destination + ) + subprocess.check_call(["chmod", "770", destination]) + + # create deb package + deb_file_name = "{}_{}_arm64.deb".format(package_name, version) + print("[ CREATING {}]".format(deb_file_name)) + subprocess.check_call(["dpkg-deb", "-b", ".", deb_file_name], cwd=build_dir) + + # copy deb package to MICROSERVICES_DEB_DIR + deb_path = os.path.join(build_dir, deb_file_name) + subprocess.check_call(["cp", deb_path, MICROSERVICES_DEB_DIR]) + + # add deb package to freight + add_deb_to_freight(package_name=deb_file_name, package_path=deb_path) + + # update freight cache + update_freight_cache() + + +def build_peach_go_sbot(): + + # constants + DEB_CONF_DIR = os.path.join(PROJECT_PATH, 'conf/templates/peach_go_sbot') + DEB_BUILD_DIR = "/tmp/peach_go_sbot" + # GO_BINARIES is a list of tuples of src_name and dest_name, + # which will be callable via /usr/bin/dest_name after installation + GO_BINARIES = [('go-sbot', 'go-sbot'), ('sbotcli', 'sbotcli')] + SRC_DIR = "/srv/peachcloud/automation/go-ssb" + + # gets the most recently built peach_go_sbot version, and increments the micro-number by 1 + version = PEACH_GO_SBOT_VERSION + print("[ BUILDING PEACH-GO-SBOT VERSION {}]".format(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) + + # cross-compile and package peach-go-sbot with new version number + crosscompile_peach_go_sbot(src_dir=SRC_DIR) + package_go_package(src_dir=SRC_DIR, + package_name="peach-go-sbot", + deb_conf_dir=DEB_CONF_DIR, + build_dir=DEB_BUILD_DIR, + go_binary_names=GO_BINARIES, + version=version) + + +def build_peach_go_ssb_room(): + + # constants + DEB_CONF_DIR = os.path.join(PROJECT_PATH, 'conf/templates/peach_go_ssb_room') + DEB_BUILD_DIR = "/tmp/peach_go_ssb_room" + # GO_BINARIES is a list of tuples of src_name and dest_name, + # which will be callable via /usr/bin/dest_name after installation + GO_BINARIES = [('server', 'go-ssb-room-server')] + SRC_DIR = "/srv/peachcloud/automation/go-ssb-room" + + # gets the most recently built peach_go_sbot version, and increments the micro-number by 1 + version = PEACH_GO_SSB_ROOM_VERSION + print("[ BUILDING PEACH-GO-SSB-ROOM VERSION {}]".format(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) + + # cross-compile and package peach-go-sbot with new version number + crosscompile_peach_go_ssb_room(src_dir=SRC_DIR) + package_go_package(src_dir=SRC_DIR, + package_name="peach-go-ssb-room", + deb_conf_dir=DEB_CONF_DIR, + build_dir=DEB_BUILD_DIR, + go_binary_names=GO_BINARIES, + version=version) + + +if __name__ == '__main__': + # build_peach_go_sbot() + build_peach_go_ssb_room() + diff --git a/peach_package_builder/build_peach_go_sbot.py b/peach_package_builder/build_peach_go_sbot.py deleted file mode 100644 index 3cb3fed..0000000 --- a/peach_package_builder/build_peach_go_sbot.py +++ /dev/null @@ -1,104 +0,0 @@ -""" -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 -""" -import subprocess -import argparse -import re -import shutil -import sys -from packaging import version as pversion - -from peach_package_builder.constants import * -from peach_package_builder.utils import render_template, add_deb_to_freight, update_freight_cache - -# manually update this version when we want to build a new peach-go-sbot package -PEACH_GO_SBOT_VERSION = '0.1.4' - -# constants -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) - 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) - - # copy deb package to MICROSERVICES_DEB_DIR - deb_path = os.path.join(DEB_BUILD_DIR, deb_file_name) - subprocess.check_call(["cp", deb_path, MICROSERVICES_DEB_DIR]) - - # add deb package to freight - add_deb_to_freight(package_name=deb_file_name, package_path=deb_path) - - # update freight cache - update_freight_cache() - - -def build_peach_go_sbot(): - - # gets the most recently built peach_go_sbot version, and increments the micro-number by 1 - version = PEACH_GO_SBOT_VERSION - print("[ BUILDING PEACH-GO-SBOT VERSION {}]".format(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) - - # cross-compile and package peach-go-sbot with new version number - crosscompile_peach_go_sbot() - package_peach_go_sbot(version=version) - - -if __name__ == '__main__': - build_peach_go_sbot() - diff --git a/peach_package_builder/build_rust_packages.py b/peach_package_builder/build_rust_packages.py index 25a91e7..355d90d 100644 --- a/peach_package_builder/build_rust_packages.py +++ b/peach_package_builder/build_rust_packages.py @@ -44,6 +44,11 @@ def build_rust_packages(default_branch=False, package=None): subprocess.check_call(["git", "checkout", branch], cwd=service_path) subprocess.check_call(["git", "fetch", "--all"], cwd=service_path) subprocess.check_call(["git", "reset", "--hard", remote_branch], cwd=service_path) + # for packages which depend on peach-lib (peach-web and peach-dyndns-udpater) + # run cargo update, so that we get the latest peach-lib version from git + if service_name in ["peach-web", "peach-dyndns-updater"]: + subprocess.check_call([CARGO_PATH, "update"], cwd=service_path) + # build debian pacakge debian_package_path = subprocess.check_output( [ CARGO_PATH,