1+ #! /bin/sh
2+ set -e
3+
4+ # --------------------------
5+ # Configuration
6+ # --------------------------
7+ PAIR_ENV=" build"
8+ PAIR_VERSION=" 2.2.3-build"
9+ PORT=" ${VAR_PORT:- 2222} "
10+ PAIR_CLI_URL=" https://s3.amazonaws.com/downloads.pairspaces.com/cli/$PAIR_ENV /linux/amd64/pair_${PAIR_VERSION} "
11+ PAIR_CLI_PATH=" /opt/pair/pair"
12+ SSHD_CONFIG_TARGET=" /etc/ssh/ps_sshd_config"
13+ SUPERVISOR_CONF_DIR=" /etc/supervisor/conf.d"
14+ SUPERVISORD_MAIN_CONF=" /etc/supervisor/supervisord.conf"
15+ TOKEN=" ${TOKEN:- CHANGE_ME} "
16+
17+ PKG_INSTALL=" "
18+ PKG_UPDATE=" "
19+
20+ # --------------------------
21+ # Detect package manager
22+ # --------------------------
23+ detect_package_manager () {
24+ if command -v apt-get > /dev/null 2>&1 ; then
25+ PKG_INSTALL=" apt-get install -y --no-install-recommends"
26+ PKG_UPDATE=" apt-get update"
27+ elif command -v apk > /dev/null 2>&1 ; then
28+ PKG_INSTALL=" apk add --no-cache"
29+ PKG_UPDATE=" :"
30+ elif command -v dnf > /dev/null 2>&1 ; then
31+ PKG_INSTALL=" dnf install -y"
32+ PKG_UPDATE=" dnf makecache"
33+ elif command -v yum > /dev/null 2>&1 ; then
34+ PKG_INSTALL=" yum install -y"
35+ PKG_UPDATE=" yum makecache"
36+ elif command -v pacman > /dev/null 2>&1 ; then
37+ PKG_INSTALL=" pacman -Sy --noconfirm"
38+ PKG_UPDATE=" pacman -Sy"
39+ else
40+ echo " Unsupported OS. Could not detect package manager."
41+ exit 1
42+ fi
43+ }
44+
45+ # --------------------------
46+ # Install system packages
47+ # --------------------------
48+ install_packages () {
49+ eval " $PKG_UPDATE "
50+ eval " $PKG_INSTALL curl bash openssh-server sudo ca-certificates iproute2 kmod supervisor"
51+ }
52+
53+ # --------------------------
54+ # Install Pair CLI
55+ # --------------------------
56+ install_pair_cli () {
57+ mkdir -p " $( dirname " $PAIR_CLI_PATH " ) "
58+ curl -fsSL " $PAIR_CLI_URL " -o " $PAIR_CLI_PATH "
59+ chmod +x " $PAIR_CLI_PATH "
60+ }
61+
62+ # --------------------------
63+ # Configure SSHD
64+ # --------------------------
65+ configure_sshd () {
66+ mkdir -p /var/run/sshd /etc/ssh
67+
68+ cat << EOF > "$SSHD_CONFIG_TARGET "
69+ AuthorizedKeysCommand /opt/pair/pair verify %u %k %t
70+ AuthorizedKeysCommandUser root
71+
72+ Port ${PORT}
73+
74+ PasswordAuthentication no
75+ PermitEmptyPasswords no
76+
77+ Subsystem sftp /usr/libexec/openssh/sftp-server
78+ EOF
79+
80+ ssh-keygen -A || true
81+ }
82+
83+ # --------------------------
84+ # Configure supervisord
85+ # --------------------------
86+ configure_supervisord () {
87+ mkdir -p " $SUPERVISOR_CONF_DIR "
88+
89+ cat << EOF > "${SUPERVISOR_CONF_DIR} /sshd.conf"
90+ [program:sshd]
91+ command=/usr/sbin/sshd -D -e -f $SSHD_CONFIG_TARGET
92+ autostart=true
93+ autorestart=true
94+ stderr_logfile=/var/log/sshd.err.log
95+ stdout_logfile=/var/log/sshd.out.log
96+ EOF
97+
98+ cat << EOF > "$SUPERVISORD_MAIN_CONF "
99+ [supervisord]
100+ nodaemon=false
101+ user=root
102+ logfile=/var/log/supervisord.log
103+ pidfile=/var/run/supervisord.pid
104+
105+ [include]
106+ files = $SUPERVISOR_CONF_DIR /*.conf
107+ EOF
108+ }
109+
110+ main () {
111+ detect_package_manager
112+ install_packages
113+ install_pair_cli
114+ configure_sshd
115+ configure_supervisord
116+ }
117+
118+ main " $@ "
0 commit comments