Skip to content

Commit

Permalink
chore(deploy): support supervisor
Browse files Browse the repository at this point in the history
  • Loading branch information
damonto committed May 7, 2024
1 parent 15572c4 commit ceb9e2b
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 13 deletions.
44 changes: 44 additions & 0 deletions Dockerfile.server
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
FROM debian:bookworm-slim

ENV SSH_PUBLIC_KEY="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAYtTh3Z4dllW6CjUXb5hzRR+/akTd4Xp8Q+gH//uSQI"

RUN set -xe \
&& apt-get update \
&& apt-get install -y --no-install-recommends supervisor openssh-server htop \
&& apt-get clean \
&& mkdir -p var/run/sshd \
&& sed -i 's/^#\(PermitRootLogin\) .*/\1 yes/' /etc/ssh/sshd_config \
&& sed -i 's/^#\(PubkeyAuthentication\) .*/\1 yes/' /etc/ssh/sshd_config \
&& mkdir -p /root/.ssh \
&& echo "$SSH_PUBLIC_KEY" > /root/.ssh/authorized_keys \
&& chmod 600 /root/.ssh/authorized_keys \
&& mkdir -p /var/log/supervisor \
&& mkdir -p /etc/supervisor/conf.d \
&& cat > /etc/supervisor/supervisord.conf <<'_EOF'
[supervisord]
nodaemon=true
logfile=/dev/null
logfile_maxbytes=0
pidfile=/tmp/supervisord.pid
stdout_logfile=/dev/stdout

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[unix_http_server]
file=/tmp/supervisor.sock

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock

[program:estkme-cloud]
command="echo 'Hello, World!' && sleep 3600"
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stderr_logfile=/dev/stderr
_EOF

EXPOSE 22 1888

CMD ["sh", "-c", "/usr/sbin/sshd; /usr/bin/supervisord"]
2 changes: 1 addition & 1 deletion internal/cloud/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (s *server) handleConn(tcpConn *net.TCPConn) {
defer s.manager.Remove(id)

if config.C.Advertising != "" {
conn.Send(TagMessageBox, []byte(config.C.Advertising))
conn.Send(TagMessageBox, []byte(config.C.GetAdvertising()))
}

for {
Expand Down
7 changes: 7 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ func (c *Config) IsValid() error {
return nil
}

func (c *Config) GetAdvertising() string {
if c.Advertising != "" {
return "!! Advertising !! \n" + c.Advertising
}
return c.Advertising
}

func (c *Config) LoadEnv() {
if os.Getenv("ESTKME_CLOUD_LISTEN_ADDRESS") != "" {
c.ListenAddress = os.Getenv("ESTKME_CLOUD_LISTEN_ADDRESS")
Expand Down
58 changes: 46 additions & 12 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ if [ ! -f /etc/debian_version ]; then
exit 1
fi

# Check systemctl or supervisorctl
if [ ! -x "$(command -v systemctl)" ] && [ ! -x "$(command -v supervisorctl)" ]; then
echo "Please install systemd or supervisor"
exit 1
fi

# Install dependencies
apt-get update -y && apt-get install -y unzip cmake pkg-config libcurl4-openssl-dev libpcsclite-dev zip curl

Expand Down Expand Up @@ -81,12 +87,13 @@ else
fi
ESTKME_CLOUD_BINARY_URL="https://github.com/damonto/estkme-cloud/releases/download/v$ESTKME_CLOUD_VERSION/$ESTKME_CLOUD_BINARY"

SYSTEMED_UNIT="estkme-cloud.service"
SYSTEMED_UNIT_PATH="/etc/systemd/system/$SYSTEMED_UNIT"
START_CMD="/opt/estkme-cloud/estkme-cloud --data-dir=/opt/estkme-cloud/data --dont-download"
if [ -n "$1" ]; then
START_CMD="$START_CMD --advertising='$1'"
fi

SYSTEMED_UNIT="estkme-cloud.service"
SYSTEMED_UNIT_PATH="/etc/systemd/system/$SYSTEMED_UNIT"
SYSTEMED_FILE="
[Unit]
Description=eSTK.me Cloud Enhance Server
Expand All @@ -103,14 +110,38 @@ TimeoutStopSec=30s
WantedBy=multi-user.target
"

if [ -x "$(command -v systemctl)" ]; then
if [ "$(systemctl is-active $SYSTEMED_UNIT)" == "active" ]; then
systemctl stop $SYSTEMED_UNIT
fi
else
if [ -n "$(pgrep -f "$START_CMD")" ]; then
pkill -f "$START_CMD"
fi
SUPRVISOR_PROGRAM="estkme-cloud"
SUPRVISOR_FILE="
[supervisord]
nodaemon=true
logfile=/dev/null
logfile_maxbytes=0
pidfile=/tmp/supervisord.pid
stdout_logfile=/dev/stdout
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[unix_http_server]
file=/tmp/supervisor.sock
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock
[program:$SUPRVISOR_PROGRAM]
command="$START_CMD"
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stderr_logfile=/dev/stderr
"

if [ -x "$(command -v systemctl)" ] && [ "$(systemctl is-active $SYSTEMED_UNIT)" == "active" ]; then
echo "Stopping eSTK.me Cloud Enhance Server in systemd"
systemctl stop $SYSTEMED_UNIT
elif [ -x "$(command -v supervisorctl)" ] && [ "$(supervisorctl status $SUPRVISOR_PROGRAM | awk '{print $2}')" == "RUNNING" ]; then
echo "Stopping eSTK.me Cloud Enhance Server in supervisor"
supervisorctl stop $SUPRVISOR_PROGRAM
fi

# Download eSTK.me Cloud Enhance Server
Expand All @@ -125,8 +156,11 @@ if [ -x "$(command -v systemctl)" ]; then
systemctl enable $SYSTEMED_UNIT
systemctl start $SYSTEMED_UNIT
else
echo "Deploying eSTK.me Cloud Enhance Server to background"
nohup $START_CMD > /dev/null 2>&1 &
echo "Deploying eSTK.me Cloud Enhance Server to supervisor"
echo "$SUPRVISOR_FILE" > /etc/supervisor/supervisord.conf
supervisorctl reread
supervisorctl update
supervisorctl reload
fi

echo "eSTK.me Cloud Server deployed successfully!"

0 comments on commit ceb9e2b

Please sign in to comment.