From b5a5df7f8c21a1a5a04e30306daf229b9a6b25bd Mon Sep 17 00:00:00 2001 From: Nicolas MASSE Date: Wed, 17 Mar 2021 16:26:59 +0100 Subject: [PATCH] start/stop script --- cluster | 52 +++++++++++++++++++++++++++++++++++++++++++++- local.env.sample | 2 ++ main.tf | 6 ++++-- post-install.tf | 12 +++++++++++ templates/start.sh | 30 ++++++++++++++++++++++++++ templates/stop.sh | 22 ++++++++++++++++++++ 6 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 templates/start.sh create mode 100644 templates/stop.sh diff --git a/cluster b/cluster index 46cf733..7f02d49 100755 --- a/cluster +++ b/cluster @@ -70,6 +70,30 @@ function apply () { openshift-install --dir="$cluster_name" wait-for install-complete } +function start () { + local cluster_name="${1:-}" + + if [ ! -d "$cluster_name" ]; then + echo "Cluster '$cluster_name' does not exist!" + exit 1 + fi + + scp "$cluster_name/start.sh" "$LIBVIRT_USER@$LIBVIRT_SERVER:/tmp/start.sh" + ssh "$LIBVIRT_USER@$LIBVIRT_SERVER" /tmp/start.sh +} + +function stop () { + local cluster_name="${1:-}" + + if [ ! -d "$cluster_name" ]; then + echo "Cluster '$cluster_name' does not exist!" + exit 1 + fi + + scp "$cluster_name/stop.sh" "$LIBVIRT_USER@$LIBVIRT_SERVER:/tmp/stop.sh" + ssh "$LIBVIRT_USER@$LIBVIRT_SERVER" /tmp/stop.sh +} + function post_install_nfs () { local cluster_name="${1:-}" @@ -155,6 +179,8 @@ if [ ! -e "local.env" ]; then fi source local.env +export LC_ALL=C +export LANG=C case "${1:-}" in init) @@ -165,6 +191,30 @@ init) shift bootstrap "$@" ;; +start) + if [ -z "${2:-}" ]; then + echo "Usage: $0 start cluster-name" + exit 1 + fi + shift + start "$@" +;; +stop) + if [ -z "${2:-}" ]; then + echo "Usage: $0 stop cluster-name" + exit 1 + fi + shift + stop "$@" +;; +apply) + if [ -z "${2:-}" ]; then + echo "Usage: $0 apply cluster-name" + exit 1 + fi + shift + apply "$@" +;; apply) if [ -z "${2:-}" ]; then echo "Usage: $0 apply cluster-name" @@ -190,7 +240,7 @@ post-install) post_install "$@" ;; *) - echo "Usage: $0 {init|apply|post-install|destroy} cluster-name" + echo "Usage: $0 {init|apply|post-install|destroy|start|stop} cluster-name" exit 1 ;; esac diff --git a/local.env.sample b/local.env.sample index 99369e2..a5c0183 100644 --- a/local.env.sample +++ b/local.env.sample @@ -5,3 +5,5 @@ export GOOGLE_CLIENT_ID="client_id" export GOOGLE_CLIENT_SECRET="client_secret" export LE_EMAIL="user@redhat.com" export OCP_ADMIN="user@redhat.com" +export LIBVIRT_USER="user" +export LIBVIRT_SERVER="libvirt.server" diff --git a/main.tf b/main.tf index b4d0707..11a98e3 100644 --- a/main.tf +++ b/main.tf @@ -25,9 +25,11 @@ terraform { } locals { - ocp_nodes = { for i in concat(libvirt_domain.bootstrap, libvirt_domain.master, libvirt_domain.worker) : i.name => i.network_interface.0.addresses[0] } + master_nodes = { for i in libvirt_domain.master : i.name => i.network_interface.0.addresses[0] } + worker_nodes = { for i in libvirt_domain.worker : i.name => i.network_interface.0.addresses[0] } + bootstrap_nodes = { for i in libvirt_domain.bootstrap : i.name => i.network_interface.0.addresses[0] } additional_nodes = { (libvirt_domain.lb.name) = cidrhost(var.network_ip_range, 4), (libvirt_domain.storage.name) = libvirt_domain.storage.network_interface.0.addresses[0] } - all_nodes = merge(local.ocp_nodes, local.additional_nodes) + all_nodes = merge(local.additional_nodes, local.master_nodes, local.worker_nodes, local.bootstrap_nodes) } output "machines" { diff --git a/post-install.tf b/post-install.tf index 14f7eac..f5c3313 100644 --- a/post-install.tf +++ b/post-install.tf @@ -16,6 +16,18 @@ resource "local_file" "dns_config" { file_permission = "0644" } +resource "local_file" "stop_sh" { + content = templatefile("${path.module}/templates/stop.sh", { masters = libvirt_domain.master.*.name, workers = libvirt_domain.worker.*.name, lb = libvirt_domain.lb.name, storage = libvirt_domain.storage.name }) + filename = "${var.cluster_name}/stop.sh" + file_permission = "0755" +} + +resource "local_file" "start_sh" { + content = templatefile("${path.module}/templates/start.sh", { masters = local.master_nodes, workers = local.worker_nodes, others = local.additional_nodes }) + filename = "${var.cluster_name}/start.sh" + file_permission = "0755" +} + resource "null_resource" "dnsmasq_config" { triggers = { network_id = libvirt_network.ocp_net.id diff --git a/templates/start.sh b/templates/start.sh new file mode 100644 index 0000000..df477be --- /dev/null +++ b/templates/start.sh @@ -0,0 +1,30 @@ +#!/bin/sh + +set -Eeuo pipefail +trap "exit" INT + +function start () { + for i; do + sudo virsh start "$i" || true + done +} + +function wait_for_ip () { + echo "Waiting for $1 to come online..." + while ! ping -n -c4 -i.2 $2 -q &>/dev/null; do + sleep 1 + done +} + +%{for host, ip in others~} +start "${host}" +wait_for_ip "${host}" "${ip}" +%{endfor~} + +%{for host, ip in masters~} +start "${host}" +%{endfor~} + +%{for host, ip in workers~} +start "${host}" +%{endfor~} diff --git a/templates/stop.sh b/templates/stop.sh new file mode 100644 index 0000000..346e9aa --- /dev/null +++ b/templates/stop.sh @@ -0,0 +1,22 @@ +#!/bin/sh + +set -Eeuo pipefail +trap "exit" INT + +function stop_group () { + for i; do + sudo virsh shutdown "$i" --mode=agent || true + done + + for i; do + echo "Waiting for $i to shutdown..." + while sudo virsh list --name | egrep -q "^$i\$"; do + sleep 1 + continue + done + done +} + +stop_group %{for host in workers}"${host}" %{endfor} +stop_group %{for host in masters}"${host}" %{endfor} +stop_group "${lb}" "${storage}"