blob: 6e32cb31caf22bdf8d2c57e787da67175206d6d3 [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}
Sławek Kapłoński08367ba2017-08-27 08:44:27 +000029ETCD_DATA_DIR="$DATA_DIR/etcd"
Davanum Srinivas546656f2017-03-14 07:05:19 -040030ETCD_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"
Antoni Segura Puimedon19279b02017-06-16 16:03:32 +020049 cmd+=" --advertise-client-urls http://${HOST_IP}:$ETCD_PORT"
Davanum Srinivas546656f2017-03-14 07:05:19 -040050 cmd+=" --listen-peer-urls http://0.0.0.0:2380 "
Antoni Segura Puimedon19279b02017-06-16 16:03:32 +020051 cmd+=" --listen-client-urls http://${HOST_IP}:$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"
Kevin Zhaoeca7ce72017-08-04 11:50:36 +080060 if is_arch "aarch64"; then
61 iniset -sudo $unitfile "Service" "Environment" "ETCD_UNSUPPORTED_ARCH=arm64"
62 fi
Davanum Srinivas546656f2017-03-14 07:05:19 -040063
64 $SYSTEMCTL daemon-reload
65 $SYSTEMCTL enable $ETCD_SYSTEMD_SERVICE
66 $SYSTEMCTL start $ETCD_SYSTEMD_SERVICE
67}
68
69# stop_etcd3() stops the etcd3 process
70function stop_etcd3 {
Davanum Srinivasd8283fd2017-05-23 22:12:39 -040071 # Don't install in sub nodes (multinode scenario)
72 if [ "$SERVICE_HOST" != "$HOST_IP" ]; then
73 return
74 fi
75
Davanum Srinivas546656f2017-03-14 07:05:19 -040076 $SYSTEMCTL stop $ETCD_SYSTEMD_SERVICE
77}
78
Davanum Srinivas853b4752017-05-25 13:03:10 -040079function cleanup_etcd3 {
Davanum Srinivasd8283fd2017-05-23 22:12:39 -040080 # Don't install in sub nodes (multinode scenario)
81 if [ "$SERVICE_HOST" != "$HOST_IP" ]; then
82 return
83 fi
84
Davanum Srinivas546656f2017-03-14 07:05:19 -040085 $SYSTEMCTL disable $ETCD_SYSTEMD_SERVICE
86
87 local unitfile="$SYSTEMD_DIR/$ETCD_SYSTEMD_SERVICE"
88 sudo rm -f $unitfile
89
90 $SYSTEMCTL daemon-reload
91
92 sudo rm -rf $ETCD_DATA_DIR
93}
94
Sean Dague62b56602017-06-19 08:27:16 -040095function install_etcd3 {
Davanum Srinivas546656f2017-03-14 07:05:19 -040096 echo "Installing etcd"
97
98 # Make sure etcd3 downloads the correct architecture
99 if is_arch "x86_64"; then
100 ETCD_ARCH="amd64"
Sean Daguebba92412017-05-24 07:56:10 -0400101 ETCD_SHA256=${ETCD_SHA256:-$ETCD_SHA256_AMD64}
Davanum Srinivas546656f2017-03-14 07:05:19 -0400102 elif is_arch "aarch64"; then
103 ETCD_ARCH="arm64"
Sean Daguebba92412017-05-24 07:56:10 -0400104 ETCD_SHA256=${ETCD_SHA256:-$ETCD_SHA256_ARM64}
Davanum Srinivas546656f2017-03-14 07:05:19 -0400105 elif is_arch "ppc64le"; then
106 ETCD_ARCH="ppc64le"
Sean Daguebba92412017-05-24 07:56:10 -0400107 ETCD_SHA256=${ETCD_SHA256:-$ETCD_SHA256_PPC64}
Davanum Srinivas546656f2017-03-14 07:05:19 -0400108 else
109 exit_distro_not_supported "invalid hardware type - $ETCD_ARCH"
110 fi
111
Sean Daguebba92412017-05-24 07:56:10 -0400112 ETCD_NAME=etcd-$ETCD_VERSION-linux-$ETCD_ARCH
113
Davanum Srinivas546656f2017-03-14 07:05:19 -0400114 # Create the necessary directories
115 sudo mkdir -p $ETCD_BIN_DIR
116 sudo mkdir -p $ETCD_DATA_DIR
117
118 # Download and cache the etcd tgz for subsequent use
Rodolfo Alonso Hernandez6f962a22017-05-31 11:00:08 +0100119 if [ ! -f "$FILES/etcd-$ETCD_VERSION-linux-$ETCD_ARCH/etcd" ]; then
Sean Daguebba92412017-05-24 07:56:10 -0400120 ETCD_DOWNLOAD_FILE=$ETCD_NAME.tar.gz
TommyLike599ecfb2017-06-20 11:32:25 +0800121 if [ ! -f "$FILES/$ETCD_DOWNLOAD_FILE" ]; then
122 wget $ETCD_DOWNLOAD_URL/$ETCD_VERSION/$ETCD_DOWNLOAD_FILE -O $FILES/$ETCD_DOWNLOAD_FILE
123 fi
Rodolfo Alonso Hernandez6f962a22017-05-31 11:00:08 +0100124 echo "${ETCD_SHA256} $FILES/${ETCD_DOWNLOAD_FILE}" > $FILES/etcd.sha256sum
Sean Daguebba92412017-05-24 07:56:10 -0400125 # NOTE(sdague): this should go fatal if this fails
Rodolfo Alonso Hernandez6f962a22017-05-31 11:00:08 +0100126 sha256sum -c $FILES/etcd.sha256sum
Davanum Srinivas546656f2017-03-14 07:05:19 -0400127
Rodolfo Alonso Hernandez6f962a22017-05-31 11:00:08 +0100128 tar xzvf $FILES/$ETCD_DOWNLOAD_FILE -C $FILES
129 sudo cp $FILES/$ETCD_NAME/etcd $ETCD_BIN_DIR/etcd
Davanum Srinivas546656f2017-03-14 07:05:19 -0400130 fi
131 if [ ! -f "$ETCD_BIN_DIR/etcd" ]; then
Rodolfo Alonso Hernandez6f962a22017-05-31 11:00:08 +0100132 sudo cp $FILES/$ETCD_NAME/etcd $ETCD_BIN_DIR/etcd
Davanum Srinivas546656f2017-03-14 07:05:19 -0400133 fi
134}
135
136# Restore xtrace
137$_XTRACE_ETCD3
138
139# Tell emacs to use shell-script-mode
140## Local variables:
141## mode: shell-script
142## End: