You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2019/01/14 16:26:53 UTC

[trafficcontrol] Diff for: [GitHub] rob05c closed pull request #3184: WIP Add CDN-in-a-Box Dynamic IPs

diff --git a/infrastructure/cdn-in-a-box/cache/Dockerfile b/infrastructure/cdn-in-a-box/cache/Dockerfile
index 01ae708aa..c75efcb9f 100644
--- a/infrastructure/cdn-in-a-box/cache/Dockerfile
+++ b/infrastructure/cdn-in-a-box/cache/Dockerfile
@@ -28,7 +28,7 @@ EXPOSE 80
 ADD https://ci.trafficserver.apache.org/RPMS/CentOS7/trafficserver-7.1.4-2.el7.x86_64.rpm /trafficserver.rpm
 ADD https://ci.trafficserver.apache.org/RPMS/CentOS7/trafficserver-devel-7.1.4-2.el7.x86_64.rpm /trafficserver-devel.rpm
 
-RUN yum install -y kyotocabinet-libs epel-release initscripts iproute net-tools nmap-ncat gettext autoconf automake libtool gcc-c++ cronie glibc-devel openssl-devel
+RUN yum install -y bind-utils kyotocabinet-libs epel-release initscripts iproute net-tools nmap-ncat gettext autoconf automake libtool gcc-c++ cronie glibc-devel openssl-devel
 RUN yum install -y /trafficserver.rpm /trafficserver-devel.rpm jq python34-psutil python34-typing python34-setuptools python34-pip && yum clean all
 RUN pip3 install --upgrade pip && pip3 install requests urllib3 distro
 
diff --git a/infrastructure/cdn-in-a-box/dns/container-resolv.conf b/infrastructure/cdn-in-a-box/dns/container-resolv.conf
deleted file mode 100644
index 2c33b328f..000000000
--- a/infrastructure/cdn-in-a-box/dns/container-resolv.conf
+++ /dev/null
@@ -1,19 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License.  You may obtain a copy of the License at
-#
-#   http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied.  See the License for the
-# specific language governing permissions and limitations
-# under the License.
-domain ciab.test
-search infra.ciab.test ciab.test
-nameserver 172.16.239.254
diff --git a/infrastructure/cdn-in-a-box/dns/entrypoint.sh b/infrastructure/cdn-in-a-box/dns/entrypoint.sh
index 804c8a10c..baed610da 100644
--- a/infrastructure/cdn-in-a-box/dns/entrypoint.sh
+++ b/infrastructure/cdn-in-a-box/dns/entrypoint.sh
@@ -50,9 +50,19 @@ create_bind_cache_dir() {
   chown root:${BIND_USER} /var/cache/bind
 }
 
+create_zonefile_cron() {
+	# this is a cron job, rather than only running once, so containers added to the Docker Network later will be picked up and inserted.
+	# This facilitates running cdn-in-a-box as independent 'docker run' containers, rather than docker-compose.
+	printf "* * * * * root /usr/local/sbin/set-zonefile.sh\n" > /etc/cron.d/set-zonefile
+	chmod 0600 /etc/cron.d/set-zonefile
+}
+
 create_pid_dir
 create_bind_data_dir
 create_bind_cache_dir
+create_zonefile_cron
+
+nohup cron -f > cron.out & # The Docker container doesn't run cron by default
 
 # allow arguments to be passed to named
 if [[ ${1:0:1} = '-' ]]; then
diff --git a/infrastructure/cdn-in-a-box/dns/set-dns.sh b/infrastructure/cdn-in-a-box/dns/set-dns.sh
new file mode 100755
index 000000000..1fcd92c97
--- /dev/null
+++ b/infrastructure/cdn-in-a-box/dns/set-dns.sh
@@ -0,0 +1,21 @@
+#!/usr/bin/env bash
+
+dns_container_hostname='dns'
+
+dns_domain='ciab.test'
+dns_search_domains='infra.ciab.test ciab.test'
+
+dnsip="$(dig +short ${dns_container_hostname})"
+
+while [ -z $dnsip ]; do
+	printf "Waiting for dns container \"${dns_container_hostname}\" to appear in Docker default dns...\n"
+	sleep 1
+	dnsip="$(dig +short ${dns_container_hostname})"
+done
+
+cat << EOF > /etc/resolv.conf
+# autogenerated by set-dns.sh
+domain ${dns_domain}
+search ${dns_search_domains}
+nameserver ${dnsip}
+EOF
diff --git a/infrastructure/cdn-in-a-box/dns/set-zonefile.sh b/infrastructure/cdn-in-a-box/dns/set-zonefile.sh
new file mode 100755
index 000000000..f9b2dd29e
--- /dev/null
+++ b/infrastructure/cdn-in-a-box/dns/set-zonefile.sh
@@ -0,0 +1,76 @@
+#!/usr/bin/env bash
+
+# TODO fix to work with docker-compose up --scale
+# TODO fix duplication with docker-compose.yml
+domain='infra.ciab.test'
+
+# service_names must match the docker-compose service name, or hostname.
+service_names='db trafficops trafficops-perl trafficportal trafficmonitor trafficvault trafficrouter edge mid origin enroller socksproxy client vnc dns'
+
+bind_zone_dir='/data/bind/etc/'
+bind_zone_file='zone.ciab.test'
+
+bind_zone_file_path="${bind_zone_dir}/${bind_zone_file}"
+
+origin="${domain}."
+origin_line="\$ORIGIN ${origin}"
+
+changed_zone_file="false"
+
+function add_zone_entry {
+	host="$1"
+	ip="$2"
+	record="$3"
+	entry="${host}                IN ${record}    ${ip}"
+
+	grep -E "^${host}\s+IN\s+${record}\s+${ip}" $bind_zone_file_path > /dev/null
+	if [ $? -eq 0 ]; then
+		: # echo "host \"$host\" ip \"$ip\" record \"$record\" already in zonefile"
+	else
+		echo "host \"$host\" ip \"$ip\" record \"$record\" not in zonefile, inserting"
+		sed -i "s/${origin_line}/${origin_line}\n\n${entry}/" ${bind_zone_file_path}
+		changed_zone_file="true"
+	fi
+}
+
+gateway_ip="$(ip route | grep default | cut -d' ' -f3)"
+gateway_ip6="$(ip -6 route | grep default | cut -d' ' -f3)"
+
+if [ -z $gateway_ip ]; then
+	echo "service gateway ip not found yet"
+else
+	add_zone_entry gw $gateway_ip A
+fi
+
+if [ -z $gateway_ip6 ]; then
+	echo "service gateway ip6 not found yet"
+else
+	add_zone_entry gw $gateway_ip6 AAAA
+fi
+
+for service_name in $service_names; do
+	service_ip="$(dig +short $service_name A)"
+	if [ -z $service_ip ]; then
+		echo "service $service_name ip not found yet"
+	else
+		add_zone_entry $service_name $service_ip A
+	fi
+
+	service_ip6="$(dig +short $service_name AAAA)"
+	if [ -z $service_ip6 ]; then
+		echo "service $service_name ip6 not found yet"
+	else
+		add_zone_entry $service_name $service_ip6 AAAA
+	fi
+done
+
+if [ "$changed_zone_file" = "true" ] ; then
+	/usr/sbin/rndc reload 2>&1 >> /rndc.log
+	rndc_exit=$?
+	while [ $rndc_exit -ne 0 ]; do
+		printf "bind reload exit $rndc_exit failed, trying again in 2s\n" >> /rndc.log
+		sleep 2
+		/usr/sbin/rndc reload 2>&1 >> /rndc.log
+		rndc_exit=$?
+	done
+fi
diff --git a/infrastructure/cdn-in-a-box/dns/zone.ciab.test b/infrastructure/cdn-in-a-box/dns/zone.ciab.test
index 3cf524dae..c0f409d4b 100644
--- a/infrastructure/cdn-in-a-box/dns/zone.ciab.test
+++ b/infrastructure/cdn-in-a-box/dns/zone.ciab.test
@@ -28,53 +28,8 @@ $TTL 30
 
 $ORIGIN infra.ciab.test.
 
-gw                IN A    172.16.239.1
-gw								IN AAAA fc01:9400:1000:8::1
-
-db                IN A    172.16.239.10
-db								IN AAAA fc01:9400:1000:8::10
-
-trafficops        IN A    172.16.239.20
-trafficops        IN AAAA fc01:9400:1000:8::20
-
-trafficops-perl   IN A    172.16.239.21
-trafficops-perl   IN AAAA fc01:9400:1000:8::21
-
-trafficportal     IN A    172.16.239.30
-trafficportal     IN AAAA fc01:9400:1000:8::30
-
-trafficmonitor    IN A    172.16.239.40
-trafficmonitor    IN AAAA fc01:9400:1000:8::40
-
-trafficvault      IN A    172.16.239.50
-trafficvault      IN AAAA fc01:9400:1000:8::50
-
-trafficrouter     IN A    172.16.239.60
-trafficrouter     IN AAAA fc01:9400:1000:8::60
-
-edge              IN A    172.16.239.100
-edge              IN AAAA fc01:9400:1000:8::100
-
-mid               IN A    172.16.239.120
-mid               IN AAAA fc01:9400:1000:8::120
-
-origin            IN A    172.16.239.140
-origin            IN AAAA fc01:9400:1000:8::140
-
-enroller          IN A    172.16.239.200
-enroller          IN AAAA fc01:9400:1000:8::200
-
-socksproxy        IN A    172.16.239.233
-socksproxy        IN AAAA fc01:9400:1000:8::233
-
-client            IN A    172.16.239.250
-client            IN AAAA fc01:9400:1000:8::250
-
-vnc               IN A    172.16.239.251
-vnc               IN AAAA fc01:9400:1000:8::251
-
-dns               IN A    172.16.239.254
-dns               IN AAAA fc01:9400:1000:8::254
+; Hosts should be dynamically inserted here by /usr/local/bin/set-zonefile.sh via /etc/cron.d/set-zonefile.
+; If this comment is all that's under ORIGIN infra.ciab.test, there is likely a problem with set-zonefile.sh.
 
 $ORIGIN mycdn.ciab.test.
 @                 NS   trafficrouter.infra.ciab.test.
diff --git a/infrastructure/cdn-in-a-box/docker-compose.yml b/infrastructure/cdn-in-a-box/docker-compose.yml
index dc567cea7..14556100f 100644
--- a/infrastructure/cdn-in-a-box/docker-compose.yml
+++ b/infrastructure/cdn-in-a-box/docker-compose.yml
@@ -59,7 +59,6 @@ services:
         ipv4_address: 172.16.239.10
         ipv6_address: "fc01:9400:1000:8::10"
     volumes:
-      - ./dns/container-resolv.conf:/etc/resolv.conf
       - /var/lib/postgresql/data
       - shared:/shared
     env_file:
@@ -91,7 +90,7 @@ services:
     ports:
       - "6443:443"
     volumes:
-      - ./dns/container-resolv.conf:/etc/resolv.conf
+      - ./dns/set-dns.sh:/usr/local/sbin/set-dns.sh
       - shared:/shared
 
   # trafficops-perl runs the legacy Traffic Ops in Perl using the Mojolicious framework.  This remains
@@ -119,7 +118,7 @@ services:
       - "60443:443"
     volumes:
       - ./traffic_ops/ca:/ca
-      - ./dns/container-resolv.conf:/etc/resolv.conf
+      - ./dns/set-dns.sh:/usr/local/sbin/set-dns.sh
       - shared:/shared
 
   # trafficportal defines the web interface for Traffic Ops.  It uses only the API exposed by Traffic Ops
@@ -144,7 +143,7 @@ services:
     ports:
       - "443:443"
     volumes:
-      - ./dns/container-resolv.conf:/etc/resolv.conf
+      - ./dns/set-dns.sh:/usr/local/sbin/set-dns.sh
       - shared:/shared
 
   # trafficmonitor is an HTTP service that monitors the caches in a CDN for a variety of metrics
@@ -157,8 +156,8 @@ services:
     depends_on:
       - enroller
     volumes:
+      - ./dns/set-dns.sh:/usr/local/sbin/set-dns.sh
       - shared:/shared
-      - ./dns/container-resolv.conf:/etc/resolv.conf
     domainname: infra.ciab.test
     env_file:
       - variables.env
@@ -195,7 +194,7 @@ services:
       - "3443:443"
       - "3333:3333"
     volumes:
-      - ./dns/container-resolv.conf:/etc/resolv.conf
+      - ./dns/set-dns.sh:/usr/local/sbin/set-dns.sh
       - shared:/shared
 
   # trafficvault runs a riak container to store private keys
@@ -213,7 +212,7 @@ services:
     labels:
       - "com.basho.riak.cluster.name=trafficvault"
     volumes:
-      - ./dns/container-resolv.conf:/etc/resolv.conf
+      - ./dns/set-dns.sh:/usr/local/sbin/set-dns.sh
       - schemas:/etc/riak/schemas
       - shared:/shared
     domainname: infra.ciab.test
@@ -253,7 +252,7 @@ services:
     ports:
       - "9000:80"
     volumes:
-      - ./dns/container-resolv.conf:/etc/resolv.conf
+      - ./dns/set-dns.sh:/usr/local/sbin/set-dns.sh
       - shared:/shared
 
   # mid cache
@@ -276,7 +275,7 @@ services:
     ports:
       - "9100:80"
     volumes:
-      - ./dns/container-resolv.conf:/etc/resolv.conf
+      - ./dns/set-dns.sh:/usr/local/sbin/set-dns.sh
       - shared:/shared
 
   # origin provides the content to be distributed through the CDN caches
@@ -297,7 +296,7 @@ services:
     ports:
       - "9200:80"
     volumes:
-      - ./dns/container-resolv.conf:/etc/resolv.conf
+      - ./dns/set-dns.sh:/usr/local/sbin/set-dns.sh
       - shared:/shared
       - ./origin/content:/var/www/html
 
@@ -317,7 +316,7 @@ services:
         ipv4_address: 172.16.239.200
         ipv6_address: "fc01:9400:1000:8::200"
     volumes:
-      - ./dns/container-resolv.conf:/etc/resolv.conf
+      - ./dns/set-dns.sh:/usr/local/sbin/set-dns.sh
       - shared:/shared
 
   # Bind9 DNS services work in combination with the traffic router to route clients to the optimal cache
@@ -328,6 +327,7 @@ services:
     env_file:
       - variables.env
     volumes:
+      - ./dns/set-zonefile.sh:/usr/local/sbin/set-zonefile.sh
       - shared:/shared
     hostname: dns
     domainname: infra.ciab.test
diff --git a/infrastructure/cdn-in-a-box/edge/run.sh b/infrastructure/cdn-in-a-box/edge/run.sh
index affb597ff..a6d36eab8 100755
--- a/infrastructure/cdn-in-a-box/edge/run.sh
+++ b/infrastructure/cdn-in-a-box/edge/run.sh
@@ -21,6 +21,8 @@ set -e
 set -x
 set -m
 
+set-dns.sh
+
 source /to-access.sh
 
 # Wait on SSL certificate generation
diff --git a/infrastructure/cdn-in-a-box/enroller/run.sh b/infrastructure/cdn-in-a-box/enroller/run.sh
index 3f0bbef25..ccfa676a5 100755
--- a/infrastructure/cdn-in-a-box/enroller/run.sh
+++ b/infrastructure/cdn-in-a-box/enroller/run.sh
@@ -21,6 +21,8 @@
 set -x
 . /to-access.sh
 
+set-dns.sh
+
 export TO_URL=https://$TO_FQDN:$TO_PORT
 export TO_USER=$TO_ADMIN_USER
 export TO_PASSWORD=$TO_ADMIN_PASSWORD
diff --git a/infrastructure/cdn-in-a-box/mid/run.sh b/infrastructure/cdn-in-a-box/mid/run.sh
index b707bc9f7..9e9645e6d 100755
--- a/infrastructure/cdn-in-a-box/mid/run.sh
+++ b/infrastructure/cdn-in-a-box/mid/run.sh
@@ -21,6 +21,8 @@ set -e
 set -x
 set -m
 
+set-dns.sh
+
 source /to-access.sh
 
 # Wait on SSL certificate generation
diff --git a/infrastructure/cdn-in-a-box/origin/Dockerfile b/infrastructure/cdn-in-a-box/origin/Dockerfile
index f54a73b7a..60c29d23c 100644
--- a/infrastructure/cdn-in-a-box/origin/Dockerfile
+++ b/infrastructure/cdn-in-a-box/origin/Dockerfile
@@ -23,7 +23,7 @@
 
 FROM alpine:latest
 
-RUN apk add --no-cache lighttpd bash curl
+RUN apk add --no-cache lighttpd bash curl bind-tools
 
 RUN rm /etc/lighttpd/lighttpd.conf
 RUN rm -rf /var/www/localhost/
diff --git a/infrastructure/cdn-in-a-box/origin/run.sh b/infrastructure/cdn-in-a-box/origin/run.sh
index c7e1566c7..79f5d1db2 100755
--- a/infrastructure/cdn-in-a-box/origin/run.sh
+++ b/infrastructure/cdn-in-a-box/origin/run.sh
@@ -21,6 +21,8 @@ set -e
 set -x
 set -m
 
+set-dns.sh
+
 source /to-access.sh
 
 # Wait on SSL certificate generation
diff --git a/infrastructure/cdn-in-a-box/traffic_monitor/run.sh b/infrastructure/cdn-in-a-box/traffic_monitor/run.sh
index 5029eec95..0720fffda 100755
--- a/infrastructure/cdn-in-a-box/traffic_monitor/run.sh
+++ b/infrastructure/cdn-in-a-box/traffic_monitor/run.sh
@@ -38,6 +38,8 @@ do
 	if [[ -z $$v ]]; then echo "$v is unset"; exit 1; fi
 done
 
+set-dns.sh
+
 source /to-access.sh
 
 # Wait on SSL certificate generation
diff --git a/infrastructure/cdn-in-a-box/traffic_ops/run-go.sh b/infrastructure/cdn-in-a-box/traffic_ops/run-go.sh
index 5914cebff..97564914b 100755
--- a/infrastructure/cdn-in-a-box/traffic_ops/run-go.sh
+++ b/infrastructure/cdn-in-a-box/traffic_ops/run-go.sh
@@ -42,6 +42,8 @@ do
 	if [[ -z $$v ]]; then echo "$v is unset"; exit 1; fi
 done
 
+set-dns.sh
+
 # Source to-access functions and FQDN vars
 source /to-access.sh
 
diff --git a/infrastructure/cdn-in-a-box/traffic_ops/run.sh b/infrastructure/cdn-in-a-box/traffic_ops/run.sh
index f1b133fe2..60927b8a0 100755
--- a/infrastructure/cdn-in-a-box/traffic_ops/run.sh
+++ b/infrastructure/cdn-in-a-box/traffic_ops/run.sh
@@ -39,6 +39,8 @@ do
 	if [[ -z $$v ]]; then echo "$v is unset"; exit 1; fi
 done
 
+set-dns.sh
+
 # Source to-access functions and FQDN vars
 source /to-access.sh
 
diff --git a/infrastructure/cdn-in-a-box/traffic_portal/run.sh b/infrastructure/cdn-in-a-box/traffic_portal/run.sh
index 453225c5b..c8056e0c9 100755
--- a/infrastructure/cdn-in-a-box/traffic_portal/run.sh
+++ b/infrastructure/cdn-in-a-box/traffic_portal/run.sh
@@ -27,6 +27,8 @@ LOGFILE="/var/log/traffic_portal/traffic_portal.log"
 MIN_UPTIME="5000"
 SPIN_SLEEP_TIME="2000"
 
+set-dns.sh
+
 source /to-access.sh
 
 # Wait on SSL certificate generation
diff --git a/infrastructure/cdn-in-a-box/traffic_router/run.sh b/infrastructure/cdn-in-a-box/traffic_router/run.sh
index e8112ac01..d7de14b52 100755
--- a/infrastructure/cdn-in-a-box/traffic_router/run.sh
+++ b/infrastructure/cdn-in-a-box/traffic_router/run.sh
@@ -17,6 +17,8 @@
 # under the License.
 NAME="Traffic Router Application"
 
+set-dns.sh
+
 # Global Vars for FQDNs, ports, etc
 source /to-access.sh
 
diff --git a/infrastructure/cdn-in-a-box/traffic_vault/run.sh b/infrastructure/cdn-in-a-box/traffic_vault/run.sh
index ed40e6b4c..84f0d4107 100755
--- a/infrastructure/cdn-in-a-box/traffic_vault/run.sh
+++ b/infrastructure/cdn-in-a-box/traffic_vault/run.sh
@@ -16,6 +16,8 @@
 # specific language governing permissions and limitations
 # under the License.
 
+set-dns.sh
+
 . /to-access.sh
 
 TO_URL=https://${TO_FQDN}:${TO_PORT}


With regards,
Apache Git Services