summaryrefslogtreecommitdiff
path: root/devel/soft-serve/files/soft-serve.in
blob: b67a1c10e8d71f83793ccb977e5455d28223dfe9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/sh

# PROVIDE: soft-serve
# REQUIRE: NETWORKING
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# soft_serve_enable (bool):	Set to NO by default.
#				Set it to YES to enable soft_serve.
#
#
# soft_serve_user (user):	User to run as. Set to %%GITUSER%% by default.
# soft_serve_port (port):	TCP port to listen on.
#                               Set to %%DEFAULTPORT%% by default.
# soft_serve_host (IP):	        IP address to listen on.
#                               Set to %%DEFAULTHOST%% by default.
# soft_serve_key_path (path):	Path to host key.
#                               Set to ~%%GITUSER%%/%%DEFAULTKEY%% by default.
# soft_serve_repo_path (path):	Path to repositories root (old)
#                               Set to ~%%GITUSER%%/%%DEFAULTREPO%% by default.
# soft_serve_data_path (path):	Path to repositories data.
#                               Set to ~%%GITUSER%%/%%DEFAULTDATA%% by default.
# soft_serve_initial_admin_key (ssh public key): SSH public key for initial
#                                                access to repositories (required)

. /etc/rc.subr

name="soft_serve"
rcvar="soft_serve_enable"

load_rc_config $name

: ${soft_serve_user:="%%GITUSER%%"}
: ${soft_serve_enable:="NO"}
: ${soft_serve_port:=%%DEFAULTPORT%%}
: ${soft_serve_host:="%%DEFAULTHOST%%"}
: ${soft_serve_key_path:="%%DEFAULTKEY%%"}
: ${soft_serve_repo_path:="%%DEFAULTREPO%%"}
: ${soft_serve_data_path:="%%DEFAULTDATA%%"}
: ${soft_serve_initial_admin_key:=""}

command="%%PREFIX%%/bin/soft-serve"
procname="%%PREFIX%%/bin/soft-serve"
githome="$(%%PW%% user show -n ${soft_serve_user} | %%CUT%% -d: -f9)"

pidfile="/var/run/${name}.pid"

extra_commands="migrate"
start_cmd="${name}_start"
migrate_cmd="${name}_migrate"

soft_serve_migrate() {
	if echo ${soft_serve_key_path} | grep -q ^/; then
		SOFT_SERVE_KEY_PATH=${soft_serve_key_path}
	else
		SOFT_SERVE_KEY_PATH=$githome/${soft_serve_key_path}
	fi
	if echo ${soft_serve_repo_path} | grep -q ^/; then
		SOFT_SERVE_REPO_PATH=${soft_serve_repo_path}
	else
		SOFT_SERVE_REPO_PATH=$githome/${soft_serve_repo_path}
	fi
	%%SU%% -l ${soft_serve_user} -c "\
		%%SETENV%% \
		\"SOFT_SERVE_KEY_PATH=${SOFT_SERVE_KEY_PATH}\" \
		\"SOFT_SERVE_REPO_PATH=${SOFT_SERVE_REPO_PATH}\" \
		$command migrate-config"
}

soft_serve_start() {
        if [ -z "${soft_serve_initial_admin_key}" ]; then
	  echo Error: Please set soft_serve_initial_admin_key to a SSH public key
	  echo which will initially have access to repositories.
	  exit 1
	fi

	# Generate host key, if user has opted to use the default key
	if [ "${soft_serve_key_path}" = "%%DEFAULTKEY%%" ]; then
	  soft_serve_key_path=${githome}/${soft_serve_key_path}
	  if ! [ -f $soft_serve_key_path ]; then
	    echo Generating SSH Host Key...
	    _soft_serve_ssh_dir=$(%%DIRNAME%% $soft_serve_key_path)
	    if ! [ -d $_soft_serve_ssh_dir  ]; then
	      %%MKDIR%% -m 700 $_soft_serve_ssh_dir
	      %%CHOWN%% $soft_serve_user $_soft_serve_ssh_dir
	    fi
	    /usr/bin/ssh-keygen -t ed25519 -N "" -f $soft_serve_key_path
	    %%CHOWN%% $soft_serve_user $soft_serve_key_path $soft_serve_key_path.pub
	  fi
	fi

	if [ "${soft_serve_data_path}" = "%%DEFAULTDATA%%" ]; then
	  soft_serve_data_path=${githome}/${soft_serve_data_path}
	  if ! [ -d $soft_serve_data_path ]; then
	    echo Creating data directory...
	    %%MKDIR%% $soft_serve_data_path
	    %%CHOWN%% $soft_serve_user $soft_serve_data_path
	  fi
	fi

	/usr/sbin/daemon -f \
		-u ${soft_serve_user} -p ${pidfile} \
		%%SETENV%% -i \
		"SOFT_SERVE_SSH_PUBLIC_URL=ssh://${soft_serve_host}:${soft_serve_port}" \
		"SOFT_SERVE_SSH_KEY_PATH=${soft_serve_key_path}" \
		"SOFT_SERVE_DATA_PATH=${soft_serve_data_path}" \
		"SOFT_SERVE_INITIAL_ADMIN_KEYS=${soft_serve_initial_admin_key}" \
		"PATH=%%LOCALBASE%%/bin:${PATH}" \
		"USER=${soft_serve_user}" \
		$command serve
}

run_rc_command "$1"