Artigos‎ > ‎Cloud‎ > ‎Google Compute Engine‎ > ‎

Main Script on Google Cloud Storage


Primeiramente gostaria de salientar que não sou o autor principal do conteúdo deste artigo. A minha intenção é de mostrar o que outros autores tem realizado (referenciando links para o conteúdo original) neste blog até como um guia pessoal para uso próprio e ainda compartilhar minhas anotações e pequenas modificações que realizo no conteúdo, desejando que possa ajudar mais alguém em algum lugar.
First and foremost, I take no credit for any of this post’s content. I am really just taking what others have done (which I have links to bellow) and am putting it on my blog for a personal reference and hopefully the small changes that I made to their guides will help someone somewhere.


link principal = https://sites.google.com/site/ubuntu4us/artigos/cloud/google-compute-engine/example-script-to-install-packages

Script to run other scripts from google storage:

#! /bin/bash

METADAURL="http://metadata/computeMetadata/v1beta1/instance/attributes/"
SCRIPTURL="gs://vanaware/scripts/"

# Creating BLANK log custom file
LOGFILE="/var/log/custom-script.log"
if [ -f "${LOGFILE}" ]; then
rm -f "${LOGFILE}"
fi
exec > >(tee -a "${LOGFILE}")
exec 2>&1

echo "Running script main.sh ..."

function runstoragescript {
declare SCRIPTNAME="${1}"
echo "Retrieving script: ${SCRIPTNAME}"
gsutil cp "${SCRIPTURL}${SCRIPTNAME}" "${TEMPDIR}${SCRIPTNAME}"
bash "${TEMPDIR}${SCRIPTNAME}"
}



if [ "$(id -u)" != "0" ]
then
echo "Not root user, aborting installation." 1>&2
exit 1
else 
echo "Running as root."
#Check if is on Google Compute Engine
GCE_OK=$(sudo grep Google /sys/firmware/dmi/entries/1-0/raw)
if [ "${GCE_OK}" != "Binary file /sys/firmware/dmi/entries/1-0/raw matches" ]
then
echo "Not running an Google Compute Engine instance, aborting installation." 1>&2
exit 1
else
echo "Running on Google Compute Engine Instance."
# Creating temporary dir
TEMPDIR="$(mktemp -d)/"
# Executing others scripts on Google Storage
SCRIPTLIST=$(gsutil cat "${SCRIPTURL}main.list")
for SCRIPTNAME in ${SCRIPTLIST}
do
runstoragescript "${SCRIPTNAME}"
done


# Deleting temporary files
rm -r ${TEMPDIR}
fi
fi
exit $?



Others scripts must be listed on file main.list as follows:
example.sh
example2.sh
example3.sh

example script.sh :

#! /bin/bash

METADAURL="http://metadata/computeMetadata/v1beta1/instance/attributes/"

PACKAGENAME="apache2"

# Creating log custom file
LOGFILE="/var/log/custom-script.log"
exec > >(tee -a "${LOGFILE}")
exec 2>&1

echo "Running script example.sh ..."

# Check Package Installation
PKG_OK=$(dpkg-query -W --showformat='${Status}\n' ${PACKAGENAME} 2>&1 | grep "install ok installed")
echo -n "Checking for package ${PACKAGENAME}: "
if [ -z "${PKG_OK}" ]
then
echo "not installed."
# Install Package
apt-get update
apt-get --force-yes --yes install ${PACKAGENAME}

##### Post Install config

# Retrieve Custom Metadata
VALUE_OF_FOO=$(curl ${METADAURL}foo)
#Post Install - script
mkdir -p /var/www/
cat <<EOF > /var/www/index.html
<html><body><h1>Hello World</h1>
<p>The value of foo: ${VALUE_OF_FOO}</p>
</body></html>
EOF
else
#Package already Installed
echo "${PKG_OK}."
##### Always run config
# Script to be run on every restart of instance
fi

exit $?

Defining default project ID :
gcutil getproject --project=<project-id> --cache_flag_values
Authenticating to Google Compute Engine (saving on file ~/.gcutil_auth):
gcutil auth --project=<project ID>

link:https://developers.google.com/compute/docs/instances#start_vm

Create an instance on Google Cloud Compute as follow (saving on file  ~/.gcutil.flags):

gcutil --project=<project-id> addinstance <instance-name> <other-flags>
gcutil --project="1010870741727" addinstance cloud --tags="vanaware" --description="Instancia inicial para servidores cloud no google criada dia 23/06/2013" --service_account=default --service_account_scopes=https://www.googleapis.com/auth/userinfo.email,storage-full,bigquery,cloudsql,compute-rw,taskqueue --metadata=startup-script-url:gs://vanaware/scripts/main.sh --zone=us-central1-a --machine_type=f1-micro --image=projects/debian-cloud/global/images/debian-7-wheezy-v20130617 --can_ip_forward=true --external_ip_address=ephemeral --network=default --wait_until_running=false --use_compute_key=false --persistent_boot_disk=true --metadata=foo:"bar - testando 2"


lista de comandos úteis:
gcutil help addinstance
gcutil listmachinetypes
gcutil listimages
gcutil listkernels --project=google
gcutil ssh --zone="us-central1-a" cloud
gcutil --project="1010870741727" getinstance

Inside instance:
cat /var/log/startupscript.log
sudo /usr/share/google/run-startup-scripts


O URL específico do gadget não foi encontrado



Comments