Skip to content
This repository has been archived by the owner on Jan 25, 2023. It is now read-only.

Commit

Permalink
Merge pull request #97 from hashicorp/quick-improv
Browse files Browse the repository at this point in the history
Fix: Retry downloading consul on install script
  • Loading branch information
Etiene authored Nov 14, 2018
2 parents 52bd5df + d149f1c commit 5087f97
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 95 deletions.
2 changes: 1 addition & 1 deletion modules/install-consul/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ example](https://github.com/hashicorp/terraform-aws-consul/tree/master/examples/
The `install-consul` script accepts the following arguments:

* `version VERSION`: Install Consul version VERSION. Optional if download-url is provided.
* `dowload-url URL`: Install the Consul package hosted in this url. Optional if version is provided.
* `download-url URL`: Install the Consul package hosted in this url. Optional if version is provided.
* `path DIR`: Install Consul into folder DIR. Optional.
* `user USER`: The install dirs will be owned by user USER. Optional.
* `ca-file-path PATH`: Path to a PEM-encoded certificate authority used to encrypt and verify authenticity of client and server connections. Optional.
Expand Down
91 changes: 58 additions & 33 deletions modules/install-consul/install-consul
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,30 @@ function print_usage {
}

function log {
local readonly level="$1"
local readonly message="$2"
local readonly timestamp=$(date +"%Y-%m-%d %H:%M:%S")
local -r level="$1"
local -r message="$2"
local -r timestamp=$(date +"%Y-%m-%d %H:%M:%S")
>&2 echo -e "${timestamp} [${level}] [$SCRIPT_NAME] ${message}"
}

function log_info {
local readonly message="$1"
local -r message="$1"
log "INFO" "$message"
}

function log_warn {
local readonly message="$1"
local -r message="$1"
log "WARN" "$message"
}

function log_error {
local readonly message="$1"
local -r message="$1"
log "ERROR" "$message"
}

function assert_not_empty {
local readonly arg_name="$1"
local readonly arg_value="$2"
local -r arg_name="$1"
local -r arg_value="$2"

if [[ -z "$arg_value" ]]; then
log_error "The value for '$arg_name' cannot be empty"
Expand All @@ -74,10 +74,10 @@ function assert_not_empty {
}

function assert_either_or {
local readonly arg1_name="$1"
local readonly arg1_value="$2"
local readonly arg2_name="$3"
local readonly arg2_value="$4"
local -r arg1_name="$1"
local -r arg1_value="$2"
local -r arg2_name="$3"
local -r arg2_value="$4"

if [[ -z "$arg1_value" && -z "$arg2_value" ]]; then
log_error "Either the value for '$arg1_name' or '$arg2_name' must be passed, both cannot be empty"
Expand All @@ -86,6 +86,30 @@ function assert_either_or {
fi
}

# A retry function that attempts to run a command a number of times and returns the output
function retry {
local -r cmd="$1"
local -r description="$2"

for i in $(seq 1 5); do
log_info "$description"

# The boolean operations with the exit status are there to temporarily circumvent the "set -e" at the
# beginning of this script which exits the script immediatelly for error status while not losing the exit status code
output=$(eval "$cmd") && exit_status=0 || exit_status=$?
log_info "$output"
if [[ $exit_status -eq 0 ]]; then
echo "$output"
return
fi
log_warn "$description failed. Will sleep for 10 seconds and try again."
sleep 10
done;

log_error "$description failed after 5 attempts."
exit $exit_status
}

# Install steps are based on: http://unix.stackexchange.com/a/291098/215969
function install_supervisord_debian {
sudo apt-get install -y supervisor
Expand Down Expand Up @@ -166,12 +190,12 @@ function install_dependencies {
}

function user_exists {
local readonly username="$1"
local -r username="$1"
id "$username" >/dev/null 2>&1
}

function create_consul_user {
local readonly username="$1"
local -r username="$1"

if $(user_exists "$username"); then
echo "User $username already exists. Will not create again."
Expand All @@ -182,8 +206,8 @@ function create_consul_user {
}

function create_consul_install_paths {
local readonly path="$1"
local readonly username="$2"
local -r path="$1"
local -r username="$2"

log_info "Creating install dirs for Consul at $path"
sudo mkdir -p "$path"
Expand All @@ -198,24 +222,25 @@ function create_consul_install_paths {
}

function fetch_binary {
local readonly version="$1"
local -r version="$1"
local download_url="$2"

if [[ -z "$download_url" && -n "$version" ]]; then
download_url="https://releases.hashicorp.com/consul/${version}/consul_${version}_linux_amd64.zip"
fi

log_info "Downloading Consul to $DOWNLOAD_PACKAGE_PATH"
curl -o "$DOWNLOAD_PACKAGE_PATH" "$download_url" --location --silent --fail --show-error
retry \
"curl -o '$DOWNLOAD_PACKAGE_PATH' '$download_url' --location --silent --fail --show-error" \
"Downloading Consul to $DOWNLOAD_PACKAGE_PATH"
}

function install_binary {
local readonly install_path="$1"
local readonly username="$2"
local -r install_path="$1"
local -r username="$2"

local readonly bin_dir="$install_path/bin"
local readonly consul_dest_path="$bin_dir/consul"
local readonly run_consul_dest_path="$bin_dir/run-consul"
local -r bin_dir="$install_path/bin"
local -r consul_dest_path="$bin_dir/consul"
local -r run_consul_dest_path="$bin_dir/run-consul"

unzip -d /tmp "$DOWNLOAD_PACKAGE_PATH"

Expand All @@ -224,7 +249,7 @@ function install_binary {
sudo chown "$username:$username" "$consul_dest_path"
sudo chmod a+x "$consul_dest_path"

local readonly symlink_path="$SYSTEM_BIN_DIR/consul"
local -r symlink_path="$SYSTEM_BIN_DIR/consul"
if [[ -f "$symlink_path" ]]; then
log_info "Symlink $symlink_path already exists. Will not add again."
else
Expand All @@ -239,14 +264,14 @@ function install_binary {
}

function install_tls_certificates {
local readonly path="$1"
local readonly user="$2"
local readonly ca_file_path="$3"
local readonly cert_file_path="$4"
local readonly key_file_path="$5"

local readonly consul_tls_certs_path="$path/tls"
local readonly ca_certs_path="$consul_tls_certs_path/ca"
local -r path="$1"
local -r user="$2"
local -r ca_file_path="$3"
local -r cert_file_path="$4"
local -r key_file_path="$5"

local -r consul_tls_certs_path="$path/tls"
local -r ca_certs_path="$consul_tls_certs_path/ca"

log_info "Moving TLS certs to $consul_tls_certs_path and $ca_certs_path"

Expand Down
24 changes: 12 additions & 12 deletions modules/install-dnsmasq/install-dnsmasq
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,30 @@ function print_usage {
}

function log {
local readonly level="$1"
local readonly message="$2"
local readonly timestamp=$(date +"%Y-%m-%d %H:%M:%S")
local -r level="$1"
local -r message="$2"
local -r timestamp=$(date +"%Y-%m-%d %H:%M:%S")
>&2 echo -e "${timestamp} [${level}] [$SCRIPT_NAME] ${message}"
}

function log_info {
local readonly message="$1"
local -r message="$1"
log "INFO" "$message"
}

function log_warn {
local readonly message="$1"
local -r message="$1"
log "WARN" "$message"
}

function log_error {
local readonly message="$1"
local -r message="$1"
log "ERROR" "$message"
}

function assert_not_empty {
local readonly arg_name="$1"
local readonly arg_value="$2"
local -r arg_name="$1"
local -r arg_value="$2"

if [[ -z "$arg_value" ]]; then
log_error "The value for '$arg_name' cannot be empty"
Expand All @@ -75,7 +75,7 @@ function has_apt_get {
}

function install_dnsmasq {
local readonly consul_ip="$1"
local -r consul_ip="$1"

log_info "Installing Dnsmasq"

Expand All @@ -95,9 +95,9 @@ function install_dnsmasq {
}

function write_consul_config {
local readonly consul_domain="$1"
local readonly consul_ip="$2"
local readonly consul_port="$3"
local -r consul_domain="$1"
local -r consul_ip="$2"
local -r consul_port="$3"

log_info "Configuring Dnsmasq to forward lookups of the '$consul_domain' domain to $consul_ip:$consul_port in $CONSUL_DNS_MASQ_CONFIG_FILE"
mkdir -p "$DNS_MASQ_CONFIG_DIR"
Expand Down
Loading

0 comments on commit 5087f97

Please sign in to comment.