From 245e64ac9492944d722ec9e0c47cfeb55ce5f640 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Wed, 28 Dec 2016 16:09:56 +0100 Subject: [PATCH] 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 --- alpine/Dockerfile | 3 +- alpine/packages/gcp/etc/init.d/gcp | 29 --------------- alpine/packages/gcp/etc/init.d/gcp-hostname | 34 ++++++++++++++++++ alpine/packages/gcp/etc/init.d/gcp-startup | 40 +++++++++++++++++++++ 4 files changed, 76 insertions(+), 30 deletions(-) delete mode 100755 alpine/packages/gcp/etc/init.d/gcp create mode 100755 alpine/packages/gcp/etc/init.d/gcp-hostname create mode 100755 alpine/packages/gcp/etc/init.d/gcp-startup diff --git a/alpine/Dockerfile b/alpine/Dockerfile index d065c5e20..c2c88057b 100644 --- a/alpine/Dockerfile +++ b/alpine/Dockerfile @@ -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"] diff --git a/alpine/packages/gcp/etc/init.d/gcp b/alpine/packages/gcp/etc/init.d/gcp deleted file mode 100755 index 9da3441a4..000000000 --- a/alpine/packages/gcp/etc/init.d/gcp +++ /dev/null @@ -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 -} diff --git a/alpine/packages/gcp/etc/init.d/gcp-hostname b/alpine/packages/gcp/etc/init.d/gcp-hostname new file mode 100755 index 000000000..c9a6e69e8 --- /dev/null +++ b/alpine/packages/gcp/etc/init.d/gcp-hostname @@ -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 +} diff --git a/alpine/packages/gcp/etc/init.d/gcp-startup b/alpine/packages/gcp/etc/init.d/gcp-startup new file mode 100755 index 000000000..813716a87 --- /dev/null +++ b/alpine/packages/gcp/etc/init.d/gcp-startup @@ -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 +}