blob: 26d07fd19ec11c87d908dc650b88c5e9f7e681e5 [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
Sławek Kapłoński08367ba2017-08-27 08:44:27 +000027ETCD_DATA_DIR="$DATA_DIR/etcd"
Davanum Srinivas546656f2017-03-14 07:05:19 -040028ETCD_SYSTEMD_SERVICE="devstack@etcd.service"
29ETCD_BIN_DIR="$DEST/bin"
30
31if is_ubuntu ; then
32 UBUNTU_RELEASE_BASE_NUM=`lsb_release -r | awk '{print $2}' | cut -d '.' -f 1`
33fi
34
35# start_etcd3() - Starts to run the etcd process
36function start_etcd3 {
Davanum Srinivas546656f2017-03-14 07:05:19 -040037 local cmd="$ETCD_BIN_DIR/etcd"
38 cmd+=" --name $HOSTNAME --data-dir $ETCD_DATA_DIR"
39 cmd+=" --initial-cluster-state new --initial-cluster-token etcd-cluster-01"
Davanum Srinivas27367be2017-11-28 08:20:48 -050040 cmd+=" --initial-cluster $HOSTNAME=http://$SERVICE_HOST:$ETCD_PEER_PORT"
41 cmd+=" --initial-advertise-peer-urls http://$SERVICE_HOST:$ETCD_PEER_PORT"
Jens Harbott146332e2017-09-20 06:18:08 +000042 cmd+=" --advertise-client-urls http://$SERVICE_HOST:$ETCD_PORT"
43 if [ "$SERVICE_LISTEN_ADDRESS" == "::" ]; then
Davanum Srinivas27367be2017-11-28 08:20:48 -050044 cmd+=" --listen-peer-urls http://[::]:$ETCD_PEER_PORT "
Jens Harbott146332e2017-09-20 06:18:08 +000045 else
Davanum Srinivas27367be2017-11-28 08:20:48 -050046 cmd+=" --listen-peer-urls http://0.0.0.0:$ETCD_PEER_PORT "
Jens Harbott146332e2017-09-20 06:18:08 +000047 fi
48 cmd+=" --listen-client-urls http://$SERVICE_HOST:$ETCD_PORT"
Davanum Srinivas546656f2017-03-14 07:05:19 -040049
50 local unitfile="$SYSTEMD_DIR/$ETCD_SYSTEMD_SERVICE"
51 write_user_unit_file $ETCD_SYSTEMD_SERVICE "$cmd" "" "root"
52
53 iniset -sudo $unitfile "Unit" "After" "network.target"
54 iniset -sudo $unitfile "Service" "Type" "notify"
55 iniset -sudo $unitfile "Service" "Restart" "on-failure"
56 iniset -sudo $unitfile "Service" "LimitNOFILE" "65536"
Kevin Zhaoeca7ce72017-08-04 11:50:36 +080057 if is_arch "aarch64"; then
58 iniset -sudo $unitfile "Service" "Environment" "ETCD_UNSUPPORTED_ARCH=arm64"
59 fi
Davanum Srinivas546656f2017-03-14 07:05:19 -040060
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
Davanum Srinivas546656f2017-03-14 07:05:19 -040095 # Create the necessary directories
96 sudo mkdir -p $ETCD_BIN_DIR
97 sudo mkdir -p $ETCD_DATA_DIR
98
99 # Download and cache the etcd tgz for subsequent use
Monty Taylord8bb2202017-09-03 12:13:59 -0500100 local etcd_file
101 etcd_file="$(get_extra_file $ETCD_DOWNLOAD_LOCATION)"
Rodolfo Alonso Hernandez6f962a22017-05-31 11:00:08 +0100102 if [ ! -f "$FILES/etcd-$ETCD_VERSION-linux-$ETCD_ARCH/etcd" ]; then
Monty Taylord8bb2202017-09-03 12:13:59 -0500103 echo "${ETCD_SHA256} $etcd_file" > $FILES/etcd.sha256sum
yuanke weic652a492017-09-17 22:18:07 +0800104 # NOTE(yuanke wei): rm the damaged file when checksum fails
105 sha256sum -c $FILES/etcd.sha256sum || (sudo rm -f $etcd_file; exit 1)
Davanum Srinivas546656f2017-03-14 07:05:19 -0400106
Monty Taylord8bb2202017-09-03 12:13:59 -0500107 tar xzvf $etcd_file -C $FILES
Rodolfo Alonso Hernandez6f962a22017-05-31 11:00:08 +0100108 sudo cp $FILES/$ETCD_NAME/etcd $ETCD_BIN_DIR/etcd
Hongbin Luad180e02017-11-29 13:21:30 -0500109 sudo cp $FILES/$ETCD_NAME/etcdctl $ETCD_BIN_DIR/etcdctl
Davanum Srinivas546656f2017-03-14 07:05:19 -0400110 fi
111 if [ ! -f "$ETCD_BIN_DIR/etcd" ]; then
Rodolfo Alonso Hernandez6f962a22017-05-31 11:00:08 +0100112 sudo cp $FILES/$ETCD_NAME/etcd $ETCD_BIN_DIR/etcd
Hongbin Luad180e02017-11-29 13:21:30 -0500113 sudo cp $FILES/$ETCD_NAME/etcdctl $ETCD_BIN_DIR/etcdctl
Davanum Srinivas546656f2017-03-14 07:05:19 -0400114 fi
115}
116
117# Restore xtrace
118$_XTRACE_ETCD3
119
120# Tell emacs to use shell-script-mode
121## Local variables:
122## mode: shell-script
123## End: