summaryrefslogtreecommitdiff
path: root/lib/etcd3
blob: bc24790782d6b9d492f91e797e40f26be7fb06e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/bin/bash
#
# lib/etcd3
#
# Functions to control the installation and configuration of etcd 3.x
# that provides a key-value store (and possibly other functions).

# Dependencies:
#
# - ``functions`` file

# ``stack.sh`` calls the entry points in this order:
#
# - start_etcd3
# - stop_etcd3
# - cleanup_etcd3

# Save trace setting
_XTRACE_ETCD3=$(set +o | grep xtrace)
set +o xtrace


# Defaults
# --------

# Set up default values for etcd
ETCD_DOWNLOAD_URL=${ETCD_DOWNLOAD_URL:-https://github.com/coreos/etcd/releases/download}
ETCD_VERSION=${ETCD_VERSION:-v3.1.7}
ETCD_DATA_DIR="$DEST/data/etcd"
ETCD_SYSTEMD_SERVICE="devstack@etcd.service"
ETCD_BIN_DIR="$DEST/bin"
ETCD_SHA256_AMD64="4fde194bbcd259401e2b5c462dfa579ee7f6af539f13f130b8f5b4f52e3b3c52"
# NOTE(sdague): etcd v3.1.7 doesn't have anything for these architectures, though 3.2.0 does.
ETCD_SHA256_ARM64=""
ETCD_SHA256_PPC64=""
ETCD_PORT=2379

if is_ubuntu ; then
    UBUNTU_RELEASE_BASE_NUM=`lsb_release -r | awk '{print $2}' | cut -d '.' -f 1`
fi

# start_etcd3() - Starts to run the etcd process
function start_etcd3 {
    local cmd="$ETCD_BIN_DIR/etcd"
    cmd+=" --name $HOSTNAME --data-dir $ETCD_DATA_DIR"
    cmd+=" --initial-cluster-state new --initial-cluster-token etcd-cluster-01"
    cmd+=" --initial-cluster $HOSTNAME=http://$SERVICE_HOST:2380"
    cmd+=" --initial-advertise-peer-urls http://$SERVICE_HOST:2380"
    cmd+=" --advertise-client-urls http://${HOST_IP}:$ETCD_PORT"
    cmd+=" --listen-peer-urls http://0.0.0.0:2380 "
    cmd+=" --listen-client-urls http://${HOST_IP}:$ETCD_PORT"

    local unitfile="$SYSTEMD_DIR/$ETCD_SYSTEMD_SERVICE"
    write_user_unit_file $ETCD_SYSTEMD_SERVICE "$cmd" "" "root"

    iniset -sudo $unitfile "Unit" "After" "network.target"
    iniset -sudo $unitfile "Service" "Type" "notify"
    iniset -sudo $unitfile "Service" "Restart" "on-failure"
    iniset -sudo $unitfile "Service" "LimitNOFILE" "65536"
    if is_arch "aarch64"; then
        iniset -sudo $unitfile "Service" "Environment" "ETCD_UNSUPPORTED_ARCH=arm64"
    fi

    $SYSTEMCTL daemon-reload
    $SYSTEMCTL enable $ETCD_SYSTEMD_SERVICE
    $SYSTEMCTL start $ETCD_SYSTEMD_SERVICE
}

# stop_etcd3() stops the etcd3 process
function stop_etcd3 {
    # Don't install in sub nodes (multinode scenario)
    if [ "$SERVICE_HOST" != "$HOST_IP" ]; then
        return
    fi

    $SYSTEMCTL stop $ETCD_SYSTEMD_SERVICE
}

function cleanup_etcd3 {
    # Don't install in sub nodes (multinode scenario)
    if [ "$SERVICE_HOST" != "$HOST_IP" ]; then
        return
    fi

    $SYSTEMCTL disable $ETCD_SYSTEMD_SERVICE

    local unitfile="$SYSTEMD_DIR/$ETCD_SYSTEMD_SERVICE"
    sudo rm -f $unitfile

    $SYSTEMCTL daemon-reload

    sudo rm -rf $ETCD_DATA_DIR
}

function install_etcd3 {
    echo "Installing etcd"

    # Make sure etcd3 downloads the correct architecture
    if is_arch "x86_64"; then
        ETCD_ARCH="amd64"
        ETCD_SHA256=${ETCD_SHA256:-$ETCD_SHA256_AMD64}
    elif is_arch "aarch64"; then
        ETCD_ARCH="arm64"
        ETCD_SHA256=${ETCD_SHA256:-$ETCD_SHA256_ARM64}
    elif is_arch "ppc64le"; then
        ETCD_ARCH="ppc64le"
        ETCD_SHA256=${ETCD_SHA256:-$ETCD_SHA256_PPC64}
    else
        exit_distro_not_supported "invalid hardware type - $ETCD_ARCH"
    fi

    ETCD_NAME=etcd-$ETCD_VERSION-linux-$ETCD_ARCH

    # Create the necessary directories
    sudo mkdir -p $ETCD_BIN_DIR
    sudo mkdir -p $ETCD_DATA_DIR

    # Download and cache the etcd tgz for subsequent use
    if [ ! -f "$FILES/etcd-$ETCD_VERSION-linux-$ETCD_ARCH/etcd" ]; then
        ETCD_DOWNLOAD_FILE=$ETCD_NAME.tar.gz
        if [ ! -f "$FILES/$ETCD_DOWNLOAD_FILE" ]; then
            wget $ETCD_DOWNLOAD_URL/$ETCD_VERSION/$ETCD_DOWNLOAD_FILE -O $FILES/$ETCD_DOWNLOAD_FILE
        fi
        echo "${ETCD_SHA256} $FILES/${ETCD_DOWNLOAD_FILE}" > $FILES/etcd.sha256sum
        # NOTE(sdague): this should go fatal if this fails
        sha256sum -c $FILES/etcd.sha256sum

        tar xzvf $FILES/$ETCD_DOWNLOAD_FILE -C $FILES
        sudo cp $FILES/$ETCD_NAME/etcd $ETCD_BIN_DIR/etcd
    fi
    if [ ! -f "$ETCD_BIN_DIR/etcd" ]; then
        sudo cp $FILES/$ETCD_NAME/etcd $ETCD_BIN_DIR/etcd
    fi
}

# Restore xtrace
$_XTRACE_ETCD3

# Tell emacs to use shell-script-mode
## Local variables:
## mode: shell-script
## End: