blob: fa60a392c95b289e815637f9c6448d91977b1b25 [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"
32
33if is_ubuntu ; then
34 UBUNTU_RELEASE_BASE_NUM=`lsb_release -r | awk '{print $2}' | cut -d '.' -f 1`
35fi
36
37# start_etcd3() - Starts to run the etcd process
38function start_etcd3 {
39 _install_etcd
40
41 local cmd="$ETCD_BIN_DIR/etcd"
42 cmd+=" --name $HOSTNAME --data-dir $ETCD_DATA_DIR"
43 cmd+=" --initial-cluster-state new --initial-cluster-token etcd-cluster-01"
44 cmd+=" --initial-cluster $HOSTNAME=http://$SERVICE_HOST:2380"
45 cmd+=" --initial-advertise-peer-urls http://$SERVICE_HOST:2380"
46 cmd+=" --advertise-client-urls http://$SERVICE_HOST:2379"
47 cmd+=" --listen-peer-urls http://0.0.0.0:2380 "
48 cmd+=" --listen-client-urls http://$SERVICE_HOST:2379"
49
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"
57
58 $SYSTEMCTL daemon-reload
59 $SYSTEMCTL enable $ETCD_SYSTEMD_SERVICE
60 $SYSTEMCTL start $ETCD_SYSTEMD_SERVICE
61}
62
63# stop_etcd3() stops the etcd3 process
64function stop_etcd3 {
65 $SYSTEMCTL stop $ETCD_SYSTEMD_SERVICE
66}
67
68function cleanup_etcd {
69 $SYSTEMCTL disable $ETCD_SYSTEMD_SERVICE
70
71 local unitfile="$SYSTEMD_DIR/$ETCD_SYSTEMD_SERVICE"
72 sudo rm -f $unitfile
73
74 $SYSTEMCTL daemon-reload
75
76 sudo rm -rf $ETCD_DATA_DIR
77}
78
79function _install_etcd {
80 echo "Installing etcd"
81
82 # Make sure etcd3 downloads the correct architecture
83 if is_arch "x86_64"; then
84 ETCD_ARCH="amd64"
85 elif is_arch "aarch64"; then
86 ETCD_ARCH="arm64"
87 elif is_arch "ppc64le"; then
88 ETCD_ARCH="ppc64le"
89 else
90 exit_distro_not_supported "invalid hardware type - $ETCD_ARCH"
91 fi
92
93 # Install the libraries needed. Note: tooz for example does not have a hard dependency on these libraries
94 pip_install etcd3
95 pip_install etcd3gw
96
97 # Create the necessary directories
98 sudo mkdir -p $ETCD_BIN_DIR
99 sudo mkdir -p $ETCD_DATA_DIR
100
101 # Download and cache the etcd tgz for subsequent use
102 if [ ! -f "$DEST/etcd/etcd-$ETCD_VERSION-linux-$ETCD_ARCH/etcd" ]; then
103 mkdir -p $DEST/etcd
104 ETCD_DOWNLOAD_FILE=etcd-$ETCD_VERSION-linux-$ETCD_ARCH.tar.gz
105 wget $ETCD_DOWNLOAD_URL/$ETCD_VERSION/$ETCD_DOWNLOAD_FILE -O $DEST/etcd/$ETCD_DOWNLOAD_FILE
106 wget $ETCD_DOWNLOAD_URL/$ETCD_VERSION/$ETCD_DOWNLOAD_FILE.asc -O $DEST/etcd/$ETCD_DOWNLOAD_FILE.asc
107
108 # use gpg to verify the artifact, use a backup key server in case the first one is down for some reason
109 gpg --keyserver hkps.pool.sks-keyservers.net --recv-key FC8A365E || gpg --keyserver pgpkeys.mit.edu --recv-key FC8A365E
110 gpg --verify $DEST/etcd/$ETCD_DOWNLOAD_FILE.asc $DEST/etcd/$ETCD_DOWNLOAD_FILE
111
112 tar xzvf $DEST/etcd/$ETCD_DOWNLOAD_FILE -C $DEST/etcd
113 sudo cp $DEST/etcd/etcd-$ETCD_VERSION-linux-$ETCD_ARCH/etcd $ETCD_BIN_DIR/etcd
114 fi
115 if [ ! -f "$ETCD_BIN_DIR/etcd" ]; then
116 sudo cp $DEST/etcd/etcd-$ETCD_VERSION-linux-$ETCD_ARCH/etcd $ETCD_BIN_DIR/etcd
117 fi
118}
119
120# Restore xtrace
121$_XTRACE_ETCD3
122
123# Tell emacs to use shell-script-mode
124## Local variables:
125## mode: shell-script
126## End: