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