Skip to content

Commit

Permalink
Update Azure Terraform deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
muratugureminoglu committed Dec 18, 2024
1 parent 87e86ad commit b83f80c
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 41 deletions.
127 changes: 88 additions & 39 deletions terraform/azure/main.tf
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
resource "azurerm_resource_group" "rg" {
location = var.resource_group_location
name = var.resource_group_name_enterprise
name = "community"
}

resource "azurerm_virtual_network" "antmedia" {
name = "antmedia-network"
resource "azurerm_virtual_network" "antmedia-marketplace" {
name = "antmedia-marketplace-network"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
}

resource "azurerm_subnet" "antmedia" {
resource "azurerm_subnet" "antmedia-marketplace" {
name = "internal"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.antmedia.name
virtual_network_name = azurerm_virtual_network.antmedia-marketplace.name
address_prefixes = ["10.0.2.0/24"]
}

resource "azurerm_network_interface" "antmedia" {
name = "antmedia-nic"
resource "azurerm_network_interface" "antmedia-marketplace" {
name = "antmedia-marketplace-nic"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name

ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.antmedia.id
subnet_id = azurerm_subnet.antmedia-marketplace.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.antmedia.id
public_ip_address_id = azurerm_public_ip.antmedia-marketplace.id
}
}

resource "azurerm_linux_virtual_machine" "antmedia" {
name = "antmedia-machine"
resource "azurerm_linux_virtual_machine" "antmedia-marketplace" {
name = "antmedia-marketplace-machine"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
size = "Standard_F2s_v2"
admin_username = var.user
network_interface_ids = [
azurerm_network_interface.antmedia.id,
azurerm_network_interface.antmedia-marketplace.id,
]

admin_ssh_key {
Expand All @@ -59,23 +59,23 @@ resource "azurerm_linux_virtual_machine" "antmedia" {
}


resource "azurerm_public_ip" "antmedia" {
name = "antmedia-public-ip"
resource "azurerm_public_ip" "antmedia-marketplace" {
name = "antmedia-marketplace-public-ip"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
allocation_method = "Static"
}

output "public_ip_address" {
value = azurerm_public_ip.antmedia.ip_address
value = azurerm_public_ip.antmedia-marketplace.ip_address
}

output "ams_version_debug" {
value = var.ams_version
}

resource "azurerm_network_security_group" "antmedia" {
name = "antmedia-nsg"
resource "azurerm_network_security_group" "antmedia-marketplace" {
name = "antmedia-marketplace-nsg"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name

Expand Down Expand Up @@ -117,25 +117,30 @@ resource "azurerm_network_security_group" "antmedia" {

}

resource "azurerm_network_interface_security_group_association" "antmedia" {
network_interface_id = azurerm_network_interface.antmedia.id
network_security_group_id = azurerm_network_security_group.antmedia.id
resource "azurerm_network_interface_security_group_association" "antmedia-marketplace" {
network_interface_id = azurerm_network_interface.antmedia-marketplace.id
network_security_group_id = azurerm_network_security_group.antmedia-marketplace.id
}


resource "null_resource" "ams-marketplace-setup" {
depends_on = [azurerm_linux_virtual_machine.antmedia]
resource "null_resource" "ams-marketplace-setup_enterprise" {
count = azurerm_resource_group.rg.name == "enterprise" ? 1 : 0
depends_on = [
azurerm_linux_virtual_machine.antmedia-marketplace,
azurerm_network_interface.antmedia-marketplace,
azurerm_public_ip.antmedia-marketplace
]
provisioner "remote-exec" {
connection {
type = "ssh"
user = var.user
host = azurerm_public_ip.antmedia.ip_address
host = azurerm_public_ip.antmedia-marketplace.ip_address
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",
"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",
Expand All @@ -152,22 +157,63 @@ resource "null_resource" "ams-marketplace-setup" {
}
}

resource "null_resource" "ams-marketplace-setup_community" {
count = azurerm_resource_group.rg.name == "community" ? 1 : 0
provisioner "remote-exec" {
connection {
type = "ssh"
user = var.user
host = azurerm_public_ip.antmedia-marketplace.ip_address
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",
"while sudo fuser /var/lib/dpkg/lock >/dev/null 2>&1; do sleep 1; done",
"while sudo fuser /var/lib/apt/lists/lock >/dev/null 2>&1; do sleep 1; done",
"sudo apt-get update -y",
"sudo apt-get dist-upgrade -y",
"wget https://raw.githubusercontent.com/ant-media/Scripts/master/install_ant-media-server.sh",
"curl -L -o ant-media-server-community.zip https://github.com/ant-media/Ant-Media-Server/releases/download/ams-v${var.ams_version}/ant-media-server-community-${var.ams_version}.zip",
"sudo bash ./install_ant-media-server.sh -i ant-media-server-community.zip",
"sudo sed -i 's/server.marketplace=.*/server.marketplace=azure/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",
"sudo waagent -deprovision+user -force",
]
}
depends_on = [
azurerm_linux_virtual_machine.antmedia-marketplace,
azurerm_network_interface.antmedia-marketplace,
azurerm_public_ip.antmedia-marketplace
]

}


resource "null_resource" "stop_vm" {
provisioner "local-exec" {
command = "az vm deallocate --name ${azurerm_linux_virtual_machine.antmedia.name} --resource-group ${azurerm_resource_group.rg.name}"
command = "az vm deallocate --name ${azurerm_linux_virtual_machine.antmedia-marketplace.name} --resource-group ${azurerm_resource_group.rg.name}"
}

depends_on = [
azurerm_linux_virtual_machine.antmedia,
azurerm_network_interface.antmedia,
azurerm_public_ip.antmedia,
null_resource.ams-marketplace-setup
azurerm_linux_virtual_machine.antmedia-marketplace,
azurerm_network_interface.antmedia-marketplace,
azurerm_public_ip.antmedia-marketplace,
null_resource.ams-marketplace-setup_community,
null_resource.ams-marketplace-setup_enterprise

]
}


resource "null_resource" "generalize_vm" {
provisioner "local-exec" {
command = "az vm generalize --resource-group ${azurerm_resource_group.rg.name} --name ${azurerm_linux_virtual_machine.antmedia.name}"
command = "az vm generalize --resource-group ${azurerm_resource_group.rg.name} --name ${azurerm_linux_virtual_machine.antmedia-marketplace.name}"
}

depends_on = [
Expand All @@ -176,7 +222,7 @@ resource "null_resource" "generalize_vm" {
}


resource "azurerm_shared_image_gallery" "antmedia" {
resource "azurerm_shared_image_gallery" "antmedia-marketplace" {
name = "antmedia_image_gallery"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
Expand All @@ -188,30 +234,30 @@ resource "azurerm_shared_image_gallery" "antmedia" {
depends_on = [null_resource.generalize_vm]
}

resource "azurerm_shared_image" "antmedia" {
resource "azurerm_shared_image" "antmedia-marketplace" {
name = var.ams_version
gallery_name = azurerm_shared_image_gallery.antmedia.name
gallery_name = azurerm_shared_image_gallery.antmedia-marketplace.name
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
os_type = "Linux"
hyper_v_generation = "V2"


identifier {
publisher = "AntMedia"
publisher = "antmedia"
offer = "Ubuntu"
sku = "Ubuntu"
}
depends_on = [null_resource.generalize_vm]
}

resource "azurerm_shared_image_version" "antmedia" {
resource "azurerm_shared_image_version" "antmedia-marketplace" {
name = var.ams_version
gallery_name = azurerm_shared_image_gallery.antmedia.name
image_name = azurerm_shared_image.antmedia.name
gallery_name = azurerm_shared_image_gallery.antmedia-marketplace.name
image_name = azurerm_shared_image.antmedia-marketplace.name
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
managed_image_id = azurerm_linux_virtual_machine.antmedia.id
managed_image_id = azurerm_linux_virtual_machine.antmedia-marketplace.id


target_region {
Expand All @@ -220,4 +266,7 @@ resource "azurerm_shared_image_version" "antmedia" {
storage_account_type = "Standard_LRS"
}
depends_on = [null_resource.generalize_vm]
}
}



2 changes: 0 additions & 2 deletions terraform/azure/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ variable "user" {
variable "ams_version" {
type = string
description = "AMS Version"
default = "2.12.0"
}

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

0 comments on commit b83f80c

Please sign in to comment.