diff --git a/etcd-2.0/.gitignore b/etcd-2.0/.gitignore new file mode 100644 index 0000000..277ea56 --- /dev/null +++ b/etcd-2.0/.gitignore @@ -0,0 +1,2 @@ +.vagrant +*.defaults diff --git a/etcd-2.0/README.md b/etcd-2.0/README.md index 562cd68..3a1ba5e 100644 --- a/etcd-2.0/README.md +++ b/etcd-2.0/README.md @@ -6,9 +6,9 @@ These files were created to allow users to use Vagrant ([http://www.vagrantup.co * **etcd.conf**: This is an Upstart script (written for Ubuntu) to start etcd. No modifications to this file should be necessary. This file is installed by the provisioning script called in `Vagrantfile`. -* **etcd-01.override, etcd-02.override, etcd-03.override**: These are machine-specific Upstart override files for each node in the etcd cluster. The appropriate node-specific file is installed as `etcd.override` in the `/etc/init` directory on each node by the provisioning script called in `Vagrantfile`. If you make any changes to hostnames or IP addresses in `servers.yml`, these files must also be modified so that the hostnames and IP addresses match. +* **etcd.defaults.erb**: A template used by Vagrant to create machine-specific service defaults files for each node in the etcd cluster. The appropriate node-specific file is installed as `etcd` in the `/etc/default` directory on each node by the provisioning script called in `Vagrantfile`. -* **provision.sh**: This provisioning script is called by the shell provisioner in `Vagrantfile`. It downloads the etcd 2.0.9 release from GitHub, expands it, creates necessary directories, and places files in the appropriate locations. +* **provision.sh**: This provisioning script is called by the shell provisioner in `Vagrantfile`. It downloads the etcd 2.0.9 release from GitHub, expands it, creates necessary directories, and places files in the appropriate locations. Finally it starts etcd ( or restarts it if already running ). * **README.md**: This file you're currently reading. @@ -26,17 +26,11 @@ These instructions assume you've already installed VMware Fusion, Vagrant, and t 3. Edit `servers.yml` to ensure that the box specified in that file matches the Ubuntu 14.04 x64 base box you just installed and will be using. _I recommend that you do not change any other values in this file unless you know it is necessary._ -4. If you changed the hostnames or IP addresses provided in `servers.yml`, you **must** edit the Upstart override files (`etcd-01.override`, `etcd-02.override`, and `etcd-03.override`). The hostnames and IP addresses specified in these files are based on the values provided in `servers.yml`. If you changed the values in `servers.yml`, change the values in these files to match. +4. From a terminal window, change into the directory where the files from this directory are stored and run `vagrant up` to bring up the VMs specified in `servers.yml` and `Vagrantfile`. -5. From a terminal window, change into the directory where the files from this directory are stored and run `vagrant up` to bring up the VMs specified in `servers.yml` and `Vagrantfile`. +5. Once Vagrant has finished creating, booting, and provisioning each of the VMs and starting etcd, log into the first system ("etcd-01" by default) using `vagrant ssh etcd-01`. -6. Once Vagrant has finished creating, booting, and provisioning each of the VMs, log into the first system ("etcd-01" by default) using `vagrant ssh etcd-01`. Once you are logged into this VM, start etcd with this command: - - sudo initctl start etcd - -7. Repeat step 6 with the remaining 2 VMs ("etcd-02" and "etcd-03", by default). - -8. Once etcd is running on all 3 VMs, you can test etcd with this command: +6. You can test etcd with this command: etcdctl member list diff --git a/etcd-2.0/Vagrantfile b/etcd-2.0/Vagrantfile index 31ad978..c9495bc 100644 --- a/etcd-2.0/Vagrantfile +++ b/etcd-2.0/Vagrantfile @@ -12,6 +12,23 @@ require 'yaml' # Be sure to edit servers.yml to provide correct IP addresses servers = YAML.load_file('servers.yml') +# Require 'erb' module +require 'erb' + +# Use config from YAML file to write out templates for etcd overrides +template = File.join(File.dirname(__FILE__), 'etcd.defaults.erb') +content = ERB.new File.new(template).read + +etcd_initial_cluster = [] +servers.each do |servers| + etcd_initial_cluster << "#{servers['name']}=http://#{servers['priv_ip']}:2380" +end +servers.each do |servers| + ip = servers['priv_ip'] + target = File.join(File.dirname(__FILE__), "#{servers['name']}.defaults") + File.open(target, 'w') { |f| f.write(content.result(binding)) } +end + # Create and configure the VMs Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| @@ -39,4 +56,4 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| end end end -end \ No newline at end of file +end diff --git a/etcd-2.0/etcd-01.override b/etcd-2.0/etcd-01.override deleted file mode 100644 index 419e9be..0000000 --- a/etcd-2.0/etcd-01.override +++ /dev/null @@ -1,12 +0,0 @@ -# Override file for etcd Upstart script - -# Environment variables -env ETCD_INITIAL_CLUSTER="etcd-01=http://192.168.101.101:2380,etcd-02=http://192.168.101.102:2380,etcd-03=http://192.168.101.103:2380" -env ETCD_INITIAL_CLUSTER_STATE="new" -env ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster-01" -env ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.101.101:2380" -env ETCD_DATA_DIR="/var/etcd" -env ETCD_LISTEN_PEER_URLS="http://192.168.101.101:2380" -env ETCD_LISTEN_CLIENT_URLS="http://192.168.101.101:2379" -env ETCD_ADVERTISE_CLIENT_URLS="http://192.168.101.101:2379" -env ETCD_NAME="etcd-01" diff --git a/etcd-2.0/etcd-02.override b/etcd-2.0/etcd-02.override deleted file mode 100644 index b83f2f0..0000000 --- a/etcd-2.0/etcd-02.override +++ /dev/null @@ -1,12 +0,0 @@ -# Override file for etcd Upstart script - -# Environment variables -env ETCD_INITIAL_CLUSTER="etcd-01=http://192.168.101.101:2380,etcd-02=http://192.168.101.102:2380,etcd-03=http://192.168.101.103:2380" -env ETCD_INITIAL_CLUSTER_STATE="new" -env ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster-01" -env ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.101.102:2380" -env ETCD_DATA_DIR="/var/etcd" -env ETCD_LISTEN_PEER_URLS="http://192.168.101.102:2380" -env ETCD_LISTEN_CLIENT_URLS="http://192.168.101.102:2379" -env ETCD_ADVERTISE_CLIENT_URLS="http://192.168.101.102:2379" -env ETCD_NAME="etcd-02" diff --git a/etcd-2.0/etcd-03.override b/etcd-2.0/etcd-03.override deleted file mode 100644 index d92dbc2..0000000 --- a/etcd-2.0/etcd-03.override +++ /dev/null @@ -1,12 +0,0 @@ -# Override file for etcd Upstart script - -# Environment variables -env ETCD_INITIAL_CLUSTER="etcd-01=http://192.168.101.101:2380,etcd-02=http://192.168.101.102:2380,etcd-03=http://192.168.101.103:2380" -env ETCD_INITIAL_CLUSTER_STATE="new" -env ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster-01" -env ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.101.103:2380" -env ETCD_DATA_DIR="/var/etcd" -env ETCD_LISTEN_PEER_URLS="http://192.168.101.103:2380" -env ETCD_LISTEN_CLIENT_URLS="http://192.168.101.103:2379" -env ETCD_ADVERTISE_CLIENT_URLS="http://192.168.101.103:2379" -env ETCD_NAME="etcd-03" diff --git a/etcd-2.0/etcd.defaults.erb b/etcd-2.0/etcd.defaults.erb new file mode 100644 index 0000000..6cc2f89 --- /dev/null +++ b/etcd-2.0/etcd.defaults.erb @@ -0,0 +1,12 @@ +# Override file for etcd Upstart script +# Generated from etcd.override.erb by Vagrant +# Environment variables +export ETCD_INITIAL_CLUSTER="<%= etcd_initial_cluster.join(',') %>" +export ETCD_INITIAL_CLUSTER_STATE="new" +export ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster-01" +export ETCD_INITIAL_ADVERTISE_PEER_URLS="http://<%= servers['priv_ip'] %>:2380" +export ETCD_DATA_DIR="/var/etcd" +export ETCD_LISTEN_PEER_URLS="http://<%= servers['priv_ip'] %>:2380" +export ETCD_LISTEN_CLIENT_URLS="http://<%= servers['priv_ip'] %>:2379,http://0.0.0.0:4001" +export ETCD_ADVERTISE_CLIENT_URLS="http://<%= servers['priv_ip'] %>:2379" +export ETCD_NAME="<%= servers['name'] %>" diff --git a/etcd-2.0/provision.sh b/etcd-2.0/provision.sh index 1f6e029..e0182bb 100644 --- a/etcd-2.0/provision.sh +++ b/etcd-2.0/provision.sh @@ -1,24 +1,35 @@ #!/bin/bash -# Get etcd download -curl -L https://github.com/coreos/etcd/releases/download/v2.0.9/etcd-v2.0.9-linux-amd64.tar.gz -o etcd-v2.0.9-linux-amd64.tar.gz +# install curl if needed +if [[ ! -e /usr/bin/curl ]]; then + apt-get update + apt-get -yqq install curl +fi -# Expand the download -tar xzvf etcd-v2.0.9-linux-amd64.tar.gz +if [[ ! -e /usr/local/bin/etcd ]]; then + # Get etcd download + curl -sL https://github.com/coreos/etcd/releases/download/v2.0.9/etcd-v2.0.9-linux-amd64.tar.gz -o etcd-v2.0.9-linux-amd64.tar.gz -# Move etcd and etcdctl to /usr/local/bin -cd etcd-v2.0.9-linux-amd64 -sudo mv etcd /usr/local/bin/ -sudo mv etcdctl /usr/local/bin/ -cd .. + # Expand the download + tar xzvf etcd-v2.0.9-linux-amd64.tar.gz -# Remove etcd download and directory -rm etcd-v2.0.9-linux-amd64.tar.gz -rm -rf etcd-v2.0.9-linux-amd64 + # Move etcd and etcdctl to /usr/local/bin + cd etcd-v2.0.9-linux-amd64 + sudo mv etcd /usr/local/bin/ + sudo mv etcdctl /usr/local/bin/ + cd .. -# Create directories needed by etcd -sudo mkdir /var/etcd + # Remove etcd download and directory + rm etcd-v2.0.9-linux-amd64.tar.gz + rm -rf etcd-v2.0.9-linux-amd64 + + # Create directories needed by etcd + sudo mkdir -p /var/etcd +fi # Copy files into the correct locations; requires shared folders sudo cp /vagrant/etcd.conf /etc/init/etcd.conf -sudo cp /vagrant/$HOSTNAME.override /etc/init/etcd.override +sudo cp /vagrant/$HOSTNAME.defaults /etc/default/etcd + +# restart if already running, otherwise start. +initctl status etcd && initctl restart etcd || initctl start etcd