Skip to content

Commit e4f9b8c

Browse files
committed
Squashing previous PR commit because I forgot to set the repo to do that
1 parent 1719613 commit e4f9b8c

12 files changed

Lines changed: 219 additions & 38 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.vagrant
2+
.DS_Store

.gitmodules

Whitespace-only changes.

linux/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

linux/instructions/step_4.md

Whitespace-only changes.

linux/scripts/init-db.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ if [[ "$(id -u)" -ne 0 ]]; then
66
exit 1
77
fi
88

9+
# Disable unattended-upgrades (if it exists) because that shit is ANNOYING
10+
systemctl stop unattended-upgrades.service || true
11+
systemctl disable unattended-upgrades.service || true
12+
apt-get remove --purge -y unattended-upgrades || true
13+
914
# Enable SSH password access
1015
sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
1116
systemctl restart sshd

linux/scripts/init.sh

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ if grep -v -q "${team_name}" /etc/hosts ; then
2222
printf '\n 127.0.0.1 %s\n' "${team_name}" >> /etc/hosts
2323
fi
2424

25+
# Disable unattended-upgrades (if it exists) because that shit is ANNOYING
26+
systemctl stop unattended-upgrades.service || true
27+
systemctl disable unattended-upgrades.service || true
28+
apt-get remove --purge -y unattended-upgrades || true
29+
2530
# Enable SSH password access
2631
sed -i -E 's/.*PasswordAuthentication.*no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
2732
systemctl restart sshd
@@ -85,12 +90,8 @@ printf 'All done!\n'
8590
## TODO: ideas for other scorable steps for teams:
8691

8792
# Simulate a git repo's history a la:
88-
# cd /opt/app
89-
# git init
90-
# git remote add origin FAKE
91-
# <do some thing to change local code>
92-
# git add .
93-
# git commit -m "WIP"
93+
# (at time of writing, this was on the branch 'feature/add-git-scoring-step')
94+
9495
# ...
9596

9697
# mess up the current branch (maybe it was a feature branch that got yeeted)?

linux/scripts/provision-ec2.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
outputs_file='/tmp/outputs.json'
5+
6+
cd "$(dirname $0)"
7+
8+
printf '>>> Getting Terraform outputs...\n'
9+
(cd ../terraform && terraform output -json) > "${outputs_file}"
10+
11+
printf '>>> Determining IP addresses of DB server...\n'
12+
db_pub_ip="$(jq -rc '.db_pub_ip.value' ${outputs_file})"
13+
db_priv_ip="$(jq -rc '.db_priv_ip.value' ${outputs_file})"
14+
printf '>>> DB IPs: Public %s, Private %s\n' "${db_pub_ip}" "${db_priv_ip}"
15+
16+
printf '>>> Determining IP addresses of Team servers...\n'
17+
num_teams="$(jq '[.instance_ips.value[]] | length' ${outputs_file})"
18+
team_server_ips="$(jq -c '[.instance_ips.value[]]' ${outputs_file})"
19+
printf '>>> %s teams, with IPs of: %s\n' "${num_teams}" "${team_server_ips}"
20+
21+
printf '>>> Adding DB server init script...\n'
22+
scp -o StrictHostKeyChecking=accept-new -r ../scripts admin@"${db_pub_ip}":/tmp
23+
printf '>>> Running DB server init script...\n'
24+
ssh admin@"${db_pub_ip}" 'sudo bash /tmp/scripts/init-db.sh'
25+
26+
for server_num in $(seq 1 "${num_teams}") ; do
27+
server_index=$((server_num - 1))
28+
server_ip=$(echo "${team_server_ips}" | jq -rc ".[${server_index}]")
29+
printf '>>> Team %s IP is %s\n' "${server_num}" "${server_ip}"
30+
31+
printf '>>> Adding files to Team server %s at %s...\n' "${server_num}" "${server_ip}"
32+
scp -r -o StrictHostKeyChecking=accept-new ../scripts ../services ../instructions ../dummy-app-src admin@"${server_ip}":/tmp
33+
34+
printf '>>> Running init on Team server %s at %s...\n' "${server_num}" "${server_ip}"
35+
ssh admin@"${server_ip}" "export team_name=Team-${server_num} && export db_addr=${db_priv_ip} && sudo -E bash /tmp/scripts/init.sh"
36+
37+
printf '>>> Running tests on Team server %s at %s...\n' "${server_num}" "${server_ip}"
38+
ssh admin@"${server_ip}" "sudo -E bats /.ws/scripts/test.bats"
39+
40+
printf '>>> Done with Team server %s at %s\n' "${server_num}" "${server_ip}"
41+
done

linux/terraform/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.terraform/
22
*.tfvars
3+
*tfstate*

linux/terraform/.terraform.lock.hcl

Lines changed: 38 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

linux/terraform/main.tf

Lines changed: 83 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,111 @@ provider "aws" {
55
}
66

77
locals {
8-
db_ip = "10.0.1.10"
9-
region = var.aws_region
10-
8+
db_ip = "10.0.1.10"
9+
region = var.aws_region
10+
name = "${var.event_name}-osc-workshop-linux"
11+
my_cidr = "${chomp(data.http.my_ip.response_body)}/32"
1112
tags = {
12-
13+
event_name = var.event_name
1314
}
1415
}
1516

1617
module "vpc" {
1718
source = "terraform-aws-modules/vpc/aws"
1819

19-
name = "osc-workshop-linux"
20+
name = local.name
2021
cidr = "10.0.0.0/16"
2122

22-
azs = ["${local.region}a"]
23+
azs = [data.aws_availability_zones.available.names[0]]
2324
public_subnets = ["10.0.1.0/24"]
2425

2526
enable_nat_gateway = false
2627

28+
manage_default_network_acl = false
29+
manage_default_route_table = false
30+
manage_default_security_group = false
31+
manage_default_vpc = false
32+
33+
tags = local.tags
34+
}
35+
36+
module "security_group" {
37+
source = "terraform-aws-modules/security-group/aws"
38+
version = "~> 4.0"
39+
40+
name = local.name
41+
description = "Security group for local IP ssh"
42+
vpc_id = module.vpc.vpc_id
43+
44+
ingress_cidr_blocks = [local.my_cidr]
45+
ingress_rules = ["ssh-tcp", "all-icmp"]
46+
egress_rules = ["all-all"]
47+
48+
ingress_with_cidr_blocks = concat(
49+
[
50+
{
51+
from_port = 8080
52+
to_port = 8080
53+
protocol = "tcp"
54+
description = "Score server dashboard"
55+
cidr_blocks = local.my_cidr
56+
},
57+
{
58+
from_port = 5432
59+
to_port = 5432
60+
protocol = "tcp"
61+
description = "Score DB for team servers"
62+
cidr_blocks = module.vpc.vpc_cidr_block
63+
},
64+
{
65+
from_port = 5432
66+
to_port = 5432
67+
protocol = "tcp"
68+
description = "Score DB for deployer"
69+
cidr_blocks = local.my_cidr
70+
}
71+
],
72+
var.custom_security_group_ingress
73+
)
74+
2775
tags = local.tags
2876
}
2977

3078
module "db" {
3179
source = "terraform-aws-modules/ec2-instance/aws"
32-
version = "~> 3.0"
80+
version = "~> 4.0"
81+
82+
name = "${local.name}-db"
83+
84+
ami = data.aws_ami.latest.id
85+
instance_type = "t2.micro"
86+
key_name = aws_key_pair.main.key_name
87+
vpc_security_group_ids = [module.security_group.security_group_id]
88+
subnet_id = module.vpc.public_subnets[0]
89+
associate_public_ip_address = true
3390

34-
subnet_id = module.vpc.public_subnets[0].subnet_id
35-
private_ip = local.db_ip
91+
tags = local.tags
3692
}
3793

38-
module "team_server" {
94+
module "team_servers" {
3995
source = "terraform-aws-modules/ec2-instance/aws"
40-
version = "~> 3.0"
96+
version = "~> 4.0"
97+
98+
count = var.num_teams
4199

42-
for_each = toset(["1", "2"])
100+
name = "${local.name}-team-${count.index + 1}"
101+
102+
ami = data.aws_ami.latest.id
103+
instance_type = "t2.micro"
104+
key_name = aws_key_pair.main.key_name
105+
vpc_security_group_ids = [module.security_group.security_group_id]
106+
subnet_id = module.vpc.public_subnets[0]
107+
associate_public_ip_address = true
108+
109+
tags = local.tags
110+
}
43111

44-
subnet_id = module.vpc.public_subnets[0].subnet_id
112+
resource "aws_key_pair" "main" {
113+
key_name = local.name
114+
public_key = file(pathexpand(var.ssh_local_key_path))
45115
}

0 commit comments

Comments
 (0)