Skip to content

Commit 8fb9223

Browse files
committed
Generate code from book
1 parent 2f68573 commit 8fb9223

File tree

107 files changed

+1945
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+1945
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
# A script that configures a web server
3+
4+
set -e
5+
6+
# Update the apt-get cache
7+
sudo apt-get update
8+
9+
# Install PHP
10+
sudo apt-get install -y php
11+
12+
# Install Apache
13+
sudo apt-get install -y apache2
14+
15+
# Copy the code from repository
16+
sudo git clone https://github.com/brikis98/php-app.git /var/www/html/app
17+
18+
# Start Apache
19+
sudo service apache2 start
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
echo "Hello, World" > index.html
3+
nohup busybox httpd -f -p 8080 &
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
export db_address=12.34.56.78
4+
export db_port=5555
5+
export server_port=8888
6+
7+
./user-data.sh
8+
9+
output=$(curl "http://localhost:$server_port")
10+
11+
if [[ $output != *"Hello, World"* ]]; then
12+
echo "Did not get back expected text 'Hello, World'"
13+
fi
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
cat > index.html <<EOF
4+
<h1>Hello, World</h1>
5+
<p>DB address: ${db_address}</p>
6+
<p>DB port: ${db_port}</p>
7+
EOF
8+
9+
nohup busybox httpd -f -p "${server_port}" &
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"builders": [{
3+
"ami_name": "packer-example",
4+
"instance_type": "t2.micro",
5+
"region": "us-east-1",
6+
"type": "amazon-ebs",
7+
"source_ami": "ami-40d28157",
8+
"ssh_username": "ubuntu"
9+
}],
10+
"provisioners": [{
11+
"type": "shell",
12+
"inline": [
13+
"sudo apt-get update",
14+
"sudo apt-get install -y php",
15+
"sudo apt-get install -y apache2",
16+
"sudo git clone https://github.com/brikis98/php-app.git /var/www/html/app"
17+
]
18+
}]
19+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require 'net/http'
2+
3+
if ARGV.length != 3
4+
raise 'Invalid args. Usage: terraform-test.rb REGION DB_BUCKET DB_KEY'
5+
end
6+
7+
vars = {
8+
# A unique (ish) 6-char string: http://stackoverflow.com/a/88341/483528
9+
:cluster_name => (0...6).map { (65 + rand(26)).chr }.join,
10+
:aws_region => ARGV[0],
11+
:db_remote_state_bucket => ARGV[1],
12+
:db_remote_state_key => ARGV[2],
13+
}
14+
vars_string = vars.map{|key, value| "-var '#{key} = \"#{value}\"'"}.join(', ')
15+
16+
begin
17+
puts "Deploying code in #{Dir.pwd}"
18+
puts `terraform get 2>&1`
19+
puts `terraform apply #{vars_string} 2>&1`
20+
21+
elb_dns_name = `terraform output -no-color elb_dns_name`
22+
url = "http://#{elb_dns_name}/"
23+
24+
retries = 0
25+
loop do
26+
retries += 1
27+
raise "Didn't get expected response after 10 retries" if retries > 10
28+
29+
puts "Checking #{url}"
30+
output = Net::HTTP.get(url)
31+
puts "Output: #{output}"
32+
33+
if output.include? 'Hello, World'
34+
puts 'Success!'
35+
break
36+
end
37+
38+
puts 'Sleeping for 30 seconds and trying again'
39+
sleep(30.seconds)
40+
end
41+
ensure
42+
puts "Undeploying code in #{Dir.pwd}"
43+
puts `terraform destroy -force #{vars_string} 2>&1`
44+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
provider "aws" {
2+
region = "us-east-1"
3+
}
4+
5+
resource "aws_instance" "example" {
6+
ami = "ami-40d28157"
7+
instance_type = "t2.micro"
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
provider "aws" {
2+
region = "us-east-1"
3+
}
4+
5+
resource "aws_instance" "example" {
6+
count = 10
7+
ami = "ami-40d28157"
8+
instance_type = "t2.micro"
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
provider "aws" {
2+
region = "us-east-1"
3+
}
4+
5+
resource "aws_instance" "example" {
6+
count = 15
7+
ami = "ami-40d28157"
8+
instance_type = "t2.micro"
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
provider "aws" {
2+
region = "us-east-1"
3+
}
4+
5+
resource "aws_instance" "example" {
6+
count = 15
7+
ami = "ami-408c7f28"
8+
instance_type = "t2.micro"
9+
}

0 commit comments

Comments
 (0)