Skip to content

Commit

Permalink
print any helper funcs
Browse files Browse the repository at this point in the history
echo informational msgs and forward ssh agent only in interactive sessions
  • Loading branch information
jinal--shah committed Apr 14, 2017
1 parent 7f89837 commit ea68e6e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 24 deletions.
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ _... alpine workspace with dev tools for aws api, credstash, docker, bash, pytho
The docker image contains audit info (use _docker inspect_ to see) including version info about
key tools e.g. docker, jq, awscli, as well as git build info.

## skel dirs

The following assets if mounted under /etc/skel will be copied to your container user's home dir as well
when you invoke /bin/bash as a login shell:

* .aws dir

* .ssh dir

* .gitconfig file

If you create a new user in the container from this image (or a derived FROM image) the new user will
also get these copied over to their home.

## building

Expand All @@ -46,7 +59,7 @@ docker pull opsgang/devbox_aws:stable # or use the tag you prefer
## running

```bash
# run an ephemeral workspace, mounting your .aws dir and the docker daemon from the host
# ... run an ephemeral workspace, mounting your .aws,.ssh and .gitconfig, and the docker daemon from the host
docker run -it --user root
-v $HOME/.aws:/etc/skel/.aws \
-v $HOME/.ssh:/etc/skel/.ssh \
Expand All @@ -63,15 +76,3 @@ docker run -it --user root
docker run --rm -i -v /path/to/script.sh:/script.sh:ro opsgang/devbox_aws:stable /script.sh
```

```bash
# make my aws creds available and run /some/python/script.py
export AWS_ACCESS_KEY_ID="i'll-never-tell" # replace glibness with your access key
export AWS_SECRET_ACCESS_KEY="that's-for-me-to-know" # amend as necessary

docker run --rm -i \ # ... run interactive to see stdout / stderr
-v /some/python/script.py:/my.py:ro \ # ... assume the file is executable
--env AWS_ACCESS_KEY_ID \ # ... will read it from your env
--env AWS_SECRET_ACCESS_KEY \ # ... will read it from your env
--env AWS_DEFAULT_REGION=eu-west-2 \ # ... adjust geography to taste
opsgang/devbox_aws:stable /my.py # script can access these env vars
```
69 changes: 67 additions & 2 deletions assets/fs/etc/skel/.bashrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,63 @@
#
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

_print_helpers() {

# ... funcs: (non _help ones)
local f=$(declare -f | grep -Po '^(\w+)(?= \(\))' | grep -v '_help$')

# ... funcs for which a _help func exists
local fh=$(declare -f | grep -Po '^(\w+)(?=_help \(\))') # funcs that have help

local func_list=$(comm -1 <(echo "$f") <(echo "$fh"))

# ... find longest name, for prettier formatting
local length_name=$(
echo "$func_list" \
| awk '{ print length()+2 | "sort -nr | head -1" }'
)

echo "SHELL FUNCTIONS: "
# ... print helper info
# ... provide default helper info if not defined.
local help_txt="usage: ... no help information provided. That sucks."
local func
for func in $func_list; do
if ! set | grep "^${func}_help ()" >/dev/null 2>&1
then
eval "function ${func}_help(){ echo \"$help_txt\"; }"
fi
_print_helper_msg "$func" "$length_name"
done
}

_print_helper_msg() {
local func="$1"
local length_name="$2"
local start_line="${func}()"
local format_str line

local oIFS=$IFS
IFS=$'\n'

for line in $(${func}_help); do
# ... info lines in cyan 'cos its purrdy Mr. Taggart...
if [[ $start_line =~ ^[\ ]$ ]]; then
line="\e[2m\e[36m$line\e[0m\e[22m"
format_str="%-${length_name}s $line\n"
else
# ... function names in green with emboldened usage info
line="\e[1m$line\e[0m"
format_str="\e[32m%-${length_name}s\e[0m $line\n"
fi
printf "$format_str" $start_line
start_line=' ' # omit function name at start of subsequent lines
done

IFS=$oIFS
echo ""
}

forwardSsh_help() {

cat <<EOM
Expand All @@ -16,11 +73,14 @@ forwardSsh() {
[[ "$1" =~ ^\-h$|\-\-help$ ]] && forwardSsh_help && return 0
[[ ! -z "$@" ]] && echo "... ignoring arguments $*"

priv_keys=$(ls -1 ~/.ssh/id_* 2>/dev/null | grep -v '\.pub')

[[ -z "$priv_keys" ]] && return 0

echo "... generating agent for ssh forwarding in cluster"
pkill ssh-agent
eval $(ssh-agent)
for privateKey in $(ls -1 $HOME/.ssh/id_* | grep -v '\.pub')
do
for privateKey in $priv_keys; do
ssh-add "$privateKey"
done
ssh-add -l # verify your key has been added to the key-ring
Expand All @@ -38,3 +98,8 @@ if [[ -d ~/profile.d ]]; then
fi

alias gtree='tree -a -C -I .git'

if [[ $- == *i* ]]; then
_print_helpers
forwardSsh
fi
2 changes: 1 addition & 1 deletion assets/fs/etc/skel/profile.d/cfg_from_skel
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ _do() {
for f in $files; do
skel=/etc/skel/$f
if [[ ! -e $h/$f ]] && [[ -e $skel ]]; then
echo "INFO: ... seeding $h from $skel"
[[ $- == *i* ]] && echo "... seeding $h from $skel"
su-exec root cp -a $skel $h/
chown -R $uid:$gid $h/$f
fi
Expand Down
16 changes: 8 additions & 8 deletions assets/fs/etc/skel/profile.d/ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ parse_git_branch() {

# get current status of git repo
parse_git_dirty() {
local ct='' # change types (symbols denoting type of git changes)
local s=$(git status 2>&1 | tee)
local bits=''
[ $(_git_change_type "$s" "renamed:") == "0" ] && bits=">${bits}" # RENAMED >
[ $(_git_change_type "$s" "Your branch is ahead of") == "0" ] && bits="*${bits}" # AHEAD *
[ $(_git_change_type "$s" "new file:") == "0" ] && bits="+${bits}" # NEW FILES +
[ $(_git_change_type "$s" "Untracked files") == "0" ] && bits="?${bits}" # UNTRACKED ?
[ $(_git_change_type "$s" "deleted:") == "0" ] && bits="x${bits}" # DELETED x
[ $(_git_change_type "$s" "modified:") == "0" ] && bits="!${bits}" # MODIFIED !
[ $(_git_change_type "$s" "renamed:") == "0" ] && ct=">${ct}" # RENAMED >
[ $(_git_change_type "$s" "Your branch is ahead of") == "0" ] && ct="*${ct}" # AHEAD *
[ $(_git_change_type "$s" "new file:") == "0" ] && ct="+${ct}" # NEW FILES +
[ $(_git_change_type "$s" "Untracked files") == "0" ] && ct="?${ct}" # UNTRACKED ?
[ $(_git_change_type "$s" "deleted:") == "0" ] && ct="x${ct}" # DELETED x
[ $(_git_change_type "$s" "modified:") == "0" ] && ct="!${ct}" # MODIFIED !

[[ ! -z "$bits" ]] && echo " $bits"
}
Expand All @@ -40,6 +40,6 @@ _git_change_type() {
echo -n "$s" 2> /dev/null | grep "$p" &> /dev/null; echo "$?"
}

_set_prompt
[[ $- == *i* ]] && _set_prompt

unset _set_prompt

0 comments on commit ea68e6e

Please sign in to comment.