blob: 15c29132be2d0cfcc16e92cb18e26afa5d863381 [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 {
43 _install_etcd
44
45 local cmd="$ETCD_BIN_DIR/etcd"
46 cmd+=" --name $HOSTNAME --data-dir $ETCD_DATA_DIR"
47 cmd+=" --initial-cluster-state new --initial-cluster-token etcd-cluster-01"
48 cmd+=" --initial-cluster $HOSTNAME=http://$SERVICE_HOST:2380"
49 cmd+=" --initial-advertise-peer-urls http://$SERVICE_HOST:2380"
50 cmd+=" --advertise-client-urls http://$SERVICE_HOST:2379"
51 cmd+=" --listen-peer-urls http://0.0.0.0:2380 "
52 cmd+=" --listen-client-urls http://$SERVICE_HOST:2379"
53
54 local unitfile="$SYSTEMD_DIR/$ETCD_SYSTEMD_SERVICE"
55 write_user_unit_file $ETCD_SYSTEMD_SERVICE "$cmd" "" "root"
56
57 iniset -sudo $unitfile "Unit" "After" "network.target"
58 iniset -sudo $unitfile "Service" "Type" "notify"
59 iniset -sudo $unitfile "Service" "Restart" "on-failure"
60 iniset -sudo $unitfile "Service" "LimitNOFILE" "65536"
61
62 $SYSTEMCTL daemon-reload
63 $SYSTEMCTL enable $ETCD_SYSTEMD_SERVICE
64 $SYSTEMCTL start $ETCD_SYSTEMD_SERVICE
65}
66
67# stop_etcd3() stops the etcd3 process
68function stop_etcd3 {
69 $SYSTEMCTL stop $ETCD_SYSTEMD_SERVICE
70}
71
72function cleanup_etcd {
73 $SYSTEMCTL disable $ETCD_SYSTEMD_SERVICE
74
75 local unitfile="$SYSTEMD_DIR/$ETCD_SYSTEMD_SERVICE"
76 sudo rm -f $unitfile
77
78 $SYSTEMCTL daemon-reload
79
80 sudo rm -rf $ETCD_DATA_DIR
81}
82
83function _install_etcd {
84 echo "Installing etcd"
85
86 # Make sure etcd3 downloads the correct architecture
87 if is_arch "x86_64"; then
88 ETCD_ARCH="amd64"
Sean Daguebba92412017-05-24 07:56:10 -040089 ETCD_SHA256=${ETCD_SHA256:-$ETCD_SHA256_AMD64}
Davanum Srinivas546656f2017-03-14 07:05:19 -040090 elif is_arch "aarch64"; then
91 ETCD_ARCH="arm64"
Sean Daguebba92412017-05-24 07:56:10 -040092 ETCD_SHA256=${ETCD_SHA256:-$ETCD_SHA256_ARM64}
Davanum Srinivas546656f2017-03-14 07:05:19 -040093 elif is_arch "ppc64le"; then
94 ETCD_ARCH="ppc64le"
Sean Daguebba92412017-05-24 07:56:10 -040095 ETCD_SHA256=${ETCD_SHA256:-$ETCD_SHA256_PPC64}
Davanum Srinivas546656f2017-03-14 07:05:19 -040096 else
97 exit_distro_not_supported "invalid hardware type - $ETCD_ARCH"
98 fi
99
Sean Daguebba92412017-05-24 07:56:10 -0400100 ETCD_NAME=etcd-$ETCD_VERSION-linux-$ETCD_ARCH
101
Davanum Srinivas546656f2017-03-14 07:05:19 -0400102 # Install the libraries needed. Note: tooz for example does not have a hard dependency on these libraries
103 pip_install etcd3
104 pip_install etcd3gw
105
106 # Create the necessary directories
107 sudo mkdir -p $ETCD_BIN_DIR
108 sudo mkdir -p $ETCD_DATA_DIR
109
110 # Download and cache the etcd tgz for subsequent use
Sean Daguebba92412017-05-24 07:56:10 -0400111 if [ ! -f "files/etcd-$ETCD_VERSION-linux-$ETCD_ARCH/etcd" ]; then
112 ETCD_DOWNLOAD_FILE=$ETCD_NAME.tar.gz
113 wget $ETCD_DOWNLOAD_URL/$ETCD_VERSION/$ETCD_DOWNLOAD_FILE -O files/$ETCD_DOWNLOAD_FILE
114 echo "${ETCD_SHA256} files/${ETCD_DOWNLOAD_FILE}" > files/etcd.sha256sum
115 # NOTE(sdague): this should go fatal if this fails
116 sha256sum -c files/etcd.sha256sum
Davanum Srinivas546656f2017-03-14 07:05:19 -0400117
Sean Daguebba92412017-05-24 07:56:10 -0400118 tar xzvf files/$ETCD_DOWNLOAD_FILE -C files
119 sudo cp files/$ETCD_NAME/etcd $ETCD_BIN_DIR/etcd
Davanum Srinivas546656f2017-03-14 07:05:19 -0400120 fi
121 if [ ! -f "$ETCD_BIN_DIR/etcd" ]; then
Sean Daguebba92412017-05-24 07:56:10 -0400122 sudo cp files/$ETCD_NAME/etcd $ETCD_BIN_DIR/etcd
Davanum Srinivas546656f2017-03-14 07:05:19 -0400123 fi
124}
125
126# Restore xtrace
127$_XTRACE_ETCD3
128
129# Tell emacs to use shell-script-mode
130## Local variables:
131## mode: shell-script
132## End: