From 53dc3500adffbd63894ec254a72ac1b9b8d4ee0a Mon Sep 17 00:00:00 2001 From: yann degat Date: Thu, 19 Mar 2020 08:45:53 +0000 Subject: [PATCH] Add option to schedule start/stop instance jobs --- main.tf | 55 ++++++++++++++++++++++++++++++++++++++++++++++ mistral-wb.yml.tpl | 22 +++++++++++++++++++ mistral-wf.sh | 3 +++ provision.sh | 2 +- requirements.txt | 7 ++++++ variables.tf | 15 +++++++++++++ 6 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 mistral-wb.yml.tpl create mode 100644 mistral-wf.sh create mode 100644 requirements.txt diff --git a/main.tf b/main.tf index aef526a..e660053 100644 --- a/main.tf +++ b/main.tf @@ -146,3 +146,58 @@ resource null_resource provision { script = "${path.module}/provision.sh" } } + +data template_file workbook { + template = file("${path.module}/mistral-wb.yml.tpl") + + vars = { + name = var.name + id = openstack_compute_instance_v2.vm.id + region = var.region_name + } +} + +resource local_file workbook { + count = var.cronjob_enabled ? 1 : 0 + content = data.template_file.workbook.rendered + filename = "${path.module}/${var.name}.wb.yml" +} + +# the mistral jobs are set using the openstack CLI as +# mistral resources arent available in the terraform openstack provider +resource null_resource openstack_mistral_setup { + count = var.cronjob_enabled ? 1 : 0 + + triggers = { + server_id = openstack_compute_instance_v2.vm.id + } + + provisioner "local-exec" { + working_dir = path.module + environment = { + OS_CLOUD = var.cloud_name + } + + command = < /dev/null 2>&1; then + openstack workbook update ${local_file.workbook.0.filename} +else + openstack workbook create ${local_file.workbook.0.filename} +fi + +if openstack cron trigger show ${var.name}.unshelve > /dev/null 2>&1; then + openstack cron trigger delete ${var.name}.unshelve +fi + +if openstack cron trigger show ${var.name}.shelve > /dev/null 2>&1; then + openstack cron trigger delete ${var.name}.shelve +fi + +openstack cron trigger create --pattern='${var.cronjob_unshelve}' --utc ${var.name}.unshelve ${var.name}.unshelve "{}" +openstack cron trigger create --pattern='${var.cronjob_shelve}' --utc ${var.name}.shelve ${var.name}.shelve "{}" +EOF + } +} diff --git a/mistral-wb.yml.tpl b/mistral-wb.yml.tpl new file mode 100644 index 0000000..28e13e5 --- /dev/null +++ b/mistral-wb.yml.tpl @@ -0,0 +1,22 @@ +--- +version: "2.0" + +name: ${name} + +workflows: + unshelve: + tasks: + start_server: + action: nova.servers_unshelve + input: + action_region: "${region}" + server: "${id}" + + shelve: + tasks: + stop_server: + action: nova.servers_shelve + input: + action_region: "${region}" + server: "${id}" + diff --git a/mistral-wf.sh b/mistral-wf.sh new file mode 100644 index 0000000..d9628ae --- /dev/null +++ b/mistral-wf.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +set -e diff --git a/provision.sh b/provision.sh index 1deb2af..afba1ee 100644 --- a/provision.sh +++ b/provision.sh @@ -6,7 +6,7 @@ export DEBIAN_FRONTEND=noninteractive sudo apt update -y -sudo apt install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common debconf-utils +sudo apt install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common debconf-utils qemu-guest-agent wget -qO - https://download.jitsi.org/jitsi-key.gpg.key | sudo apt-key add - sudo sh -c "echo 'deb https://download.jitsi.org stable/' > /etc/apt/sources.list.d/jitsi-stable.list" diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..1a6612d --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +python-cinderclient==6.0.0 +python-designateclient==3.1.0 +python-glanceclient==3.0.0 +python-keystoneclient==3.22.0 +python-mistralclient==3.10.0 +python-novaclient==16.0.0 +python-openstackclient==5.0.0 diff --git a/variables.tf b/variables.tf index 2f03e31..60dd81f 100644 --- a/variables.tf +++ b/variables.tf @@ -53,3 +53,18 @@ variable fqdn { description = "Fully qualified domain name of the server. By default, will use the reverse name of the public ip" default = "" } + +variable cronjob_enabled { + description = "Enable cronjob to shelve/unshelve the jitsi server so it is not billed" + default = false +} + +variable cronjob_unshelve { + description = "Unshelve instance" + default = "00 16 * * *" +} + +variable cronjob_shelve { + description = "Shelve instance. (Put instance to sleep so it is not billed)" + default = "30 22 * * *" +}