|
| 1 | +from invoke import task |
| 2 | +from os.path import join, exists |
| 3 | +from os import makedirs |
| 4 | +from shutil import copy, rmtree |
| 5 | +from subprocess import run |
| 6 | + |
| 7 | +from tasks.util.env import ( |
| 8 | + BIN_DIR, |
| 9 | + GLOBAL_BIN_DIR, |
| 10 | + K9S_VERSION, |
| 11 | +) |
| 12 | + |
| 13 | +from tasks.util.version import get_k8s_version |
| 14 | + |
| 15 | + |
| 16 | +def _download_binary(url, binary_name): |
| 17 | + makedirs(BIN_DIR, exist_ok=True) |
| 18 | + cmd = "curl -LO {}".format(url) |
| 19 | + run(cmd, shell=True, check=True, cwd=BIN_DIR) |
| 20 | + run("chmod +x {}".format(binary_name), shell=True, check=True, cwd=BIN_DIR) |
| 21 | + |
| 22 | + return join(BIN_DIR, binary_name) |
| 23 | + |
| 24 | + |
| 25 | +def _symlink_global_bin(binary_path, name): |
| 26 | + global_path = join(GLOBAL_BIN_DIR, name) |
| 27 | + if exists(global_path): |
| 28 | + print("Removing existing binary at {}".format(global_path)) |
| 29 | + run( |
| 30 | + "sudo rm -f {}".format(global_path), |
| 31 | + shell=True, |
| 32 | + check=True, |
| 33 | + ) |
| 34 | + |
| 35 | + print("Symlinking {} -> {}".format(global_path, binary_path)) |
| 36 | + run( |
| 37 | + "sudo ln -s {} {}".format(binary_path, name), |
| 38 | + shell=True, |
| 39 | + check=True, |
| 40 | + cwd=GLOBAL_BIN_DIR, |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +@task |
| 45 | +def install_kubectl(ctx, system=False): |
| 46 | + """ |
| 47 | + Install the k8s CLI (kubectl) |
| 48 | + """ |
| 49 | + k8s_ver = get_k8s_version() |
| 50 | + url = "https://dl.k8s.io/release/v{}/bin/linux/amd64/kubectl".format( |
| 51 | + k8s_ver |
| 52 | + ) |
| 53 | + |
| 54 | + binary_path = _download_binary(url, "kubectl") |
| 55 | + |
| 56 | + # Symlink for kubectl globally |
| 57 | + if system: |
| 58 | + _symlink_global_bin(binary_path, "kubectl") |
| 59 | + |
| 60 | + |
| 61 | +@task |
| 62 | +def install_k9s(ctx, system=False): |
| 63 | + """ |
| 64 | + Install the K9s CLI |
| 65 | + """ |
| 66 | + tar_name = "k9s_Linux_amd64.tar.gz" |
| 67 | + url = "https://github.com/derailed/k9s/releases/download/v{}/{}".format( |
| 68 | + K9S_VERSION, tar_name |
| 69 | + ) |
| 70 | + print(url) |
| 71 | + |
| 72 | + # Download the TAR |
| 73 | + workdir = "/tmp/k9s-csg" |
| 74 | + makedirs(workdir, exist_ok=True) |
| 75 | + |
| 76 | + cmd = "curl -LO {}".format(url) |
| 77 | + run(cmd, shell=True, check=True, cwd=workdir) |
| 78 | + |
| 79 | + # Untar |
| 80 | + run("tar -xf {}".format(tar_name), shell=True, check=True, cwd=workdir) |
| 81 | + |
| 82 | + # Copy k9s into place |
| 83 | + binary_path = join(BIN_DIR, "k9s") |
| 84 | + copy(join(workdir, "k9s"), binary_path) |
| 85 | + |
| 86 | + # Remove tar |
| 87 | + rmtree(workdir) |
| 88 | + |
| 89 | + # Symlink for k9s command globally |
| 90 | + if system: |
| 91 | + _symlink_global_bin(binary_path, "k9s") |
0 commit comments