Skip to content

Instantly share code, notes, and snippets.

@navarrothiago
Created June 1, 2021 23:45
Show Gist options
  • Save navarrothiago/0f609f30fe9a3e8ad2cf7b8ebb22cdf7 to your computer and use it in GitHub Desktop.
Save navarrothiago/0f609f30fe9a3e8ad2cf7b8ebb22cdf7 to your computer and use it in GitHub Desktop.

Revisions

  1. navarrothiago created this gist Jun 1, 2021.
    91 changes: 91 additions & 0 deletions tmux_kafka.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    #!/bin/bash

    # ==============================================
    # Author: Thiago Navarro
    # email: [email protected]
    #
    # Create a grid (panes) for kafka example
    # Login k8s1
    # https://docs.cilium.io/en/v1.10/gettingstarted/kafka/#gs-kafka
    #
    # Dependencies: tmux, cilium, vagrant
    #
    # TODO:
    #=============================================

    CMD_LOGIN_K8s1="ssh [email protected] -p 2222"
    CMD_LOGIN_EMPIRE_HQ="HQ_POD=\$(kubectl get pods -l app=empire-hq -o jsonpath='{.items[0].metadata.name}') && kubectl exec -it \$HQ_POD -- sh -c \"PS1=\\\"empire-hq \$\\\" /bin/bash\""
    CMD_LOGIN_EMPIRE_BACKUP="BACKUP_POD=\$(kubectl get pods -l app=empire-backup -o jsonpath='{.items[0].metadata.name}') && kubectl exec -it \$BACKUP_POD -- sh -c \"PS1=\\\"empire-backup \$\\\" /bin/bash\""
    CMD_LOGIN_OUTPUT_8888="OUTPOST_8888_POD=\$(kubectl get pods -l outpostid=8888 -o jsonpath='{.items[0].metadata.name}') && kubectl exec -it \$OUTPOST_8888_POD -- sh -c \"PS1=\\\"outpost-8888 \$\\\" /bin/bash\""
    CMD_LOGIN_OUTPUT_9999="OUTPOST_9999_POD=\$(kubectl get pods -l outpostid=9999 -o jsonpath='{.items[0].metadata.name}') && kubectl exec -it \$OUTPOST_9999_POD -- sh -c \"PS1=\\\"outpost-9999 \$\\\" /bin/bash\""

    #Create remote session with panes
    initialize() {
    echo "Initializing on session "${session_name}"..."

    # Initialize windowns and panes
    tmux kill-session -t $session_name -n testbed 2>/dev/null
    # Create window 0
    tmux -2 new-session -d -s $session_name
    tmux rename-window -t 0 'kafka'

    # Configure layout
    tmux split-window -t $session_name:0.0 -v \; selectl tiled
    tmux split-window -t $session_name:0.1 -v \; selectl tiled
    tmux split-window -t $session_name:0.2 -v \; selectl tiled
    tmux split-window -t $session_name:0.3 -v \; selectl tiled
    tmux select-layout -t $session_name even-vertical

    local k8s1_pane=(0 1 2 3 4)

    # Select window 0 pane 2
    tmux select-pane -t "$session_name":0.0

    # Login K8s1
    for value in "${k8s1_pane[@]}"; do
    tmux send-keys -t $session_name:0."${value}" "${CMD_LOGIN_K8s1}" C-m
    done

    # Run commands k8s1
    tmux send-keys -t $session_name:0.${k8s1_pane[1]} "${CMD_LOGIN_EMPIRE_HQ}" C-m
    tmux send-keys -t $session_name:0.${k8s1_pane[2]} "${CMD_LOGIN_EMPIRE_BACKUP}" C-m
    tmux send-keys -t $session_name:0.${k8s1_pane[3]} "${CMD_LOGIN_OUTPUT_8888}" C-m
    tmux send-keys -t $session_name:0.${k8s1_pane[4]} "${CMD_LOGIN_OUTPUT_9999}" C-m
    }

    attach() {
    echo "Attaching on session "${session_name}"..."
    tmux select-pane -t $session_name:0.0
    tmux -2 attach-session -t $session_name
    }

    stop() {
    echo "Stopping on session "${session_name}"..."
    }

    force_kill() {
    echo "Killing on session "${session_name}"..."
    #sleep 1
    tmux kill-session -t $session_name 2>/dev/null
    }

    main() {
    # set -o errexit
    set -o pipefail
    set -o nounset
    # set -x

    local -r dirname="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
    local -r filename="${dirname}/$(basename "${BASH_SOURCE[0]}")"
    local -r session_name="cilium-kafka"

    # Enable nested tmux session.
    unset TMUX

    initialize
    attach
    stop
    force_kill
    }

    main "$@"