1

I have a compute engine instance and the start-up script to it includes the following lines.

# Get the application source code from the Google Cloud Repository.
# git requires $HOME and it's not set during the startup script.
export HOME=/root
git config --global credential.helper gcloud.sh
git clone https://source.developers.google.com/p/$PROJECTID /opt/app

This code tells the VM to get source code from the cloud repository, my application code. Whenever I modify my sourcecode and push the changes to the repository and re-start my vm, the vm isn't executing the new code. How would I make the vm run the new code without deleting the instance and creating a new one?

Misha Brukman
  • 12,938
  • 4
  • 61
  • 78
Rockstar5645
  • 4,376
  • 8
  • 37
  • 62

2 Answers2

4

GCE VM startup scripts run on every boot, not just the first boot, so you should only clone the repo on first time, and then update every other time, e.g.,

# Note: this script is untested but should work.

export HOME=/root
git config --global credential.helper gcloud.sh

declare -r LOCAL_GIT_REPO="/opt/app"
if ! [[ -e "${LOCAL_GIT_REPO}" ]]; then
  git clone https://source.developers.google.com/p/$PROJECTID "${LOCAL_GIT_REPO}"
else
  cd "${LOCAL_GIT_REPO}"
  git pull
fi

Then, you can re-run this script manually anytime to update your repo while your instance is running. If you want to have the instance update its own code automatically, call this script from cron. You can learn how to set up periodic command runs via man cron and man crontab.

Misha Brukman
  • 12,938
  • 4
  • 61
  • 78
  • I edited the startup script and re-ran, but I didn't get the confirmation messages "google Running startup script..." and "google Finished running startup script..." as they said in the documentation. https://cloud.google.com/compute/docs/startupscript?hl=en#rerunthescript – Rockstar5645 Oct 03 '15 at 08:28
  • @Rockstar5645 — those messages only happen at startup when the VM automatically runs the script for you, not when you run it yourself. If you want to see status updates from your script, you can add the command `echo "Message goes here"` to various points in your script. – Misha Brukman Oct 04 '15 at 04:43
0

To answer your question directly, on most distros, GCE puts your startup script in this location: /usr/share/google/run-startup-scripts. You don't need to restart your instance and have downtime. Just re-run it.

The link is here: https://cloud.google.com/compute/docs/startupscript?hl=en#rerunthescript

Just some advice. I'd leverage an automated tool to do any sort of git pulls or code deploys. Jenkins or Travis can do it. I would encourage you to look at CM tools as well. Ansible is user friendly and is a great one to learn if you're just starting out with CM.

Good luck!

bbuckley123
  • 1,879
  • 13
  • 18
  • if I re-run the script, since I used node to create my app, what happens exactly. the command node app.js starts running the script but if I re-run the script, does that automatically stop the server that's already running and start a new one or does it create two and waste resources? – Rockstar5645 Oct 05 '15 at 14:25