diff --git a/Dockerfile.server b/Dockerfile.server new file mode 100644 index 0000000..032b98e --- /dev/null +++ b/Dockerfile.server @@ -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"] diff --git a/internal/cloud/server.go b/internal/cloud/server.go index 36c7202..cbe4076 100644 --- a/internal/cloud/server.go +++ b/internal/cloud/server.go @@ -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 { diff --git a/internal/config/config.go b/internal/config/config.go index 6ca2ed2..04eefab 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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") diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 1a0a2f9..e3a047f 100755 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -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 @@ -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 @@ -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 @@ -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!"