Skip to content

Commit

Permalink
Add option to schedule start/stop instance jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
yann degat committed Mar 19, 2020
1 parent 8e52d6a commit 53dc350
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 1 deletion.
55 changes: 55 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<EOF
set -e
sudo pip3 install -r ${path.module}/requirements.txt
if openstack workbook show ${var.name} > /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
}
}
22 changes: 22 additions & 0 deletions mistral-wb.yml.tpl
Original file line number Diff line number Diff line change
@@ -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}"

3 changes: 3 additions & 0 deletions mistral-wf.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

set -e
2 changes: 1 addition & 1 deletion provision.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 7 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 * * *"
}

0 comments on commit 53dc350

Please sign in to comment.