Add script to build peach-go-sbot debian package #10

Merged
mhfowler merged 3 commits from peach-go-sbot into main 2021-03-08 09:36:59 +00:00
9 changed files with 203 additions and 1 deletions

3
.gitignore vendored
View File

@ -2,4 +2,5 @@ deploy.sh
secret*
secret_files*
ssh.sh
notes.txt
notes.txt
*__pycache__*

View File

@ -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 <version_number>`
This builds 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:

View File

@ -0,0 +1,5 @@
Package: peach-go-sbot
Version: {{version}}
Architecture: all
Maintainer: Andrew Reid <gnomad@cryptolab.net>
Description: debian package for go-ssb-server on arm64

View File

@ -0,0 +1,40 @@
#!/bin/sh
set -e
# create user which go-sbot runs as
adduser --quiet --system peach-go-sbot
mycognosist commented 2021-03-08 08:36:26 +00:00 (Migrated from github.com)
Review

This is so nice <3 Being able to create the user and set binary permissions right here is awesome. Great find! This is going to make our whole installation and system maintenance process so much simpler.

This is so nice <3 Being able to create the user and set binary permissions right here is awesome. Great find! This is going to make our whole installation and system maintenance process so much simpler.
# set permissions
chown peach-go-sbot /usr/bin/go-sbot
chown peach-go-sbot /usr/bin/sbotcli
# 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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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
mycognosist commented 2021-03-08 08:39:11 +00:00 (Migrated from github.com)
Review

Sounds like a job for Regex :)

Sounds like a job for Regex :)
mhfowler commented 2021-03-08 09:29:57 +00:00 (Migrated from github.com)
Review

Just added a github issue for this so we don't forget it https://github.com/peachcloud/peach-vps/issues/11

Just added a github issue for this so we don't forget it https://github.com/peachcloud/peach-vps/issues/11
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')
mycognosist commented 2021-03-08 08:43:22 +00:00 (Migrated from github.com)
Review

Is SERVICE_DIR missing a forward slash?

DEB_BUILD_DIR = "/tmp/peach_go_sbot" and we're appending "lib/systemd/system", which would make SERVICE_DIR =

/tmp/peach_go_sbotlib/systemd/system

Maybe that's intended and I'm missing something?

Is `SERVICE_DIR` missing a forward slash? `DEB_BUILD_DIR` = "/tmp/peach_go_sbot" and we're appending "lib/systemd/system", which would make `SERVICE_DIR` = `/tmp/peach_go_sbotlib/systemd/system` Maybe that's intended and I'm missing something?
mycognosist commented 2021-03-08 08:43:58 +00:00 (Migrated from github.com)
Review

Same thing appears on line 59 for BIN_DIR.

Same thing appears on line 59 for `BIN_DIR`.
mhfowler commented 2021-03-08 09:28:31 +00:00 (Migrated from github.com)
Review

I just confirmed in a python shell, os.path.join inserts the "/" in between parts as needed.
the way I think about it, the second part is like a relative path joined to the first part (and so the second part doesn't start with a slash)

I just confirmed in a python shell, os.path.join inserts the "/" in between parts as needed. the way I think about it, the second part is like a relative path joined to the first part (and so the second part doesn't start with a slash)
mycognosist commented 2021-03-08 09:35:15 +00:00 (Migrated from github.com)
Review

Ok awesome, thanks for checking! I figured it must work somehow, since you've already been using the script successfully. Nice to have learned a little extra about os.path.join.

Ok awesome, thanks for checking! I figured it must work somehow, since you've already been using the script successfully. Nice to have learned a little extra about `os.path.join`.
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()

View File

@ -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"