Deploy a Rust or Go binary to a VPS daemonized
Example deploying to a Digital Ocean linux droplet via email/password authentication and use the screen command to keep the service running as a daemon.
Assuming the binary is compiled to target/release/server
and its statically linked (typical for Rust and Go projects), and you have the following ENV variables set:
- DROPLET_IP: the IP of your server
- DROPLET_USER: usually this is “root”
- DROPLET_PASSWORD: the users’ password
# Use sshpass to handle password prompt of ssh and scp
# Install sshpass if not already installed
if ! command -v sshpass &> /dev/null
then
echo "sshpass could not be found"
echo "Installing sshpass..."
sudo apt-get install sshpass
fi
# SSH into the server and stop the daemonized tempest process
echo "stopping current server process"
sshpass -p $DROPLET_PASSWORD ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $DROPLET_USER@$DROPLET_IP << EOF
# Check if the tempest screen session is running
if screen -list | grep -q "tempest"; then
echo "Stopping tempest process..."
# Send the command to terminate the screen session
screen -S tempest -X quit
else
echo "No tempest process found"
fi
EOF
# Transfer the binary to the droplet
echo "transfer binary to server"
sshpass -p $DROPLET_PASSWORD scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null target/release/server $DROPLET_USER@$DROPLET_IP:tempest
# SSH into the server and start the tempest binary as a monitored daemon process
echo "starting new server process"
sshpass -p $DROPLET_PASSWORD ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $DROPLET_USER@$DROPLET_IP << EOF
# Check if screen is installed, if not install it
if ! command -v screen &> /dev/null
then
echo "screen could not be found"
echo "Installing screen..."
apt-get install screen
fi
# Start the tempest binary in a screen session
screen -dmS tempest ./tempest
EOF
Note that this script uses the sshpass
command available on Linux, so that it works directly from a github action.
Linked Technologies
What it's made of
Go
Fast, simple, and efficient. Ideal for solopreneurs, Go's straightforward syntax and powerful performance allow for quick development and deployment.
Rust
A language empowering everyone to build reliable and efficient software. Futuristic swiss army knife and probably the most powerful mainstream technology today
Shell
Shell scripting: Automate and streamline your tasks with powerful command-line scripts. Unlock efficiency in every command! Contains bash, zsh, sh, ...
Linked Categories
Where it's useful
DevOps
Get a glimpse into the world of DevOps, where coding meets collaboration, speeding up everything from software builds to system fixes. Learn about the seamless integration of development and operations to make tech life easier and more efficient.