From 0907c07c974c0da725b8c4da3a0f1faf9bd1f9bd Mon Sep 17 00:00:00 2001 From: notplants Date: Mon, 11 Apr 2022 16:57:05 -0400 Subject: [PATCH] Attempting to make systemctl user unit --- .../{peach_go_sbot => go_sbot}/DEBIAN/control | 2 +- .../DEBIAN/postinst | 23 +++++---- conf/templates/go_sbot/DEBIAN/postrm | 21 +++++++++ conf/templates/go_sbot/DEBIAN/prerm | 7 +++ conf/templates/go_sbot/config.toml | 15 ++++++ .../go-sbot.service} | 3 +- conf/templates/peach_go_sbot/DEBIAN/postrm | 21 --------- conf/templates/peach_go_sbot/DEBIAN/prerm | 7 --- ...uild_peach_go_sbot.py => build_go_sbot.py} | 47 +++++++++++-------- 9 files changed, 87 insertions(+), 59 deletions(-) rename conf/templates/{peach_go_sbot => go_sbot}/DEBIAN/control (85%) rename conf/templates/{peach_go_sbot => go_sbot}/DEBIAN/postinst (53%) create mode 100755 conf/templates/go_sbot/DEBIAN/postrm create mode 100755 conf/templates/go_sbot/DEBIAN/prerm create mode 100644 conf/templates/go_sbot/config.toml rename conf/templates/{peach_go_sbot/peach-go-sbot.service => go_sbot/go-sbot.service} (74%) delete mode 100755 conf/templates/peach_go_sbot/DEBIAN/postrm delete mode 100755 conf/templates/peach_go_sbot/DEBIAN/prerm rename peach_package_builder/{build_peach_go_sbot.py => build_go_sbot.py} (71%) diff --git a/conf/templates/peach_go_sbot/DEBIAN/control b/conf/templates/go_sbot/DEBIAN/control similarity index 85% rename from conf/templates/peach_go_sbot/DEBIAN/control rename to conf/templates/go_sbot/DEBIAN/control index 0a54d94..0c09512 100644 --- a/conf/templates/peach_go_sbot/DEBIAN/control +++ b/conf/templates/go_sbot/DEBIAN/control @@ -1,4 +1,4 @@ -Package: peach-go-sbot +Package: go-sbot Version: {{version}} Architecture: all Maintainer: Andrew Reid diff --git a/conf/templates/peach_go_sbot/DEBIAN/postinst b/conf/templates/go_sbot/DEBIAN/postinst similarity index 53% rename from conf/templates/peach_go_sbot/DEBIAN/postinst rename to conf/templates/go_sbot/DEBIAN/postinst index b66485f..0425b4e 100755 --- a/conf/templates/peach_go_sbot/DEBIAN/postinst +++ b/conf/templates/go_sbot/DEBIAN/postinst @@ -1,40 +1,43 @@ #!/bin/sh set -e -# create user which go-sbot runs as -adduser --quiet --system peach-go-sbot +# create user which go-sbot runs as (if doesnt exist) +id -u peach &>/dev/null || adduser --quiet peach # set permissions -chown peach-go-sbot /usr/bin/go-sbot -chown peach-go-sbot /usr/bin/sbotcli +chown peach /usr/bin/go-sbot +chown peach /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 + deb-systemd-helper --user unmask 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 + if deb-systemd-helper --user --quiet was-enabled 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 + deb-systemd-helper --user enable 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 + deb-systemd-helper --user update-state 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 +# USER="$(who | head -1 | awk '{print $1;}')" + USER="peach" + runuser -l $USER -c "XDG_RUNTIME_DIR=/run/user/$(id -u $USER) systemctl --user 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 + runuser -l $USER -c "XDG_RUNTIME_DIR=/run/user/$(id -u $USER) deb-systemd-invoke --user $_dh_action go-sbot.service >/dev/null || true" fi fi # End automatically added section \ No newline at end of file diff --git a/conf/templates/go_sbot/DEBIAN/postrm b/conf/templates/go_sbot/DEBIAN/postrm new file mode 100755 index 0000000..cbe5c72 --- /dev/null +++ b/conf/templates/go_sbot/DEBIAN/postrm @@ -0,0 +1,21 @@ +# Automatically added by cargo-deb +if [ -d /run/systemd/system ]; then + # USER="$(who | head -1 | awk '{print $1;}')" + USER="peach" + runuser -l $USER -c "XDG_RUNTIME_DIR=/run/user/$(id -u $USER) systemctl --user 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 --user mask service >/dev/null || true + fi +fi + +if [ "$1" = "purge" ]; then + if [ -x "/usr/bin/deb-systemd-helper" ]; then + deb-systemd-helper --user purge service >/dev/null || true + deb-systemd-helper --user unmask service >/dev/null || true + fi +fi +# End automatically added section \ No newline at end of file diff --git a/conf/templates/go_sbot/DEBIAN/prerm b/conf/templates/go_sbot/DEBIAN/prerm new file mode 100755 index 0000000..e4f421d --- /dev/null +++ b/conf/templates/go_sbot/DEBIAN/prerm @@ -0,0 +1,7 @@ +# Automatically added by cargo-deb +if [ -d /run/systemd/system ] && [ "$1" = remove ]; then +# USER="$(who | head -1 | awk '{print $1;}')" + USER="peach" + runuser -l $USER -c "XDG_RUNTIME_DIR=/run/user/$(id -u $USER) deb-systemd-invoke --user stop service >/dev/null || true" +fi +# End automatically added section \ No newline at end of file diff --git a/conf/templates/go_sbot/config.toml b/conf/templates/go_sbot/config.toml new file mode 100644 index 0000000..1ee1d71 --- /dev/null +++ b/conf/templates/go_sbot/config.toml @@ -0,0 +1,15 @@ +# For details about go-sbot configuration, please visit the repo: https://github.com/cryptoscope/ssb +repo = "/home/peach/.ssb-go" +debugdir = "" +shscap = "1KHLiKZvAvjbY1ziZEHMXawbCEIM6qwjCDm3VYRan/s=" +hmac = "" +hops = 2 +lis = "127.0.0.1:8008" +wslis = ":8987" +debuglis = "localhost:6078" +localadv = true +localdiscov = true +enable_ebt = true +promisc = true +nounixsock = false +repair = true \ No newline at end of file diff --git a/conf/templates/peach_go_sbot/peach-go-sbot.service b/conf/templates/go_sbot/go-sbot.service similarity index 74% rename from conf/templates/peach_go_sbot/peach-go-sbot.service rename to conf/templates/go_sbot/go-sbot.service index ad396f1..061db13 100644 --- a/conf/templates/peach_go_sbot/peach-go-sbot.service +++ b/conf/templates/go_sbot/go-sbot.service @@ -3,7 +3,8 @@ Description=peachs go-sbot [Service] Type=simple -User=peach-go-sbot +User=peach +Environment="LIBRARIAN_WRITEALL=0" ExecStart=/usr/bin/go-sbot Restart=always diff --git a/conf/templates/peach_go_sbot/DEBIAN/postrm b/conf/templates/peach_go_sbot/DEBIAN/postrm deleted file mode 100755 index b747e03..0000000 --- a/conf/templates/peach_go_sbot/DEBIAN/postrm +++ /dev/null @@ -1,21 +0,0 @@ -#!/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 deleted file mode 100755 index 7dad053..0000000 --- a/conf/templates/peach_go_sbot/DEBIAN/prerm +++ /dev/null @@ -1,7 +0,0 @@ -#!/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/peach_package_builder/build_peach_go_sbot.py b/peach_package_builder/build_go_sbot.py similarity index 71% rename from peach_package_builder/build_peach_go_sbot.py rename to peach_package_builder/build_go_sbot.py index 3cb3fed..e0be04d 100644 --- a/peach_package_builder/build_peach_go_sbot.py +++ b/peach_package_builder/build_go_sbot.py @@ -14,15 +14,15 @@ 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' +GO_SBOT_VERSION = '0.1.21' # constants -DEB_CONF_DIR = os.path.join(PROJECT_PATH, 'conf/templates/peach_go_sbot') -DEB_BUILD_DIR = "/tmp/peach_go_sbot" +DEB_CONF_DIR = os.path.join(PROJECT_PATH, 'conf/templates/go_sbot') +DEB_BUILD_DIR = "/tmp/go_sbot" GO_SSB_DIR = "/srv/peachcloud/automation/go-ssb" -def crosscompile_peach_go_sbot(): +def crosscompile_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) @@ -30,8 +30,8 @@ def crosscompile_peach_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 ]") +def package_go_sbot(version): + print("[ PACKAGING 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') @@ -43,16 +43,25 @@ def package_peach_go_sbot(version): 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") + src = os.path.join("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') + SERVICE_DIR = os.path.join(DEB_BUILD_DIR, 'home/peach/.config/systemd') + # 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') + os.path.join(DEB_CONF_DIR, 'go-sbot.service'), + os.path.join(SERVICE_DIR, 'go-sbot.service') + ) + + # copy go-sbot config.toml + SBOT_CONFIG_DIR = os.path.join(DEB_BUILD_DIR, 'home/peach/.ssb-go') + os.makedirs(SBOT_CONFIG_DIR) + shutil.copyfile( + os.path.join(DEB_CONF_DIR, 'config.toml'), + os.path.join(SBOT_CONFIG_DIR, 'config.toml') ) # copy cross-compiled binaries @@ -68,7 +77,7 @@ def package_peach_go_sbot(version): subprocess.check_call(["chmod", "770", destination]) # create deb package - deb_file_name = "peach-go-sbot_{}_arm64.deb".format(version) + deb_file_name = "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) @@ -83,22 +92,22 @@ def package_peach_go_sbot(version): update_freight_cache() -def build_peach_go_sbot(): +def build_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)) + # gets the most recently built go_sbot version, and increments the micro-number by 1 + version = GO_SBOT_VERSION + print("[ BUILDING 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) + # cross-compile and package go-sbot with new version number + crosscompile_go_sbot() + package_go_sbot(version=version) if __name__ == '__main__': - build_peach_go_sbot() + build_go_sbot()