From b293cd63d628f30d60e323f5f271b0bfa1134ad3 Mon Sep 17 00:00:00 2001 From: Brandon Philips Date: Wed, 10 Jul 2013 17:39:22 -0700 Subject: [PATCH] feat(dev-db): coordinate with the c10n service this is an experiment to coordinate etcd with c10n. --- .../dev-db/etcd/etcd-0.0.1.ebuild | 3 ++ .../dev-db/etcd/etcd-99999.ebuild | 5 ++- .../files/{coreos-c10n.sh => coreos-c10n} | 2 +- .../dev-db/etcd/files/etcd-bootstrap | 43 +++++++++++++++++++ .../dev-db/etcd/files/etcd.service | 2 + 5 files changed, 53 insertions(+), 2 deletions(-) rename sdk_container/src/third_party/coreos-overlay/dev-db/etcd/files/{coreos-c10n.sh => coreos-c10n} (89%) mode change 100644 => 100755 create mode 100755 sdk_container/src/third_party/coreos-overlay/dev-db/etcd/files/etcd-bootstrap diff --git a/sdk_container/src/third_party/coreos-overlay/dev-db/etcd/etcd-0.0.1.ebuild b/sdk_container/src/third_party/coreos-overlay/dev-db/etcd/etcd-0.0.1.ebuild index 40d529c70f..38b388fdab 100644 --- a/sdk_container/src/third_party/coreos-overlay/dev-db/etcd/etcd-0.0.1.ebuild +++ b/sdk_container/src/third_party/coreos-overlay/dev-db/etcd/etcd-0.0.1.ebuild @@ -31,6 +31,9 @@ src_compile() { src_install() { dobin ${S}/${PN} + dobin ${FILESDIR}/coreos-c10n + dobin ${FILESDIR}/etcd-bootstrap + systemd_dounit "${FILESDIR}"/${PN}.service systemd_enable_service multi-user.target ${PN}.service } diff --git a/sdk_container/src/third_party/coreos-overlay/dev-db/etcd/etcd-99999.ebuild b/sdk_container/src/third_party/coreos-overlay/dev-db/etcd/etcd-99999.ebuild index 71c7abc9c0..3c19817a38 100644 --- a/sdk_container/src/third_party/coreos-overlay/dev-db/etcd/etcd-99999.ebuild +++ b/sdk_container/src/third_party/coreos-overlay/dev-db/etcd/etcd-99999.ebuild @@ -29,7 +29,10 @@ src_compile() { } src_install() { - dosbin ${S}/${PN} + dobin ${S}/${PN} + dobin ${FILESDIR}/coreos-c10n + dobin ${FILESDIR}/etcd-bootstrap + systemd_dounit "${FILESDIR}"/${PN}.service systemd_enable_service multi-user.target ${PN}.service } diff --git a/sdk_container/src/third_party/coreos-overlay/dev-db/etcd/files/coreos-c10n.sh b/sdk_container/src/third_party/coreos-overlay/dev-db/etcd/files/coreos-c10n old mode 100644 new mode 100755 similarity index 89% rename from sdk_container/src/third_party/coreos-overlay/dev-db/etcd/files/coreos-c10n.sh rename to sdk_container/src/third_party/coreos-overlay/dev-db/etcd/files/coreos-c10n index 558bbdd7bb..886e729c7e --- a/sdk_container/src/third_party/coreos-overlay/dev-db/etcd/files/coreos-c10n.sh +++ b/sdk_container/src/third_party/coreos-overlay/dev-db/etcd/files/coreos-c10n @@ -32,4 +32,4 @@ for IP4 in `curl -s $META_URL/meta-data/ | grep ipv4`; do fi done -curl $C10N_ENDPOINT -d "c10n_url=$URL" -d"ip_list=$IP_LIST" +curl $C10N_ENDPOINT -d "c10n_url=$URL" -d"ip_list=$IP_LIST" > /var/run/etcd/bootstrap.config diff --git a/sdk_container/src/third_party/coreos-overlay/dev-db/etcd/files/etcd-bootstrap b/sdk_container/src/third_party/coreos-overlay/dev-db/etcd/files/etcd-bootstrap new file mode 100755 index 0000000000..0876dea338 --- /dev/null +++ b/sdk_container/src/third_party/coreos-overlay/dev-db/etcd/files/etcd-bootstrap @@ -0,0 +1,43 @@ +#!/bin/bash + +# Test an IP address for validity: +# Usage: +# valid_ip IP_ADDRESS +# if [[ $? -eq 0 ]]; then echo good; else echo bad; fi +# OR +# if valid_ip IP_ADDRESS; then echo good; else echo bad; fi +# http://www.linuxjournal.com/content/validating-ip-address-bash-script +function valid_ip() +{ + local ip=$1 + local stat=1 + + if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then + OIFS=$IFS + IFS='.' + ip=($ip) + IFS=$OIFS + [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \ + && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]] + stat=$? + fi + return $stat +} + +BOOTSTRAP="/var/run/etcd/bootstrap.config" +for HOSTS in `[ -e $BOOTSTRAP ] && cat $BOOTSTRAP`; do + for IP in `echo $HOSTS | sed 's/,/\'$'\n/g' `; do + echo $IP + LEADER=`curl --connect-timeout 1 -s $IP:7001/leader` + if [ $? -eq 0 ]; then + LEADER_IP=`echo $LEADER | cut -d':' -f1` + LEADER_PORT=`echo $LEADER | cut -d':' -f2` + if valid_ip $LEADER_IP; then + echo GOT LEADER $LEADER + exec /usr/bin/etcd -i -C $LEADER + fi + fi + done +done +echo "Not able to connect to a leader, just starting..." +exec /usr/bin/etcd -i diff --git a/sdk_container/src/third_party/coreos-overlay/dev-db/etcd/files/etcd.service b/sdk_container/src/third_party/coreos-overlay/dev-db/etcd/files/etcd.service index bd08c04c9d..593d12a87e 100644 --- a/sdk_container/src/third_party/coreos-overlay/dev-db/etcd/files/etcd.service +++ b/sdk_container/src/third_party/coreos-overlay/dev-db/etcd/files/etcd.service @@ -1,4 +1,6 @@ [Service] +ExecStartPre=-/bin/mkdir -p /var/run/etcd +ExecStartPre=/usr/bin/coreos-c10n ExecStart=/usr/bin/etcd-bootstrap [Install]