diff --git a/resources/terraform/do/README.md b/resources/terraform/do/README.md index 338babb5..ce429ff0 100644 --- a/resources/terraform/do/README.md +++ b/resources/terraform/do/README.md @@ -7,7 +7,7 @@ 3. Obtain a Digital Ocean API key and store the key as an environment variable ``` - +export DIGITALOCEAN_TOKEN=... ``` 4. Copy terraform.tfvars.sample to terraform.tfvars @@ -28,17 +28,17 @@ All of the variables and any default values can be found in variables.tf. 6. Create a new instance of dSIPRouter -The following command will create a new instance of dSIPRouter based on the master branch. The OS image will be Debian 11 and the dsiprouter_prefix will be overriden by demo. +The following command will create a new instance of dSIPRouter based on the master branch. The OS image will be Debian 12 and the dsiprouter_prefix will be overriden by demo. ``` -terraform apply -var branch=master -var dsiprouter_prefix=demo -var image=debian-11-x64 +terraform apply -var dsip_ver=master -var dsiprouter_prefix=demo -var image=debian-12-x64 ``` If you want to create a demo instance of dSIPRouter in your Digitalocean environment with a DNS record use this. Note, you will need to change the dns_demo_domain variable to a domain that you have hosted with DigitalOcean. ``` -terraform apply -var branch=master -var image=debian-11-x64 -var dsiprouter_prefix=demo -var additional_commands='dsiprouter setcredentials -dc admin:ZmIwMTdmY2I5NjE4' -var dns_demo_enabled=1 -var dns_demo_domain=dsiprouter.org -var dns_demo_hostname=demo +terraform apply -var dsip_ver=master -var image=debian-12-x64 -var dsiprouter_prefix=demo -var additional_commands='dsiprouter setcredentials -dc admin:ZmIwMTdmY2I5NjE4' -var dns_demo_enabled=1 -var dns_demo_domain=dsiprouter.org -var dns_demo_hostname=demo ``` 7. Destroy your instance if you are done with it by using the terraform destory command diff --git a/resources/terraform/do/install.tftpl b/resources/terraform/do/install.tftpl new file mode 100644 index 00000000..ef2d7373 --- /dev/null +++ b/resources/terraform/do/install.tftpl @@ -0,0 +1,49 @@ +#!/usr/bin/env bash +# +# adapted from build_instance.sh in the github repo +# + +function cmdExists() { + if command -v "$1" > /dev/null 2>&1; then + return 0 + else + return 1 + fi +} + +# wait for any other programs using package manager to complete +if cmdExists "apt-get"; then + while fuser /var/lib/apt/lists/lock >/dev/null 2>&1; do + sleep 1 + done + + # make package manager quieter + export DEBIAN_FRONTEND="noninteractive" + export DEBIAN_PRIORITY="critical" + + apt-get update -qq -y >/dev/null + apt-get install -qq -y git perl >/dev/null + + # TODO: move to installScriptRequirements() + # make sure english UTF-8 locale is installed + if ! locale -a 2>/dev/null | grep -q 'en_US.UTF-8'; then + perl -i -pe 's%# (en_US\.UTF-8 UTF-8)%\1%' /etc/locale.gen + locale-gen + fi +elif cmdExists "yum"; then + while [ -f /var/run/yum.pid ]; do + sleep 1 + done + + yum makecache -y -q -e 0 >/dev/null + yum install -y -q -e 0 git >/dev/null +fi + +# clone and install +git clone --depth 1 -c advice.detachedHead=false ${dsip_repo} -b ${dsip_ver} ${dsip_dir} || exit 1 +${dsip_dir}/dsiprouter.sh ${build_options} | tee /var/log/dsip-install.log +# note the "$${}" escaping of template interpolation +(( $${PIPESTATUS[0]} == 0 )) || exit 1 +# additional commands to setup this box +${additional_commands} || exit 1 +exit 0 diff --git a/resources/terraform/do/main.tf b/resources/terraform/do/main.tf index 0faf46a3..c10d1ae4 100644 --- a/resources/terraform/do/main.tf +++ b/resources/terraform/do/main.tf @@ -1,44 +1,47 @@ terraform { required_providers { - digitalocean = { - source = "digitalocean/digitalocean" - version = "2.17.0" - } + digitalocean = { source = "digitalocean/digitalocean" } } } -provider "digitalocean" { -} - data "digitalocean_ssh_key" "ssh_key" { name = var.pub_key_name } - resource "digitalocean_droplet" "dsiprouter" { - name = "${var.dsiprouter_prefix}-dsip-${var.branch}${count.index}" - count = var.number_of_environments - region = "tor1" - size="1gb" - image=var.image - ssh_keys = [ data.digitalocean_ssh_key.ssh_key.fingerprint ] - - connection { - host = self.ipv4_address - user = "root" - type = "ssh" - private_key = file(var.pvt_key_path) - timeout = "5m" - } - - provisioner "remote-exec" { - inline = [ - "apt-get update -y && sleep 30 && apt-get install -y git && cd /opt && git clone https://github.com/dOpensource/dsiprouter.git -b ${var.branch} && cd dsiprouter && ./dsiprouter.sh install -all && ${var.additional_commands}" - ] - } + name = var.node_name + count = var.number_of_environments + region = "nyc1" + size = "1gb" + image = var.image + ssh_keys = [data.digitalocean_ssh_key.ssh_key.fingerprint] + + connection { + host = self.ipv4_address + user = "root" + type = "ssh" + private_key = file(var.pvt_key_path) + timeout = "5m" + } + provisioner "file" { + content = templatefile("${path.module}/install.tftpl", { + dsip_ver = var.dsip_ver + dsip_dir = var.dsip_dir + dsip_repo = var.dsip_repo + build_options = var.dsip_options + additional_commands = var.additional_commands + }) + destination = "/tmp/build_dsip.sh" + } + provisioner "remote-exec" { + inline = [ + "while [ ! -f /var/lib/cloud/instance/boot-finished ]; do sleep 3; done", + "chmod +x /tmp/build_dsip.sh", + "sudo /tmp/build_dsip.sh", + ] + } } - resource "digitalocean_record" "dns_demo_record" { count = var.dns_demo_enabled domain = var.dns_demo_domain diff --git a/resources/terraform/do/variables.tf b/resources/terraform/do/variables.tf index fe912db1..68fc936e 100644 --- a/resources/terraform/do/variables.tf +++ b/resources/terraform/do/variables.tf @@ -1,46 +1,61 @@ variable "pvt_key_path" { - type=string + type = string } variable "pub_key_name" { - type=string + type = string } -variable "dsiprouter_prefix" { - type=string - default="" +variable "node_name" { + type = string + default = "demo" } variable "dns_demo_domain" { - type=string - default="" + type = string + default = "" } -variable "dns_demo_enabled" { - type=number - default=0 +variable "dns_demo_enabled" { + type = number + default = 0 } variable "dns_demo_hostname" { - type=string - default="demo" + type = string + default = "demo" } variable "number_of_environments" { - type=number - default="1" + type = number + default = "1" } -variable "branch" { - type=string - default="master" +variable "dsip_ver" { + type = string + default = "master" +} + +variable "dsip_dir" { + type = string + default = "/opt/dsiprouter" +} + +variable "dsip_repo" { + type = string + default = "https://github.com/dOpensource/dsiprouter.git" +} + +variable "dsip_options" { + type = string + default = "install -all" } variable "image" { - type=string - default="debian-10-x64" + type = string + default = "debian-12-x64" } variable "additional_commands" { - type=string - default="echo" + type = string + default = "echo ''" }