Davanum Srinivas | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 1 | #!/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) |
| 20 | set +o xtrace |
| 21 | |
| 22 | |
| 23 | # Defaults |
| 24 | # -------- |
| 25 | |
| 26 | # Set up default values for etcd |
| 27 | ETCD_DOWNLOAD_URL=${ETCD_DOWNLOAD_URL:-https://github.com/coreos/etcd/releases/download} |
| 28 | ETCD_VERSION=${ETCD_VERSION:-v3.1.7} |
| 29 | ETCD_DATA_DIR="$DEST/data/etcd" |
| 30 | ETCD_SYSTEMD_SERVICE="devstack@etcd.service" |
| 31 | ETCD_BIN_DIR="$DEST/bin" |
| 32 | |
| 33 | if is_ubuntu ; then |
| 34 | UBUNTU_RELEASE_BASE_NUM=`lsb_release -r | awk '{print $2}' | cut -d '.' -f 1` |
| 35 | fi |
| 36 | |
| 37 | # start_etcd3() - Starts to run the etcd process |
| 38 | function start_etcd3 { |
Davanum Srinivas | d8283fd | 2017-05-23 22:12:39 -0400 | [diff] [blame^] | 39 | # Don't install in sub nodes (multinode scenario) |
| 40 | if [ "$SERVICE_HOST" != "$HOST_IP" ]; then |
| 41 | return |
| 42 | fi |
| 43 | |
Davanum Srinivas | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 44 | _install_etcd |
| 45 | |
| 46 | local cmd="$ETCD_BIN_DIR/etcd" |
| 47 | cmd+=" --name $HOSTNAME --data-dir $ETCD_DATA_DIR" |
| 48 | cmd+=" --initial-cluster-state new --initial-cluster-token etcd-cluster-01" |
| 49 | cmd+=" --initial-cluster $HOSTNAME=http://$SERVICE_HOST:2380" |
| 50 | cmd+=" --initial-advertise-peer-urls http://$SERVICE_HOST:2380" |
| 51 | cmd+=" --advertise-client-urls http://$SERVICE_HOST:2379" |
| 52 | cmd+=" --listen-peer-urls http://0.0.0.0:2380 " |
| 53 | cmd+=" --listen-client-urls http://$SERVICE_HOST:2379" |
| 54 | |
| 55 | local unitfile="$SYSTEMD_DIR/$ETCD_SYSTEMD_SERVICE" |
| 56 | write_user_unit_file $ETCD_SYSTEMD_SERVICE "$cmd" "" "root" |
| 57 | |
| 58 | iniset -sudo $unitfile "Unit" "After" "network.target" |
| 59 | iniset -sudo $unitfile "Service" "Type" "notify" |
| 60 | iniset -sudo $unitfile "Service" "Restart" "on-failure" |
| 61 | iniset -sudo $unitfile "Service" "LimitNOFILE" "65536" |
| 62 | |
| 63 | $SYSTEMCTL daemon-reload |
| 64 | $SYSTEMCTL enable $ETCD_SYSTEMD_SERVICE |
| 65 | $SYSTEMCTL start $ETCD_SYSTEMD_SERVICE |
| 66 | } |
| 67 | |
| 68 | # stop_etcd3() stops the etcd3 process |
| 69 | function stop_etcd3 { |
Davanum Srinivas | d8283fd | 2017-05-23 22:12:39 -0400 | [diff] [blame^] | 70 | # Don't install in sub nodes (multinode scenario) |
| 71 | if [ "$SERVICE_HOST" != "$HOST_IP" ]; then |
| 72 | return |
| 73 | fi |
| 74 | |
Davanum Srinivas | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 75 | $SYSTEMCTL stop $ETCD_SYSTEMD_SERVICE |
| 76 | } |
| 77 | |
| 78 | function cleanup_etcd { |
Davanum Srinivas | d8283fd | 2017-05-23 22:12:39 -0400 | [diff] [blame^] | 79 | # Don't install in sub nodes (multinode scenario) |
| 80 | if [ "$SERVICE_HOST" != "$HOST_IP" ]; then |
| 81 | return |
| 82 | fi |
| 83 | |
Davanum Srinivas | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 84 | $SYSTEMCTL disable $ETCD_SYSTEMD_SERVICE |
| 85 | |
| 86 | local unitfile="$SYSTEMD_DIR/$ETCD_SYSTEMD_SERVICE" |
| 87 | sudo rm -f $unitfile |
| 88 | |
| 89 | $SYSTEMCTL daemon-reload |
| 90 | |
| 91 | sudo rm -rf $ETCD_DATA_DIR |
| 92 | } |
| 93 | |
| 94 | function _install_etcd { |
| 95 | echo "Installing etcd" |
| 96 | |
| 97 | # Make sure etcd3 downloads the correct architecture |
| 98 | if is_arch "x86_64"; then |
| 99 | ETCD_ARCH="amd64" |
| 100 | elif is_arch "aarch64"; then |
| 101 | ETCD_ARCH="arm64" |
| 102 | elif is_arch "ppc64le"; then |
| 103 | ETCD_ARCH="ppc64le" |
| 104 | else |
| 105 | exit_distro_not_supported "invalid hardware type - $ETCD_ARCH" |
| 106 | fi |
| 107 | |
| 108 | # Install the libraries needed. Note: tooz for example does not have a hard dependency on these libraries |
| 109 | pip_install etcd3 |
| 110 | pip_install etcd3gw |
| 111 | |
| 112 | # Create the necessary directories |
| 113 | sudo mkdir -p $ETCD_BIN_DIR |
| 114 | sudo mkdir -p $ETCD_DATA_DIR |
| 115 | |
| 116 | # Download and cache the etcd tgz for subsequent use |
| 117 | if [ ! -f "$DEST/etcd/etcd-$ETCD_VERSION-linux-$ETCD_ARCH/etcd" ]; then |
| 118 | mkdir -p $DEST/etcd |
| 119 | ETCD_DOWNLOAD_FILE=etcd-$ETCD_VERSION-linux-$ETCD_ARCH.tar.gz |
| 120 | wget $ETCD_DOWNLOAD_URL/$ETCD_VERSION/$ETCD_DOWNLOAD_FILE -O $DEST/etcd/$ETCD_DOWNLOAD_FILE |
| 121 | wget $ETCD_DOWNLOAD_URL/$ETCD_VERSION/$ETCD_DOWNLOAD_FILE.asc -O $DEST/etcd/$ETCD_DOWNLOAD_FILE.asc |
| 122 | |
| 123 | # use gpg to verify the artifact, use a backup key server in case the first one is down for some reason |
| 124 | gpg --keyserver hkps.pool.sks-keyservers.net --recv-key FC8A365E || gpg --keyserver pgpkeys.mit.edu --recv-key FC8A365E |
| 125 | gpg --verify $DEST/etcd/$ETCD_DOWNLOAD_FILE.asc $DEST/etcd/$ETCD_DOWNLOAD_FILE |
| 126 | |
| 127 | tar xzvf $DEST/etcd/$ETCD_DOWNLOAD_FILE -C $DEST/etcd |
| 128 | sudo cp $DEST/etcd/etcd-$ETCD_VERSION-linux-$ETCD_ARCH/etcd $ETCD_BIN_DIR/etcd |
| 129 | fi |
| 130 | if [ ! -f "$ETCD_BIN_DIR/etcd" ]; then |
| 131 | sudo cp $DEST/etcd/etcd-$ETCD_VERSION-linux-$ETCD_ARCH/etcd $ETCD_BIN_DIR/etcd |
| 132 | fi |
| 133 | } |
| 134 | |
| 135 | # Restore xtrace |
| 136 | $_XTRACE_ETCD3 |
| 137 | |
| 138 | # Tell emacs to use shell-script-mode |
| 139 | ## Local variables: |
| 140 | ## mode: shell-script |
| 141 | ## End: |