text Vagrantfile

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text Vagrantfile相关的知识,希望对你有一定的参考价值。

# -*- mode: ruby -*-
# vi: set ft=ruby :

BOX_NAME = ENV['BOX_NAME'] || "centos/7"
VAGRANTFILE_API_VERSION = "2"

CONSUL_ENCRYPT = "eEZJ2MTe0keJc2EkvZ+l3Q=="
CONSUL_DATACENTER = "nyc"
CONSUL_DATA_DIR = "/var/consul"
CONSUL_NODE_IP = {
  node1: "172.20.20.2",
  node2: "172.20.20.3",
  node3: "172.20.20.4",
  node4: "172.20.20.5",
  node5: "172.20.20.6",
}
CONSUL_NODE_HOSTS = <<HOSTS
172.20.20.2 node1
172.20.20.3 node2
172.20.20.4 node3
172.20.20.5 node4
172.20.20.6 node5
HOSTS

def consul_config(bind_addr, bootstrap = false, server = false, ui = false)
  config =<<-EOF
{
    "bootstrap":            #{bootstrap},
    "server":               #{server},
    "ui":                   #{ui},
    "datacenter":           "#{CONSUL_DATACENTER}",
    "data_dir":             "#{CONSUL_DATA_DIR}",
    "encrypt":              "#{CONSUL_ENCRYPT}",
    "log_level":            "INFO",
    "enable_syslog":        true,
    "enable_script_checks": true,
    "bind_addr":            "#{bind_addr}",
    "client_addr":          "0.0.0.0"
}
  EOF
  config
end

$init_script = <<SCRIPT
echo "Installing Consul ..."
if [ -f /vagrant/consul_0.9.0_linux_amd64 ]; then
  echo "Find consul"
  cp /vagrant/consul_0.9.0_linux_amd64 /tmp/consul
else
  echo "Can not find Consul"
  exit 1
fi

if [ -f /vagrant/consul-manager.sh ]; then
  echo "Find Consul Manager"
  cp /vagrant/consul-manager.sh /usr/bin/consul-manager
else
  echo "Can not find Consul Manager"
  exit 1
fi

sudo chmod a+rwx /usr/bin/consul-manager

cd /tmp
sudo chmod +x consul
sudo mv consul /usr/bin/consul

echo "Configuring consul"
sudo mkdir /etc/consul.d
sudo chmod a+w /etc/consul.d
sudo mkdir /var/consul
sudo adduser consul
sudo chown -R consul:consul /etc/consul.d
sudo chown -R consul:consul /var/consul

echo "Add Hosts"
echo "#{CONSUL_NODE_HOSTS}" >> /etc/hosts
SCRIPT

$script_node1 = <<SCRIPT
sudo su consul
echo '#{consul_config(CONSUL_NODE_IP[:node1], true, true, true)}' \
> /etc/consul.d/config.json
sudo consul-manager start
SCRIPT

$script_node2 = <<SCRIPT
sudo su consul
echo '#{consul_config(CONSUL_NODE_IP[:node2], false, true)}' \
> /etc/consul.d/config.json
sudo consul-manager start
SCRIPT

$script_node3 = <<SCRIPT
sudo su consul
echo '#{consul_config(CONSUL_NODE_IP[:node3], false, true)}' \
> /etc/consul.d/config.json
sudo consul-manager start
SCRIPT

$script_node4 = <<SCRIPT
sudo su consul
echo '#{consul_config(CONSUL_NODE_IP[:node4])}' \
> /etc/consul.d/config.json
sudo consul-manager start
SCRIPT

$script_node5 = <<SCRIPT
sudo su consul
echo '#{consul_config(CONSUL_NODE_IP[:node5])}' \
> /etc/consul.d/config.json
sudo consul-manager start
SCRIPT

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = BOX_NAME
  config.vm.provision "shell", inline: $init_script

  config.vm.define "node1" do |n|
    n.vm.provision "shell", inline: $script_node1
    n.vm.hostname = "node1"
    n.vm.network "private_network", ip: CONSUL_NODE_IP[:node1]
    n.vm.network "forwarded_port", guest: 8500, host: 8500, protocol: "tcp"
  end

  config.vm.define "node2" do |n|
    n.vm.provision "shell", inline: $script_node2
    n.vm.hostname = "node2"
    n.vm.network "private_network", ip: CONSUL_NODE_IP[:node2]
  end

  config.vm.define "node3" do |n|
    n.vm.provision "shell", inline: $script_node3
    n.vm.hostname = "node3"
    n.vm.network "private_network", ip: CONSUL_NODE_IP[:node3]
  end

  config.vm.define "node4" do |n|
    n.vm.provision "shell", inline: $script_node4
    n.vm.hostname = "node4"
    n.vm.network "private_network", ip: CONSUL_NODE_IP[:node4]
  end

  config.vm.define "node5" do |n|
    n.vm.provision "shell", inline: $script_node5
    n.vm.hostname = "node5"
    n.vm.network "private_network", ip: CONSUL_NODE_IP[:node5]
  end
end

以上是关于text Vagrantfile的主要内容,如果未能解决你的问题,请参考以下文章

text Vagrantfile

text 使用Ansible置备的示例Vagrantfile

text Vagrantfile

text vagrantfile

text 用于Ubuntu 16.04 LTS(Xenial64)Box的Vagrantfile

text 设置VALUE-DOMAIN DDNS,ConoHa用Vagrantfile(vagrant-conoha)