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

Add terraform files for Marketplace Images #292

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
89 changes: 89 additions & 0 deletions terraform/GCP/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
resource "google_compute_instance" "ams-marketplace" {

name = "ams-marketplace-${var.ams_version}"
machine_type = var.machine_type
zone = var.zone
tags = ["allow-all"]
boot_disk {
initialize_params {
image = var.image
}
}
network_interface {
network = "default"
access_config {

}
}

metadata = {
ssh-keys = "${var.user}:${file(var.publickeypath)}"
}
}

resource "google_compute_firewall" "ams-allow_port_5080" {
name = "ams-allow-port-5080"
network = "default"

allow {
protocol = "tcp"
ports = ["5080"]
}

source_ranges = ["0.0.0.0/0"]
}

resource "null_resource" "ams-marketplace-setup" {
provisioner "remote-exec" {
connection {
type = "ssh"
user = var.user
host = google_compute_instance.ams-marketplace.network_interface[0].access_config[0].nat_ip
private_key = file(var.privatekeypath)
}
inline = [
"sudo sed -i 's/#\\$nrconf{kernelhints} = -1;/\\$nrconf{kernelhints} = -1;/g' /etc/needrestart/needrestart.conf",
"echo 'NEEDRESTART_SUSPEND=1' >> /etc/environment",
"sudo source /etc/environment",
"sudo apt-get update",
"sudo apt-get dist-upgrade -y",
"wget https://raw.githubusercontent.com/ant-media/Scripts/master/install_ant-media-server.sh",
"curl -L 'https://drive.usercontent.google.com/download?id=${var.zip_file_id}&export=download&confirm=t' -o 'ams.zip'",
"sudo bash ./install_ant-media-server.sh -i ams.zip",
"sudo sed -i 's/server.marketplace=.*/server.marketplace=gcp/g' /usr/local/antmedia/conf/red5.properties",
"sudo systemctl stop antmedia",
"sudo rm -rf /usr/local/antmedia/conf/instanceId",
"sudo rm -rf /usr/local/antmedia/*.db.* && sudo rm -rf /usr/local/antmedia/*.db",
"sudo rm -rf /root/*.zip && sudo rm -rf /root/install*",
"sudo rm -rf /root/.ssh",
]
}
}

resource "null_resource" "stop_instance" {
provisioner "local-exec" {
command = "gcloud compute instances stop ams-marketplace-${var.ams_version} --project=${var.project} --zone=${var.zone}"
}
depends_on = [null_resource.ams-marketplace-setup]
}


resource "google_compute_image" "ams_marketplace_image" {
name = "ams-marketplace-${var.ams_version}"
source_disk = "projects/antmedia-dev/zones/${var.zone}/disks/ams-marketplace-${var.ams_version}"
licenses = ["projects/${var.public_project}/global/licenses/cloud-marketplace-211adc9aa41170ec-df1ebeb69c0ba664"]
description = "AMS-ams-marketplace-${var.ams_version}"
project = "${var.public_project}"
depends_on = [null_resource.stop_instance]
}


resource "google_compute_image_iam_binding" "iam" {
image = "projects/${var.public_project}/global/images/ams-marketplace-${var.ams_version}"
role = "roles/compute.imageUser"

members = [
"allAuthenticatedUsers"
]
depends_on = [google_compute_image.ams_marketplace_image]
}
6 changes: 6 additions & 0 deletions terraform/GCP/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
provider "google" {
# credentials = file("antmedia-dev.json")
project = var.project
region = var.region
zone = var.zone
}
64 changes: 64 additions & 0 deletions terraform/GCP/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
variable "project" {
type = string
description = "The project ID to deploy to"
default = "antmedia-dev"
}

variable "public_project" {
type = string
description = "The project ID to deploy to"
default = "antmedia-public-385620"
}


variable "region" {
type = string
description = "The region to deploy to"
default = "us-central1"

}

variable "zone" {
type = string
description = "The zone to deploy to"
default = "us-central1-a"
}

variable "machine_type" {
type = string
description = "The machine type to deploy to"
default = "e2-medium"
}

variable "image" {
type = string
description = "The image to deploy to"
default = "ubuntu-os-cloud/ubuntu-2204-lts"
}

variable "ams_version" {
type = string
description = "Version number of AMS"
}

variable "publickeypath" {
type = string
default = "./ssh/id_rsa.pub"
}

variable "privatekeypath" {
type = string
default = "./ssh/id_rsa"
}

variable "user" {
type = string
default = "root"

}

variable "zip_file_id" {
description = "Google drive ID"
type = string
#default = "xxxxxxx"
}
42 changes: 42 additions & 0 deletions terraform/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# GCP

1. Log in first
```
gcloud auth application-default login
```
2. Create a file called terraforms.tfvars and add the following variables
```
zip_file_id = ""
ams_version = ""

```
3. Generate a new SSH key
```
mkdir ./ssh
ssh-keygen -t rsa -f ./ssh/id_rsa
```
# DO

1. First create a DO token

2. Create a file called terraforms.tfvars and add the following variables

```
zip_file_id = ""
do_token = ""
ams_version = ""
```
3. Generate a new SSH key
```
mkdir ./ssh
ssh-keygen -t rsa -f ./ssh/id_rsa
```

## Make sure everything works properly
```
terraform plan
```
## Install the deployments
```
terraform apply -auto-approve
```
32 changes: 32 additions & 0 deletions terraform/do/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash
sudo sed -i 's/#\\$nrconf{kernelhints} = -1;/\\$nrconf{kernelhints} = -1;/g' /etc/needrestart/needrestart.conf
echo 'NEEDRESTART_SUSPEND=1' >> /etc/environment
sudo source /etc/environment
sudo apt-get update
sudo DEBIAN_FRONTEND=noninteractive apt-get dist-upgrade -y
sudo apt-get purge droplet-agent -y
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp
sudo ufw allow 443/tcp
sudo ufw allow 80/tcp
sudo ufw allow 5080/tcp
sudo ufw allow 1935/tcp
sudo ufw allow 5443/tcp
sudo ufw allow 50000:65000/udp
sudo ufw allow 4200/udp
echo "y" | sudo ufw enable
sudo systemctl stop antmedia
sudo sed -i 's/server.marketplace=.*/server.marketplace=do/g' /usr/local/antmedia/conf/red5.properties
sudo rm -rf /root/.ssh
sudo rm -rf /usr/local/antmedia/conf/instanceId
sudo rm -rf /usr/local/antmedia/*.db.*
sudo rm -rf /usr/local/antmedia/*.db
sudo rm -rf /root/*.zip && rm -rf /root/install*

wget https://raw.githubusercontent.com/digitalocean/marketplace-partners/master/scripts/90-cleanup.sh
wget https://raw.githubusercontent.com/digitalocean/marketplace-partners/master/scripts/99-img-check.sh

bash 90-cleanup.sh
bash 99-img-check.sh
history -c
95 changes: 95 additions & 0 deletions terraform/do/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
resource "digitalocean_ssh_key" "default" {
name = "Terraform_Temp"
public_key = file("./ssh/id_rsa.pub")
}

resource "digitalocean_droplet" "enterprise" {
count = var.do_droplet_enable ? 1 : 0
image = var.do_image
name = "ams-server-enterprise"
region = var.do_region
size = var.do_instance_type
ssh_keys = [digitalocean_ssh_key.default.fingerprint]

provisioner "file" {
source = "init.sh"
destination = "/tmp/init.sh"

connection {
type = "ssh"
user = "root"
private_key = file("./ssh/id_rsa")
host = digitalocean_droplet.enterprise[count.index].ipv4_address
}

}


provisioner "remote-exec" {
inline = [
"sudo apt-get purge droplet-agent -y",
"wget https://raw.githubusercontent.com/ant-media/Scripts/master/install_ant-media-server.sh",
"curl -L 'https://drive.usercontent.google.com/download?id=${var.zip_file_id}&export=download&confirm=t' -o 'ams.zip'",
"bash install_ant-media-server.sh -i ams.zip",
"bash /tmp/init.sh",
]

connection {
type = "ssh"
user = "root"
private_key = file("./ssh/id_rsa")
host = digitalocean_droplet.enterprise[count.index].ipv4_address
}
}
}

resource "digitalocean_droplet_snapshot" "ams-enterprise-snapshot" {
count = var.do_droplet_enable ? 1 : 0
droplet_id = digitalocean_droplet.enterprise[count.index].id
name = "ams-enterprise-snapshot-01"
}

resource "digitalocean_droplet" "community" {
count = var.do_droplet_enable ? 1 : 0
image = var.do_image
name = "ams-server-community"
region = var.do_region
size = var.do_instance_type
ssh_keys = [digitalocean_ssh_key.default.fingerprint]

provisioner "file" {
source = "init.sh"
destination = "/tmp/init.sh"

connection {
type = "ssh"
user = "root"
private_key = file("./ssh/id_rsa")
host = digitalocean_droplet.community[count.index].ipv4_address
}

}


provisioner "remote-exec" {
inline = [
"sudo apt-get purge droplet-agent -y",
"wget https://raw.githubusercontent.com/ant-media/Scripts/master/install_ant-media-server.sh",
"bash install_ant-media-server.sh",
"bash /tmp/init.sh",
]

connection {
type = "ssh"
user = "root"
private_key = file("./ssh/id_rsa")
host = digitalocean_droplet.community[count.index].ipv4_address
}
}
}

resource "digitalocean_droplet_snapshot" "ams-community-snapshot" {
count = var.do_droplet_enable ? 1 : 0
droplet_id = digitalocean_droplet.community[count.index].id
name = "ams-community-snapshot-01"
}
12 changes: 12 additions & 0 deletions terraform/do/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
terraform {
required_providers {
digitalocean = {
source = "digitalocean/digitalocean"
version = "~> 2.0"
}
}
}

provider "digitalocean"{
token = var.do_token
}
26 changes: 26 additions & 0 deletions terraform/do/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
variable "do_token" {
type = string
}

variable "do_droplet_enable" {
default = true
}

variable "do_region" {
default = "fra1"
}

variable "do_instance_type" {
default = "c-2"
}

variable "do_image" {
default = "ubuntu-22-04-x64"
}

variable "zip_file_id" {
description = "Google drive ID"
type = string
#default = "xxxxxxx"
}

Loading