The start up script is used to initialize the VM during a boot or a restart with the necessary software (MongoDB, Node.js, supervisor, and others) and loads the application from the source code repository. The following script can be found in the /Chapter01/ folder of the Git repository. The start up script performs the following tasks:
Installs the logging agent which is an application based on Fluentd.
Installs the MongoDB database to be used by the KeystoneJS application.
Installs Node.js, Git, and supervisor. Supervisor is a process control system which is used to run our KeystoneJS application as a process.
Clones the application code from the repository to the local folder. Update the code at #Line 60, to reflect your repository's URL:
Installs the dependencies and creates the .env file to hold the environment variables:
COOKIE_SECRET=<Long Random String>
The application is configured to run under the supervisor:
#! /bin/bash
# Source url: https://github.com/GoogleCloudPlatform/nodejs-getting-started/blob/master/7-gce/gce/startup-script.sh
# The startup-script is modified to suit the Chapter 01-Recipe 01 of our book # Copyright 2017, Google, Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# [START startup]
set -v
Talks to the metadata server to get the project ID:
PROJECTID=$(curl -s "http://metadata.google.internal/computeMetadata/v1/project/project-id" -H "Metadata-Flavor: Google") # Install logging monitor. The monitor will automatically pick up logs sent to # syslog. # [START logging] curl -s "https://storage.googleapis.com/signals-agents/logging/google-fluentd-install.sh" | bash service google-fluentd restart & # [END logging]
Gets the application source code from the Google Cloud Source Repositories:
# 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/<Project ID>/r/gcpcookbook /opt/app