|
| 1 | +# -*- mode: ruby -*- |
| 2 | +# vi: set ft=ruby : |
| 3 | + |
| 4 | +require 'fileutils' |
| 5 | + |
| 6 | +# netplugin_synced_gopath="/opt/golang" |
| 7 | +gopath_folder="/opt/gopath" |
| 8 | + |
| 9 | +ANSIBLE_GROUPS = { |
| 10 | + "master" => ["node1"], |
| 11 | + "nodes" => ["node2"], |
| 12 | + "all_groups:children" => ["master, ""nodes"] |
| 13 | + } |
| 14 | + |
| 15 | +provision_common = <<SCRIPT |
| 16 | +## setup the environment file. Export the env-vars passed as args to 'vagrant up' |
| 17 | +echo Args passed: [[ $@ ]] |
| 18 | +
|
| 19 | +echo -n "$1" > /etc/hostname |
| 20 | +hostname -F /etc/hostname |
| 21 | +
|
| 22 | +/sbin/ip addr add "$3/24" dev eth1 |
| 23 | +/sbin/ip link set eth1 up |
| 24 | +/sbin/ip link set eth2 up |
| 25 | +
|
| 26 | +echo 'export GOPATH=#{gopath_folder}' > /etc/profile.d/envvar.sh |
| 27 | +echo 'export GOBIN=$GOPATH/bin' >> /etc/profile.d/envvar.sh |
| 28 | +echo 'export GOSRC=$GOPATH/src' >> /etc/profile.d/envvar.sh |
| 29 | +echo 'export PATH=$PATH:/usr/local/go/bin:$GOBIN' >> /etc/profile.d/envvar.sh |
| 30 | +echo "export http_proxy='$4'" >> /etc/profile.d/envvar.sh |
| 31 | +echo "export https_proxy='$5'" >> /etc/profile.d/envvar.sh |
| 32 | +echo "export no_proxy=192.168.2.10,192.168.2.11,127.0.0.1,localhost,netmaster" >> /etc/profile.d/envvar.sh |
| 33 | +echo "export CLUSTER_NODE_IPS=192.168.2.10,192.168.2.11" >> /etc/profile.d/envvar.sh |
| 34 | +echo "export USE_RELEASE=$6" >> /etc/profile.d/envvar.sh |
| 35 | +
|
| 36 | +
|
| 37 | +source /etc/profile.d/envvar.sh |
| 38 | +
|
| 39 | +# setup docker cluster store |
| 40 | +cp #{gopath_folder}/src/github.com/contiv/netplugin/scripts/docker.service /lib/systemd/system/docker.service |
| 41 | +
|
| 42 | +# setup docker remote api |
| 43 | +cp #{gopath_folder}/src/github.com/contiv/netplugin/scripts/docker-tcp.socket /etc/systemd/system/docker-tcp.socket |
| 44 | +systemctl enable docker-tcp.socket |
| 45 | +
|
| 46 | +mkdir /etc/systemd/system/docker.service.d |
| 47 | +echo "[Service]" | sudo tee -a /etc/systemd/system/docker.service.d/http-proxy.conf |
| 48 | +echo "Environment=\\\"no_proxy=192.168.2.10,192.168.2.11,127.0.0.1,localhost,netmaster\\\" \\\"http_proxy=$http_proxy\\\" \\\"https_proxy=$https_proxy\\\"" | sudo tee -a /etc/systemd/system/docker.service.d/http-proxy.conf |
| 49 | +sudo systemctl daemon-reload |
| 50 | +sudo systemctl stop docker |
| 51 | +systemctl start docker-tcp.socket |
| 52 | +sudo systemctl start docker |
| 53 | +
|
| 54 | +if [ $# -gt 6 ]; then |
| 55 | + shift; shift; shift; shift; shift; shift |
| 56 | + echo "export $@" >> /etc/profile.d/envvar.sh |
| 57 | +fi |
| 58 | +
|
| 59 | +
|
| 60 | +# remove duplicate docker key |
| 61 | +rm /etc/docker/key.json |
| 62 | +
|
| 63 | +(service docker restart) || exit 1 |
| 64 | +
|
| 65 | +(ovs-vsctl set-manager tcp:127.0.0.1:6640 && \ |
| 66 | + ovs-vsctl set-manager ptcp:6640) || exit 1 |
| 67 | +
|
| 68 | +docker load --input #{gopath_folder}/src/github.com/contiv/netplugin/scripts/dnscontainer.tar |
| 69 | +SCRIPT |
| 70 | + |
| 71 | +VAGRANTFILE_API_VERSION = "2" |
| 72 | +Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| |
| 73 | + config.vm.box = "contiv/centos71-netplugin" |
| 74 | + config.vm.box_version = "0.3.1" |
| 75 | + |
| 76 | + num_nodes = 2 |
| 77 | + if ENV['CONTIV_NODES'] && ENV['CONTIV_NODES'] != "" then |
| 78 | + num_nodes = ENV['CONTIV_NODES'].to_i |
| 79 | + end |
| 80 | + base_ip = "192.168.33." |
| 81 | + node_ips = num_nodes.times.collect { |n| base_ip + "#{n+10}" } |
| 82 | + node_names = num_nodes.times.collect { |n| "node#{n+1}" } |
| 83 | + node_peers = [] |
| 84 | + |
| 85 | + num_nodes.times do |n| |
| 86 | + node_name = node_names[n] |
| 87 | + node_addr = node_ips[n] |
| 88 | + node_peers += ["#{node_name}=http://#{node_addr}:2380,#{node_name}=http://#{node_addr}:7001"] |
| 89 | + consul_join_flag = if n > 0 then "-join #{node_ips[0]}" else "" end |
| 90 | + consul_bootstrap_flag = "-bootstrap-expect=3" |
| 91 | + swarm_flag = "slave" |
| 92 | + if num_nodes < 3 then |
| 93 | + if n == 0 then |
| 94 | + consul_bootstrap_flag = "-bootstrap" |
| 95 | + swarm_flag = "master" |
| 96 | + else |
| 97 | + consul_bootstrap_flag = "" |
| 98 | + swarm_flag = "slave" |
| 99 | + end |
| 100 | + end |
| 101 | + config.vm.define node_name do |node| |
| 102 | + # node.vm.hostname = node_name |
| 103 | + # create an interface for etcd cluster |
| 104 | + node.vm.network :private_network, ip: node_addr, virtualbox__intnet: "true", auto_config: false |
| 105 | + # create an interface for bridged network |
| 106 | + node.vm.network :private_network, ip: "0.0.0.0", virtualbox__intnet: "true", auto_config: false |
| 107 | + node.vm.provider "virtualbox" do |v| |
| 108 | + # make all nics 'virtio' to take benefit of builtin vlan tag |
| 109 | + # support, which otherwise needs to be enabled in Intel drivers, |
| 110 | + # which are used by default by virtualbox |
| 111 | + v.customize ['modifyvm', :id, '--nictype1', 'virtio'] |
| 112 | + v.customize ['modifyvm', :id, '--nictype2', 'virtio'] |
| 113 | + v.customize ['modifyvm', :id, '--nictype3', 'virtio'] |
| 114 | + v.customize ['modifyvm', :id, '--nicpromisc2', 'allow-all'] |
| 115 | + v.customize ['modifyvm', :id, '--nicpromisc3', 'allow-all'] |
| 116 | + v.customize ['modifyvm', :id, '--paravirtprovider', "kvm"] |
| 117 | + end |
| 118 | + |
| 119 | + # mount the host directories |
| 120 | + node.vm.synced_folder "../../bin", File.join(gopath_folder, "bin") |
| 121 | + if ENV["GOPATH"] && ENV['GOPATH'] != "" |
| 122 | + node.vm.synced_folder "../../../../../", File.join(gopath_folder, "src"), rsync: true |
| 123 | + else |
| 124 | + node.vm.synced_folder "../../", File.join(gopath_folder, "src/github.com/contiv/netplugin"), rsync: true |
| 125 | + end |
| 126 | + |
| 127 | + node.vm.provision "shell" do |s| |
| 128 | + s.inline = "echo '#{node_ips[0]} netmaster' >> /etc/hosts; echo '#{node_addr} #{node_name}' >> /etc/hosts" |
| 129 | + end |
| 130 | + node.vm.provision "shell" do |s| |
| 131 | + s.inline = provision_common |
| 132 | + s.args = [node_name, ENV["CONTIV_NODE_OS"] || "", node_addr, ENV["http_proxy"] || "", ENV["https_proxy"] || "", ENV["USE_RELEASE"] || "", *ENV['CONTIV_ENV']] |
| 133 | + end |
| 134 | +provision_node = <<SCRIPT |
| 135 | +## start etcd with generated config |
| 136 | +set -x |
| 137 | +(nohup etcd --name #{node_name} --data-dir /tmp/etcd \ |
| 138 | + --listen-client-urls http://0.0.0.0:2379,http://0.0.0.0:4001 \ |
| 139 | + --advertise-client-urls http://#{node_addr}:2379,http://#{node_addr}:4001 \ |
| 140 | + --initial-advertise-peer-urls http://#{node_addr}:2380,http://#{node_addr}:7001 \ |
| 141 | + --listen-peer-urls http://#{node_addr}:2380 \ |
| 142 | + --initial-cluster #{node_peers.join(",")} --initial-cluster-state new \ |
| 143 | + 0<&- &>/tmp/etcd.log &) || exit 1 |
| 144 | +
|
| 145 | +## start consul |
| 146 | +(nohup consul agent -server #{consul_join_flag} #{consul_bootstrap_flag} \ |
| 147 | + -bind=#{node_addr} -data-dir /opt/consul 0<&- &>/tmp/consul.log &) || exit 1 |
| 148 | +
|
| 149 | +SCRIPT |
| 150 | + node.vm.provision "shell", run: "always" do |s| |
| 151 | + s.inline = provision_node |
| 152 | + end |
| 153 | + |
| 154 | + if n == (num_nodes - 1) then |
| 155 | + node.vm.provision "ansible" do |ansible| |
| 156 | + ansible.playbook = "playbook.yml" |
| 157 | + ansible.groups = ANSIBLE_GROUPS |
| 158 | + ansible.limit = "all" |
| 159 | + end |
| 160 | + end |
| 161 | + # forward netmaster port |
| 162 | + if n == 0 then |
| 163 | + node.vm.network "forwarded_port", guest: 5050, host: 5050 |
| 164 | + node.vm.network "forwarded_port", guest: 8080, host: 8080 |
| 165 | + node.vm.network "forwarded_port", guest: 9999, host: 9090 |
| 166 | + end |
| 167 | + end |
| 168 | + end |
| 169 | +end |
0 commit comments