Skip to content

Commit

Permalink
Merge pull request #12 from malscent/main
Browse files Browse the repository at this point in the history
Added GCP Specific Pre-requisite setup
  • Loading branch information
malscent authored Mar 10, 2021
2 parents cd0155b + 78719ad commit b1b8d1f
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 7 deletions.
2 changes: 1 addition & 1 deletion compose-files/CentosClusterCompose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ services:
networks:
- cb-net-centos
container_name: centos-clusterhost
command: --version 6.6.1 -u couchbase -p foo123! -ch clusterhost -d -os CENTOS -r
command: --version 6.6.1 -u couchbase -p foo123! -ch clusterhost -d -os CENTOS -r -w 3
privileged: true
nodeone:
image: couchbase/centos
Expand Down
2 changes: 1 addition & 1 deletion compose-files/DebianClusterCompose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ services:
networks:
- cb-net-debian
container_name: debian-clusterhost
command: --version 6.6.1 -u couchbase -p foo123! -ch clusterhost -d -os DEBIAN -r
command: --version 6.6.1 -u couchbase -p foo123! -ch clusterhost -d -os DEBIAN -r -w 3
privileged: true
nodeone:
image: couchbase/debian
Expand Down
2 changes: 1 addition & 1 deletion compose-files/RHELClusterCompose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ services:
networks:
- cb-net-redhat
container_name: redhat-clusterhost
command: --version 6.6.1 -u couchbase -p foo123! -ch clusterhost -d -os RHEL -r
command: --version 6.6.1 -u couchbase -p foo123! -ch clusterhost -d -os RHEL -r -w 3
privileged: true
nodeone:
image: couchbase/redhat
Expand Down
2 changes: 1 addition & 1 deletion compose-files/UbuntuClusterCompose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ services:
networks:
- cb-net-ubuntu
container_name: ubuntu-clusterhost
command: --version 6.6.1 -u couchbase -p foo123! -ch clusterhost -d -os UBUNTU -r
command: --version 6.6.1 -u couchbase -p foo123! -ch clusterhost -d -os UBUNTU -r -w 3
privileged: true
nodeone:
image: couchbase/ubuntu
Expand Down
98 changes: 97 additions & 1 deletion installers/installer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,37 @@ function __debian_prerequisites() {
__ubuntu_prerequisites "$1"
}

function __get_gcp_metadata_value() {
wget -O - \
--header="Metadata-Flavor:Google" \
-q \
--retry-connrefused \
--waitretry=1 \
--read-timeout=10 \
--timeout=10 \
-t 5 \
"http://metadata/computeMetadata/v1/$1"
}

function __get_gcp_attribute_value() {
__get_gcp_metadata_value "instance/attributes/$1"
}

#These values are for GCP
export ACCESS_TOKEN=""
export PROJECT_ID=""
export EXTERNAL_IP=""
export CONFIG=""
export EXTERNAL_IP_VAR_PATH=""
export SUCCESS_STATUS_PATH=""
export FAILURE_STATUS_PATH=""
export NODE_PRIVATE_DNS=""
export EXTERNAL_IP_PAYLOAD=""

function __install_prerequisites() {
local os=$1
local sync_gateway=$2
local env=$3
__check_os_version "$os"
__log_debug "Prequisites Installation"
if [[ "$os" == "CENTOS" ]]; then
Expand All @@ -92,8 +120,48 @@ function __install_prerequisites() {
else
__ubuntu_prerequisites "$sync_gateway"
fi

#There are some "startup" functions that need run for GCP script
if [[ "$env" == "GCP" ]]; then
echo "Running GCP Prequisites"
ACCESS_TOKEN=$(__get_gcp_metadata_value "instance/service-accounts/default/token" | jq -r '.access_token')
__log_debug "GCP Access Token: $ACCESS_TOKEN"
PROJECT_ID=$(__get_gcp_metadata_value "project/project-id")
__log_debug "GCP Project Id: $PROJECT_ID"
EXTERNAL_IP=$(__get_gcp_metadata_value "instance/network-interfaces/0/access-configs/0/external-ip")
__log_debug "GCP External IP: $EXTERNAL_IP"
CONFIG=$(__get_gcp_attribute_value "runtime-config-name")
__log_debug "GCP Config: $CONFIG"
EXTERNAL_IP_VAR_PATH=$(__get_gcp_attribute_value "external-ip-variable-path")
__log_debug "GCP External Ip Var Path: $EXTERNAL_IP_VAR_PATH"
SUCCESS_STATUS_PATH="$(__get_gcp_attribute_value "status-success-base-path")/$(hostname)"
__log_debug "GCP Success Status Path: $SUCCESS_STATUS_PATH"
FAILURE_STATUS_PATH="$(__get_gcp_attribute_value "status-failure-base-path")/$(hostname)"
__log_debug "GCP Failure Status Path: $FAILURE_STATUS_PATH"
NODE_PRIVATE_DNS=$(__get_gcp_metadata_value "instance/hostname")
__log_debug "GCP Node Private DNS: $NODE_PRIVATE_DNS"
EXTERNAL_IP_PAYLOAD="$(printf '{"name": "%s", "text": "%s"}' \
"projects/${PROJECT_ID}/configs/${CONFIG}/variables/${EXTERNAL_IP_VAR_PATH}" \
"${EXTERNAL_IP}")"

wget -O - \
-q \
--retry-connrefused \
--waitretry=1 \
--read-timeout=10 \
--timeout=10 \
-t 5 \
--header="Authorization: Bearer ${ACCESS_TOKEN}" \
--header "Content-Type: application/json" \
--header "X-GFE-SSL: yes" \
--method=PUT \
--body-data="$EXTERNAL_IP_PAYLOAD" \
"https://runtimeconfig.googleapis.com/v1beta1/projects/${PROJECT_ID}/configs/variables/${EXTERNAL_IP_VAR_PATH}"
fi
__log_debug "Prequisites Complete"
}


# https://docs.couchbase.com/server/current/install/thp-disable.html
turnOffTransparentHugepages ()
{
Expand Down Expand Up @@ -350,7 +418,7 @@ function __install_couchbase_centos() {
local tmp=$2
__log_info "Installing Couchbase Server v${version}..."
__log_debug "Downloading installer to: ${tmp}"
curl --output "${tmp}/couchbase-release-1.0-x86_64.rpm" https://packages.couchbase.com/releases/couchbase-release/couchbase-release-1.0-x86_64.rpm
wget -O "${tmp}/couchbase-release-1.0-x86_64.rpm" https://packages.couchbase.com/releases/couchbase-release/couchbase-release-1.0-x86_64.rpm -q
__log_debug "Download Complete. Beginning Unpacking"
rpm -i "${tmp}/couchbase-release-1.0-x86_64.rpm"
__log_debug "Unpacking complete. Beginning Installation"
Expand Down Expand Up @@ -457,4 +525,32 @@ function __install_couchbase() {

export CLI_INSTALL_LOCATION="/opt/couchbase/bin"

}
# This is a method to perform any final actions after the cluster has been created and/or joined
# Precipitated because GCP requires us to send a "Success" after we're done doing our work
function __post_install_finalization() {
__log_debug "Beginning Post Install Finalization"
local env=$1
if [[ "$env" == "GCP" ]]; then
SUCCESS_PAYLOAD="$(printf '{"name": "%s", "text": "%s"}' \
"projects/${PROJECT_ID}/configs/${CONFIG}/variables/${SUCCESS_STATUS_PATH}" \
"success")"

__log_debug "Sending success notification for startup waiter on GCP"

# Notify waiter
wget -O - \
-q \
--retry-connrefused \
--waitretry=1 \
--read-timeout=10 \
--timeout=10 \
-t 5 \
--body-data="${SUCCESS_PAYLOAD}" \
--header="Authorization: Bearer ${ACCESS_TOKEN}" \
--header "Content-Type: application/json" \
--header "X-GFE-SSL: yes" \
--method=POST
"https://runtimeconfig.googleapis.com/v1beta1/projects/${PROJECT_ID}/configs/${CONFIG}/variables"
fi
}
36 changes: 35 additions & 1 deletion main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ CB_PASSWORD=$DEFAULT_PASSWORD
DAEMON=0
STARTUP=0
SYNC_GATEWAY=0
WAIT=0

# Here we're setting up a handler for unexpected errors during operations
function handle_exit() {
Expand Down Expand Up @@ -89,7 +90,7 @@ case $key in
fi; #past argment
;;
-u|--user)
if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
CB_USERNAME=$2
shift 2
else
Expand Down Expand Up @@ -140,6 +141,15 @@ case $key in
exit 1
fi;
;;
-w|--wait-nodes)
if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
WAIT=$2
shift 2
else
__log_error "Error: Argument for $1 is missing" >&2
exit 1
fi; #past argment
;;
-h|--help)
HELP=1
shift # past argument
Expand Down Expand Up @@ -306,8 +316,32 @@ then
sleep 10
done
fi
__log_debug "Waiting for $WAIT nodes"

if [[ "$WAIT" -ne "0" && "$SYNC_GATEWAY" -ne "1" ]]; then
__log_info "Beginning wait for cluster to have $WAIT nodes"
healthy=0
checks=0

until [[ "$healthy" -eq "$WAIT" ]] || [[ "$checks" -ge "50" ]]; do
__log_debug "Healthy node check - $healthy/$WAIT"
healthy=$(wget -O - \
--user "$CB_USERNAME" \
--password "$CB_PASSWORD" \
http://localhost:8091/pools/nodes \
-q \
| jq '[.nodes[] | select(.status == "healthy")] | length')
(( checks += 1 ))
sleep 3
done
__log_info "All nodes are healty - $healthy/$WAIT"
fi

__post_install_finalization "$ENV"
__log_info "Installation of Couchbase v${VERSION} is complete."



if [[ "$DAEMON" == 1 ]]; then
__log_info "Going into daemon mode. Will continue execution until cancelled."
sleep infinity
Expand Down
12 changes: 11 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ Bundling:

**Purpose**: Specifies the install script to run in a mode that is intended to run on every boot of the server

### ```-w|--wait-nodes```

**Usage**: ```./main.sh -w 5

**Purpose**: Specifies that the script should not report success until it sees N nodes in the cluster. This is used for GCP notifications on completion

### ```-h|--help```

**Usage**: ```./main.sh -```
Expand Down Expand Up @@ -132,4 +138,8 @@ To take down the cluster:

```
docker-compose -f ./compose-files/<OS SPECIFIC FILE>.yml down
```
```

## Development Considerations

* Use Wget not Curl

0 comments on commit b1b8d1f

Please sign in to comment.