Skip to content

Commit c3fa06e

Browse files
committed
Automating more parts of the Packet NAT setup bits
1 parent e50fada commit c3fa06e

File tree

8 files changed

+106
-45
lines changed

8 files changed

+106
-45
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
.env
66
.ruby-version
77
.terraform/
8+
/tmp/
89
assets/*.tar.bz2
910
aws-*/config/
1011
crash.log

modules/packet_network/cloud-config.yml.tpl

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ write_files:
55
- content: '${base64encode(github_users_env)}'
66
encoding: b64
77
path: /etc/default/github-users
8+
- content: '${base64encode(network_env)}'
9+
encoding: b64
10+
path: /etc/default/travis-network
811
- content: '${base64encode(file("${assets}/rsyslog/rsyslog.conf"))}'
912
encoding: b64
1013
path: /etc/rsyslog.conf

modules/packet_network/cloud-init.bash

+21-12
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ main() {
1313
__setup_travis_user
1414
__install_packages
1515
__setup_sysctl
16-
__setup_iptables
16+
__setup_networking
1717

1818
hostname >"${RUNDIR}/instance-hostname.tmpl"
1919
}
@@ -33,43 +33,52 @@ __install_packages() {
3333
}
3434

3535
__setup_sysctl() {
36-
# NOTE: we do this mostly to ensure file IO chatty services like mysql will
37-
# play nicely with others, such as when multiple containers are running mysql,
38-
# which is the default on precise + trusty. The value we set here is 16^5,
39-
# which is one power higher than the default of 16^4 :sparkles:.
4036
echo 1048576 >/proc/sys/fs/aio-max-nr
4137
sysctl -w fs.aio-max-nr=1048576
4238

4339
echo 1 >/proc/sys/net/ipv4/ip_forward
4440
sysctl -w net.ipv4.ip_forward=1
4541
}
4642

47-
__setup_iptables() {
43+
__setup_networking() {
4844
local pub_iface priv_iface elastic_ip
4945
pub_iface="$(__find_public_interface)"
5046
priv_iface="$(__find_private_interface)"
5147
elastic_ip="$(__find_elastic_ip)"
5248

53-
iptables -t nat -A POSTROUTING -o "${pub_iface}" -j SNAT --to "${elastic_ip}"
49+
if [[ -n "${elastic_ip}" ]]; then
50+
ip address add "${elastic_ip}/32" dev lo
51+
iptables -t nat -A POSTROUTING -o "${pub_iface}" -j SNAT --to "${elastic_ip}"
52+
fi
5453
iptables -t nat -A POSTROUTING -o "${pub_iface}" -j MASQUERADE
5554
iptables -A FORWARD -i "${pub_iface}" -o "${priv_iface}" \
5655
-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
5756
iptables -A FORWARD -i "${priv_iface}" -o "${pub_iface}" -j ACCEPT
5857
}
5958

6059
__find_private_interface() {
61-
# FIXME: dynamically do
62-
echo enp2s0d1
60+
local iface=enp2s0d1
61+
iface="$(ip -o addr show | grep 'inet 192' | awk '{ print $2 }')"
62+
echo "${iface:-enp2s0d1}"
6363
}
6464

6565
__find_public_interface() {
66-
# FIXME: dynamically do
67-
echo bond0
66+
local iface=bond0
67+
iface="$(ip -o addr show | grep -vE 'inet (172|127|10|192)\.' | grep -v inet6 |
68+
awk '{ print $2 }' | grep -v '^lo$' | head -n 1)"
69+
echo "${iface:-bond0}"
6870
}
6971

7072
__find_elastic_ip() {
7173
# FIXME: inject this from somewhere?
72-
echo 127.0.0.1
74+
local elastic_ip
75+
if [[ -f /etc/default/travis-network ]]; then
76+
# shellcheck source=/dev/null
77+
source /etc/default/travis-network
78+
elastic_ip="${TRAVIS_NETWORK_ELASTIC_IP}"
79+
fi
80+
81+
echo "${elastic_ip}"
7382
}
7483

7584
main "$@"

modules/packet_network/main.tf

+38-23
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ variable "nat_server_plan" {
1717
variable "project_id" {}
1818
variable "syslog_address" {}
1919

20+
resource "packet_reserved_ip_block" "ips" {
21+
project_id = "${var.project_id}"
22+
facility = "${var.facility}"
23+
quantity = 1
24+
}
25+
2026
data "template_file" "duo_config" {
2127
template = <<EOF
2228
# Written by cloud-init :heart:
@@ -28,18 +34,30 @@ failmode = secure
2834
EOF
2935
}
3036

37+
data "template_file" "network_env" {
38+
template = <<EOF
39+
export TRAVIS_NETWORK_ELASTIC_IP=${cidrhost(packet_reserved_ip_block.ips.cidr_notation, 0)}
40+
EOF
41+
}
42+
3143
data "template_file" "cloud_config" {
3244
template = "${file("${path.module}/cloud-config.yml.tpl")}"
3345

3446
vars {
3547
assets = "${path.module}/../../assets"
3648
github_users_env = "export GITHUB_USERS='${var.github_users}'"
3749
here = "${path.module}"
50+
network_env = "${data.template_file.network_env.rendered}"
3851
syslog_address = "${var.syslog_address}"
3952
duo_config = "${data.template_file.duo_config.rendered}"
4053
}
4154
}
4255

56+
resource "local_file" "user_data_dump" {
57+
filename = "${path.module}/../../tmp/packet-${var.env}-${var.index}-nat-user-data.yml"
58+
content = "${data.template_file.cloud_config.rendered}"
59+
}
60+
4361
resource "packet_device" "nat" {
4462
billing_cycle = "${var.billing_cycle}"
4563
facility = "${var.facility}"
@@ -50,37 +68,30 @@ resource "packet_device" "nat" {
5068
user_data = "${data.template_file.cloud_config.rendered}"
5169
}
5270

53-
resource "packet_reserved_ip_block" "ips" {
54-
project_id = "${var.project_id}"
55-
facility = "${var.facility}"
56-
quantity = 1
57-
}
58-
59-
resource "packet_ip_attachment" "nat" {
60-
device_id = "${packet_device.nat.id}"
61-
cidr_notation = "${packet_reserved_ip_block.ips.cidr_notation}"
62-
}
63-
64-
resource "null_resource" "nat_post_provisioning_todo" {
71+
resource "null_resource" "user_data_copy" {
6572
triggers {
66-
nat_public_ip = "${cidrhost(packet_ip_attachment.nat.cidr_notation, 0)}"
73+
user_data_sha1 = "${sha1(data.template_file.cloud_config.rendered)}"
6774
}
6875

69-
provisioner "local-exec" {
70-
command = <<EOF
71-
cat <<EOCAT
72-
TODO: finish configuring the nat with something like
76+
depends_on = ["packet_device.nat", "local_file.user_data_dump"]
7377

74-
ip addr add ${cidrhost(packet_ip_attachment.nat.cidr_notation, 0)} dev bond0
75-
ip route delete default
76-
ip route add default via ${cidrhost(packet_ip_attachment.nat.cidr_notation, 0)} dev bond0
77-
curl icanhazip.com # <=== should be ${cidrhost(packet_ip_attachment.nat.cidr_notation, 0)}
78+
provisioner "file" {
79+
source = "${local_file.user_data_dump.filename}"
80+
destination = "/var/tmp/user-data.yml"
81+
}
7882

79-
EOCAT
80-
EOF
83+
connection {
84+
type = "ssh"
85+
user = "root"
86+
host = "${packet_device.nat.access_public_ipv4}"
8187
}
8288
}
8389

90+
resource "packet_ip_attachment" "nat" {
91+
device_id = "${packet_device.nat.id}"
92+
cidr_notation = "${packet_reserved_ip_block.ips.cidr_notation}"
93+
}
94+
8495
output "nat_ip" {
8596
value = "${packet_device.nat.access_private_ipv4}"
8697
}
@@ -89,6 +100,10 @@ output "nat_public_ip" {
89100
value = "${cidrhost(packet_ip_attachment.nat.cidr_notation, 0)}"
90101
}
91102

103+
output "nat_maint_ip" {
104+
value = "${packet_device.nat.access_public_ipv4}"
105+
}
106+
92107
output "facility" {
93108
value = "${var.facility}"
94109
}

modules/packet_worker/cloud-init.bash

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ main() {
4242
fi
4343

4444
if [[ "${TRAVIS_NETWORK_NAT_IP}" ]]; then
45-
# FIXME: halp
46-
: something with iptables maybe
45+
ip route del default
46+
ip route add default via "${TRAVIS_NETWORK_NAT_IP}"
4747
fi
4848

4949
__wait_for_docker

modules/packet_worker/main.tf

+29
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
variable "bastion_ip" {}
12
variable "billing_cycle" {
23
default = "hourly"
34
}
@@ -13,6 +14,7 @@ variable "index" {}
1314

1415
variable "project_id" {}
1516
variable "nat_ip" {}
17+
variable "nat_public_ip" {}
1618
variable "server_count" {}
1719

1820
variable "server_plan" {
@@ -59,6 +61,7 @@ EOF
5961
data "template_file" "network_env" {
6062
template = <<EOF
6163
export TRAVIS_NETWORK_NAT_IP=${var.nat_ip}
64+
export TRAVIS_NETWORK_ELASTIC_IP=${var.nat_public_ip}
6265
EOF
6366
}
6467

@@ -98,6 +101,11 @@ data "template_file" "cloud_config" {
98101
}
99102
}
100103

104+
resource "local_file" "user_data_dump" {
105+
filename = "${path.module}/../../tmp/packet-${var.env}-${var.index}-worker-user-data.yml"
106+
content = "${data.template_file.cloud_config.rendered}"
107+
}
108+
101109
resource "packet_device" "worker" {
102110
count = "${var.server_count}"
103111
billing_cycle = "${var.billing_cycle}"
@@ -108,3 +116,24 @@ resource "packet_device" "worker" {
108116
project_id = "${var.project_id}"
109117
user_data = "${data.template_file.cloud_config.rendered}"
110118
}
119+
120+
resource "null_resource" "user_data_copy" {
121+
triggers {
122+
user_data_sha1 = "${sha1(data.template_file.cloud_config.rendered)}"
123+
}
124+
125+
depends_on = ["packet_device.worker", "local_file.user_data_dump"]
126+
127+
provisioner "file" {
128+
source = "${local_file.user_data_dump.filename}"
129+
destination = "/var/tmp/user-data.yml"
130+
}
131+
132+
connection {
133+
type = "ssh"
134+
user = "root"
135+
host = "${packet_device.worker.access_private_ipv4}"
136+
bastion_host = "${var.bastion_ip}"
137+
bastion_user = "root"
138+
}
139+
}

packet-staging-1/main.tf

+12-8
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,17 @@ EOF
8181

8282
module "packet_workers_com" {
8383
source = "../modules/packet_worker"
84+
bastion_ip = "${module.packet_network_sjc1.nat_maint_ip}"
8485
env = "${var.env}"
8586
facility = "${module.packet_network_sjc1.facility}"
8687
github_users = "${var.github_users}"
8788
index = "${var.index}"
8889
nat_ip = "${module.packet_network_sjc1.nat_ip}"
90+
nat_public_ip = "${module.packet_network_sjc1.nat_public_ip}"
91+
project_id = "${var.project_id}"
92+
server_count = 1
93+
site = "com"
94+
syslog_address = "${var.syslog_address_com}"
8995
worker_config = "${data.template_file.worker_config_com.rendered}"
9096
worker_docker_image_android = "${var.latest_docker_image_amethyst}"
9197
worker_docker_image_default = "${var.latest_docker_image_garnet}"
@@ -99,19 +105,21 @@ module "packet_workers_com" {
99105
worker_docker_image_python = "${var.latest_docker_image_garnet}"
100106
worker_docker_image_ruby = "${var.latest_docker_image_garnet}"
101107
worker_docker_self_image = "${var.latest_docker_image_worker}"
102-
server_count = 1
103-
syslog_address = "${var.syslog_address_com}"
104-
site = "com"
105-
project_id = "${var.project_id}"
106108
}
107109

108110
module "packet_workers_org" {
109111
source = "../modules/packet_worker"
112+
bastion_ip = "${module.packet_network_sjc1.nat_maint_ip}"
110113
env = "${var.env}"
111114
facility = "${module.packet_network_sjc1.facility}"
112115
github_users = "${var.github_users}"
113116
index = "${var.index}"
114117
nat_ip = "${module.packet_network_sjc1.nat_ip}"
118+
nat_public_ip = "${module.packet_network_sjc1.nat_public_ip}"
119+
project_id = "${var.project_id}"
120+
server_count = 1
121+
site = "org"
122+
syslog_address = "${var.syslog_address_org}"
115123
worker_config = "${data.template_file.worker_config_org.rendered}"
116124
worker_docker_image_android = "${var.latest_docker_image_amethyst}"
117125
worker_docker_image_default = "${var.latest_docker_image_garnet}"
@@ -125,10 +133,6 @@ module "packet_workers_org" {
125133
worker_docker_image_python = "${var.latest_docker_image_garnet}"
126134
worker_docker_image_ruby = "${var.latest_docker_image_garnet}"
127135
worker_docker_self_image = "${var.latest_docker_image_worker}"
128-
server_count = 1
129-
syslog_address = "${var.syslog_address_org}"
130-
site = "org"
131-
project_id = "${var.project_id}"
132136
}
133137

134138
resource "aws_route53_record" "nat" {

tmp/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)