blob: a1a2ac6a74db0890f0180165e57283603f011196 [file] [log] [blame]
Davanum Srinivas546656f2017-03-14 07:05:19 -04001#!/bin/bash
2#
3# lib/etcd3
4#
5# Functions to control the installation and configuration of etcd 3.x
6# that provides a key-value store (and possibly other functions).
7
8# Dependencies:
9#
10# - ``functions`` file
11
12# ``stack.sh`` calls the entry points in this order:
13#
14# - start_etcd3
15# - stop_etcd3
16# - cleanup_etcd3
17
18# Save trace setting
19_XTRACE_ETCD3=$(set +o | grep xtrace)
20set +o xtrace
21
22
23# Defaults
24# --------
25
26# Set up default values for etcd
27ETCD_DOWNLOAD_URL=${ETCD_DOWNLOAD_URL:-https://github.com/coreos/etcd/releases/download}
28ETCD_VERSION=${ETCD_VERSION:-v3.1.7}
29ETCD_DATA_DIR="$DEST/data/etcd"
30ETCD_SYSTEMD_SERVICE="devstack@etcd.service"
31ETCD_BIN_DIR="$DEST/bin"
Sean Daguebba92412017-05-24 07:56:10 -040032ETCD_SHA256_AMD64="4fde194bbcd259401e2b5c462dfa579ee7f6af539f13f130b8f5b4f52e3b3c52"
33# NOTE(sdague): etcd v3.1.7 doesn't have anything for these architectures, though 3.2.0 does.
34ETCD_SHA256_ARM64=""
35ETCD_SHA256_PPC64=""
Davanum Srinivas546656f2017-03-14 07:05:19 -040036
37if is_ubuntu ; then
38 UBUNTU_RELEASE_BASE_NUM=`lsb_release -r | awk '{print $2}' | cut -d '.' -f 1`
39fi
40
41# start_etcd3() - Starts to run the etcd process
42function start_etcd3 {
Davanum Srinivasd8283fd2017-05-23 22:12:39 -040043 # Don't install in sub nodes (multinode scenario)
44 if [ "$SERVICE_HOST" != "$HOST_IP" ]; then
45 return
46 fi
47
Davanum Srinivas546656f2017-03-14 07:05:19 -040048 _install_etcd
49
50 local cmd="$ETCD_BIN_DIR/etcd"
51 cmd+=" --name $HOSTNAME --data-dir $ETCD_DATA_DIR"
52 cmd+=" --initial-cluster-state new --initial-cluster-token etcd-cluster-01"
53 cmd+=" --initial-cluster $HOSTNAME=http://$SERVICE_HOST:2380"
54 cmd+=" --initial-advertise-peer-urls http://$SERVICE_HOST:2380"
55 cmd+=" --advertise-client-urls http://$SERVICE_HOST:2379"
56 cmd+=" --listen-peer-urls http://0.0.0.0:2380 "
57 cmd+=" --listen-client-urls http://$SERVICE_HOST:2379"
58
59 local unitfile="$SYSTEMD_DIR/$ETCD_SYSTEMD_SERVICE"
60 write_user_unit_file $ETCD_SYSTEMD_SERVICE "$cmd" "" "root"
61
62 iniset -sudo $unitfile "Unit" "After" "network.target"
63 iniset -sudo $unitfile "Service" "Type" "notify"
64 iniset -sudo $unitfile "Service" "Restart" "on-failure"
65 iniset -sudo $unitfile "Service" "LimitNOFILE" "65536"
66
67 $SYSTEMCTL daemon-reload
68 $SYSTEMCTL enable $ETCD_SYSTEMD_SERVICE
69 $SYSTEMCTL start $ETCD_SYSTEMD_SERVICE
70}
71
72# stop_etcd3() stops the etcd3 process
73function stop_etcd3 {
Davanum Srinivasd8283fd2017-05-23 22:12:39 -040074 # Don't install in sub nodes (multinode scenario)
75 if [ "$SERVICE_HOST" != "$HOST_IP" ]; then
76 return
77 fi
78
Davanum Srinivas546656f2017-03-14 07:05:19 -040079 $SYSTEMCTL stop $ETCD_SYSTEMD_SERVICE
80}
81
Davanum Srinivas853b4752017-05-25 13:03:10 -040082function cleanup_etcd3 {
Davanum Srinivasd8283fd2017-05-23 22:12:39 -040083 # Don't install in sub nodes (multinode scenario)
84 if [ "$SERVICE_HOST" != "$HOST_IP" ]; then
85 return
86 fi
87
Davanum Srinivas546656f2017-03-14 07:05:19 -040088 $SYSTEMCTL disable $ETCD_SYSTEMD_SERVICE
89
90 local unitfile="$SYSTEMD_DIR/$ETCD_SYSTEMD_SERVICE"
91 sudo rm -f $unitfile
92
93 $SYSTEMCTL daemon-reload
94
95 sudo rm -rf $ETCD_DATA_DIR
96}
97
98function _install_etcd {
99 echo "Installing etcd"
100
101 # Make sure etcd3 downloads the correct architecture
102 if is_arch "x86_64"; then
103 ETCD_ARCH="amd64"
Sean Daguebba92412017-05-24 07:56:10 -0400104 ETCD_SHA256=${ETCD_SHA256:-$ETCD_SHA256_AMD64}
Davanum Srinivas546656f2017-03-14 07:05:19 -0400105 elif is_arch "aarch64"; then
106 ETCD_ARCH="arm64"
Sean Daguebba92412017-05-24 07:56:10 -0400107 ETCD_SHA256=${ETCD_SHA256:-$ETCD_SHA256_ARM64}
Davanum Srinivas546656f2017-03-14 07:05:19 -0400108 elif is_arch "ppc64le"; then
109 ETCD_ARCH="ppc64le"
Sean Daguebba92412017-05-24 07:56:10 -0400110 ETCD_SHA256=${ETCD_SHA256:-$ETCD_SHA256_PPC64}
Davanum Srinivas546656f2017-03-14 07:05:19 -0400111 else
112 exit_distro_not_supported "invalid hardware type - $ETCD_ARCH"
113 fi
114
Sean Daguebba92412017-05-24 07:56:10 -0400115 ETCD_NAME=etcd-$ETCD_VERSION-linux-$ETCD_ARCH
116
Davanum Srinivas546656f2017-03-14 07:05:19 -0400117 # Install the libraries needed. Note: tooz for example does not have a hard dependency on these libraries
118 pip_install etcd3
119 pip_install etcd3gw
120
121 # Create the necessary directories
122 sudo mkdir -p $ETCD_BIN_DIR
123 sudo mkdir -p $ETCD_DATA_DIR
124
125 # Download and cache the etcd tgz for subsequent use
Rodolfo Alonso Hernandez6f962a22017-05-31 11:00:08 +0100126 if [ ! -f "$FILES/etcd-$ETCD_VERSION-linux-$ETCD_ARCH/etcd" ]; then
Sean Daguebba92412017-05-24 07:56:10 -0400127 ETCD_DOWNLOAD_FILE=$ETCD_NAME.tar.gz
Rodolfo Alonso Hernandez6f962a22017-05-31 11:00:08 +0100128 wget $ETCD_DOWNLOAD_URL/$ETCD_VERSION/$ETCD_DOWNLOAD_FILE -O $FILES/$ETCD_DOWNLOAD_FILE
129 echo "${ETCD_SHA256} $FILES/${ETCD_DOWNLOAD_FILE}" > $FILES/etcd.sha256sum
Sean Daguebba92412017-05-24 07:56:10 -0400130 # NOTE(sdague): this should go fatal if this fails
Rodolfo Alonso Hernandez6f962a22017-05-31 11:00:08 +0100131 sha256sum -c $FILES/etcd.sha256sum
Davanum Srinivas546656f2017-03-14 07:05:19 -0400132
Rodolfo Alonso Hernandez6f962a22017-05-31 11:00:08 +0100133 tar xzvf $FILES/$ETCD_DOWNLOAD_FILE -C $FILES
134 sudo cp $FILES/$ETCD_NAME/etcd $ETCD_BIN_DIR/etcd
Davanum Srinivas546656f2017-03-14 07:05:19 -0400135 fi
136 if [ ! -f "$ETCD_BIN_DIR/etcd" ]; then
Rodolfo Alonso Hernandez6f962a22017-05-31 11:00:08 +0100137 sudo cp $FILES/$ETCD_NAME/etcd $ETCD_BIN_DIR/etcd
Davanum Srinivas546656f2017-03-14 07:05:19 -0400138 fi
139}
140
141# Restore xtrace
142$_XTRACE_ETCD3
143
144# Tell emacs to use shell-script-mode
145## Local variables:
146## mode: shell-script
147## End: