Skip to content

Commit

Permalink
Reorganize install_on_demand scripts. (#102)
Browse files Browse the repository at this point in the history
Introduce `etcdctl` and `k9s` on demand scripts.
Automatically generate `etcdctl` connectivity parameters when `wrapper` runs.
  • Loading branch information
mimiteto authored Apr 10, 2024
1 parent e5a3175 commit 80c162e
Show file tree
Hide file tree
Showing 13 changed files with 249 additions and 83 deletions.
31 changes: 23 additions & 8 deletions dockerfile-configs/common-components.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,34 +89,49 @@
from: ./hacks
to: /hacks
info: ~
- name: install_on_demand
from: ./install_on_demand
to: /etc/bash/install_on_demand
info: ~

- bash:
- name: /nonroot/hacks
command: |
mkdir -p /nonroot && mkdir -p /nonroot/hacks && chown 65532:65532 -R /nonroot
info: A directory intended to be used by nonroot users.
- name: /opt/bin
command: |
mkdir -p /opt/bin && chmod -R 777 /opt/bin
info: A directory intended to allow common install point for ad-hoc install scripts

- copy:
- name: install_k9s
from: ./hacks/install_k9s
to: /nonroot/hacks
command: --chown=65532:65532
info: Bash script which installs the `k9s` tool in the container. If you are running this container as non-root, execute the `install_k9s` script in the `/nonroot/hacks` directory.
- name: install_etcdctl
from: ./hacks/install_etcdctl
to: /nonroot/hacks
command: --chown=65532:65532
info: Bash script which installs the `etcdctl` tool in the container. If you are running this container as non-root, execute the `install-etcdctl` script in the `/nonroot/hacks` directory.
info: Bash script which installs the `etcdctl` tool in the container. If you are running this container as non-root, execute the `install_etcdctl` script in the `/nonroot/hacks` directory.

- bash:
- name: bash-completion
command: |
echo "" >> /root/.bashrc;\
echo "source /etc/profile.d/bash_completion.sh" >> /root/.bashrc
info: ~
- name: table
- name: install_on_demand
command: |
echo "source /root/dotfiles/.install_on_demand/.table" >> /root/dotfiles/.bashrc
info: Helpful tool that can be used to simplify the analysis of iptables entries. Pass a string argument to filter the output via grep.
- name: wg
command : |
echo "source /root/dotfiles/.install_on_demand/.wireguard" >> /root/dotfiles/.bashrc
info: Command line tool for the wireguard VPN.
echo "source /etc/bash/install_on_demand/.shrc" >> /etc/bash.bashrc
info: |
A set of "on demand" install scripts that are helpful for troubleshooting.
Currently supported are:
table: Simplifies iptables analysis. Pass string to filter output via grep.
wg: Command line tool for the wireguard VPN.
etcdctl: Command line tool for manual operations on etcdctl
k9s: TUI for administration of k8s clusters
- name: dotfiles
command: |
echo "" >> /root/.bashrc;\
Expand Down
1 change: 1 addition & 0 deletions dotfiles/.bashrc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ if [[ ! -f "$git_config_personal" ]]; then
[[ -n "$dotfiles_user_email" ]] && echo -e "[user]\n email = $dotfiles_user_email" >> "$git_config_personal"
fi

mkdir -p "${DOTFILES_HOME}/bin" "${DOTFILES_HOME}/scripts"
# add bin dir to PATH
[[ -d "$DOTFILES_HOME/bin" ]] && PATH="$DOTFILES_HOME/bin:$PATH"

Expand Down
19 changes: 0 additions & 19 deletions dotfiles/.install_on_demand/.wireguard

This file was deleted.

18 changes: 12 additions & 6 deletions generator/lib/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class BaseComponentConfig:
def __init__(self, name, info):
self.name = name
self.info = info
self.info = info.replace("\n", "\\\\n") if info is not None else None

def get_info(self):
return self.info
Expand All @@ -27,11 +27,11 @@ def __init__(self, config):

class DictComponentConfig(BaseComponentConfig):
required_keys = [
{"key": "name", "types":(str)},
{"key": "name", "types": (str)},
]
optional_keys = [
{"key": "info", "types":(str, type(None))},
{"key": "provides", "types":(str, list, type(None))}
{"key": "info", "types": (str, type(None))},
{"key": "provides", "types": (str, list, type(None))}
]

def __init__(self, config):
Expand Down Expand Up @@ -114,6 +114,7 @@ class AptRepoConfig(DictComponentConfig):
{"key": "repo", "types": (str)},
{"key": "keyring", "types": (str)}
]

def __init__(self, config):
DictComponentConfig.__init__(self, config)
self.release_prefix = config.get("release-prefix", "")
Expand All @@ -139,7 +140,13 @@ def get_key_url(self):


class ComponentConfigParser:
registered_classes = [StringComponentConfig, DictComponentConfig, BashCommandConfig, ToolConfig, AptRepoConfig]
registered_classes = [
StringComponentConfig,
DictComponentConfig,
BashCommandConfig,
ToolConfig,
AptRepoConfig,
]

def __init__(self, *argv):
for component_class in argv:
Expand All @@ -162,4 +169,3 @@ def parse_components(self, component_configs):
raise lastErr
components.append(component)
return components

12 changes: 8 additions & 4 deletions hacks/ghelp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def retrieve_hacks_info():
return tools


ghelp_info_path="/var/lib/ghelp_info"
ghelp_info_path = "/var/lib/ghelp_info"
ghelp_info = None
with open(ghelp_info_path, "r") as f:
ghelp_info = json.load(f)
Expand All @@ -72,7 +72,7 @@ if ghelp_info is None:
print("Failed to load ghelp info")
exit(1)

apttools = retrieve_packages_info(ghelp_info["apt"], "apt", "show", "=", "\n\n" , "Package", "Version", "Description")
apttools = retrieve_packages_info(ghelp_info["apt"], "apt", "show", "=", "\n\n", "Package", "Version", "Description")
piptools = retrieve_packages_info(ghelp_info["pip"], "pip", "show", "==", "---", "Name", "Version", "Summary")
downloaded_tools = retrieve_downloaded_tools_info(ghelp_info["downloaded"])
hack_tools = retrieve_hacks_info()
Expand All @@ -90,8 +90,12 @@ max_info_width = (terminal_width-10)//2
for i in range(len(table)):
table[i][0] = textwrap.fill(table[i][0], width=max_name_width)
if table[i][1] is not None:
table[i] [1] = textwrap.fill(table[i][1], width=max_name_width)
table[i][2] = textwrap.fill(table[i][2], width=max_info_width)
table[i][1] = textwrap.fill(table[i][1], width=max_name_width)
table[i][2] = textwrap.fill(
table[i][2].replace("\\n", "\n"),
width=max_info_width,
replace_whitespace=False
)

table_headers = ["TOOL/PACKAGE", "VERSION", "NOTES"]
table_headers[0] = textwrap.fill(table_headers[0], width=max_name_width)
Expand Down
43 changes: 30 additions & 13 deletions hacks/install_etcdctl
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,47 @@ function show_help () {
}

function install () {
local version=$1
etcd_version="v3.4.26"
local version="${1:-${etcd_version}}"
local download_url
local arch
local platform
local pkg
local dest
local yellow="\033[0;33m"
local nc="\033[0m"
local tmp_dir

if [ -z "$version" ]
then # fetch v3.4.26
version="v3.4.26"
else
if [[ ! $version == v* ]]
then
version="v${version}"
tmp_dir="$(mktemp -d)"
mkdir -p "${tmp_dir}/dest"

dest="/opt/bin/etcdctl"
if uname="$(whoami 2> /dev/null)"; then
if [[ "${uname}" == "root" ]]; then
if [ -w "/usr/local/bin" ]; then
dest="/usr/local/bin/etcdctl"
fi
fi
fi

arch=$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')
arch=$(uname -m | sed 's/^x86_64$/amd64/;s/^aarch64$/arm64/')

echo "installing etcdctl version ${version}"
download_url="https://github.com/coreos/etcd/releases/download/${version}/etcd-${version}-linux-${arch}.tar.gz"
platform="$(uname -s | tr '[:upper:]' '[:lower:]')"
pkg="etcd-${version}-${platform}-${arch}"

curl -sL ${download_url} -o etcd-${version}-linux-${arch}.tar.gz && tar -zxvf etcd-${version}-linux-${arch}.tar.gz && mv etcd-${version}-linux-${arch}/etcdctl . && rm etcd-${version}-linux-${arch}.tar.gz && rm -r etcd-${version}-linux-${arch}
download_url="https://github.com/etcd-io/etcd/releases/download/${version}/${pkg}.tar.gz"

curl -sL "${download_url}" \
-o "${tmp_dir}/${pkg}.tar.gz" && \
tar -zxf "${tmp_dir}/${pkg}.tar.gz" -C "${tmp_dir}/dest" && \
mv "${tmp_dir}/dest/${pkg}/etcdctl" "${dest}" && \
rm -rf "${tmp_dir}"

echo -e "${yellow}"
echo "You can now start using etcdctl. Just execute \"etcdctl\" to use it. See https://etcd.io/docs/v3.4/dev-guide/interacting_v3/ for more details."
echo "This tool assumes that it is being run in an ephemeral container in a pod. If this is not the case then please ensure that you provide the correct ecrtificates, the correct endpoint and have all necessary accesses."
echo "This tool assumes that it is being run in an ephemeral container in a pod. If this is not the case, please ensure that you provide the correct certificates, the correct endpoint, and have all necessary accesses."
echo "Certificates to be passed to the command should be mounted onto any container in the pod having a shared process namespace. Please run \"ps -A\" to list all processes and then access the certificates using \"/proc/<proc number>/root/<file-path>\". Pass these certificate file paths to the etcdctl command :)"
echo -e "${nc}"
}

case "$1" in
Expand Down
55 changes: 40 additions & 15 deletions hacks/install_k9s
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,51 @@ function show_help () {
function install () {
local version=$1
local download_url
local arch=$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')

if [ -z "$version" ]
then # fetch latest
local latest_release=$(curl -sL https://api.github.com/repos/derailed/k9s/releases/latest)
version=$(echo "${latest_release}" | jq -r '.tag_name')
echo "installing latest version ${version}"
download_url=$(echo "${latest_release}" | jq -r --arg filename "k9s_Linux_${arch}.tar.gz" '.assets[] | select(.name == $filename) | .browser_download_url')
else
if [[ ! $version == v* ]]
then
version="v${version}"
local yellow="\033[0;33m"
local nc="\033[0m"
local arch
local platform
local pkg
local dest
local version_url="https://api.github.com/repos/derailed/k9s/releases/latest"

tmp_dir="$(mktemp -d)"
mkdir -p "${tmp_dir}/dest"

dest="/opt/bin/k9s"
if uname="$(whoami 2> /dev/null)"; then
if [[ "${uname}" == "root" ]]; then
if [ -w "/usr/local/bin" ]; then
dest="/usr/local/bin/k9s"
fi
fi
echo "installing k9s version ${version}"
download_url="https://github.com/derailed/k9s/releases/download/${version}/k9s_Linux_${arch}.tar.gz"
fi

curl -sL ${download_url} -o k9s.tar.gz && tar -zxvf k9s.tar.gz k9s && mv k9s /usr/local/bin/k9s && chmod 755 /usr/local/bin/k9s && rm k9s.tar.gz
arch=$(uname -m | sed 's/^x86_64$/amd64/;s/^aarch64$/arm64/')

platform="$(uname -s)"
pkg="k9s_${platform}_${arch}"

if [ -z "$version" ]; then # fetch latest
version=$(curl -sL "${version_url}" | jq -r '.tag_name')
fi

if [[ ! $version == v* ]]; then
version="v${version}"
fi

download_url="https://github.com/derailed/k9s/releases/download/${version}/${pkg}.tar.gz"

curl -sL "${download_url}" \
-o "${tmp_dir}/${pkg}.tar.gz" && \
tar -zxf "${tmp_dir}/${pkg}.tar.gz" -C "${tmp_dir}/dest" && \
mv "${tmp_dir}/dest/k9s" "${dest}" && \
chmod 755 "${dest}" && \
rm -rf "${tmp_dir}"

echo -e "${yellow}"
echo "You can now start using k9s. Just execute \"k9s\" to use it or \"k9s -n mynamespace\" to target a namespace. See https://github.com/derailed/k9s for more details."
echo -e "${nc}"
}

case "$1" in
Expand Down
11 changes: 5 additions & 6 deletions hacks/ops-pod
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ sanitize_hostname() {
}
name="$(sanitize_hostname "ops-pod-$(whoami)")"

default_image="europe-docker.pkg.dev/sap-se-gcp-k8s-delivery/releases-public/eu_gcr_io/gardener-project/gardener/ops-toolbelt:latest"
default_image="europe-docker.pkg.dev/gardener-project/releases/gardener/ops-toolbelt:latest"
function get_default_namespace() {
_namespace=$(kubectl config view -o jsonpath="{.contexts[?(@.name == \"$(kubectl config current-context)\")].context.namespace}")
echo "${_namespace:-default}"
Expand Down Expand Up @@ -149,7 +149,6 @@ $tolerations_array
containers:
- name: ops-pod
image: ${image}
imagePullPolicy: Always
command:
- sleep
- "43200"
Expand Down Expand Up @@ -183,12 +182,12 @@ while [[ $(kubectl -n $namespace get pods | sed -n -r "s/^$name.*Running.*$/Runn

# exec into pod (and chroot into node if a node was selected)
if [[ ${node_chroot} -eq ${TRUE} ]]; then
kubectl -n $namespace exec -ti $name -- bash -c "rm -rf /host/root/dotfiles 1> /dev/null; \
kubectl -n $namespace exec -ti $name -- bash -c 'rm -rf /host/root/dotfiles 1> /dev/null; \
cp -r /root/dotfiles /host/root 1> /dev/null; \
cp -r /hacks /host 1> /dev/null; rm -f /host/root/.bashrc; \
ln -s /root/dotfiles/.bashrc /host/root/.bashrc 1> /dev/null; export PATH=\"/hacks:$PATH\"; \
echo -e '\nBE CAREFUL!!! Node root directory mounted under / \n'; \
chroot /host /bin/bash"
ln -s /root/dotfiles/.bashrc /host/root/.bashrc 1> /dev/null; export PATH="/hacks:$PATH"; \
echo -e "\nBE CAREFUL!!! Node root directory mounted under / \n"; \
chroot /host /bin/bash'
else
kubectl -n $namespace exec -ti $name -- bash -c "echo -e '\nNode root dir is mounted under /host' >> /etc/motd; /bin/bash"
fi
Loading

0 comments on commit 80c162e

Please sign in to comment.