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 #50 from YouSeeU/custom-datacenter
Browse files Browse the repository at this point in the history
Specify custom datacenter in run-consul script
  • Loading branch information
brikis98 authored Mar 2, 2018
2 parents 2e2fbcf + 29707d1 commit 0e5a7b1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
2 changes: 2 additions & 0 deletions modules/run-consul/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ The `run-consul` script accepts the following arguments:
in `--cluster-tag-value`.
* `cluster-tag-value` (optional): Automatically form a cluster with Instances that have the tag key in
`--cluster-tag-key` and this tag value.
* `datacenter` (optional): The name of the datacenter the cluster reports. Default is the AWS region name.
* `config-dir` (optional): The path to the Consul config folder. Default is to take the absolute path of `../config`,
relative to the `run-consul` script itself.
* `data-dir` (optional): The path to the Consul config folder. Default is to take the absolute path of `../data`,
Expand Down Expand Up @@ -107,6 +108,7 @@ available.

* [datacenter](https://www.consul.io/docs/agent/options.html#datacenter): Set to the current AWS region (e.g.
`us-east-1`), as fetched from [Metadata](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html).
If the `--datacenter` flag is provided, then that value is used instead.

* [node_name](https://www.consul.io/docs/agent/options.html#node_name): Set to the instance id, as fetched from
[Metadata](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html).
Expand Down
18 changes: 15 additions & 3 deletions modules/run-consul/run-consul
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function print_usage {
echo -e " --client\t\tIf set, run in client mode. Optional. Exactly one of --server or --client must be set."
echo -e " --cluster-tag-key\tAutomatically form a cluster with Instances that have this tag key and the tag value in --cluster-tag-value. Optional."
echo -e " --cluster-tag-value\tAutomatically form a cluster with Instances that have the tag key in --cluster-tag-key and this tag value. Optional."
echo -e " --datacenter\tThe name of the datacenter Consul is running in. Optional. If not specified, will default to AWS region name."
echo -e " --config-dir\t\tThe path to the Consul config folder. Optional. Default is the absolute path of '../config', relative to this script."
echo -e " --data-dir\t\tThe path to the Consul data folder. Optional. Default is the absolute path of '../data', relative to this script."
echo -e " --log-dir\t\tThe path to the Consul log folder. Optional. Default is the absolute path of '../log', relative to this script."
Expand Down Expand Up @@ -178,6 +179,7 @@ function generate_consul_config {
local readonly user="$3"
local readonly cluster_tag_key="$4"
local readonly cluster_tag_value="$5"
local readonly datacenter="$6"
local readonly config_path="$config_dir/$CONSUL_CONFIG_FILE"

local instance_id=""
Expand Down Expand Up @@ -218,7 +220,7 @@ EOF
"bind_addr": "$instance_ip_address",
$bootstrap_expect
"client_addr": "0.0.0.0",
"datacenter": "$instance_region",
"datacenter": "$datacenter",
"node_name": "$instance_id",
$retry_join_json
"server": $server,
Expand Down Expand Up @@ -273,6 +275,7 @@ function run {
local user=""
local cluster_tag_key=""
local cluster_tag_value=""
local datacenter=""
local skip_consul_config="false"
local all_args=()

Expand Down Expand Up @@ -321,6 +324,11 @@ function run {
cluster_tag_value="$2"
shift
;;
--datacenter)
assert_not_empty "$key" "$2"
datacenter="$2"
shift
;;
--skip-consul-config)
skip_consul_config="true"
;;
Expand Down Expand Up @@ -368,14 +376,18 @@ function run {
user=$(get_owner_of_path "$config_dir")
fi

if [[ -z "$datacenter" ]]; then
datacenter=$(get_instance_region)
fi

if [[ "$skip_consul_config" == "true" ]]; then
log_info "The --skip-consul-config flag is set, so will not generate a default Consul config file."
else
generate_consul_config "$server" "$config_dir" "$user" "$cluster_tag_key" "$cluster_tag_value"
generate_consul_config "$server" "$config_dir" "$user" "$cluster_tag_key" "$cluster_tag_value" "$datacenter"
fi

generate_supervisor_config "$SUPERVISOR_CONFIG_PATH" "$config_dir" "$data_dir" "$log_dir" "$bin_dir" "$user"
start_consul
}

run "$@"
run "$@"

0 comments on commit 0e5a7b1

Please sign in to comment.