diff --git a/install-app.sh b/install-app.sh index 0fef5aa..f952954 100644 --- a/install-app.sh +++ b/install-app.sh @@ -1,33 +1,29 @@ #!/bin/bash -# Update and install python3-pip +# Script to silently install and start the todo web app on the virtual machine. +# Note that all commands bellow are without sudo - that's because extention mechanism +# runs scripts under root user. + +# install system updates and isntall python3-pip package using apt. '-yq' flags are +# used to suppress any interactive prompts - we won't be able to confirm operation +# when running the script as VM extention. sudo apt-get update -yq sudo apt-get install python3-pip -yq -# Create a directory for the app if it doesn't exist -if [ ! -d /home/azureuser/app ]; then - sudo mkdir /home/azureuser/app -fi +# Create a directory for the app and download the files. +sudo mkdir /home/azureuser/app -# Change ownership of directories sudo chown -R azureuser:azureuser /home/azureuser/devops_todolist_terraform_task sudo chown -R azureuser:azureuser /home/azureuser/app +# sudo chmod 644 /home/azureuser/app/start.sh -# Copy files to the app directory -if [ -d /home/azureuser/devops_todolist_terraform_task/app ]; then - sudo cp -r /home/azureuser/devops_todolist_terraform_task/app/* /home/azureuser/app -else - echo "Source directory /home/azureuser/devops_todolist_terraform_task/app does not exist." - exit 1 -fi +# make sure to uncomment the line bellow and update the link with your GitHub username +# git clone https://github.com//azure_task_12_deploy_app_with_vm_extention.git +cp -r /home/azureuser/devops_todolist_terraform_task/app/* /home/azureuser/app -# Move and enable the service -if [ -f /home/azureuser/app/todoapp.service ]; then - sudo mv /home/azureuser/app/todoapp.service /etc/systemd/system/ - sudo systemctl daemon-reload - sudo systemctl start todoapp - sudo systemctl enable todoapp -else - echo "Service file /home/azureuser/app/todoapp.service does not exist." - exit 1 -fi +# create a service for the app via systemctl and start the app +sudo mv /home/azureuser/app/todoapp.service /etc/systemd/system/ +sudo chmod 644 /etc/systemd/system/todoapp.service +sudo systemctl daemon-reload +sudo systemctl start todoapp +sudo systemctl enable todoapp diff --git a/modules/compute/main.tf b/modules/compute/main.tf index 91b466d..8897f4c 100644 --- a/modules/compute/main.tf +++ b/modules/compute/main.tf @@ -42,8 +42,15 @@ resource "azurerm_virtual_machine_extension" "custom_script" { publisher = "Microsoft.Azure.Extensions" type = "CustomScript" type_handler_version = "2.1" - settings = jsonencode({ - fileUris = ["https://raw.githubusercontent.com/Serveladik/devops_todolist_terraform_task/main/install-app.sh"] - commandToExecute = "bash install-app.sh" - }) + + settings = <