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" |
Sean Dague | bba9241 | 2017-05-24 07:56:10 -0400 | [diff] [blame] | 32 | ETCD_SHA256_AMD64="4fde194bbcd259401e2b5c462dfa579ee7f6af539f13f130b8f5b4f52e3b3c52" |
| 33 | # NOTE(sdague): etcd v3.1.7 doesn't have anything for these architectures, though 3.2.0 does. |
| 34 | ETCD_SHA256_ARM64="" |
| 35 | ETCD_SHA256_PPC64="" |
Davanum Srinivas | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 36 | |
| 37 | if is_ubuntu ; then |
| 38 | UBUNTU_RELEASE_BASE_NUM=`lsb_release -r | awk '{print $2}' | cut -d '.' -f 1` |
| 39 | fi |
| 40 | |
| 41 | # start_etcd3() - Starts to run the etcd process |
| 42 | function start_etcd3 { |
Davanum Srinivas | d8283fd | 2017-05-23 22:12:39 -0400 | [diff] [blame] | 43 | # Don't install in sub nodes (multinode scenario) |
| 44 | if [ "$SERVICE_HOST" != "$HOST_IP" ]; then |
| 45 | return |
| 46 | fi |
| 47 | |
Davanum Srinivas | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 48 | _install_etcd |
| 49 | |
| 50 | local cmd="$ETCD_BIN_DIR/etcd" |
| 51 | cmd+=" --name $HOSTNAME --data-dir $ETCD_DATA_DIR" |
| 52 | cmd+=" --initial-cluster-state new --initial-cluster-token etcd-cluster-01" |
| 53 | cmd+=" --initial-cluster $HOSTNAME=http://$SERVICE_HOST:2380" |
| 54 | cmd+=" --initial-advertise-peer-urls http://$SERVICE_HOST:2380" |
| 55 | cmd+=" --advertise-client-urls http://$SERVICE_HOST:2379" |
| 56 | cmd+=" --listen-peer-urls http://0.0.0.0:2380 " |
| 57 | cmd+=" --listen-client-urls http://$SERVICE_HOST:2379" |
| 58 | |
| 59 | local unitfile="$SYSTEMD_DIR/$ETCD_SYSTEMD_SERVICE" |
| 60 | write_user_unit_file $ETCD_SYSTEMD_SERVICE "$cmd" "" "root" |
| 61 | |
| 62 | iniset -sudo $unitfile "Unit" "After" "network.target" |
| 63 | iniset -sudo $unitfile "Service" "Type" "notify" |
| 64 | iniset -sudo $unitfile "Service" "Restart" "on-failure" |
| 65 | iniset -sudo $unitfile "Service" "LimitNOFILE" "65536" |
| 66 | |
| 67 | $SYSTEMCTL daemon-reload |
| 68 | $SYSTEMCTL enable $ETCD_SYSTEMD_SERVICE |
| 69 | $SYSTEMCTL start $ETCD_SYSTEMD_SERVICE |
| 70 | } |
| 71 | |
| 72 | # stop_etcd3() stops the etcd3 process |
| 73 | function stop_etcd3 { |
Davanum Srinivas | d8283fd | 2017-05-23 22:12:39 -0400 | [diff] [blame] | 74 | # Don't install in sub nodes (multinode scenario) |
| 75 | if [ "$SERVICE_HOST" != "$HOST_IP" ]; then |
| 76 | return |
| 77 | fi |
| 78 | |
Davanum Srinivas | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 79 | $SYSTEMCTL stop $ETCD_SYSTEMD_SERVICE |
| 80 | } |
| 81 | |
Davanum Srinivas | 853b475 | 2017-05-25 13:03:10 -0400 | [diff] [blame] | 82 | function cleanup_etcd3 { |
Davanum Srinivas | d8283fd | 2017-05-23 22:12:39 -0400 | [diff] [blame] | 83 | # Don't install in sub nodes (multinode scenario) |
| 84 | if [ "$SERVICE_HOST" != "$HOST_IP" ]; then |
| 85 | return |
| 86 | fi |
| 87 | |
Davanum Srinivas | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 88 | $SYSTEMCTL disable $ETCD_SYSTEMD_SERVICE |
| 89 | |
| 90 | local unitfile="$SYSTEMD_DIR/$ETCD_SYSTEMD_SERVICE" |
| 91 | sudo rm -f $unitfile |
| 92 | |
| 93 | $SYSTEMCTL daemon-reload |
| 94 | |
| 95 | sudo rm -rf $ETCD_DATA_DIR |
| 96 | } |
| 97 | |
| 98 | function _install_etcd { |
| 99 | echo "Installing etcd" |
| 100 | |
| 101 | # Make sure etcd3 downloads the correct architecture |
| 102 | if is_arch "x86_64"; then |
| 103 | ETCD_ARCH="amd64" |
Sean Dague | bba9241 | 2017-05-24 07:56:10 -0400 | [diff] [blame] | 104 | ETCD_SHA256=${ETCD_SHA256:-$ETCD_SHA256_AMD64} |
Davanum Srinivas | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 105 | elif is_arch "aarch64"; then |
| 106 | ETCD_ARCH="arm64" |
Sean Dague | bba9241 | 2017-05-24 07:56:10 -0400 | [diff] [blame] | 107 | ETCD_SHA256=${ETCD_SHA256:-$ETCD_SHA256_ARM64} |
Davanum Srinivas | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 108 | elif is_arch "ppc64le"; then |
| 109 | ETCD_ARCH="ppc64le" |
Sean Dague | bba9241 | 2017-05-24 07:56:10 -0400 | [diff] [blame] | 110 | ETCD_SHA256=${ETCD_SHA256:-$ETCD_SHA256_PPC64} |
Davanum Srinivas | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 111 | else |
| 112 | exit_distro_not_supported "invalid hardware type - $ETCD_ARCH" |
| 113 | fi |
| 114 | |
Sean Dague | bba9241 | 2017-05-24 07:56:10 -0400 | [diff] [blame] | 115 | ETCD_NAME=etcd-$ETCD_VERSION-linux-$ETCD_ARCH |
| 116 | |
Davanum Srinivas | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 117 | # Install the libraries needed. Note: tooz for example does not have a hard dependency on these libraries |
| 118 | pip_install etcd3 |
| 119 | pip_install etcd3gw |
| 120 | |
| 121 | # Create the necessary directories |
| 122 | sudo mkdir -p $ETCD_BIN_DIR |
| 123 | sudo mkdir -p $ETCD_DATA_DIR |
| 124 | |
| 125 | # Download and cache the etcd tgz for subsequent use |
Rodolfo Alonso Hernandez | 6f962a2 | 2017-05-31 11:00:08 +0100 | [diff] [blame^] | 126 | if [ ! -f "$FILES/etcd-$ETCD_VERSION-linux-$ETCD_ARCH/etcd" ]; then |
Sean Dague | bba9241 | 2017-05-24 07:56:10 -0400 | [diff] [blame] | 127 | ETCD_DOWNLOAD_FILE=$ETCD_NAME.tar.gz |
Rodolfo Alonso Hernandez | 6f962a2 | 2017-05-31 11:00:08 +0100 | [diff] [blame^] | 128 | wget $ETCD_DOWNLOAD_URL/$ETCD_VERSION/$ETCD_DOWNLOAD_FILE -O $FILES/$ETCD_DOWNLOAD_FILE |
| 129 | echo "${ETCD_SHA256} $FILES/${ETCD_DOWNLOAD_FILE}" > $FILES/etcd.sha256sum |
Sean Dague | bba9241 | 2017-05-24 07:56:10 -0400 | [diff] [blame] | 130 | # NOTE(sdague): this should go fatal if this fails |
Rodolfo Alonso Hernandez | 6f962a2 | 2017-05-31 11:00:08 +0100 | [diff] [blame^] | 131 | sha256sum -c $FILES/etcd.sha256sum |
Davanum Srinivas | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 132 | |
Rodolfo Alonso Hernandez | 6f962a2 | 2017-05-31 11:00:08 +0100 | [diff] [blame^] | 133 | tar xzvf $FILES/$ETCD_DOWNLOAD_FILE -C $FILES |
| 134 | sudo cp $FILES/$ETCD_NAME/etcd $ETCD_BIN_DIR/etcd |
Davanum Srinivas | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 135 | fi |
| 136 | if [ ! -f "$ETCD_BIN_DIR/etcd" ]; then |
Rodolfo Alonso Hernandez | 6f962a2 | 2017-05-31 11:00:08 +0100 | [diff] [blame^] | 137 | sudo cp $FILES/$ETCD_NAME/etcd $ETCD_BIN_DIR/etcd |
Davanum Srinivas | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 138 | fi |
| 139 | } |
| 140 | |
| 141 | # Restore xtrace |
| 142 | $_XTRACE_ETCD3 |
| 143 | |
| 144 | # Tell emacs to use shell-script-mode |
| 145 | ## Local variables: |
| 146 | ## mode: shell-script |
| 147 | ## End: |