| 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 | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 44 | local cmd="$ETCD_BIN_DIR/etcd" | 
|  | 45 | cmd+=" --name $HOSTNAME --data-dir $ETCD_DATA_DIR" | 
|  | 46 | cmd+=" --initial-cluster-state new --initial-cluster-token etcd-cluster-01" | 
|  | 47 | cmd+=" --initial-cluster $HOSTNAME=http://$SERVICE_HOST:2380" | 
|  | 48 | cmd+=" --initial-advertise-peer-urls http://$SERVICE_HOST:2380" | 
| Antoni Segura Puimedon | 19279b0 | 2017-06-16 16:03:32 +0200 | [diff] [blame] | 49 | cmd+=" --advertise-client-urls http://${HOST_IP}:$ETCD_PORT" | 
| Davanum Srinivas | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 50 | cmd+=" --listen-peer-urls http://0.0.0.0:2380 " | 
| Antoni Segura Puimedon | 19279b0 | 2017-06-16 16:03:32 +0200 | [diff] [blame] | 51 | cmd+=" --listen-client-urls http://${HOST_IP}:$ETCD_PORT" | 
| Davanum Srinivas | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 52 |  | 
|  | 53 | local unitfile="$SYSTEMD_DIR/$ETCD_SYSTEMD_SERVICE" | 
|  | 54 | write_user_unit_file $ETCD_SYSTEMD_SERVICE "$cmd" "" "root" | 
|  | 55 |  | 
|  | 56 | iniset -sudo $unitfile "Unit" "After" "network.target" | 
|  | 57 | iniset -sudo $unitfile "Service" "Type" "notify" | 
|  | 58 | iniset -sudo $unitfile "Service" "Restart" "on-failure" | 
|  | 59 | iniset -sudo $unitfile "Service" "LimitNOFILE" "65536" | 
|  | 60 |  | 
|  | 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 | 
|  | 67 | function stop_etcd3 { | 
| Davanum Srinivas | d8283fd | 2017-05-23 22:12:39 -0400 | [diff] [blame] | 68 | # Don't install in sub nodes (multinode scenario) | 
|  | 69 | if [ "$SERVICE_HOST" != "$HOST_IP" ]; then | 
|  | 70 | return | 
|  | 71 | fi | 
|  | 72 |  | 
| Davanum Srinivas | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 73 | $SYSTEMCTL stop $ETCD_SYSTEMD_SERVICE | 
|  | 74 | } | 
|  | 75 |  | 
| Davanum Srinivas | 853b475 | 2017-05-25 13:03:10 -0400 | [diff] [blame] | 76 | function cleanup_etcd3 { | 
| Davanum Srinivas | d8283fd | 2017-05-23 22:12:39 -0400 | [diff] [blame] | 77 | # Don't install in sub nodes (multinode scenario) | 
|  | 78 | if [ "$SERVICE_HOST" != "$HOST_IP" ]; then | 
|  | 79 | return | 
|  | 80 | fi | 
|  | 81 |  | 
| Davanum Srinivas | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 82 | $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 Dague | 62b5660 | 2017-06-19 08:27:16 -0400 | [diff] [blame] | 92 | function install_etcd3 { | 
| Davanum Srinivas | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 93 | echo "Installing etcd" | 
|  | 94 |  | 
|  | 95 | # Make sure etcd3 downloads the correct architecture | 
|  | 96 | if is_arch "x86_64"; then | 
|  | 97 | ETCD_ARCH="amd64" | 
| Sean Dague | bba9241 | 2017-05-24 07:56:10 -0400 | [diff] [blame] | 98 | ETCD_SHA256=${ETCD_SHA256:-$ETCD_SHA256_AMD64} | 
| Davanum Srinivas | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 99 | elif is_arch "aarch64"; then | 
|  | 100 | ETCD_ARCH="arm64" | 
| Sean Dague | bba9241 | 2017-05-24 07:56:10 -0400 | [diff] [blame] | 101 | ETCD_SHA256=${ETCD_SHA256:-$ETCD_SHA256_ARM64} | 
| Davanum Srinivas | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 102 | elif is_arch "ppc64le"; then | 
|  | 103 | ETCD_ARCH="ppc64le" | 
| Sean Dague | bba9241 | 2017-05-24 07:56:10 -0400 | [diff] [blame] | 104 | ETCD_SHA256=${ETCD_SHA256:-$ETCD_SHA256_PPC64} | 
| Davanum Srinivas | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 105 | else | 
|  | 106 | exit_distro_not_supported "invalid hardware type - $ETCD_ARCH" | 
|  | 107 | fi | 
|  | 108 |  | 
| Sean Dague | bba9241 | 2017-05-24 07:56:10 -0400 | [diff] [blame] | 109 | ETCD_NAME=etcd-$ETCD_VERSION-linux-$ETCD_ARCH | 
|  | 110 |  | 
| Davanum Srinivas | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 111 | # Create the necessary directories | 
|  | 112 | sudo mkdir -p $ETCD_BIN_DIR | 
|  | 113 | sudo mkdir -p $ETCD_DATA_DIR | 
|  | 114 |  | 
|  | 115 | # Download and cache the etcd tgz for subsequent use | 
| Rodolfo Alonso Hernandez | 6f962a2 | 2017-05-31 11:00:08 +0100 | [diff] [blame] | 116 | if [ ! -f "$FILES/etcd-$ETCD_VERSION-linux-$ETCD_ARCH/etcd" ]; then | 
| Sean Dague | bba9241 | 2017-05-24 07:56:10 -0400 | [diff] [blame] | 117 | ETCD_DOWNLOAD_FILE=$ETCD_NAME.tar.gz | 
| TommyLike | 599ecfb | 2017-06-20 11:32:25 +0800 | [diff] [blame] | 118 | if [ ! -f "$FILES/$ETCD_DOWNLOAD_FILE" ]; then | 
|  | 119 | wget $ETCD_DOWNLOAD_URL/$ETCD_VERSION/$ETCD_DOWNLOAD_FILE -O $FILES/$ETCD_DOWNLOAD_FILE | 
|  | 120 | fi | 
| Rodolfo Alonso Hernandez | 6f962a2 | 2017-05-31 11:00:08 +0100 | [diff] [blame] | 121 | echo "${ETCD_SHA256} $FILES/${ETCD_DOWNLOAD_FILE}" > $FILES/etcd.sha256sum | 
| Sean Dague | bba9241 | 2017-05-24 07:56:10 -0400 | [diff] [blame] | 122 | # NOTE(sdague): this should go fatal if this fails | 
| Rodolfo Alonso Hernandez | 6f962a2 | 2017-05-31 11:00:08 +0100 | [diff] [blame] | 123 | sha256sum -c $FILES/etcd.sha256sum | 
| Davanum Srinivas | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 124 |  | 
| Rodolfo Alonso Hernandez | 6f962a2 | 2017-05-31 11:00:08 +0100 | [diff] [blame] | 125 | tar xzvf $FILES/$ETCD_DOWNLOAD_FILE -C $FILES | 
|  | 126 | sudo cp $FILES/$ETCD_NAME/etcd $ETCD_BIN_DIR/etcd | 
| Davanum Srinivas | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 127 | fi | 
|  | 128 | if [ ! -f "$ETCD_BIN_DIR/etcd" ]; then | 
| Rodolfo Alonso Hernandez | 6f962a2 | 2017-05-31 11:00:08 +0100 | [diff] [blame] | 129 | sudo cp $FILES/$ETCD_NAME/etcd $ETCD_BIN_DIR/etcd | 
| Davanum Srinivas | 546656f | 2017-03-14 07:05:19 -0400 | [diff] [blame] | 130 | fi | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | # Restore xtrace | 
|  | 134 | $_XTRACE_ETCD3 | 
|  | 135 |  | 
|  | 136 | # Tell emacs to use shell-script-mode | 
|  | 137 | ## Local variables: | 
|  | 138 | ## mode: shell-script | 
|  | 139 | ## End: |