Skip to content

Commit

Permalink
Fix parsing of configuration file argument.
Browse files Browse the repository at this point in the history
Add a postinstall script.
  • Loading branch information
joshuar committed Dec 11, 2022
1 parent 684767c commit d16a3ff
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 6 deletions.
9 changes: 8 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
before:
hooks:
- go mod tidy
Expand Down Expand Up @@ -33,7 +34,7 @@ nfpms:
maintainer: Josh Rich <[email protected]>
description: A Dynamic DNS client for Cloudflare.
license: MIT
release: 1
release: "1"

formats:
- deb
Expand All @@ -57,3 +58,9 @@ nfpms:
- src: systemd/cf-ddns.service
dst: /usr/lib/systemd/system/cf-ddns.service
type: config

scripts:
# preinstall: "scripts/preinstall.sh"
postinstall: "scripts/postinstall.sh"
# preremove: "scripts/preremove.sh"
# postremove: "scripts/postremove.sh"
8 changes: 4 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -81,9 +81,9 @@ func init() {
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "c", "config file (default is $HOME/.cf-ddns.yaml)")
rootCmd.Flags().BoolVarP(&debugFlag, "debug", "d", false, "debug output")
rootCmd.Flags().BoolVarP(&profileFlag, "profile", "p", false, "enable profiling")
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "/etc/cf-ddns/cf-ddns.yml", "config file (default is /etc/cf-ddns/cf-ddns.yaml)")
rootCmd.Flags().BoolVarP(&debugFlag, "debug", "d", false, "debug output (default is false)")
rootCmd.Flags().BoolVarP(&profileFlag, "profile", "p", false, "enable profiling (default is false)")
}

// initConfig reads in config file and ENV variables if set.
Expand Down
82 changes: 82 additions & 0 deletions scripts/postinstall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/bin/sh

# Step 1, decide if we should use systemd or init/upstart
use_systemctl="True"
systemd_version=0
if ! command -V systemctl >/dev/null 2>&1; then
use_systemctl="False"
else
systemd_version=$(systemctl --version | head -1 | sed 's/systemd //g')
fi

cleanup() {
# This is where you remove files that were not needed on this platform / system
if [ "${use_systemctl}" = "False" ]; then
rm -f /usr/lib/systemd/system/cf-ddns.service
# else
# rm -f /etc/chkconfig/cf-ddns
# rm -f /etc/init.d/cf-ddns
fi
}

cleanInstall() {
printf "\033[32m Post Install of an clean install\033[0m\n"
# Step 3 (clean install), enable the service in the proper way for this platform
if [ "${use_systemctl}" = "False" ]; then
# if command -V chkconfig >/dev/null 2>&1; then
# chkconfig --add cf-ddns
# fi
printf "\033[32m Non-systemd services not supported yet\033[0m\n"
# service cf-ddns restart ||:
else
# rhel/centos7 cannot use ExecStartPre=+ to specify the pre start should be run as root
# even if you want your service to run as non root.
if [ "${systemd_version}" -lt 231 ]; then
printf "\033[31m systemd version %s is less then 231, fixing the service file \033[0m\n" "${systemd_version}"
sed -i "s/=+/=/g" /usr/lib/systemd/system/cf-ddns.service
fi
printf "\033[32m Reload the service unit from disk\033[0m\n"
systemctl daemon-reload ||:
printf "\033[32m Unmask the service\033[0m\n"
systemctl unmask cf-ddns ||:
printf "\033[32m Set the preset flag for the service unit\033[0m\n"
systemctl preset cf-ddns ||:
printf "\033[32m Set the enabled flag for the service unit\033[0m\n"
systemctl enable cf-ddns ||:
systemctl restart cf-ddns ||:
fi
}

upgrade() {
printf "\033[32m Post Install of an upgrade\033[0m\n"
# Step 3(upgrade), do what you need
# ...
}

# Step 2, check if this is a clean install or an upgrade
action="$1"
if [ "$1" = "configure" ] && [ -z "$2" ]; then
# Alpine linux does not pass args, and deb passes $1=configure
action="install"
elif [ "$1" = "configure" ] && [ -n "$2" ]; then
# deb passes $1=configure $2=<current version>
action="upgrade"
fi

case "$action" in
"1" | "install")
cleanInstall
;;
"2" | "upgrade")
printf "\033[32m Post Install of an upgrade\033[0m\n"
upgrade
;;
*)
# $1 == version being installed
printf "\033[32m Alpine\033[0m"
cleanInstall
;;
esac

# Step 4, clean up unused files, yes you get a warning when you remove the package, but that is ok.
cleanup
2 changes: 1 addition & 1 deletion systemd/cf-ddns.service
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ After=network-online.target

[Service]
Type=simple
ExecStart=/usr/local/bin/cf-ddns -c /etc/cf-ddns/cf-ddns.yml
ExecStart=/usr/local/bin/cf-ddns
ProtectSystem=strict
ProtectHome=yes

Expand Down

0 comments on commit d16a3ff

Please sign in to comment.