Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gcp: add gce deployment #6

Merged
merged 4 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ This repo is using [Opentofu](https://opentofu.org/) and other tools to create c

| | VM | Docker | K8s |
|-----------------------|-------------|-----------------|--------------|
| Microsoft Azure | VM ✅ | ACI 🔲 | AKS 🔲 |
| Amazon Web Services | EC2 ✅ | ECS 🔲 | EKS 🔲 |
| Google Cloud Platform | GCE 🚧 | GAE 🔲 | GKE 🔲 |
| Microsoft Azure | VM ✅ | ACI 🚧 | AKS 🔲 |
| Amazon Web Services | EC2 ✅ | ECS 🚧 | EKS 🔲 |
| Google Cloud Platform | GCE | GAE 🚧 | GKE 🔲 |
| Render | ❌ | DOCKER ✅(See 1) | ❌ |
| Heroku | ❌ | HCR 🔲 | ❌ |
| DigitalOcean | Droplets 🔲 | CR 🔲 | DOKS 🔲 |
Expand Down
2 changes: 1 addition & 1 deletion modules/aws-ec2/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ VEHICLE_POSITIONS_URL=https://api.cityofkingston.ca/gtfs-realtime/vehicleupdates
TRIP_UPDATES_URL=https://api.cityofkingston.ca/gtfs-realtime/tripupdates.pb
ALERTS_URL=https://api.cityofkingston.ca/gtfs-realtime/alerts.pb
REFRESH_INTERVAL=30
AGENCY_ID=Kingston Transit
AGENCY_ID=0
TZ=America/Toronto
GOOGLE_MAPS_API_KEY=<YOUR_GOOGLE_MAPS_API_KEY>
GOOGLE_MAPS_CHANNEL_ID=<YOUR_GOOGLE_MAPS_CHANNEL_ID>
Expand Down
4 changes: 2 additions & 2 deletions modules/aws-ec2/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ resource "null_resource" "remove_ssh_keys" {
always_run = timestamp()
}

# replace `rm` with `del` if you are using Windows
# replace with `del ${path.module}\\ssh\\id_rsa ${path.module}\\ssh\\id_rsa.pub` if you are using Windows
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

provisioner "local-exec" {
command = "rm ${path.module}\\ssh\\id_rsa ${path.module}\\ssh\\id_rsa.pub"
command = "rm -f ${path.module}/ssh/id_rsa ${path.module}/ssh/id_rsa.pub"
when = destroy
}
}
Expand Down
11 changes: 6 additions & 5 deletions modules/azure-vm/.env.example
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
GTFS_URL=https://opendata.burlington.ca/gtfs-rt/GTFS_Data.zip
GTFS_URL=https://api.cityofkingston.ca/gtfs/gtfs.zip
TEST_API_KEY=test
VEHICLE_POSITIONS_URL=https://opendata.burlington.ca/gtfs-rt/GTFS_VehiclePositions.pb
TRIP_UPDATES_URL=https://opendata.burlington.ca/gtfs-rt/GTFS_TripUpdates.pb
ALERTS_URL=https://opendata.burlington.ca/gtfs-rt/GTFS_ServiceAlerts.pb
VEHICLE_POSITIONS_URL=https://api.cityofkingston.ca/gtfs-realtime/vehicleupdates.pb
TRIP_UPDATES_URL=https://api.cityofkingston.ca/gtfs-realtime/tripupdates.pb
ALERTS_URL=https://api.cityofkingston.ca/gtfs-realtime/alerts.pb
REFRESH_INTERVAL=30
AGENCY_ID=Burlington
AGENCY_ID=0
TZ=America/Toronto
GOOGLE_MAPS_API_KEY=<YOUR_GOOGLE_MAPS_API_KEY>
GOOGLE_MAPS_CHANNEL_ID=<YOUR_GOOGLE_MAPS_CHANNEL_ID>
Expand All @@ -14,3 +14,4 @@ DOMAIN=oba.example.com
# OBA image version. You can find the available versions at:
# https://hub.docker.com/r/opentransitsoftwarefoundation/onebusaway-api-webapp/tags
OBA_VERSION=latest

16 changes: 16 additions & 0 deletions modules/gcp-gce/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
GTFS_URL=https://api.cityofkingston.ca/gtfs/gtfs.zip
TEST_API_KEY=test
VEHICLE_POSITIONS_URL=https://api.cityofkingston.ca/gtfs-realtime/vehicleupdates.pb
TRIP_UPDATES_URL=https://api.cityofkingston.ca/gtfs-realtime/tripupdates.pb
ALERTS_URL=https://api.cityofkingston.ca/gtfs-realtime/alerts.pb
REFRESH_INTERVAL=30
AGENCY_ID=0
TZ=America/Toronto
GOOGLE_MAPS_API_KEY=<YOUR_GOOGLE_MAPS_API_KEY>
GOOGLE_MAPS_CHANNEL_ID=<YOUR_GOOGLE_MAPS_CHANNEL_ID>
GOOGLE_MAPS_CLIENT_ID=<YOUR_GOOGLE_MAPS_CLIENT_ID>
# Your Domain Name, leave blank if you don't have one
DOMAIN=oba.example.com
# OBA image version. You can find the available versions at:
# https://hub.docker.com/r/opentransitsoftwarefoundation/onebusaway-api-webapp/tags
OBA_VERSION=latest
99 changes: 99 additions & 0 deletions modules/gcp-gce/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions modules/gcp-gce/docker-compose.caddy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: '3'

services:
caddy:
image: lucaslorentz/caddy-docker-proxy:ci-alpine
ports:
- 80:80
- 443:443
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- caddy_data:/data
restart: unless-stopped

volumes:
caddy_data: {}
37 changes: 37 additions & 0 deletions modules/gcp-gce/init.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash
# install Docker
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh

# install Docker Compose
curl -L "https://github.com/docker/compose/releases/download/v2.27.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

cd /home/${user}

# clone repo
git clone https://github.com/onebusaway/onebusaway-docker.git

cd /home/${user}/onebusaway-docker

# create .env file
echo "${docker_env}" > .env


# onebusaway-api-webapp depends on mysql, normally this will handle by docker-compose
# sometimes, the mysql container will not be ready when onebusaway-api-webapp starts
# which leads to the error `Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set`
# so we need to start mysql container first
docker-compose -f docker-compose.prod.yml up -d oba_database

sleep 5s

# start Docker Compose
docker-compose -f docker-compose.prod.yml up -d

if [ -n "${caddy}" ]; then
# create docker-compose.caddy.yml
echo "${docker_compose}" > docker-compose.caddy.yml
# start Caddy
docker-compose -f docker-compose.caddy.yml up -d
fi
111 changes: 111 additions & 0 deletions modules/gcp-gce/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
provider "google" {
project = var.project_id
region = var.region
}


resource "google_compute_network" "main" {
name = var.network_name
auto_create_subnetworks = false
}

resource "google_compute_subnetwork" "main" {
name = var.subnet_name
ip_cidr_range = "10.0.1.0/24"
region = var.region
network = google_compute_network.main.id
}

resource "google_compute_address" "main" {
name = var.public_ip_name
region = var.region
}

resource "google_compute_firewall" "main" {
name = var.firewall_name
network = google_compute_network.main.self_link

allow {
protocol = "tcp"
ports = ["80", "443", "8080", "22"]
}

source_ranges = ["0.0.0.0/0"]
}

resource "tls_private_key" "ssh_key" {
algorithm = "RSA"
rsa_bits = 4096
}

resource "local_sensitive_file" "private_key" {
content = tls_private_key.ssh_key.private_key_pem
filename = "${path.module}/ssh/id_rsa"
}

resource "local_file" "public_key" {
content = tls_private_key.ssh_key.public_key_openssh
filename = "${path.module}/ssh/id_rsa.pub"
}

data "template_file" "main" {
template = file("${path.module}/init.tpl")
vars = {
user = var.admin_username,
caddy = var.caddy,
docker_compose = file("${path.module}/docker-compose.caddy.yml"),
docker_env = file("${path.module}/.env")
}
}

resource "google_compute_instance" "main" {
name = var.vm_name
machine_type = var.machine_type
zone = var.zone

boot_disk {
initialize_params {
image = "projects/ubuntu-os-cloud/global/images/family/ubuntu-2204-lts"
type = "pd-standard"
}
}

network_interface {
network = google_compute_network.main.id
subnetwork = google_compute_subnetwork.main.id
access_config {
nat_ip = google_compute_address.main.address
}
}

metadata_startup_script = data.template_file.main.rendered

metadata = {
ssh-keys = "${var.admin_username}:${tls_private_key.ssh_key.public_key_openssh}"
}


service_account {
email = google_service_account.main.email
scopes = ["https://www.googleapis.com/auth/cloud-platform"]
}

tags = ["onebusaway"]
}

resource "google_service_account" "main" {
account_id = "onebusaway-sa"
display_name = "Onebusaway Service Account"
}

resource "null_resource" "remove_ssh_keys" {
triggers = {
always_run = timestamp()
}

# replace with `del ${path.module}\\ssh\\id_rsa ${path.module}\\ssh\\id_rsa.pub` if you are using Windows
provisioner "local-exec" {
command = "rm -f ${path.module}/ssh/id_rsa ${path.module}/ssh/id_rsa.pub"
when = destroy
}
}
9 changes: 9 additions & 0 deletions modules/gcp-gce/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
output "private_ip_address" {
description = "The private IP address of the instance"
value = google_compute_instance.main.network_interface[0].network_ip
}

output "public_ip_address" {
description = "The public IP address of the instance"
value = google_compute_address.main.address
}
Loading