Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add post script functionality #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,21 @@ Examples:

Additional details are available from [`ssh_config(5)`](https://linux.die.net/man/5/ssh_config)

#### SSH_POST_SCRIPT

When set, SSH_POST_SCRIPT will be the location of a shell script to execute after starting autossh.
This is useful to send some details to the server about the autossh client.
For example, if a random port is used, then it's useful for the server to know who is on this port.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's useful for the server to know who is on this port.

This does not make sense just reading it.

Do you mean to say you can associate a specific container/instance of autossh is using a specific port remotely? I'm a bit confused as to the usecase.

SSH_TUNNEL_PORT can be used in the post script like so:
```
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add another space, and don't forget to call out the specific language in the markdown. (e.g. '''sh, note the incorrect use of quotes because I couldn't easily figure out how to add in backticks). See line 112 of this file for an example.

FILE=/tmp/autossh.${SSH_CLIENT_ID}
echo "${SSH_TUNNEL_PORT}" > ${FILE}
scp ${FILE} ${SSH_REMOTE_USER}@${SSH_REMOTE_HOST}:/tmp
```
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add one blank line below codeblock.

In this example SSH_CLIENT_ID is set to some identifier that represents the client and the port is
set in a file. This could be useful when you have multiple client connections and you need to know
on the server which client to connect to. Mount the shell script in a docker volume.

#### Additional Environment variables

- [`autossh(1)`](https://linux.die.net/man/1/autossh)
Expand Down
21 changes: 19 additions & 2 deletions rootfs/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,22 @@ COMMAND="autossh "\

echo "[INFO ] # ${COMMAND}"

# Run command
exec ${COMMAND}
# Run autossh command in background and save pid
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please write a test for this in a new container, you may call it the local-post-script container if you'd like.

The test container should exit with 0 when you have proven the post_script ran successfully and return non-zero when the script failed to run.

exec ${COMMAND} &
pid=$!
sleep 1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we sleeping here? If we're just sleeping here to give time for the tunnel to instantiate, we should find a better way to confirm that.

Also, if that fails, we should exit() in some fashion.


# Export the Tunnel Port for post script to use
export SSH_TUNNEL_PORT=${SSH_TUNNEL_PORT}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any other interesting variables we can/should expose?


if [[ -e /proc/$pid ]] && [[ -n "${SSH_POST_SCRIPT}" ]]; then
if [[ -f "${SSH_POST_SCRIPT}" ]]; then
# Execute Post Script
echo "[INFO ] Executing ${SSH_POST_SCRIPT}"
/bin/sh "${SSH_POST_SCRIPT}"
else
echo "[WARN ] ${SSH_POST_SCRIPT} was not found"
fi
fi

wait $pid