3
votes

Startup scripts are not behaving the way that I expected them to.

I have a .sh file in a storage bucket and have included a startup-script-url meta tag with the value gs://bucket-name/start-script.sh

[[0;32m  OK  [0m] Started Google Compute Engine Accounts Daemon.
         Starting Google Compute Engine Startup Scripts...
[[0;32m  OK  [0m] Started Google Compute Engine Shutdown Scripts.
[[0;32m  OK  [0m] Started Google Compute Engine Startup Scripts.
[[0;32m  OK  [0m] Started Docker Application Container Engine.
[[0;32m  OK  [0m] Started Wait for Google Container Registry (GCR) to be accessible.
[[0;32m  OK  [0m] Reached target Google Container Registry (GCR) is Online.
[[0;32m  OK  [0m] Started Containers on GCE Setup.
[    8.001248] konlet-startup[1018]: 2018/03/08 20:23:56 Starting Konlet container startup agent

The below script is not executed as expected. I tried using the startup-script metadata tag and using something simple like echo "hello" but that's not working either. I have full Cloud API access scopes enabled.

If I copy the contents of the below file and paste it into the ssh terminal it works perfectly.

Could really use some help =D

start-script.sh

#! /bin/bash

image_name=gcr.io/some-image:version-2
docker_images=$(docker inspect --type=image $image_name)
array_count=${#docker_images[0]}

# Check if docker image is available
while ((array_count = 2));
do
    docker_images=$(docker inspect --type=image ${image_name})
    array_count=${#docker_images[0]}

    if (($array_count > 2)); then
        break
    fi
done

################################
#
#   Docker image now injected
#   by google compute engine
#
################################

echo "docker image ${image_name} available"

container_ids=($(docker container ls | grep ${image_name} | awk '{ print $1}'))

for (( i=0; i < ${#container_ids[@]}; i++ ));
do
    # run command for each container
    container_id=${container_ids[i]}
    echo "running commands for container: ${container_ids[i]}"

    # start cloud sql proxy
    docker container run -d -p $HOST_PORT:$APPLICATION_PORT \
    ${container_ids[i]} \
    ./cloud_sql_proxy \
    -instances=$PG_INSTANCE_CONNECTION_NAME=tcp:$PG_PORT \
    -credential_file=$PG_CREDENTIAL_FILE_LOCATION

    # HTTP - Start unicorn webserver
    docker exec -d \
    ${container_ids[i]} \
    bundle exec unicorn -c config/unicorn.rb
done
3

3 Answers

4
votes

Okay... after some scenario testing... found out that startup scripts do not run if you use the "Deploy a container image to this VM instance" option. Hope this saves you from tearing your hair out haha.

Deploy a container image to this VM instance Option

2
votes

startup-script always run when use the "Deploy a container image to this VM instance" option.

enter image description here

You can use cmd sudo journalctl -u google-startup-scripts.service -f to check script run result.

enter image description here

You can use cmd sudo google_metadata_script_runner -d --script-type startup to debug script.

2021.11.09 Updated: sudo google_metadata_script_runner startup.

doc: https://cloud.google.com/compute/docs/instances/startup-scripts

0
votes

I had a similar issue, after I've removed execution rights for files from /tmp. Take into account that startup scripts are copied to /tmp/ and afterwards ran from there.