Skip to content

Instantly share code, notes, and snippets.

@mtcoffee
Last active June 2, 2024 12:02
Show Gist options
  • Save mtcoffee/79744090a1c4ce1e0ac04d64df510f3b to your computer and use it in GitHub Desktop.
Save mtcoffee/79744090a1c4ce1e0ac04d64df510f3b to your computer and use it in GitHub Desktop.
Install AWX on K3s
#!/bin/bash
check_sudo() {
if [ "$(id -u)" -ne 0 ]
then echo "Please run as sudo"
exit
fi
}
# List of required packages
required_packages=("curl" "wget" "git" "vim")
# Function to check if a package is installed on Debian-based systems
check_debian() {
for package in "${required_packages[@]}"; do
if dpkg -l | grep -q "^ii $package "; then
echo "$package is installed."
else
echo "$package is NOT installed."
fi
done
}
# Function to check if a package is installed on Red Hat-based systems
check_redhat() {
for package in "${required_packages[@]}"; do
if rpm -qa | grep -q "^$package"; then
echo "$package is installed."
else
echo "$package is NOT installed."
fi
done
}
rhel_reqs() {
# If the host OS is RHEL-based, configure settings
sudo setenforce 0
sudo sed -i 's/^SELINUX=.*/SELINUX=permissive/' /etc/selinux/config
sudo systemctl disable firewalld --now
}
# Determine the platform and call the appropriate function
check_platform() {
if [ -f /etc/debian_version ]; then
echo "Debian-based system detected."
check_debian
elif [ -f /etc/redhat-release ]; then
echo "Red Hat-based system detected."
check_redhat
rhel_reqs
else
echo "Unsupported platform."
exit 1
fi
}
install_k3s_helm() {
echo "####Installing K3s####"
# Install K3s
curl -sfL https://get.k3s.io | sh -s - --write-kubeconfig-mode 644
# Install Helm
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 > install-helm.sh
chmod u+x install-helm.sh
./install-helm.sh
}
install_awx_on_k3s() {
# Install AWX Operator on K3s
export PATH=/usr/local/bin:$PATH
helm repo add awx-operator https://ansible.github.io/awx-operator/
helm repo update
helm install -n awx --create-namespace my-awx-operator awx-operator/awx-operator
# Wait for Traefik deployment to be complete
echo "####Waiting for Traefik deployment to be complete...#####"
while true; do
if kubectl -n kube-system get deploy traefik &> /dev/null; then
ready_replicas=$(kubectl -n kube-system get deploy traefik -o jsonpath='{.status.readyReplicas}')
if [[ ! -z "$ready_replicas" && "$ready_replicas" -ge 1 ]]; then
break
fi
fi
sleep 2
done
echo "Traefik deployment is complete."
echo "K3s installation (including Traefik) is complete."
# Wait for the AWX operator to come online
echo "####Waiting for the operator to come online####"
kubectl wait pod \
--timeout=300s \
--all \
--for=condition=Ready \
--namespace=awx
# Create AWX install traefik ingress
echo "####Installing AWX####"
kubectl apply -f - <<EOF
---
apiVersion: awx.ansible.com/v1beta1
kind: AWX
metadata:
name: ansible-awx
namespace: awx
spec:
service_type: ClusterIP
---
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: awx-ingress-redirect
namespace: awx
spec:
redirectScheme:
scheme: https
permanent: true
---
kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
name: awx-ingress
namespace: awx
annotations:
kubernetes.io/ingress.class: traefik
## MUST be <namespace>-<name>@kubernetescrd
traefik.ingress.kubernetes.io/router.middlewares: awx-awx-ingress-redirect@kubernetescrd
spec:
ingressClassName: traefik #override default if needed
rules:
- host: #optionally specify host name to allow sharing port 80 with other target services
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: ansible-awx-service
port:
name: http
EOF
}
check_install() {
echo "####Waiting and Checking Install####"
# Define variables for website monitoring
URL="https://localhost" # Replace with the URL of the site you want to monitor
RETRY_COUNT=5
RETRY_DELAY=30
# Function to check website status
check_website() {
STATUS_CODE=$(curl -k --head -X GET --retry $RETRY_COUNT --retry-connrefused --retry-delay $RETRY_DELAY -s -o /dev/null -w "%{http_code}" $URL)
if [ "$STATUS_CODE" -eq 200 ]; then
echo -e "AWX Site is online.\nNow create an admin user using these commands:\nawxPod=\$(kubectl get pods -n awx --no-headers | grep '^ansible-awx-web' | awk '{print \$1}')\nkubectl exec pod/\$awxPod -n awx --container ansible-awx-web -it awx-manage createsuperuser"
return 0
else
echo -e "$(date): $URL is not available yet. Status code: $STATUS_CODE. For verbose details check logs with: \nkubectl logs -f deployments/awx-operator-controller-manager -c awx-manager -n awx"
return 1
fi
}
# Main loop to check the website status until it is online
while true; do
if check_website; then
break
fi
sleep 60 # Wait for 60 seconds before checking again
done
}
check_sudo
check_platform
install_k3s_helm
install_awx_on_k3s
check_install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment