diff --git a/terraform/azure/main.tf b/terraform/azure/main.tf index e0c5c2ba..71c4544b 100644 --- a/terraform/azure/main.tf +++ b/terraform/azure/main.tf @@ -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 { @@ -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 @@ -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", @@ -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 = [ @@ -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 @@ -188,9 +234,9 @@ 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" @@ -198,20 +244,20 @@ resource "azurerm_shared_image" "antmedia" { 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 { @@ -220,4 +266,7 @@ resource "azurerm_shared_image_version" "antmedia" { storage_account_type = "Standard_LRS" } depends_on = [null_resource.generalize_vm] -} \ No newline at end of file +} + + + diff --git a/terraform/azure/variables.tf b/terraform/azure/variables.tf index c7bef77c..b621db5f 100644 --- a/terraform/azure/variables.tf +++ b/terraform/azure/variables.tf @@ -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 = "" }