Set hostname on GCP

Split GCP startup script in two:

 + One started before Docker, to set the hostname
 + Another to run the startup script

Signed-off-by: David Gageot <david@gageot.net>
This commit is contained in:
David Gageot 2016-12-28 16:09:56 +01:00
parent 8c70f94a26
commit 245e64ac94
4 changed files with 76 additions and 30 deletions

View File

@ -57,7 +57,8 @@ RUN \
rc-update add containerd default && \
rc-update add aws default && \
rc-update add azure default && \
rc-update add gcp default && \
rc-update add gcp-hostname default && \
rc-update add gcp-startup default && \
true
CMD ["/mkinitrd.sh"]

View File

@ -1,29 +0,0 @@
#!/sbin/openrc-run
description="Bootstrap procedure if running on Docker GCP edition"
depend()
{
need docker
need net
}
start()
{
[ "$(mobyplatform)" != "gcp" ] && exit 0
ebegin "Running GCP-specific initialization"
ebegin "Run startup script"
temp_file=$(mktemp)
curl -s http://metadata.google.internal/computeMetadata/v1/instance/attributes/startup-script -H "Metadata-Flavor: Google" > ${temp_file}
[ -s "${temp_file}" ] && sh "${temp_file}"
rm -f "${temp_file}"
eend 0
}
stop()
{
[ "$(mobyplatform)" != "gcp" ] && exit 0
}

View File

@ -0,0 +1,34 @@
#!/sbin/openrc-run
description="Set hostname on Docker GCP edition"
depend()
{
need net
before docker
}
metadata() {
curl -sH 'Metadata-Flavor: Google' http://metadata.google.internal/computeMetadata/v1/${1}
}
start()
{
[ "$(mobyplatform)" != "gcp" ] && exit 0
ebegin "Set hostname based on what GCP tells us it should be"
IP=$(metadata instance/network-interfaces/0/ip)
HOSTNAME=$(metadata instance/hostname)
echo "${IP} ${HOSTNAME} ${HOSTNAME%%.*} # Added by Google" >> /etc/hosts
echo "169.254.169.254 metadata.google.internal # Added by Google" >> /etc/hosts
echo ${HOSTNAME%%.*} >/etc/hostname
hostname -F /etc/hostname
eend 0
}
stop()
{
[ "$(mobyplatform)" != "gcp" ] && exit 0
}

View File

@ -0,0 +1,40 @@
#!/sbin/openrc-run
description="Run startup/shudown scripts on Docker GCP edition"
depend()
{
need net
}
metadata() {
curl -sH 'Metadata-Flavor: Google' http://metadata.google.internal/computeMetadata/v1/${1}
}
start()
{
[ "$(mobyplatform)" != "gcp" ] && exit 0
ebegin "Run startup script"
temp_file=$(mktemp)
metadata /instance/attributes/startup-script > ${temp_file}
[ -s "${temp_file}" ] && sh "${temp_file}"
rm -f "${temp_file}"
eend 0
}
stop()
{
[ "$(mobyplatform)" != "gcp" ] && exit 0
ebegin "Run shutdown script"
temp_file=$(mktemp)
metadata /instance/attributes/shutdown-script > ${temp_file}
[ -s "${temp_file}" ] && sh "${temp_file}"
rm -f "${temp_file}"
eend 0
}