@@ -45,28 +45,35 @@ locals {
4545 })
4646}
4747
48- locals {
49- init_commands = [
50- " tar xf sources.tar" ,
51- # Set required environment variables
52- " echo '${ local . env_content } ' >> /root/.forest_env" ,
53- " echo '. ~/.forest_env' >> .bashrc" ,
54- " . ~/.forest_env" ,
55- " nohup sh ./init.sh > init_log.txt &" ,
56- " cp ./restart.service /etc/systemd/system/" ,
57- " systemctl enable restart.service" ,
58- # Exiting without a sleep sometimes kills the script :-/
59- " sleep 60s" ,
60- ]
61- }
62-
6348resource "digitalocean_droplet" "forest" {
6449 image = var. image
6550 name = format (" %s-%s" , var. environment , var. name )
6651 region = var. region
6752 size = var. size
6853 # Re-initialize resource if this hash changes:
69- user_data = join (" -" , [data . local_file . sources . content_sha256 , sha256 (join (" " , local. init_commands ))])
54+ user_data = <<- EOT
55+ #!/bin/bash
56+ set -e
57+
58+ # Extract sources
59+ tar xf /root/sources.tar -C /root
60+
61+ # Set required environment variables
62+ cat <<EOF >> /root/.forest_env
63+ ${ local . env_content }
64+ EOF
65+
66+ echo '. /root/.forest_env' >> /root/.bashrc
67+ source /root/.forest_env
68+
69+ # Run initialization script
70+ nohup sh /root/init.sh > /root/init_log.txt
71+
72+ # Configure and enable restart service
73+ cp /root/restart.service /etc/systemd/system/
74+ systemctl enable restart.service
75+ EOT
76+
7077 tags = [" iac" , var . environment ]
7178 ssh_keys = data. digitalocean_ssh_keys . keys . ssh_keys [* ]. fingerprint
7279 monitoring = true
@@ -85,11 +92,8 @@ resource "digitalocean_droplet" "forest" {
8592 source = data. local_file . sources . filename
8693 destination = " /root/sources.tar"
8794 }
88-
89- provisioner "remote-exec" {
90- inline = local. init_commands
91- }
9295}
96+
9397
9498data "digitalocean_project" "forest_project" {
9599 name = var. project
0 commit comments