- Google Cloud Platform Cookbook
- Legorie Rajan PS
- 379字
- 2021-08-27 19:13:23
Creating the start up script
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:
git clone https://source.developers.google.com/p/<PROJECT ID>/r/<REPOSITORY NAME> /opt/app #Line 60
- 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]
- Installs MongoDB:
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
apt-get update
apt-get install -y mongodb-org
cat > /etc/systemd/system/mongodb.service << EOF
[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
[Service]
User=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
[Install]
WantedBy=multi-user.target
EOF
systemctl start mongodb
systemctl enable mongodb
- Installs dependencies from apt:
apt-get install -yq ca-certificates git nodejs build-essential supervisor
- Installs Node.js:
mkdir /opt/nodejs
curl https://nodejs.org/dist/v4.2.2/node-v4.2.2-linux-x64.tar.gz | tar xvzf - -C /opt/nodejs --strip-components=1
ln -s /opt/nodejs/bin/node /usr/bin/node
ln -s /opt/nodejs/bin/npm /usr/bin/npm
- 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
- Installs the app dependencies:
cd /opt/app/Chapter01/mysite
npm install
cat >./.env << EOF
COOKIE_SECRET=d44d5c45e7f8149aabc06a830dba5716b4bd952a639c82499954
MONGODB_URI=mongodb://localhost:27017
EOF
- Creates a nodeapp user. The application will run as this user:
useradd -m -d /home/nodeapp nodeapp
chown -R nodeapp:nodeapp /opt/app
- Configures the supervisor to run the nodeapp:
cat >/etc/supervisor/conf.d/node-app.conf << EOF
[program:nodeapp]
directory=/opt/app/Chapter01/mysite
command=npm start
autostart=true
autorestart=true
user=nodeapp
environment=HOME="/home/nodeapp",USER="nodeapp",NODE_ENV="production"
stdout_logfile=syslog
stderr_logfile=syslog
EOF
supervisorctl reread
supervisorctl update
# Application should now be running under supervisor
# [END startup]
推薦閱讀
- Instant Raspberry Pi Gaming
- 走入IBM小型機世界
- 并行數據挖掘及性能優化:關聯規則與數據相關性分析
- 離散事件系統建模與仿真
- Visual C# 2008開發技術詳解
- Creo Parametric 1.0中文版從入門到精通
- 工業機器人操作與編程
- Linux:Powerful Server Administration
- Salesforce for Beginners
- 悟透AutoCAD 2009案例自學手冊
- Visual FoxPro程序設計
- Word 2007,Excel 2007辦公應用融會貫通
- 人工智能:語言智能處理
- JRuby語言實戰技術
- Hands-On Business Intelligence with Qlik Sense