Nicolas Massé 4 years ago
parent
commit
065e3242cd
  1. 16
      ansible/start.yaml
  2. 19
      ansible/stop.yaml
  3. 74
      clusterctl
  4. 3
      local.env.sample
  5. 5
      templates/inventory

16
ansible/start.yaml

@ -1,17 +1,7 @@
- name: Add the libvirt server to the inventory
hosts: localhost
gather_facts: no
tasks:
- add_host:
hostname: '{{ lookup("env", "LIBVIRT_SERVER") }}'
ansible_host: '{{ lookup("env", "LIBVIRT_SERVER") }}'
ansible_user: '{{ lookup("env", "LIBVIRT_USER") }}'
groups: hypervisor
- name: Start the OpenShift cluster - name: Start the OpenShift cluster
hosts: hypervisor hosts: localhost
gather_facts: no gather_facts: no
become: yes become: no
vars: vars:
lb: '{{ nodes | selectattr("role", "eq", "lb") | first }}' lb: '{{ nodes | selectattr("role", "eq", "lb") | first }}'
storage: '{{ nodes | selectattr("role", "eq", "storage") | first }}' storage: '{{ nodes | selectattr("role", "eq", "storage") | first }}'
@ -22,6 +12,7 @@
community.libvirt.virt: community.libvirt.virt:
name: '{{ item.name }}' name: '{{ item.name }}'
state: running state: running
uri: '{{ lookup("env", "LIBVIRT_DEFAULT_URI") }}'
loop: loop:
- '{{ lb }}' - '{{ lb }}'
- '{{ storage }}' - '{{ storage }}'
@ -42,6 +33,7 @@
community.libvirt.virt: community.libvirt.virt:
name: '{{ item.name }}' name: '{{ item.name }}'
state: running state: running
uri: '{{ lookup("env", "LIBVIRT_DEFAULT_URI") }}'
loop: '{{ masters + workers }}' loop: '{{ masters + workers }}'
loop_control: loop_control:
label: "{{ item.name }}" label: "{{ item.name }}"

19
ansible/stop.yaml

@ -1,17 +1,7 @@
- name: Add the libvirt server to the inventory
hosts: localhost
gather_facts: no
tasks:
- add_host:
hostname: '{{ lookup("env", "LIBVIRT_SERVER") }}'
ansible_host: '{{ lookup("env", "LIBVIRT_SERVER") }}'
ansible_user: '{{ lookup("env", "LIBVIRT_USER") }}'
groups: hypervisor
- name: Stop the OpenShift cluster - name: Stop the OpenShift cluster
hosts: hypervisor hosts: localhost
gather_facts: no gather_facts: no
become: yes become: no
vars: vars:
lb: '{{ nodes | selectattr("role", "eq", "lb") | first }}' lb: '{{ nodes | selectattr("role", "eq", "lb") | first }}'
storage: '{{ nodes | selectattr("role", "eq", "storage") | first }}' storage: '{{ nodes | selectattr("role", "eq", "storage") | first }}'
@ -22,6 +12,7 @@
community.libvirt.virt: community.libvirt.virt:
name: '{{ item.name }}' name: '{{ item.name }}'
state: shutdown state: shutdown
uri: '{{ lookup("env", "LIBVIRT_DEFAULT_URI") }}'
loop: '{{ workers }}' loop: '{{ workers }}'
loop_control: loop_control:
label: "{{ item.name }}" label: "{{ item.name }}"
@ -30,6 +21,7 @@
community.libvirt.virt: community.libvirt.virt:
name: '{{ item.name }}' name: '{{ item.name }}'
command: info command: info
uri: '{{ lookup("env", "LIBVIRT_DEFAULT_URI") }}'
register: vm register: vm
until: "vm[item.name].state == 'shutdown'" until: "vm[item.name].state == 'shutdown'"
retries: 48 retries: 48
@ -42,6 +34,7 @@
community.libvirt.virt: community.libvirt.virt:
name: '{{ item.name }}' name: '{{ item.name }}'
state: shutdown state: shutdown
uri: '{{ lookup("env", "LIBVIRT_DEFAULT_URI") }}'
loop: '{{ masters }}' loop: '{{ masters }}'
loop_control: loop_control:
label: "{{ item.name }}" label: "{{ item.name }}"
@ -50,6 +43,7 @@
community.libvirt.virt: community.libvirt.virt:
name: '{{ item.name }}' name: '{{ item.name }}'
command: info command: info
uri: '{{ lookup("env", "LIBVIRT_DEFAULT_URI") }}'
register: vm register: vm
until: "vm[item.name].state == 'shutdown'" until: "vm[item.name].state == 'shutdown'"
retries: 48 retries: 48
@ -62,6 +56,7 @@
community.libvirt.virt: community.libvirt.virt:
name: '{{ item.name }}' name: '{{ item.name }}'
state: shutdown state: shutdown
uri: '{{ lookup("env", "LIBVIRT_DEFAULT_URI") }}'
loop: loop:
- '{{ lb }}' - '{{ lb }}'
- '{{ storage }}' - '{{ storage }}'

74
clusterctl

@ -3,6 +3,19 @@
set -Eeuo pipefail set -Eeuo pipefail
trap "exit" INT trap "exit" INT
function assert_cluster_name () {
local cluster_name="${1:-}"
if [ ! -d ".clusters/$cluster_name" ]; then
echo "Cluster '$cluster_name' does not exist!"
exit 1
fi
if [ -f ".clusters/$cluster_name/local.env" ]; then
source ".clusters/$cluster_name/local.env"
fi
}
function init () { function init () {
local cluster_name="${1:-}" local cluster_name="${1:-}"
@ -25,25 +38,17 @@ function init () {
} }
function destroy () { function destroy () {
assert_cluster_name "$@"
local cluster_name="${1:-}" local cluster_name="${1:-}"
if [ ! -d ".clusters/$cluster_name" ]; then
echo "Cluster '$cluster_name' does not exist!"
exit 1
fi
terraform destroy -var-file=".clusters/$cluster_name/terraform.tfvars" -state=".clusters/$cluster_name/terraform.tfstate" terraform destroy -var-file=".clusters/$cluster_name/terraform.tfvars" -state=".clusters/$cluster_name/terraform.tfstate"
sed -i.bak 's/^\s*bootstrap_nodes\s*=\s*.*$/bootstrap_nodes = 1/' ".clusters/$cluster_name/terraform.tfvars" sed -i.bak 's/^\s*bootstrap_nodes\s*=\s*.*$/bootstrap_nodes = 1/' ".clusters/$cluster_name/terraform.tfvars"
} }
function prepare () { function prepare () {
assert_cluster_name "$@"
local cluster_name="${1:-}" local cluster_name="${1:-}"
if [ ! -d ".clusters/$cluster_name" ]; then
echo "Cluster '$cluster_name' does not exist!"
exit 1
fi
# Make a backup since the openshift-install command will consume it # Make a backup since the openshift-install command will consume it
if [ -f ".clusters/$cluster_name/install-config.yaml" ]; then if [ -f ".clusters/$cluster_name/install-config.yaml" ]; then
cp ".clusters/$cluster_name/install-config.yaml" ".clusters/$cluster_name/install-config.yaml.bak" cp ".clusters/$cluster_name/install-config.yaml" ".clusters/$cluster_name/install-config.yaml.bak"
@ -58,13 +63,9 @@ function prepare () {
} }
function apply () { function apply () {
assert_cluster_name "$@"
local cluster_name="${1:-}" local cluster_name="${1:-}"
if [ ! -d ".clusters/$cluster_name" ]; then
echo "Cluster '$cluster_name' does not exist!"
exit 1
fi
prepare "$cluster_name" prepare "$cluster_name"
# Create installation files # Create installation files
@ -89,48 +90,32 @@ function apply () {
} }
function ping () { function ping () {
assert_cluster_name "$@"
local cluster_name="${1:-}" local cluster_name="${1:-}"
if [ ! -d ".clusters/$cluster_name" ]; then
echo "Cluster '$cluster_name' does not exist!"
exit 1
fi
oc --insecure-skip-tls-verify --kubeconfig=".clusters/$cluster_name/auth/kubeconfig" whoami oc --insecure-skip-tls-verify --kubeconfig=".clusters/$cluster_name/auth/kubeconfig" whoami
} }
function approve_csr () { function approve_csr () {
assert_cluster_name "$@"
local cluster_name="${1:-}" local cluster_name="${1:-}"
if [ ! -d ".clusters/$cluster_name" ]; then
echo "Cluster '$cluster_name' does not exist!"
exit 1
fi
oc --insecure-skip-tls-verify --kubeconfig=".clusters/$cluster_name/auth/kubeconfig" get csr --no-headers \ oc --insecure-skip-tls-verify --kubeconfig=".clusters/$cluster_name/auth/kubeconfig" get csr --no-headers \
| awk '/Pending/ {print $1}' \ | awk '/Pending/ {print $1}' \
| xargs --no-run-if-empty oc --insecure-skip-tls-verify --kubeconfig=".clusters/$cluster_name/auth/kubeconfig" adm certificate approve | xargs --no-run-if-empty oc --insecure-skip-tls-verify --kubeconfig=".clusters/$cluster_name/auth/kubeconfig" adm certificate approve
} }
function start () { function start () {
assert_cluster_name "$@"
local cluster_name="${1:-}" local cluster_name="${1:-}"
if [ ! -d ".clusters/$cluster_name" ]; then
echo "Cluster '$cluster_name' does not exist!"
exit 1
fi
ansible-playbook -i ".clusters/$cluster_name/inventory" ansible/start.yaml ansible-playbook -i ".clusters/$cluster_name/inventory" ansible/start.yaml
} }
function stop () { function stop () {
assert_cluster_name "$@"
local cluster_name="${1:-}" local cluster_name="${1:-}"
if [ ! -d ".clusters/$cluster_name" ]; then
echo "Cluster '$cluster_name' does not exist!"
exit 1
fi
ansible-playbook -i ".clusters/$cluster_name/inventory" ansible/stop.yaml ansible-playbook -i ".clusters/$cluster_name/inventory" ansible/stop.yaml
} }
@ -193,14 +178,10 @@ EOF
} }
function post_install () { function post_install () {
assert_cluster_name "$@"
local cluster_name="${1:-}" local cluster_name="${1:-}"
shift shift
if [ ! -d ".clusters/$cluster_name" ]; then
echo "Cluster '$cluster_name' does not exist!"
exit 1
fi
if [ $# -eq 0 ]; then if [ $# -eq 0 ]; then
set nfs sso le set nfs sso le
fi fi
@ -262,25 +243,17 @@ EOF
} }
function install_addon () { function install_addon () {
assert_cluster_name "$@"
local cluster_name="${1:-}" local cluster_name="${1:-}"
local addon="${2:-}" local addon="${2:-}"
if [ ! -d ".clusters/$cluster_name" ]; then
echo "Cluster '$cluster_name' does not exist!"
exit 1
fi
install_addon_$addon "$cluster_name" install_addon_$addon "$cluster_name"
} }
function shell () { function shell () {
assert_cluster_name "$@"
local cluster_name="${1:-}" local cluster_name="${1:-}"
if [ ! -d ".clusters/$cluster_name" ]; then
echo "Cluster '$cluster_name' does not exist!"
exit 1
fi
# Ansible # Ansible
export DEFAULT_HOST_LIST="$PWD/.clusters/$cluster_name" export DEFAULT_HOST_LIST="$PWD/.clusters/$cluster_name"
@ -319,7 +292,6 @@ fi
source local.env source local.env
export LC_ALL=C export LC_ALL=C
export LANG=C export LANG=C
export LIBVIRT_DEFAULT_URI="qemu+ssh://$LIBVIRT_USER@$LIBVIRT_SERVER/system"
case "${1:-}" in case "${1:-}" in
init) init)

3
local.env.sample

@ -4,5 +4,4 @@ export GOOGLE_CLIENT_ID="client_id"
export GOOGLE_CLIENT_SECRET="client_secret" export GOOGLE_CLIENT_SECRET="client_secret"
export LE_EMAIL="user@redhat.com" export LE_EMAIL="user@redhat.com"
export OCP_ADMIN="user@redhat.com" export OCP_ADMIN="user@redhat.com"
export LIBVIRT_USER="user" export LIBVIRT_DEFAULT_URI="qemu+ssh://user@libvirt.server/system"
export LIBVIRT_SERVER="libvirt.server"

5
templates/inventory

@ -1,4 +1,3 @@
[hypervisor] [all:vars]
[hypervisor:vars]
nodes=${jsonencode(nodes)} nodes=${jsonencode(nodes)}

Loading…
Cancel
Save