blob: 5472926f6403b045902defa696a3842627f975ec [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=""
Hongbin Lude858062017-05-24 18:42:33 +000036ETCD_PORT=2379
Davanum Srinivas546656f2017-03-14 07:05:19 -040037
38if is_ubuntu ; then
39 UBUNTU_RELEASE_BASE_NUM=`lsb_release -r | awk '{print $2}' | cut -d '.' -f 1`
40fi
41
42# start_etcd3() - Starts to run the etcd process
43function start_etcd3 {
Davanum Srinivasd8283fd2017-05-23 22:12:39 -040044 # Don't install in sub nodes (multinode scenario)
45 if [ "$SERVICE_HOST" != "$HOST_IP" ]; then
46 return
47 fi
48
Davanum Srinivas546656f2017-03-14 07:05:19 -040049 _install_etcd
50
51 local cmd="$ETCD_BIN_DIR/etcd"
52 cmd+=" --name $HOSTNAME --data-dir $ETCD_DATA_DIR"
53 cmd+=" --initial-cluster-state new --initial-cluster-token etcd-cluster-01"
54 cmd+=" --initial-cluster $HOSTNAME=http://$SERVICE_HOST:2380"
55 cmd+=" --initial-advertise-peer-urls http://$SERVICE_HOST:2380"
Hongbin Lude858062017-05-24 18:42:33 +000056 cmd+=" --advertise-client-urls http://$SERVICE_HOST:$ETCD_PORT"
Davanum Srinivas546656f2017-03-14 07:05:19 -040057 cmd+=" --listen-peer-urls http://0.0.0.0:2380 "
Hongbin Lude858062017-05-24 18:42:33 +000058 cmd+=" --listen-client-urls http://$SERVICE_HOST:$ETCD_PORT"
Davanum Srinivas546656f2017-03-14 07:05:19 -040059
60 local unitfile="$SYSTEMD_DIR/$ETCD_SYSTEMD_SERVICE"
61 write_user_unit_file $ETCD_SYSTEMD_SERVICE "$cmd" "" "root"
62
63 iniset -sudo $unitfile "Unit" "After" "network.target"
64 iniset -sudo $unitfile "Service" "Type" "notify"
65 iniset -sudo $unitfile "Service" "Restart" "on-failure"
66 iniset -sudo $unitfile "Service" "LimitNOFILE" "65536"
67
68 $SYSTEMCTL daemon-reload
69 $SYSTEMCTL enable $ETCD_SYSTEMD_SERVICE
70 $SYSTEMCTL start $ETCD_SYSTEMD_SERVICE
71}
72
73# stop_etcd3() stops the etcd3 process
74function stop_etcd3 {
Davanum Srinivasd8283fd2017-05-23 22:12:39 -040075 # Don't install in sub nodes (multinode scenario)
76 if [ "$SERVICE_HOST" != "$HOST_IP" ]; then
77 return
78 fi
79
Davanum Srinivas546656f2017-03-14 07:05:19 -040080 $SYSTEMCTL stop $ETCD_SYSTEMD_SERVICE
81}
82
83function cleanup_etcd {
Davanum Srinivasd8283fd2017-05-23 22:12:39 -040084 # Don't install in sub nodes (multinode scenario)
85 if [ "$SERVICE_HOST" != "$HOST_IP" ]; then
86 return
87 fi
88
Davanum Srinivas546656f2017-03-14 07:05:19 -040089 $SYSTEMCTL disable $ETCD_SYSTEMD_SERVICE
90
91 local unitfile="$SYSTEMD_DIR/$ETCD_SYSTEMD_SERVICE"
92 sudo rm -f $unitfile
93
94 $SYSTEMCTL daemon-reload
95
96 sudo rm -rf $ETCD_DATA_DIR
97}
98
99function _install_etcd {
100 echo "Installing etcd"
101
102 # Make sure etcd3 downloads the correct architecture
103 if is_arch "x86_64"; then
104 ETCD_ARCH="amd64"
Sean Daguebba92412017-05-24 07:56:10 -0400105 ETCD_SHA256=${ETCD_SHA256:-$ETCD_SHA256_AMD64}
Davanum Srinivas546656f2017-03-14 07:05:19 -0400106 elif is_arch "aarch64"; then
107 ETCD_ARCH="arm64"
Sean Daguebba92412017-05-24 07:56:10 -0400108 ETCD_SHA256=${ETCD_SHA256:-$ETCD_SHA256_ARM64}
Davanum Srinivas546656f2017-03-14 07:05:19 -0400109 elif is_arch "ppc64le"; then
110 ETCD_ARCH="ppc64le"
Sean Daguebba92412017-05-24 07:56:10 -0400111 ETCD_SHA256=${ETCD_SHA256:-$ETCD_SHA256_PPC64}
Davanum Srinivas546656f2017-03-14 07:05:19 -0400112 else
113 exit_distro_not_supported "invalid hardware type - $ETCD_ARCH"
114 fi
115
Sean Daguebba92412017-05-24 07:56:10 -0400116 ETCD_NAME=etcd-$ETCD_VERSION-linux-$ETCD_ARCH
117
Davanum Srinivas546656f2017-03-14 07:05:19 -0400118 # Install the libraries needed. Note: tooz for example does not have a hard dependency on these libraries
119 pip_install etcd3
120 pip_install etcd3gw
121
122 # Create the necessary directories
123 sudo mkdir -p $ETCD_BIN_DIR
124 sudo mkdir -p $ETCD_DATA_DIR
125
126 # Download and cache the etcd tgz for subsequent use
Sean Daguebba92412017-05-24 07:56:10 -0400127 if [ ! -f "files/etcd-$ETCD_VERSION-linux-$ETCD_ARCH/etcd" ]; then
128 ETCD_DOWNLOAD_FILE=$ETCD_NAME.tar.gz
129 wget $ETCD_DOWNLOAD_URL/$ETCD_VERSION/$ETCD_DOWNLOAD_FILE -O files/$ETCD_DOWNLOAD_FILE
130 echo "${ETCD_SHA256} files/${ETCD_DOWNLOAD_FILE}" > files/etcd.sha256sum
131 # NOTE(sdague): this should go fatal if this fails
132 sha256sum -c files/etcd.sha256sum
Davanum Srinivas546656f2017-03-14 07:05:19 -0400133
Sean Daguebba92412017-05-24 07:56:10 -0400134 tar xzvf files/$ETCD_DOWNLOAD_FILE -C files
135 sudo cp files/$ETCD_NAME/etcd $ETCD_BIN_DIR/etcd
Davanum Srinivas546656f2017-03-14 07:05:19 -0400136 fi
137 if [ ! -f "$ETCD_BIN_DIR/etcd" ]; then
Sean Daguebba92412017-05-24 07:56:10 -0400138 sudo cp files/$ETCD_NAME/etcd $ETCD_BIN_DIR/etcd
Davanum Srinivas546656f2017-03-14 07:05:19 -0400139 fi
140}
141
142# Restore xtrace
143$_XTRACE_ETCD3
144
145# Tell emacs to use shell-script-mode
146## Local variables:
147## mode: shell-script
148## End: