2018-10-07 00:34:16 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# -*- mode: ruby -*-
|
|
|
|
# vi: set ft=ruby :
|
|
|
|
|
2022-07-28 06:49:19 +00:00
|
|
|
ENV['VAGRANT_NO_PARALLEL']="true"
|
|
|
|
|
2018-10-07 00:34:16 +00:00
|
|
|
Vagrant.configure('2') do |config|
|
2022-10-02 08:59:05 +00:00
|
|
|
ubuntu_version = File.readlines("Dockerfile").filter { |line|
|
|
|
|
line.start_with?("FROM ")
|
|
|
|
}.first.match(/\d\d\.\d\d/)[0]
|
|
|
|
|
2024-11-11 10:08:27 +00:00
|
|
|
docker_version = File.readlines("Dockerfile").filter { |line|
|
|
|
|
line.start_with?("ARG docker_version=")
|
|
|
|
}.first.match(/"([\d\.]+)"/)[1]
|
2022-10-02 08:59:05 +00:00
|
|
|
|
2024-11-11 10:08:27 +00:00
|
|
|
config.vm.box = "chaifeng/ubuntu-#{ubuntu_version}-docker-#{docker_version}"
|
2018-10-07 00:34:16 +00:00
|
|
|
|
|
|
|
config.vm.provider 'virtualbox' do |vb|
|
|
|
|
vb.memory = '1024'
|
2018-12-02 03:43:32 +00:00
|
|
|
vb.default_nic_type = "virtio"
|
2018-10-07 00:34:16 +00:00
|
|
|
end
|
|
|
|
|
2022-07-28 06:49:19 +00:00
|
|
|
config.vm.provider 'parallels' do |prl|
|
|
|
|
prl.memory = '1024'
|
|
|
|
prl.check_guest_tools = false
|
|
|
|
end
|
|
|
|
|
2018-10-07 00:34:16 +00:00
|
|
|
ip_prefix="192.168.56"
|
|
|
|
|
2019-05-20 08:29:13 +00:00
|
|
|
config.vm.provision 'docker-daemon-config', type: 'shell', inline: <<-SHELL
|
2019-10-20 15:18:13 +00:00
|
|
|
set -eu
|
2018-10-07 00:34:16 +00:00
|
|
|
if [[ ! -f /etc/docker/daemon.json ]]; then
|
|
|
|
echo '{' >> /etc/docker/daemon.json
|
|
|
|
echo ' "insecure-registries": ["localhost:5000", "#{ip_prefix}.130:5000"]' >> /etc/docker/daemon.json
|
|
|
|
[[ -n "#{ENV['DOCKER_REGISTRY_MIRROR']}" ]] &&
|
|
|
|
echo ' , "registry-mirrors": ["#{ENV['DOCKER_REGISTRY_MIRROR']}"]' >> /etc/docker/daemon.json
|
|
|
|
echo '}' >> /etc/docker/daemon.json
|
|
|
|
if type systemctl &>/dev/null; then
|
|
|
|
systemctl restart docker
|
|
|
|
else
|
|
|
|
service docker restart
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
SHELL
|
|
|
|
|
|
|
|
config.vm.provision 'ufw-docker', type: 'shell', inline: <<-SHELL
|
2019-10-20 15:18:13 +00:00
|
|
|
set -euo pipefail
|
2019-05-20 08:29:13 +00:00
|
|
|
export DEBUG=true
|
|
|
|
lsb_release -is | grep -Fi ubuntu
|
2018-12-01 04:08:01 +00:00
|
|
|
/vagrant/ufw-docker check || {
|
2018-10-07 00:34:16 +00:00
|
|
|
ufw allow OpenSSH
|
|
|
|
ufw allow from #{ip_prefix}.128/28 to any
|
|
|
|
|
2019-10-20 15:18:13 +00:00
|
|
|
yes | ufw enable || true
|
|
|
|
ufw status | grep '^Status: active'
|
|
|
|
|
2018-10-07 00:34:16 +00:00
|
|
|
/vagrant/ufw-docker install
|
|
|
|
|
|
|
|
sed -i -e 's,192\.168\.0\.0/16,#{ip_prefix}.128/28,' /etc/ufw/after.rules
|
|
|
|
|
2018-12-01 04:31:59 +00:00
|
|
|
systemctl restart ufw
|
|
|
|
|
2018-12-01 04:08:20 +00:00
|
|
|
[[ -L /usr/local/bin/ufw-docker ]] || ln -s /vagrant/ufw-docker /usr/local/bin/
|
2018-12-02 02:38:30 +00:00
|
|
|
|
|
|
|
iptables -I DOCKER-USER 4 -p udp -j LOG --log-prefix '[UFW DOCKER] '
|
2018-10-07 00:34:16 +00:00
|
|
|
}
|
|
|
|
SHELL
|
|
|
|
|
2019-10-20 15:18:13 +00:00
|
|
|
private_registry="#{ip_prefix}.130:5000"
|
|
|
|
|
2018-10-07 00:34:16 +00:00
|
|
|
config.vm.define "master" do |master|
|
2022-07-28 06:49:19 +00:00
|
|
|
master_ip_address = "#{ip_prefix}.130"
|
2018-10-07 00:34:16 +00:00
|
|
|
master.vm.hostname = "master"
|
2022-07-28 06:49:19 +00:00
|
|
|
master.vm.network "private_network", ip: "#{master_ip_address}"
|
2018-10-07 00:34:16 +00:00
|
|
|
|
2022-07-28 06:49:19 +00:00
|
|
|
master.vm.provision "unit-testing", preserve_order: true, type: 'shell', inline: <<-SHELL
|
2019-10-20 15:18:13 +00:00
|
|
|
set -euo pipefail
|
|
|
|
/vagrant/test.sh
|
2019-10-17 20:56:29 +00:00
|
|
|
SHELL
|
|
|
|
|
2022-07-28 06:49:19 +00:00
|
|
|
master.vm.provision "docker-registry", preserve_order: true, type: 'docker' do |d|
|
2018-12-01 04:32:33 +00:00
|
|
|
d.run "registry",
|
|
|
|
image: "registry:2",
|
|
|
|
args: "-p 5000:5000",
|
|
|
|
restart: "always",
|
|
|
|
daemonize: true
|
|
|
|
end
|
|
|
|
|
2022-09-26 14:00:05 +00:00
|
|
|
ufw_docker_agent_image = "#{private_registry}/chaifeng/ufw-docker-agent:test-legacy"
|
2018-12-01 14:04:21 +00:00
|
|
|
|
2022-07-28 06:49:19 +00:00
|
|
|
master.vm.provision "docker-build-ufw-docker-agent", preserve_order: true, type: 'shell', inline: <<-SHELL
|
2019-10-20 15:18:13 +00:00
|
|
|
set -euo pipefail
|
2022-09-26 14:00:05 +00:00
|
|
|
suffix="$(iptables --version | grep -o '\\(nf_tables\\|legacy\\)')"
|
|
|
|
docker build -t "#{ufw_docker_agent_image}-${suffix}" /vagrant
|
|
|
|
docker push "#{ufw_docker_agent_image}-${suffix}"
|
2018-12-01 23:55:34 +00:00
|
|
|
|
2022-09-26 14:00:05 +00:00
|
|
|
echo "export UFW_DOCKER_AGENT_IMAGE=#{ufw_docker_agent_image}-${suffix}" > /etc/profile.d/ufw-docker.sh
|
2018-12-02 02:38:30 +00:00
|
|
|
echo "export DEBUG=true" >> /etc/profile.d/ufw-docker.sh
|
|
|
|
|
2018-12-02 04:12:44 +00:00
|
|
|
echo "Defaults env_keep += UFW_DOCKER_AGENT_IMAGE" > /etc/sudoers.d/98_ufw-docker
|
|
|
|
echo "Defaults env_keep += DEBUG" >> /etc/sudoers.d/98_ufw-docker
|
2018-12-01 14:04:21 +00:00
|
|
|
SHELL
|
|
|
|
|
2022-07-28 06:49:19 +00:00
|
|
|
master.vm.provision "swarm-init", preserve_order: true, type: 'shell', inline: <<-SHELL
|
2019-10-20 15:18:13 +00:00
|
|
|
set -euo pipefail
|
2018-12-01 04:32:33 +00:00
|
|
|
docker info | fgrep 'Swarm: active' && exit 0
|
|
|
|
|
2022-07-28 06:49:19 +00:00
|
|
|
docker swarm init --advertise-addr "#{master_ip_address}"
|
2018-10-07 00:34:16 +00:00
|
|
|
docker swarm join-token worker --quiet > /vagrant/.vagrant/docker-join-token
|
|
|
|
SHELL
|
2019-10-20 15:18:13 +00:00
|
|
|
|
2022-07-28 06:49:19 +00:00
|
|
|
master.vm.provision "build-webapp", preserve_order: true, type: 'shell', inline: <<-SHELL
|
2019-10-20 15:18:13 +00:00
|
|
|
set -euo pipefail
|
|
|
|
docker build -t #{private_registry}/chaifeng/hostname-webapp - <<\\DOCKERFILE
|
|
|
|
FROM httpd:alpine
|
|
|
|
|
|
|
|
RUN { echo '#!/bin/sh'; \\
|
|
|
|
echo 'set -e; (echo -n "${name:-Hi} "; hostname;) > /usr/local/apache2/htdocs/index.html'; \\
|
2022-09-26 14:00:05 +00:00
|
|
|
echo 'grep "^Listen 7000" || echo Listen 7000 >> /usr/local/apache2/conf/httpd.conf'; \\
|
|
|
|
echo 'grep "^Listen 8080" || echo Listen 8080 >> /usr/local/apache2/conf/httpd.conf'; \\
|
2019-10-20 15:18:13 +00:00
|
|
|
echo 'exec "$@"'; \\
|
|
|
|
} > /entrypoint.sh; chmod +x /entrypoint.sh
|
|
|
|
|
|
|
|
ENTRYPOINT ["/entrypoint.sh"]
|
|
|
|
CMD ["httpd-foreground"]
|
|
|
|
DOCKERFILE
|
|
|
|
docker push #{private_registry}/chaifeng/hostname-webapp
|
|
|
|
SHELL
|
|
|
|
|
2022-07-28 06:49:19 +00:00
|
|
|
master.vm.provision "local-webapp", preserve_order: true, type: 'shell', inline: <<-SHELL
|
2019-10-20 15:18:13 +00:00
|
|
|
set -euo pipefail
|
2020-08-12 05:33:24 +00:00
|
|
|
for name in public:18080 local:8000; do
|
2019-10-20 15:18:13 +00:00
|
|
|
webapp="${name%:*}_webapp"
|
|
|
|
port="${name#*:}"
|
2020-08-12 05:33:24 +00:00
|
|
|
if docker inspect "$webapp" &>/dev/null; then docker rm -f "$webapp"; fi
|
2020-01-06 05:17:19 +00:00
|
|
|
docker run -d --restart unless-stopped --name "$webapp" \
|
2019-10-20 15:18:13 +00:00
|
|
|
-p "$port:80" --env name="$webapp" #{private_registry}/chaifeng/hostname-webapp
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
|
|
|
|
ufw-docker allow public_webapp
|
|
|
|
SHELL
|
|
|
|
|
2022-07-28 06:49:19 +00:00
|
|
|
master.vm.provision "multiple-network", preserve_order: true, type: 'shell', inline: <<-SHELL
|
2021-09-21 21:58:34 +00:00
|
|
|
set -euo pipefail
|
|
|
|
if ! docker network ls | grep -F foo-internal; then
|
|
|
|
docker network create --internal foo-internal
|
|
|
|
fi
|
|
|
|
if ! docker network ls | grep -F bar-external; then
|
|
|
|
docker network create bar-external
|
|
|
|
fi
|
|
|
|
|
|
|
|
for app in internal-multinet-app:7000 public-multinet-app:17070; do
|
|
|
|
if ! docker inspect "${app%:*}" &>/dev/null; then
|
|
|
|
docker run -d --restart unless-stopped --name "${app%:*}" \
|
|
|
|
-p "${app#*:}":80 --env name="${app}" \
|
|
|
|
--network foo-internal \
|
|
|
|
192.168.56.130:5000/chaifeng/hostname-webapp
|
|
|
|
docker network connect bar-external "${app%:*}"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
ufw-docker allow public-multinet-app 80 bar-external
|
|
|
|
ufw-docker allow internal-multinet-app 80 foo-internal
|
|
|
|
SHELL
|
|
|
|
|
2022-07-28 06:49:19 +00:00
|
|
|
master.vm.provision "swarm-webapp", preserve_order: true, type: 'shell', inline: <<-SHELL
|
2019-10-20 15:18:13 +00:00
|
|
|
set -euo pipefail
|
2020-08-12 05:33:24 +00:00
|
|
|
for name in public:29090 local:9000; do
|
2019-10-20 15:18:13 +00:00
|
|
|
webapp="${name%:*}_service"
|
|
|
|
port="${name#*:}"
|
|
|
|
if docker service inspect "$webapp" &>/dev/null; then docker service rm "$webapp"; fi
|
|
|
|
docker service create --name "$webapp" \
|
|
|
|
--publish "${port}:80" --env name="$webapp" --replicas 3 #{private_registry}/chaifeng/hostname-webapp
|
|
|
|
done
|
|
|
|
|
|
|
|
ufw-docker service allow public_service 80/tcp
|
2022-09-22 22:49:48 +00:00
|
|
|
|
2024-11-11 10:08:27 +00:00
|
|
|
docker service inspect "public_multiport" ||
|
|
|
|
docker service create --name "public_multiport" \
|
|
|
|
--publish "40080:80" --publish "47000:7000" --publish "48080:8080" \
|
|
|
|
--env name="public_multiport" --replicas 3 #{private_registry}/chaifeng/hostname-webapp
|
2022-09-22 22:49:48 +00:00
|
|
|
|
|
|
|
ufw-docker service allow public_multiport 80/tcp
|
2022-09-26 14:00:05 +00:00
|
|
|
ufw-docker service allow public_multiport 8080/tcp
|
2019-10-20 15:18:13 +00:00
|
|
|
SHELL
|
2018-10-07 00:34:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
1.upto 2 do |ip|
|
|
|
|
config.vm.define "node#{ip}" do | node |
|
|
|
|
node.vm.hostname = "node#{ip}"
|
|
|
|
node.vm.network "private_network", ip: "#{ip_prefix}.#{ 130 + ip }"
|
|
|
|
|
2022-07-28 06:49:19 +00:00
|
|
|
node.vm.provision "swarm-join", preserve_order: true, type: 'shell', inline: <<-SHELL
|
2019-10-20 15:18:13 +00:00
|
|
|
set -euo pipefail
|
2018-12-01 04:32:33 +00:00
|
|
|
docker info | fgrep 'Swarm: active' && exit 0
|
|
|
|
|
2018-10-07 00:34:16 +00:00
|
|
|
[[ -f /vagrant/.vagrant/docker-join-token ]] &&
|
|
|
|
docker swarm join --token "$(</vagrant/.vagrant/docker-join-token)" #{ip_prefix}.130:2377
|
|
|
|
SHELL
|
|
|
|
end
|
|
|
|
end
|
2019-10-20 15:18:13 +00:00
|
|
|
|
2024-11-08 02:25:22 +00:00
|
|
|
config.vm.define "node-internal" do |node|
|
|
|
|
node.vm.hostname = "node-internal"
|
|
|
|
node.vm.network "private_network", ip: "#{ip_prefix}.142"
|
|
|
|
end
|
|
|
|
|
2019-10-20 15:18:13 +00:00
|
|
|
config.vm.define "external" do |external|
|
|
|
|
external.vm.hostname = "external"
|
|
|
|
external.vm.network "private_network", ip: "#{ip_prefix}.127"
|
|
|
|
|
2022-07-28 06:49:19 +00:00
|
|
|
external.vm.provision "testing", preserve_order: true, type: 'shell', inline: <<-SHELL
|
2019-10-20 15:18:13 +00:00
|
|
|
set -euo pipefail
|
2020-01-06 05:17:19 +00:00
|
|
|
set -x
|
2019-10-20 15:18:13 +00:00
|
|
|
server="http://#{ip_prefix}.130"
|
2022-09-26 14:00:05 +00:00
|
|
|
function test-webapp() {
|
|
|
|
if timeout 3 curl --silent "$@"
|
|
|
|
then echo "Success: $*"
|
|
|
|
else echo "Cannot visit: $*"; return 1
|
|
|
|
fi
|
|
|
|
}
|
2020-08-12 05:33:24 +00:00
|
|
|
test-webapp "$server:18080"
|
2019-10-20 15:18:13 +00:00
|
|
|
! test-webapp "$server:8000"
|
|
|
|
|
2021-09-21 21:58:34 +00:00
|
|
|
test-webapp "$server:17070" # multiple networks app
|
|
|
|
! test-webapp "$server:7000" # internal multiple networks app
|
|
|
|
|
2020-08-12 05:33:24 +00:00
|
|
|
test-webapp "$server:29090"
|
2019-10-20 15:18:13 +00:00
|
|
|
! test-webapp "$server:9000"
|
|
|
|
|
2022-09-26 14:00:05 +00:00
|
|
|
test-webapp "$server:40080"
|
|
|
|
test-webapp "$server:48080"
|
|
|
|
! test-webapp "$server:47000"
|
2022-09-22 22:49:48 +00:00
|
|
|
|
2019-10-20 15:18:13 +00:00
|
|
|
echo "====================="
|
|
|
|
echo " TEST DONE "
|
|
|
|
echo "====================="
|
|
|
|
SHELL
|
|
|
|
end
|
2018-10-07 00:34:16 +00:00
|
|
|
end
|