Been trying to trouble shoot this for two days. Not sure if it is a terraform or GCP issue. Or my code. I'm trying to create a VM and run some installs. It then creates a file in /var/run called flag.txt. If that file is present the startup script should exit and not run on reboots. I wrote a python script to write the date and time to the flag.txt file so I could test. However, everytime I reboot the time and date are updated in the flag.txt file showing that the startup script is running.
Here is my metadata_startup_script code
metadata_startup_script = <<-EOF#!/bin/bashif [ ! -f /var/run/flag.txt ];thensudo apt-get updatesudo apt-get install -y gcloudecho '${local.script_content}'> /tmp/install_docker.shecho '${local.flag_content}'> /tmp/date_flag.pychmod +x /tmp/install_docker.shchmod +x /tmp/date_flag.py#Below command is just to show root is executing this script#whoami >> /usr/bin/runner_idbash /tmp/install_docker.sh/usr/bin/python3 /tmp/date_flag.pyelseexit 0fiEOF}
Here is the date_flag.py file that creates the flag.txt file
import datetimecurrent_datetime = datetime.datetime.now()formatted_datetime = current_datetime.strftime("%Y-%m-%d_%H-%M-%S")file_name = f"{formatted_datetime}.txt"with open("/var/run/flag.txt", "w") as file: file.write("This file was created at: " + formatted_date
Any thoughts or suggestions are welcome. This is really driving me crazy.