Skip to content

Commit eaa52ef

Browse files
committed
feat(docker-pussh): check remote docker command permissions and use sudo if needed
1 parent b4c0ade commit eaa52ef

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

docker-pussh

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ fi
1818
# SSH command arguments to be used for all ssh commands after establishing a shared "master" connection
1919
# using establish_ssh_connection.
2020
declare -a SSH_ARGS=()
21+
# sudo prefix for remote docker commands.
22+
REMOTE_SUDO=""
2123

2224
# Colors for output.
2325
RED='\033[0;31m'
@@ -65,6 +67,27 @@ establish_ssh_connection() {
6567
SSH_ARGS+=("$ssh_addr")
6668
}
6769

70+
# Check if the remote host has Docker installed and if we can run docker commands.
71+
# If sudo is required, it sets the REMOTE_SUDO variable to "sudo".
72+
check_remote_docker() {
73+
# Check if docker command is available.
74+
if ! ssh "${SSH_ARGS[@]}" "command -v docker" >/dev/null 2>&1; then
75+
error "'docker' command not found on remote host. Please ensure Docker is installed."
76+
fi
77+
# Check if we need sudo to run docker commands.
78+
if ! ssh "${SSH_ARGS[@]}" "docker version" >/dev/null 2>&1; then
79+
# Check if we're not root and if sudo docker works.
80+
if ssh "${SSH_ARGS[@]}" "[ \$(id -u) -ne 0 ] && sudo docker version" >/dev/null; then
81+
REMOTE_SUDO="sudo"
82+
else
83+
error "failed to run docker commands on remote host. Please ensure:
84+
- Docker is installed and running on the remote host
85+
- SSH user has permissions to run docker commands (user is root or non-root user is in 'docker' group)
86+
- If sudo is required, ensure the user can run 'sudo docker' without a password prompt"
87+
fi
88+
fi
89+
}
90+
6891
# Generate a random port in range 55000-65535.
6992
random_port() {
7093
echo $((55000 + RANDOM % 10536))
@@ -165,16 +188,6 @@ cleanup() {
165188
warning "Cleaning up after error..."
166189
fi
167190

168-
# Cancel SSH tunnel forwarding
169-
if [ -n "${LOCAL_PORT:-}" ] && [ -n "${REMOTE_PORT:-}" ] && [ -n "${SSH_CONTROL_SOCKET:-}" ] && [ -S "$SSH_CONTROL_SOCKET" ]; then
170-
info "Closing SSH tunnel..."
171-
local ssh_opts=(-o "ControlPath=$SSH_CONTROL_SOCKET")
172-
if [ -n "$SSH_KEY" ]; then
173-
ssh_opts+=(-i "$SSH_KEY")
174-
fi
175-
ssh "${ssh_opts[@]}" -O cancel -L "$LOCAL_PORT:127.0.0.1:$REMOTE_PORT" "$SSH_ADDRESS" 2>/dev/null || true
176-
fi
177-
178191
# Remove Docker Desktop tunnel container if exists
179192
if [ -n "${TUNNEL_CONTAINER:-}" ]; then
180193
info "Removing tunnel container..."
@@ -184,7 +197,7 @@ cleanup() {
184197
# Stop and remove remote unregistry container
185198
if [ -n "${UNREGISTRY_CONTAINER:-}" ] && [ -n "${SSH_ADDRESS:-}" ]; then
186199
info "Stopping remote unregistry container..."
187-
ssh "${SSH_ARGS[@]}" "docker rm -f $UNREGISTRY_CONTAINER" >/dev/null 2>&1 || true
200+
ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO docker rm -f $UNREGISTRY_CONTAINER" >/dev/null 2>&1 || true
188201
fi
189202

190203
# Clean up local tags
@@ -196,21 +209,17 @@ cleanup() {
196209
# Clean up remote tags if push was successful
197210
if [ -n "${REMOTE_TEMP_TAG:-}" ] && [ -n "${SSH_ADDRESS:-}" ] && [ "${PUSH_SUCCESS:-false}" = "true" ]; then
198211
info "Removing remote temporary tag..."
199-
ssh "${SSH_ARGS[@]}" "docker rmi $REMOTE_TEMP_TAG" >/dev/null 2>&1 || true
212+
ssh "${SSH_ARGS[@]}" "$REMOTE_SUDO rmi $REMOTE_TEMP_TAG" >/dev/null 2>&1 || true
200213
fi
201214

202-
# Clean up SSH ControlMaster socket
203-
if [ -n "${SSH_CONTROL_SOCKET:-}" ] && [ -S "$SSH_CONTROL_SOCKET" ]; then
204-
info "Closing SSH ControlMaster connection..."
205-
local ssh_opts=(-o "ControlPath=$SSH_CONTROL_SOCKET")
206-
if [ -n "$SSH_KEY" ]; then
207-
ssh_opts+=(-i "$SSH_KEY")
208-
fi
209-
ssh "${ssh_opts[@]}" -O exit "$SSH_ADDRESS" 2>/dev/null || true
210-
rm -f "$SSH_CONTROL_SOCKET"
215+
# Terminate the shared SSH connection if it was established.
216+
if [ ${#SSH_ARGS[@]} -ne 0 ]; then
217+
ssh "${SSH_ARGS[@]}" -O exit 2>/dev/null || true
211218
fi
212219
}
213220
trap cleanup INT TERM EXIT
214221

222+
info "Connecting to $SSH_ADDRESS..."
215223
establish_ssh_connection "$SSH_ADDRESS"
216-
info "Connected to $SSH_ADDRESS"
224+
225+
check_remote_docker

0 commit comments

Comments
 (0)