Skip to content

Commit

Permalink
add systemd user unit instance template
Browse files Browse the repository at this point in the history
  • Loading branch information
qrkourier committed Jan 2, 2025
1 parent 119c7e4 commit ec8e060
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 40 deletions.
3 changes: 3 additions & 0 deletions .goreleaser-linux-amd64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ nfpms:
- dst: /lib/systemd/system/
src: ./nfpm/zrok-share.service

- dst: /usr/lib/systemd/user/
src: ./nfpm/[email protected]

- dst: /etc/systemd/system/zrok-share.service.d/override.conf
src: ./nfpm/zrok-share.service.override.conf

Expand Down
3 changes: 3 additions & 0 deletions .goreleaser-linux-arm64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ nfpms:
- dst: /lib/systemd/system/
src: ./nfpm/zrok-share.service

- dst: /usr/lib/systemd/user/
src: ./nfpm/[email protected]

- dst: /etc/systemd/system/zrok-share.service.d/override.conf
src: ./nfpm/zrok-share.service.override.conf

Expand Down
6 changes: 6 additions & 0 deletions .goreleaser-linux-armel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ nfpms:
- dst: /lib/systemd/system/
src: ./nfpm/zrok-share.service

- dst: /usr/lib/systemd/user/
src: ./nfpm/[email protected]

- dst: /usr/lib/systemd/user/
src: ./nfpm/[email protected]

- dst: /etc/systemd/system/zrok-share.service.d/override.conf
src: ./nfpm/zrok-share.service.override.conf

Expand Down
3 changes: 3 additions & 0 deletions .goreleaser-linux-armhf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ nfpms:
- dst: /lib/systemd/system/
src: ./nfpm/zrok-share.service

- dst: /usr/lib/systemd/user/
src: ./nfpm/[email protected]

- dst: /etc/systemd/system/zrok-share.service.d/override.conf
src: ./nfpm/zrok-share.service.override.conf

Expand Down
58 changes: 25 additions & 33 deletions nfpm/zrok-share.bash
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,20 @@ fi
echo "DEBUG: zrok state directory is ${HOME}/.zrok"

: "${ZROK_SHARE_RESERVED:=true}"

echo "DEBUG: ZROK_SHARE_RESERVED=${ZROK_SHARE_RESERVED}"

if (( $# )); then
if [[ -s "$1" ]]; then
while (( $# )); do
if [[ "${1:0:1}" == @ ]]; then
ZROK_INSTANCE="${1:1}"
shift
elif [[ -s "$1" ]]; then
echo "INFO: reading share configuration from $1"
source "$1"
shift
else
echo "ERROR: '$1' is empty or not readable" >&2
exit 1
fi
else
# TODO: consider defining a default environment file
# if [[ -s /opt/openziti/etc/zrok.env ]]; then
# source /opt/openziti/etc/zrok.env
# else
# echo "ERROR: need /opt/openziti/etc/zrok.env or filename argument to read share configuration" >&2
# exit 1
# fi
echo "INFO: reading share configuration from environment variables"
fi
done

ZROK_RESERVATION_FILE="${HOME}/.zrok/reserved${ZROK_INSTANCE:+@${ZROK_INSTANCE}}.json"

[[ -n "${ZROK_TARGET:-}" ]] || {
echo "ERROR: ZROK_TARGET is not defined." >&2
Expand All @@ -70,14 +62,14 @@ if [[ "${ZROK_FRONTEND_MODE:-}" == temp-public ]]; then
ZROK_CMD="share public"
elif [[ "${ZROK_FRONTEND_MODE:-}" == temp-private ]]; then
ZROK_CMD="share private"
elif [[ -s ~/.zrok/reserved.json ]]; then
ZROK_RESERVED_TOKEN="$(jq -r '.token' ~/.zrok/reserved.json 2>/dev/null)"
if [[ -z "${ZROK_RESERVED_TOKEN}" || "${ZROK_RESERVED_TOKEN}" == null ]]; then
echo "ERROR: invalid reserved.json: '$(jq -c . ~/.zrok/reserved.json)'" >&2
elif [[ -s "${ZROK_RESERVATION_FILE}" ]]; then
ZROK_RESERVATION_TOKEN="$(jq -r '.token' "${ZROK_RESERVATION_FILE}" 2>/dev/null)"
if [[ -z "${ZROK_RESERVATION_TOKEN}" || "${ZROK_RESERVATION_TOKEN}" == null ]]; then
echo "ERROR: invalid reservation file: '$(jq -c . "${ZROK_RESERVATION_FILE}")'" >&2
exit 1
else
echo "INFO: zrok backend is already reserved: ${ZROK_RESERVED_TOKEN}"
ZROK_CMD="${ZROK_RESERVED_TOKEN} ${ZROK_TARGET}"
echo "INFO: zrok backend is already reserved: ${ZROK_RESERVATION_TOKEN}"
ZROK_CMD="${ZROK_RESERVATION_TOKEN} ${ZROK_TARGET}"
if [[ "${ZROK_SHARE_RESERVED}" == true ]]; then
exec_share_reserved ${ZROK_CMD}
else
Expand Down Expand Up @@ -208,30 +200,30 @@ if [[ "${ZROK_FRONTEND_MODE:-}" =~ ^temp- ]]; then
exec_with_common_opts ${ZROK_CMD}
else
# reserve and continue
zrok ${ZROK_CMD} > ~/.zrok/reserved.json
zrok ${ZROK_CMD} > "${ZROK_RESERVATION_FILE}"
# share the reserved backend target until exit
if ! [[ -s ~/.zrok/reserved.json ]]; then
echo "ERROR: empty or missing $(realpath ~/.zrok)/reserved.json" >&2
if ! [[ -s "${ZROK_RESERVATION_FILE}" ]]; then
echo "ERROR: empty or missing $(realpath "${ZROK_RESERVATION_FILE}")" >&2
exit 1
elif ! jq . < ~/.zrok/reserved.json &>/dev/null; then
echo "ERROR: invalid JSON in $(realpath ~/.zrok)/reserved.json" >&2
elif ! jq . < "${ZROK_RESERVATION_FILE}" &>/dev/null; then
echo "ERROR: invalid JSON in $(realpath "${ZROK_RESERVATION_FILE}")" >&2
exit 1
else
if [[ "${ZROK_FRONTEND_MODE:-}" == reserved-public ]]; then
ZROK_PUBLIC_URLS=$(jq -cr '.frontend_endpoints' ~/.zrok/reserved.json 2>/dev/null)
ZROK_PUBLIC_URLS=$(jq -cr '.frontend_endpoints' "${ZROK_RESERVATION_FILE}" 2>/dev/null)
if [[ -z "${ZROK_PUBLIC_URLS}" || "${ZROK_PUBLIC_URLS}" == null ]]; then
echo "ERROR: frontend endpoints not defined in $(realpath ~/.zrok)/reserved.json" >&2
echo "ERROR: frontend endpoints not defined in $(realpath "${ZROK_RESERVATION_FILE}")" >&2
exit 1
else
echo "INFO: zrok public URLs: ${ZROK_PUBLIC_URLS}"
fi
fi
ZROK_RESERVED_TOKEN=$(jq -r '.token' ~/.zrok/reserved.json 2>/dev/null)
if [[ -z "${ZROK_RESERVED_TOKEN}" || "${ZROK_RESERVED_TOKEN}" == null ]]; then
echo "ERROR: zrok reservation token not defined in $(realpath ~/.zrok)/reserved.json" >&2
ZROK_RESERVATION_TOKEN=$(jq -r '.token' "${ZROK_RESERVATION_FILE}" 2>/dev/null)
if [[ -z "${ZROK_RESERVATION_TOKEN}" || "${ZROK_RESERVATION_TOKEN}" == null ]]; then
echo "ERROR: zrok reservation token not defined in $(realpath "${ZROK_RESERVATION_FILE}")" >&2
exit 1
fi
ZROK_CMD="${ZROK_RESERVED_TOKEN} ${ZROK_TARGET}"
ZROK_CMD="${ZROK_RESERVATION_TOKEN} ${ZROK_TARGET}"
if [[ "${ZROK_SHARE_RESERVED}" == true ]]; then
exec_share_reserved ${ZROK_CMD}
else
Expand Down
6 changes: 4 additions & 2 deletions nfpm/zrok-share.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
#
## ZROK ENVIRONMENT
#
# You MUST enable a zrok environment by setting the environment enable token here. This file must be readable by
# 'other'. Obtain the enable token from the zrok console after accepting your invitation and creating a password.
# The variables in this section are not used by user units, i.e., systemctl --user, because it is assumed the user's
# environment in ~/.zrok is already enabled. The variables in this section are required by system-wide service units.
# For system services, you MUST enable a zrok environment by setting the environment enable token here. This file must
# be readable by 'other'. Obtain the enable token from the zrok console.
#
# WARNING: changing these values has no effect if /var/lib/zrok-share/.zrok/environment.json exists. Remove that file to
# enable a new environment and /var/lib/zrok-share/.zrok/reserved.json to provision a new frontend URL for the specified
Expand Down
7 changes: 4 additions & 3 deletions nfpm/zrok-share.service
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
[Unit]
Description=zrok reserved public share service
Description=zrok share service
After=network-online.target

[Service]
Type=simple
DynamicUser=yes
StateDirectory=zrok-share
UMask=0007
ExecStartPre=/opt/openziti/bin/zrok-enable.bash /opt/openziti/etc/zrok/zrok-share.env
ExecStart=/opt/openziti/bin/zrok-share.bash /opt/openziti/etc/zrok/zrok-share.env
EnvironmentFile=/opt/openziti/etc/zrok/zrok-share.env
ExecStartPre=/opt/openziti/bin/zrok-enable.bash
ExecStart=/opt/openziti/bin/zrok-share.bash
Restart=always
RestartSec=3

Expand Down
2 changes: 0 additions & 2 deletions nfpm/zrok-share.service.override.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@

# allow adding tun device and IP routes and iptables rules; required when ZROK_BACKEND_MODE=vpn
# AmbientCapabilities=CAP_NET_ADMIN

# you must run 'systemctl daemon-reload' after modifying this file
17 changes: 17 additions & 0 deletions nfpm/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

# /usr/lib/systemd/user/[email protected]

[Unit]
Description=zrok share user service unit @%i
After=network-online.target

[Service]
Type=simple
UMask=0007
EnvironmentFile=%h/.zrok/zrok-share@%i.env
ExecStart=/opt/openziti/bin/zrok-share.bash @%i
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target

0 comments on commit ec8e060

Please sign in to comment.