Skip to content

Instantly share code, notes, and snippets.

@sumansrivastava
Created June 19, 2024 11:40
Show Gist options
  • Select an option

  • Save sumansrivastava/c2ebd1cc3375f134a41d2b2872ae9816 to your computer and use it in GitHub Desktop.

Select an option

Save sumansrivastava/c2ebd1cc3375f134a41d2b2872ae9816 to your computer and use it in GitHub Desktop.
import subprocess
def run_command(command, shell=False):
"""Run a shell command and raise an error if it fails."""
subprocess.run(command, shell=shell, check=True)
def add_apt_key(url):
"""Add an APT key from a URL."""
run_command(["curl", "-fsSL", url, "|", "sudo", "apt-key", "add", "-"], shell=True)
def add_apt_repository(repo):
"""Add an APT repository."""
run_command(["sudo", "add-apt-repository", repo], shell=True)
def install_packages(packages):
"""Install APT packages."""
run_command(["sudo", "apt-get", "install", "-y"] + packages)
def install_kubectl():
"""Install kubectl."""
add_apt_key("https://packages.cloud.google.com/apt/doc/apt-key.gpg")
add_apt_repository("deb https://apt.kubernetes.io/ kubernetes-xenial main")
print("kubectl installation setup completed.")
def install_az_cli():
"""Install Azure CLI."""
run_command(["curl", "-sL", "https://aka.ms/InstallAzureCLIDeb", "|", "sudo", "bash"], shell=True)
print("Azure CLI installation setup completed.")
def install_helm():
"""Install Helm."""
add_apt_key("https://baltocdn.com/helm/signing.asc")
run_command(["echo", "deb https://baltocdn.com/helm/stable/debian/ all main", "|", "sudo", "tee", "/etc/apt/sources.list.d/helm-stable-debian.list"], shell=True)
print("Helm installation setup completed.")
def install_terraform():
"""Install Terraform."""
add_apt_key("https://apt.releases.hashicorp.com/gpg")
add_apt_repository("deb https://apt.releases.hashicorp.com $(lsb_release -cs) main")
print("Terraform installation setup completed.")
def update_and_install():
"""Update package lists and install all packages."""
run_command(["sudo", "apt-get", "update"])
install_packages(["apt-transport-https", "ca-certificates", "curl", "gnupg", "software-properties-common"])
install_packages(["kubectl", "helm", "terraform"])
print("All tools installed.")
def verify_installations():
"""Verify that all tools are installed and print their versions."""
tools = {
"kubectl": ["kubectl", "version", "--client", "--short"],
"az cli": ["az", "--version"],
"helm": ["helm", "version", "--short"],
"terraform": ["terraform", "--version"]
}
for tool, command in tools.items():
try:
version = subprocess.check_output(command, text=True)
print(f"{tool} version: {version.splitlines()[0]}")
except subprocess.CalledProcessError:
print(f"{tool} installation failed.")
if __name__ == "__main__":
install_kubectl()
install_az_cli()
install_helm()
install_terraform()
update_and_install()
verify_installations()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment