Skip to content

Latest commit

 

History

History
128 lines (101 loc) · 3.26 KB

README.md

File metadata and controls

128 lines (101 loc) · 3.26 KB

CLI Logo

07. CLI Tools

CLI Banner

In this Tutorial we are going to prepare our cli.sh script, to download all necessary CLI tools from GitHub. The amount of tools will grow and will be added to that script.

Video

In this video I demonstrate how I prepared our cli.sh script, to download all necessary CLI tools from GitHub.

Video

Links

Prerequisites

Version Script

  • I have defined version Environment Variables in SS/version/cli/version.sh

    export TF_VAR_VERSION_CLI_GITLEAKS="8.18.0" # https://github.com/gitleaks/gitleaks/releases
    export TF_VAR_VERSION_CLI_HELM="3.12.3" # https://github.com/helm/helm/releases
    export TF_VAR_VERSION_CLI_JQ="1.6" # https://github.com/jqlang/jq/releases
    export TF_VAR_VERSION_CLI_K6="0.46.0" # https://github.com/grafana/k6/releases
    export TF_VAR_VERSION_CLI_KIND="0.20.0" # https://github.com/kubernetes-sigs/kind/releases
    export TF_VAR_VERSION_CLI_KUBECTL="1.28.2" # https://kubernetes.io/docs/tasks/tools/
    export TF_VAR_VERSION_CLI_LYCHEE="0.13.0" # https://github.com/lycheeverse/lychee/releases
    export TF_VAR_VERSION_CLI_MOVE2KUBE="0.3.10" # https://github.com/konveyor/move2kube/releases
    export TF_VAR_VERSION_CLI_POPEYE="0.11.1" # https://github.com/derailed/popeye/releases
    export TF_VAR_VERSION_CLI_STEP="0.24.4" # https://github.com/smallstep/cli/releases
    export TF_VAR_VERSION_CLI_TERRAFORM="1.5.7" # https://github.com/hashicorp/terraform/releases

The CLI Script

  • I have created the script for Downloading the CLI tools in SS/cli.sh.

    Tutorials/SS/SS/cli.sh

    Lines 1 to 262 in 6c2f4e4

    #!/bin/bash
    OSTYPE_LINUX="linux-gnu"
    OSTYPE_WINDOWS="msys"
    CLI_NAME_GITLEAKS="gitleaks"
    CLI_NAME_HELM="helm"
    CLI_NAME_JQ="jq"
    CLI_NAME_K6="k6"
    CLI_NAME_KIND="kind"
    CLI_NAME_KUBECTL="kubectl"
    CLI_NAME_LYCHEE="lychee"
    CLI_NAME_MOVE2KUBE="move2kube"
    CLI_NAME_POPEYE="popeye"
    CLI_NAME_STEP="step"
    CLI_NAME_TERRAFORM="terraform"
    cli_install_gitleaks() {
    if ! compgen -G "$TF_VAR_PATH_CLI/$CLI_NAME_GITLEAKS*" > /dev/null; then
    if [ $OSTYPE == $OSTYPE_LINUX ]; then
    curl -sSLo $TF_VAR_PATH_CLI/$CLI_NAME_GITLEAKS.tar.gz https://github.com/$CLI_NAME_GITLEAKS/$CLI_NAME_GITLEAKS/releases/download/v$TF_VAR_VERSION_CLI_GITLEAKS/${CLI_NAME_GITLEAKS}_${TF_VAR_VERSION_CLI_GITLEAKS}_linux_x32.tar.gz
    tar -zxf $TF_VAR_PATH_CLI/$CLI_NAME_GITLEAKS.tar.gz -C $TF_VAR_PATH_CLI $CLI_NAME_GITLEAKS
    rm -f $TF_VAR_PATH_CLI/$CLI_NAME_GITLEAKS.tar.gz
    chmod +x $TF_VAR_PATH_CLI/$CLI_NAME_GITLEAKS
    elif [ $OSTYPE == $OSTYPE_WINDOWS ]; then
    curl -sSLo $TF_VAR_PATH_CLI/$CLI_NAME_GITLEAKS.zip https://github.com/$CLI_NAME_GITLEAKS/$CLI_NAME_GITLEAKS/releases/download/v$TF_VAR_VERSION_CLI_GITLEAKS/${CLI_NAME_GITLEAKS}_${TF_VAR_VERSION_CLI_GITLEAKS}_windows_x32.zip
    unzip -oqj $TF_VAR_PATH_CLI/$CLI_NAME_GITLEAKS.zip $CLI_NAME_GITLEAKS.exe -d $TF_VAR_PATH_CLI
    rm -f $TF_VAR_PATH_CLI/$CLI_NAME_GITLEAKS.zip
    chmod +x $TF_VAR_PATH_CLI/$CLI_NAME_GITLEAKS.exe
    fi
    echo -n ""
    else
    echo -n ""
    fi
    [ $INITIALIZE == false ] && echo $CLI_NAME_GITLEAKS" "$(gitleaks version)
    }
    cli_install_helm() {
    if ! compgen -G "$TF_VAR_PATH_CLI/$CLI_NAME_HELM*" > /dev/null; then
    if [ $OSTYPE == $OSTYPE_LINUX ]; then
    curl -sSLo $TF_VAR_PATH_CLI/$CLI_NAME_HELM.tar.gz https://get.$CLI_NAME_HELM.sh/$CLI_NAME_HELM-v$TF_VAR_VERSION_CLI_HELM-linux-amd64.tar.gz
    tar -zxf $TF_VAR_PATH_CLI/$CLI_NAME_HELM.tar.gz -C $TF_VAR_PATH_CLI linux-amd64/$CLI_NAME_HELM --strip-components 1
    rm -f $TF_VAR_PATH_CLI/$CLI_NAME_HELM.tar.gz
    chmod +x $TF_VAR_PATH_CLI/$CLI_NAME_HELM
    elif [ $OSTYPE == $OSTYPE_WINDOWS ]; then
    curl -sSLo $TF_VAR_PATH_CLI/$CLI_NAME_HELM.zip https://get.$CLI_NAME_HELM.sh/$CLI_NAME_HELM-v$TF_VAR_VERSION_CLI_HELM-windows-amd64.zip
    unzip -oqj $TF_VAR_PATH_CLI/$CLI_NAME_HELM.zip windows-amd64/$CLI_NAME_HELM.exe -d $TF_VAR_PATH_CLI
    rm -f $TF_VAR_PATH_CLI/$CLI_NAME_HELM.zip
    chmod +x $TF_VAR_PATH_CLI/$CLI_NAME_HELM.exe
    fi
    echo -n ""
    else
    echo -n ""
    fi
    [ $INITIALIZE == false ] && echo $CLI_NAME_HELM" "$(helm version)
    }
    cli_install_jq() {
    if ! compgen -G "$TF_VAR_PATH_CLI/$CLI_NAME_JQ*" > /dev/null; then
    if [ $OSTYPE == $OSTYPE_LINUX ]; then
    curl -sSLo $TF_VAR_PATH_CLI/$CLI_NAME_JQ https://github.com/jqlang/$CLI_NAME_JQ/releases/download/$CLI_NAME_JQ-$TF_VAR_VERSION_CLI_JQ/$CLI_NAME_JQ-linux64
    chmod +x $TF_VAR_PATH_CLI/$CLI_NAME_JQ
    elif [ $OSTYPE == $OSTYPE_WINDOWS ]; then
    curl -sSLo $TF_VAR_PATH_CLI/$CLI_NAME_JQ.exe https://github.com/jqlang/$CLI_NAME_JQ/releases/download/$CLI_NAME_JQ-$TF_VAR_VERSION_CLI_JQ/$CLI_NAME_JQ-win64.exe
    chmod +x $TF_VAR_PATH_CLI/$CLI_NAME_JQ.exe
    fi
    echo -n ""
    else
    echo -n ""
    fi
    [ $INITIALIZE == false ] && echo $(jq --version)
    }
    cli_install_k6() {
    if ! compgen -G "$TF_VAR_PATH_CLI/$CLI_NAME_K6*" > /dev/null; then
    if [ $OSTYPE == $OSTYPE_LINUX ]; then
    curl -sSLo $TF_VAR_PATH_CLI/$CLI_NAME_K6.tar.gz https://github.com/grafana/$CLI_NAME_K6/releases/download/v$TF_VAR_VERSION_CLI_K6/$CLI_NAME_K6-v$TF_VAR_VERSION_CLI_K6-linux-amd64.tar.gz
    tar -zxf $TF_VAR_PATH_CLI/$CLI_NAME_K6.tar.gz -C $TF_VAR_PATH_CLI $CLI_NAME_K6-v$TF_VAR_VERSION_CLI_K6-linux-amd64/$CLI_NAME_K6 --strip-components 1
    rm -f $TF_VAR_PATH_CLI/$CLI_NAME_K6.tar.gz
    chmod +x $TF_VAR_PATH_CLI/$CLI_NAME_K6
    elif [ $OSTYPE == $OSTYPE_WINDOWS ]; then
    curl -sSLo $TF_VAR_PATH_CLI/$CLI_NAME_K6.zip https://github.com/grafana/$CLI_NAME_K6/releases/download/v$TF_VAR_VERSION_CLI_K6/$CLI_NAME_K6-v$TF_VAR_VERSION_CLI_K6-windows-amd64.zip
    unzip -oqj $TF_VAR_PATH_CLI/$CLI_NAME_K6.zip $CLI_NAME_K6-v$TF_VAR_VERSION_CLI_K6-windows-amd64/$CLI_NAME_K6.exe -d $TF_VAR_PATH_CLI
    rm -f $TF_VAR_PATH_CLI/$CLI_NAME_K6.zip
    chmod +x $TF_VAR_PATH_CLI/$CLI_NAME_K6.exe
    fi
    echo -n ""
    else
    echo -n ""
    fi
    [ $INITIALIZE == false ] && echo $(k6 version)
    }
    cli_install_kind() {
    if ! compgen -G "$TF_VAR_PATH_CLI/$CLI_NAME_KIND*" > /dev/null; then
    if [ $OSTYPE == $OSTYPE_LINUX ]; then
    curl -sSLo $TF_VAR_PATH_CLI/$CLI_NAME_KIND https://github.com/kubernetes-sigs/$CLI_NAME_KIND/releases/download/v$TF_VAR_VERSION_CLI_KIND/$CLI_NAME_KIND-linux-amd64
    chmod +x $TF_VAR_PATH_CLI/$CLI_NAME_KIND
    elif [ $OSTYPE == $OSTYPE_WINDOWS ]; then
    curl -sSLo $TF_VAR_PATH_CLI/$CLI_NAME_KIND.exe https://github.com/kubernetes-sigs/$CLI_NAME_KIND/releases/download/v$TF_VAR_VERSION_CLI_KIND/$CLI_NAME_KIND-windows-amd64
    chmod +x $TF_VAR_PATH_CLI/$CLI_NAME_KIND.exe
    fi
    echo -n ""
    else
    echo -n ""
    fi
    [ $INITIALIZE == false ] && echo $(kind version)
    }
    cli_install_kubectl() {
    if ! compgen -G "$TF_VAR_PATH_CLI/$CLI_NAME_KUBECTL*" > /dev/null; then
    if [ $OSTYPE == $OSTYPE_LINUX ]; then
    curl -sSLo $TF_VAR_PATH_CLI/$CLI_NAME_KUBECTL https://dl.k8s.io/release/v$TF_VAR_VERSION_CLI_KUBECTL/bin/linux/amd64/$CLI_NAME_KUBECTL
    chmod +x $TF_VAR_PATH_CLI/$CLI_NAME_KUBECTL
    elif [ $OSTYPE == $OSTYPE_WINDOWS ]; then
    curl -sSLo $TF_VAR_PATH_CLI/$CLI_NAME_KUBECTL.exe https://dl.k8s.io/release/v$TF_VAR_VERSION_CLI_KUBECTL/bin/windows/amd64/$CLI_NAME_KUBECTL.exe
    chmod +x $TF_VAR_PATH_CLI/$CLI_NAME_KUBECTL.exe
    fi
    echo -n ""
    else
    echo -n ""
    fi
    [ $INITIALIZE == false ] && echo $CLI_NAME_KUBECTL" "$(kubectl version --client=false)
    }
    cli_install_lychee() {
    if ! compgen -G "$TF_VAR_PATH_CLI/$CLI_NAME_LYCHEE*" > /dev/null; then
    if [ $OSTYPE == $OSTYPE_LINUX ]; then
    curl -sSLo $TF_VAR_PATH_CLI/$CLI_NAME_LYCHEE.tar.gz https://github.com/lycheeverse/$CLI_NAME_LYCHEE/releases/download/v$TF_VAR_VERSION_CLI_LYCHEE/$CLI_NAME_LYCHEE-v$TF_VAR_VERSION_CLI_LYCHEE-x86_64-unknown-linux-gnu.tar.gz
    tar -zxf $TF_VAR_PATH_CLI/$CLI_NAME_LYCHEE.tar.gz -C $TF_VAR_PATH_CLI
    rm -f $TF_VAR_PATH_CLI/$CLI_NAME_LYCHEE.tar.gz
    chmod +x $TF_VAR_PATH_CLI/$CLI_NAME_LYCHEE
    elif [ $OSTYPE == $OSTYPE_WINDOWS ]; then
    curl -sSLo $TF_VAR_PATH_CLI/$CLI_NAME_LYCHEE.exe https://github.com/lycheeverse/$CLI_NAME_LYCHEE/releases/download/v$TF_VAR_VERSION_CLI_LYCHEE/$CLI_NAME_LYCHEE-v$TF_VAR_VERSION_CLI_LYCHEE-windows-x86_64.exe
    chmod +x $TF_VAR_PATH_CLI/$CLI_NAME_LYCHEE.exe
    fi
    echo -n ""
    else
    echo -n ""
    fi
    [ $INITIALIZE == false ] && echo $(lychee --version)
    }
    cli_install_move2kube() {
    if ! compgen -G "$TF_VAR_PATH_CLI/$CLI_NAME_MOVE2KUBE*" > /dev/null; then
    if [ $OSTYPE == $OSTYPE_LINUX ]; then
    curl -sSLo $TF_VAR_PATH_CLI/$CLI_NAME_MOVE2KUBE.tar.gz https://github.com/konveyor/$CLI_NAME_MOVE2KUBE/releases/download/v$TF_VAR_VERSION_CLI_MOVE2KUBE/move2kube-v$TF_VAR_VERSION_CLI_MOVE2KUBE-linux-amd64.tar.gz
    tar -zxf $TF_VAR_PATH_CLI/$CLI_NAME_MOVE2KUBE.tar.gz -C $TF_VAR_PATH_CLI $CLI_NAME_MOVE2KUBE/$CLI_NAME_MOVE2KUBE --strip-components 1
    rm -f $TF_VAR_PATH_CLI/$CLI_NAME_MOVE2KUBE.tar.gz
    chmod +x $TF_VAR_PATH_CLI/$CLI_NAME_MOVE2KUBE
    elif [ $OSTYPE == $OSTYPE_WINDOWS ]; then
    curl -sSLo $TF_VAR_PATH_CLI/$CLI_NAME_MOVE2KUBE.zip https://github.com/konveyor/$CLI_NAME_MOVE2KUBE/releases/download/v$TF_VAR_VERSION_CLI_MOVE2KUBE/move2kube-v$TF_VAR_VERSION_CLI_MOVE2KUBE-windows-amd64.zip
    unzip -oqj $TF_VAR_PATH_CLI/$CLI_NAME_MOVE2KUBE.zip $CLI_NAME_MOVE2KUBE/$CLI_NAME_MOVE2KUBE.exe -d $TF_VAR_PATH_CLI
    rm -f $TF_VAR_PATH_CLI/$CLI_NAME_MOVE2KUBE.zip
    chmod +x $TF_VAR_PATH_CLI/$CLI_NAME_MOVE2KUBE.exe
    fi
    echo -n ""
    else
    echo -n ""
    fi
    [ $INITIALIZE == false ] && echo $CLI_NAME_MOVE2KUBE" "$(move2kube version)
    }
    cli_install_popeye() {
    if ! compgen -G "$TF_VAR_PATH_CLI/$CLI_NAME_POPEYE*" > /dev/null; then
    if [ $OSTYPE == $OSTYPE_LINUX ]; then
    curl -sSLo $TF_VAR_PATH_CLI/$CLI_NAME_POPEYE.tar.gz https://github.com/derailed/$CLI_NAME_POPEYE/releases/download/v$TF_VAR_VERSION_CLI_POPEYE/${CLI_NAME_POPEYE}_Linux_x86_64.tar.gz
    tar -zxf $TF_VAR_PATH_CLI/$CLI_NAME_POPEYE.tar.gz -C $TF_VAR_PATH_CLI $CLI_NAME_POPEYE
    rm -f $TF_VAR_PATH_CLI/$CLI_NAME_POPEYE.tar.gz
    chmod +x $TF_VAR_PATH_CLI/$CLI_NAME_POPEYE
    elif [ $OSTYPE == $OSTYPE_WINDOWS ]; then
    curl -sSLo $TF_VAR_PATH_CLI/$CLI_NAME_POPEYE.tar.gz https://github.com/derailed/$CLI_NAME_POPEYE/releases/download/v$TF_VAR_VERSION_CLI_POPEYE/${CLI_NAME_POPEYE}_Windows_x86_64.tar.gz
    tar -zxf $TF_VAR_PATH_CLI/$CLI_NAME_POPEYE.tar.gz -C $TF_VAR_PATH_CLI $CLI_NAME_POPEYE.exe
    rm -f $TF_VAR_PATH_CLI/$CLI_NAME_POPEYE.tar.gz
    chmod +x $TF_VAR_PATH_CLI/$CLI_NAME_POPEYE.exe
    fi
    echo -n ""
    else
    echo -n ""
    fi
    [ $INITIALIZE == false ] && echo $CLI_NAME_POPEYE $TF_VAR_VERSION_CLI_POPEYE
    }
    cli_install_step() {
    if ! compgen -G "$TF_VAR_PATH_CLI/$CLI_NAME_STEP*" > /dev/null; then
    if [ $OSTYPE == $OSTYPE_LINUX ]; then
    curl -sSLo $TF_VAR_PATH_CLI/$CLI_NAME_STEP.tar.gz https://dl.smallstep.com/gh-release/cli/gh-release-header/v0.24.4/${CLI_NAME_STEP}_linux_${TF_VAR_VERSION_CLI_STEP}_amd64.tar.gz
    tar -zxf $TF_VAR_PATH_CLI/$CLI_NAME_STEP.tar.gz -C $TF_VAR_PATH_CLI ${CLI_NAME_STEP}_$TF_VAR_VERSION_CLI_STEP/bin/$CLI_NAME_STEP --strip-components 2
    rm -f $TF_VAR_PATH_CLI/$CLI_NAME_STEP.tar.gz
    chmod +x $TF_VAR_PATH_CLI/$CLI_NAME_STEP
    elif [ $OSTYPE == $OSTYPE_WINDOWS ]; then
    curl -sSLo $TF_VAR_PATH_CLI/$CLI_NAME_STEP.zip https://dl.smallstep.com/gh-release/cli/gh-release-header/v0.24.4/${CLI_NAME_STEP}_windows_${TF_VAR_VERSION_CLI_STEP}_amd64.zip
    unzip -oqj $TF_VAR_PATH_CLI/$CLI_NAME_STEP.zip ${CLI_NAME_STEP}_$TF_VAR_VERSION_CLI_STEP/bin/$CLI_NAME_STEP.exe -d $TF_VAR_PATH_CLI
    rm -f $TF_VAR_PATH_CLI/$CLI_NAME_STEP.zip
    chmod +x $TF_VAR_PATH_CLI/$CLI_NAME_STEP.exe
    fi
    echo -n ""
    else
    echo -n ""
    fi
    [ $INITIALIZE == false ] && echo $(step version)
    }
    cli_install_terraform() {
    if ! compgen -G "$TF_VAR_PATH_CLI/$CLI_NAME_TERRAFORM*" > /dev/null; then
    if [ $OSTYPE == $OSTYPE_LINUX ]; then
    curl -sSLo $TF_VAR_PATH_CLI/$CLI_NAME_TERRAFORM.zip https://releases.hashicorp.com/$CLI_NAME_TERRAFORM/$TF_VAR_VERSION_CLI_TERRAFORM/${CLI_NAME_TERRAFORM}_${TF_VAR_VERSION_CLI_TERRAFORM}_linux_386.zip
    unzip -oqj $TF_VAR_PATH_CLI/$CLI_NAME_TERRAFORM.zip $CLI_NAME_TERRAFORM -d $TF_VAR_PATH_CLI
    rm -f $TF_VAR_PATH_CLI/$CLI_NAME_TERRAFORM.zip
    chmod +x $TF_VAR_PATH_CLI/$CLI_NAME_TERRAFORM
    elif [ $OSTYPE == $OSTYPE_WINDOWS ]; then
    curl -sSLo $TF_VAR_PATH_CLI/$CLI_NAME_TERRAFORM.zip https://releases.hashicorp.com/$CLI_NAME_TERRAFORM/$TF_VAR_VERSION_CLI_TERRAFORM/${CLI_NAME_TERRAFORM}_${TF_VAR_VERSION_CLI_TERRAFORM}_windows_386.zip
    unzip -oqj $TF_VAR_PATH_CLI/$CLI_NAME_TERRAFORM.zip $CLI_NAME_TERRAFORM.exe -d $TF_VAR_PATH_CLI
    rm -f $TF_VAR_PATH_CLI/$CLI_NAME_TERRAFORM.zip
    chmod +x $TF_VAR_PATH_CLI/$CLI_NAME_TERRAFORM.exe
    fi
    echo -n ""
    else
    echo -n ""
    fi
    [ $INITIALIZE == false ] && echo $(terraform version)
    }
    echo $DIV
    echo ⬇️ " Downloading CLI tools"
    echo $DIV
    # cli_install_gitleaks
    # cli_install_helm
    # cli_install_jq
    # cli_install_k6
    # cli_install_kind
    # cli_install_kubectl
    # cli_install_lychee
    cli_install_move2kube
    # cli_install_popeye
    # cli_install_step
    # cli_install_terraform
    [ $INITIALIZE != false ] && echo

Execute the Script

  • Add Execution Privileges to cli.sh

    chmod +x $TF_VAR_PATH_MAIN/cli.sh
  • Execute the script

    $TF_VAR_PATH_MAIN/cli.sh

Calling from .bash_profile

  • I have added a call to the cli.sh Script in our start.sh file:
    # start.sh snippet
    
    # ...
    
    # -- INSTALL CLI TOOLS
    install_cli () {
      chmod +x $TF_VAR_PATH_MAIN/cli.sh
      $TF_VAR_PATH_MAIN/cli.sh
    }
    
    # ...
    
    load_env_vars
    initialize
    install_cli # ⬅️ added
    finalize
    
    export INITIALIZE=false

Tool Demos

  • Gitleaks
    gitleaks detect -v -s $TF_VAR_PATH_MAIN --redact
  • Helm
    helm create $TF_VAR_PATH_APP/helm/vaultwarden
  • JQ
    echo '{"foo": 0}' | jq
  • K6
    k6
  • Kind
    kind create cluster
    # kind delete cluster
  • KubeCtl
    k cluster-info
  • Lychee
    lychee . --offline
  • Podman
    podman info
  • Popeye
    popeye
  • Step
    step base64 $TF_VAR_VAULTWARDEN_ADMIN_TOKEN
  • Terraform
    t workspace list