#! /bin/sh # debug set -x set -v # Get initial cluster data from metadata global NAME_PREFIX := $[cat /etc/etcd/name_prefix] global INIT_CLUSTER := $[cat /etc/etcd/init_cluster] # Wait till we have an IP address global IP := ''"" while [ -z "$IP" ] { global IP := $[ifconfig eth0 !2 >/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://] sleep 1 } # Where to store data global DATADIR := '/var/lib/etcd' # Name is NAME_PREFIX+last octet of IP address global NUM := $[echo $(IP) | cut -d . -f 4] global NAME := "$(NAME_PREFIX)$(NUM)" # We currently have no easy way to determine if we join a cluster for # the first time or if we got restarted and need to join an existing # cluster. So we first try joining a *new* cluster. This fails if we # had already joined it previously. As a fallback we then try to join # an existing cluster. # Try to join an new cluster /usr/local/bin/etcd \ --name $(NAME) \ --debug \ --log-package-levels etcdmain=DEBUG,etcdserver=DEBUG \ --data-dir $DATADIR \ --initial-advertise-peer-urls http://$(IP):2380 \ --listen-peer-urls http://$(IP):2380 \ --listen-client-urls http://$(IP):2379,http://127.0.0.1:2379 \ --advertise-client-urls http://$(IP):2379 \ --initial-cluster-token etcd-cluster-1 \ --initial-cluster $(INIT_CLUSTER) \ --initial-cluster-state new test $Status -eq 0 && exit 0 # Try to join an existing cluster /usr/local/bin/etcd \ --name $(NAME) \ --debug \ --log-package-levels etcdmain=DEBUG,etcdserver=DEBUG \ --data-dir $DATADIR \ --initial-advertise-peer-urls http://$(IP):2380 \ --listen-peer-urls http://$(IP):2380 \ --listen-client-urls http://$(IP):2379,http://127.0.0.1:2379 \ --advertise-client-urls http://$(IP):2379 \ --initial-cluster $(INIT_CLUSTER) \ --initial-cluster-state existing (CommandList children: [ (C {(set)} {(-x)}) (C {(set)} {(-v)}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:NAME_PREFIX) op: Equal rhs: { (DQ (CommandSubPart command_list: (CommandList children:[(C {(cat)} {(/etc/etcd/name_prefix)})]) left_token: spids: [21 25] ) ) } spids: [19] ) ] spids: [19] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:INIT_CLUSTER) op: Equal rhs: { (DQ (CommandSubPart command_list: (CommandList children:[(C {(cat)} {(/etc/etcd/init_cluster)})]) left_token: spids: [30 34] ) ) } spids: [28] ) ] spids: [28] ) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:IP) op:Equal rhs:{(DQ )} spids:[41])] spids: [41] ) (While cond: [ (Sentence child: (C {(Lit_Other "[")} {(-z)} {(DQ ($ VSub_Name "$IP"))} {(Lit_Other "]")}) terminator: ) ] body: (DoGroup children: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:IP) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (SimpleCommand words: [{(ifconfig)} {(eth0)}] redirects: [ (Redir op_id: Redir_Great fd: 2 arg_word: {(/dev/null)} spids: [67] ) ] ) (C {(awk)} {(SQ <"/inet addr:/ {print $2}">)}) (C {(sed)} {(SQ <"s/addr://">)}) ] negated: False ) ] ) left_token: spids: [62 81] ) } spids: [61] ) ] spids: [61] ) (C {(sleep)} {(1)}) ] spids: [58 88] ) ) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:DATADIR) op:Equal rhs:{(/var/lib/etcd)} spids:[94])] spids: [94] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:NUM) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (C {(echo)} {(${ VSub_Name IP)}) (C {(cut)} {(-d)} {(.)} {(-f)} {(4)}) ] negated: False ) ] ) left_token: spids: [102 120] ) } spids: [101] ) ] spids: [101] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:NAME) op: Equal rhs: {(${ VSub_Name NAME_PREFIX) (${ VSub_Name NUM)} spids: [122] ) ] spids: [122] ) (C {(/usr/local/bin/etcd)} {(--name)} {(${ VSub_Name NAME)} {(--debug)} {(--log-package-levels)} {(Lit_VarLike "etcdmain=") (DEBUG) (Lit_Comma ",") (Lit_VarLike "etcdserver=") (DEBUG)} {(--data-dir)} {($ VSub_Name "$DATADIR")} {(--initial-advertise-peer-urls)} {(http) (Lit_Other ":") (//) (${ VSub_Name IP) (Lit_Other ":") (2380)} {(--listen-peer-urls)} {(http) (Lit_Other ":") (//) (${ VSub_Name IP) (Lit_Other ":") (2380)} {(--listen-client-urls)} {(http) (Lit_Other ":") (//) (${ VSub_Name IP) (Lit_Other ":") (2379) (Lit_Comma ",") (http) (Lit_Other ":") (//127.0.0.1) (Lit_Other ":") (2379) } {(--advertise-client-urls)} {(http) (Lit_Other ":") (//) (${ VSub_Name IP) (Lit_Other ":") (2379)} {(--initial-cluster-token)} {(etcd-cluster-1)} {(--initial-cluster)} {(${ VSub_Name INIT_CLUSTER)} {(--initial-cluster-state)} {(new)} ) (AndOr children: [ (C {(Lit_Other "[")} {($ VSub_QMark "$?")} {(-eq)} {(0)} {(Lit_Other "]")}) (C {(exit)} {(0)}) ] op_id: Op_DAmp ) (C {(/usr/local/bin/etcd)} {(--name)} {(${ VSub_Name NAME)} {(--debug)} {(--log-package-levels)} {(Lit_VarLike "etcdmain=") (DEBUG) (Lit_Comma ",") (Lit_VarLike "etcdserver=") (DEBUG)} {(--data-dir)} {($ VSub_Name "$DATADIR")} {(--initial-advertise-peer-urls)} {(http) (Lit_Other ":") (//) (${ VSub_Name IP) (Lit_Other ":") (2380)} {(--listen-peer-urls)} {(http) (Lit_Other ":") (//) (${ VSub_Name IP) (Lit_Other ":") (2380)} {(--listen-client-urls)} {(http) (Lit_Other ":") (//) (${ VSub_Name IP) (Lit_Other ":") (2379) (Lit_Comma ",") (http) (Lit_Other ":") (//127.0.0.1) (Lit_Other ":") (2379) } {(--advertise-client-urls)} {(http) (Lit_Other ":") (//) (${ VSub_Name IP) (Lit_Other ":") (2379)} {(--initial-cluster)} {(${ VSub_Name INIT_CLUSTER)} {(--initial-cluster-state)} {(existing)} ) ] )