This is now working very well for me locally, using hetzner/k8s and
local/k3d.
Showing
4 changed files
with
113 additions
and
18 deletions
1 | These docs are very basic right now. | 1 | Steps to work with this: |
2 | 2 | ||
3 | This image only works if $UID = 1000, which is obviously a bug. | 3 | * Check out this repo |
4 | 4 | * run build.sh | |
5 | docker build --tag brainfood-tools --progress=plain . | 5 | * Then from your working folder, directly run wrapper.sh. |
6 | |||
7 | docker run --rm -ti -v $KUBECONFIG:/home/tool/.kube/config -v $PWD:/srv/app -v $SSH_AUTH_SOCK:$SSH_AUTH_SOCK -e SSH_AUTH_SOCK=$SSH_AUTH_SOCK -e SSH_KNOWN_HOSTS="gitlab.brainfood.com" -w /srv/app -u tool brainfood-tools | ||
8 | 6 | ... | ... |
1 | #!/bin/bash | 1 | #!/bin/bash |
2 | 2 | ||
3 | set -ex | 3 | set -e |
4 | 4 | ||
5 | configure_ssh_known_hosts() { | 5 | tool_uid="$(id -u tool)" |
6 | if [[ -z $SSH_KNOWN_HOSTS ]]; then return; fi | 6 | tool_gid="$(id -g tool)" |
7 | for host in $SSH_KNOWN_HOSTS; do | 7 | |
8 | ssh-keyscan $host 2>/dev/null | 8 | adjust_tool_uid_gid() { |
9 | done > "/home/tool/.ssh/known_hosts" | 9 | declare usermod_args=() |
10 | chown tool:tool "/home/tool/.ssh/known_hosts" | 10 | declare -a new_groups=() |
11 | |||
12 | if [[ $MAP_UID ]]; then | ||
13 | [[ $(id -u tool) -ne $MAP_UID ]] && usermod_args+=(-u $MAP_UID) | ||
14 | fi | ||
15 | if [[ $MAP_GROUPS ]]; then | ||
16 | set -- $MAP_GROUPS | ||
17 | [[ $(id -g tool) -ne $1 ]] && usermod_args+=(-g $1) | ||
18 | shift | ||
19 | for group in "$@"; do | ||
20 | groupadd -g $group -o tool_$group | ||
21 | new_groups+=(tool_$group) | ||
22 | done | ||
23 | IFS=, | ||
24 | if [[ ${#new_groups[*]} -gt 0 ]]; then | ||
25 | usermod_args+=(-aG "${new_groups[*]}") | ||
26 | fi | ||
27 | fi | ||
28 | if [[ ${#usermod_args[*]} ]]; then | ||
29 | usermod "${usermod_args[@]}" tool | ||
30 | fi | ||
11 | } | 31 | } |
12 | 32 | ||
13 | configure_ssh_config() { | 33 | configure_ssh() { |
34 | if [[ ! -d /home/tool/.ssh ]]; then | ||
35 | mkdir /home/tool/.ssh | ||
36 | chmod 700 /home/tool/.ssh | ||
37 | fi | ||
38 | if [[ ! -d $run_state/ssh_known_hosts ]]; then | ||
39 | touch "$run_state/ssh_known_hosts" | ||
40 | fi | ||
41 | known_hosts_file="/home/tool/.ssh/known_hosts" | ||
42 | ssh_config_file="/home/tool/.ssh/config" | ||
43 | ln -sf "$run_state/ssh_known_hosts" "$known_hosts_file" | ||
44 | if [[ $SSH_KNOWN_HOSTS ]]; then | ||
45 | # If known_hosts was kept between runs, this loop would be faster. | ||
46 | for host in $SSH_KNOWN_HOSTS; do | ||
47 | ssh-keygen -q -F $host -f "$known_hosts_file" 1>/dev/null || ssh-keyscan $host >> ""$known_hosts_file"" 2>/dev/null | ||
48 | done | ||
49 | fi | ||
14 | { | 50 | { |
15 | echo "Host *" | 51 | echo "Host *" |
16 | echo " PubkeyAcceptedAlgorithms +ssh-rsa" | 52 | echo " PubkeyAcceptedAlgorithms +ssh-rsa" |
53 | echo " StrictHostKeyChecking accept-new" | ||
17 | } > "/home/tool/.ssh/config" | 54 | } > "/home/tool/.ssh/config" |
18 | chown tool:tool "/home/tool/.ssh/config" | 55 | chown tool:tool -R "/home/tool/.ssh" |
19 | } | 56 | } |
20 | 57 | ||
21 | configure_ssh_known_hosts | 58 | configure_bash() { |
22 | configure_ssh_config | 59 | if [[ ! -e $run_state/bash_history ]]; then |
60 | touch "$run_state/bash_history" | ||
61 | fi | ||
62 | ln -sf "$run_state/bash_history" "/home/tool/.bash_history" | ||
63 | chown -h tool:tool "/home/tool/.bash_history" | ||
64 | } | ||
65 | |||
66 | configure_cache() { | ||
67 | if [[ ! -d $run_state/cache ]]; then | ||
68 | mkdir "$run_state/cache" | ||
69 | fi | ||
70 | ln -sf "$run_state/cache" /home/tool/.cache | ||
71 | chown -h tool:tool "/home/tool/.cache" | ||
72 | } | ||
73 | |||
74 | # TODO: Save /run/user between calls | ||
75 | mkdir -p /run/user/$tool_uid | ||
76 | |||
77 | adjust_tool_uid_gid | ||
78 | |||
79 | tool_uid="$(id -u tool)" | ||
80 | tool_gid="$(id -g tool)" | ||
81 | run_state="/run/user/$tool_uid/inkluster-dev" | ||
82 | |||
83 | if [[ ! -d $run_state ]]; then | ||
84 | mkdir -p "$run_state" | ||
85 | fi | ||
86 | configure_ssh | ||
87 | configure_bash | ||
88 | configure_cache | ||
89 | |||
90 | chown -R tool:tool /run/user/$tool_uid | ||
23 | 91 | ||
24 | [[ $# -eq 0 ]] && set -- "bash" | 92 | [[ $# -eq 0 ]] && set -- "bash" |
25 | exec "$@" | 93 | cmd="$(which "$1")" |
94 | shift | ||
95 | |||
96 | start-stop-daemon --chuid tool:tool --start -d $PWD -u tool --exec "$cmd" "$@" | ... | ... |
wrapper.sh
0 → 100755
1 | #!/usr/bin/env bash | ||
2 | |||
3 | set -e | ||
4 | |||
5 | declare -a docker_args=(--rm -i) | ||
6 | |||
7 | [[ -t 0 ]] && docker_args+=(-t) | ||
8 | |||
9 | docker_args+=(--network host) | ||
10 | docker_args+=(-v /:/srv/host-root) | ||
11 | docker_args+=(-w /srv/host-root/$PWD) | ||
12 | [[ $SSH_AUTH_SOCK ]] && docker_args+=(-v "$SSH_AUTH_SOCK:$SSH_AUTH_SOCK" -e "SSH_AUTH_SOCK=$SSH_AUTH_SOCK") | ||
13 | if [[ $KUBECONFIG ]]; then | ||
14 | docker_args+=(-v "$KUBECONFIG:/srv/host-root/$KUBECONFIG" -e "KUBECONFIG=/srv/host-root/$KUBECONFIG") | ||
15 | elif [[ $HOME/.kube/config ]]; then | ||
16 | docker_args+=(-e "KUBECONFIG=/srv/host-root/$HOME/.kube/config") | ||
17 | fi | ||
18 | |||
19 | docker_args+=(-e MAP_UID="$(id -u)" -e MAP_GROUPS="$(id -G)") | ||
20 | |||
21 | docker run "${docker_args[@]}" brainfood-tools | ||
22 |
-
Please register or sign in to post a comment