blob: 25de238ff83a55cc88d1bb48339f8cd996f6d314 [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 Srinivas546656f2017-03-14 07:05:19 -040044 local cmd="$ETCD_BIN_DIR/etcd"
45 cmd+=" --name $HOSTNAME --data-dir $ETCD_DATA_DIR"
46 cmd+=" --initial-cluster-state new --initial-cluster-token etcd-cluster-01"
47 cmd+=" --initial-cluster $HOSTNAME=http://$SERVICE_HOST:2380"
48 cmd+=" --initial-advertise-peer-urls http://$SERVICE_HOST:2380"
Hongbin Lude858062017-05-24 18:42:33 +000049 cmd+=" --advertise-client-urls http://$SERVICE_HOST:$ETCD_PORT"
Davanum Srinivas546656f2017-03-14 07:05:19 -040050 cmd+=" --listen-peer-urls http://0.0.0.0:2380 "
Hongbin Lude858062017-05-24 18:42:33 +000051 cmd+=" --listen-client-urls http://$SERVICE_HOST:$ETCD_PORT"
Davanum Srinivas546656f2017-03-14 07:05:19 -040052
53 local unitfile="$SYSTEMD_DIR/$ETCD_SYSTEMD_SERVICE"
54 write_user_unit_file $ETCD_SYSTEMD_SERVICE "$cmd" "" "root"
55
56 iniset -sudo $unitfile "Unit" "After" "network.target"
57 iniset -sudo $unitfile "Service" "Type" "notify"
58 iniset -sudo $unitfile "Service" "Restart" "on-failure"
59 iniset -sudo $unitfile "Service" "LimitNOFILE" "65536"
60
61 $SYSTEMCTL daemon-reload
62 $SYSTEMCTL enable $ETCD_SYSTEMD_SERVICE
63 $SYSTEMCTL start $ETCD_SYSTEMD_SERVICE
64}
65
66# stop_etcd3() stops the etcd3 process
67function stop_etcd3 {
Davanum Srinivasd8283fd2017-05-23 22:12:39 -040068 # Don't install in sub nodes (multinode scenario)
69 if [ "$SERVICE_HOST" != "$HOST_IP" ]; then
70 return
71 fi
72
Davanum Srinivas546656f2017-03-14 07:05:19 -040073 $SYSTEMCTL stop $ETCD_SYSTEMD_SERVICE
74}
75
Davanum Srinivas853b4752017-05-25 13:03:10 -040076function cleanup_etcd3 {
Davanum Srinivasd8283fd2017-05-23 22:12:39 -040077 # Don't install in sub nodes (multinode scenario)
78 if [ "$SERVICE_HOST" != "$HOST_IP" ]; then
79 return
80 fi
81
Davanum Srinivas546656f2017-03-14 07:05:19 -040082 $SYSTEMCTL disable $ETCD_SYSTEMD_SERVICE
83
84 local unitfile="$SYSTEMD_DIR/$ETCD_SYSTEMD_SERVICE"
85 sudo rm -f $unitfile
86
87 $SYSTEMCTL daemon-reload
88
89 sudo rm -rf $ETCD_DATA_DIR
90}
91
Sean Dague62b56602017-06-19 08:27:16 -040092function install_etcd3 {
Davanum Srinivas546656f2017-03-14 07:05:19 -040093 echo "Installing etcd"
94
95 # Make sure etcd3 downloads the correct architecture
96 if is_arch "x86_64"; then
97 ETCD_ARCH="amd64"
Sean Daguebba92412017-05-24 07:56:10 -040098 ETCD_SHA256=${ETCD_SHA256:-$ETCD_SHA256_AMD64}
Davanum Srinivas546656f2017-03-14 07:05:19 -040099 elif is_arch "aarch64"; then
100 ETCD_ARCH="arm64"
Sean Daguebba92412017-05-24 07:56:10 -0400101 ETCD_SHA256=${ETCD_SHA256:-$ETCD_SHA256_ARM64}
Davanum Srinivas546656f2017-03-14 07:05:19 -0400102 elif is_arch "ppc64le"; then
103 ETCD_ARCH="ppc64le"
Sean Daguebba92412017-05-24 07:56:10 -0400104 ETCD_SHA256=${ETCD_SHA256:-$ETCD_SHA256_PPC64}
Davanum Srinivas546656f2017-03-14 07:05:19 -0400105 else
106 exit_distro_not_supported "invalid hardware type - $ETCD_ARCH"
107 fi
108
Sean Daguebba92412017-05-24 07:56:10 -0400109 ETCD_NAME=etcd-$ETCD_VERSION-linux-$ETCD_ARCH
110
Davanum Srinivas546656f2017-03-14 07:05:19 -0400111 # Create the necessary directories
112 sudo mkdir -p $ETCD_BIN_DIR
113 sudo mkdir -p $ETCD_DATA_DIR
114
115 # Download and cache the etcd tgz for subsequent use
Rodolfo Alonso Hernandez6f962a22017-05-31 11:00:08 +0100116 if [ ! -f "$FILES/etcd-$ETCD_VERSION-linux-$ETCD_ARCH/etcd" ]; then
Sean Daguebba92412017-05-24 07:56:10 -0400117 ETCD_DOWNLOAD_FILE=$ETCD_NAME.tar.gz
Rodolfo Alonso Hernandez6f962a22017-05-31 11:00:08 +0100118 wget $ETCD_DOWNLOAD_URL/$ETCD_VERSION/$ETCD_DOWNLOAD_FILE -O $FILES/$ETCD_DOWNLOAD_FILE
119 echo "${ETCD_SHA256} $FILES/${ETCD_DOWNLOAD_FILE}" > $FILES/etcd.sha256sum
Sean Daguebba92412017-05-24 07:56:10 -0400120 # NOTE(sdague): this should go fatal if this fails
Rodolfo Alonso Hernandez6f962a22017-05-31 11:00:08 +0100121 sha256sum -c $FILES/etcd.sha256sum
Davanum Srinivas546656f2017-03-14 07:05:19 -0400122
Rodolfo Alonso Hernandez6f962a22017-05-31 11:00:08 +0100123 tar xzvf $FILES/$ETCD_DOWNLOAD_FILE -C $FILES
124 sudo cp $FILES/$ETCD_NAME/etcd $ETCD_BIN_DIR/etcd
Davanum Srinivas546656f2017-03-14 07:05:19 -0400125 fi
126 if [ ! -f "$ETCD_BIN_DIR/etcd" ]; then
Rodolfo Alonso Hernandez6f962a22017-05-31 11:00:08 +0100127 sudo cp $FILES/$ETCD_NAME/etcd $ETCD_BIN_DIR/etcd
Davanum Srinivas546656f2017-03-14 07:05:19 -0400128 fi
129}
130
131# Restore xtrace
132$_XTRACE_ETCD3
133
134# Tell emacs to use shell-script-mode
135## Local variables:
136## mode: shell-script
137## End: