I have a multi-container app that I need to run twice a day at specific times for about 30 mins each time. To save costs, I would like to:
Turn on a Linux VM on Microsoft Azure
Run a script that runs docker-compose up -d, waits 30 mins, then calls docker-compose stop
Turn off the VM
I'd love for this to happen automatically on a cron schedule.
So far, I've failed to find any Azure solution that can do all of this. Azure automation allows you to turn on/off VMs but it doesn't allow you to run a sh script on it after you've turned it on. Azure Logic apps allows you to spin up containers on a schedule but not with docker-compose.
Any idea on how I might accomplish this in a way that doesn't involve me having a VM that is always on?
No worries , you can use this AZ powershell command to run sh script on your Azure VMs directly via Azure automation runbook with prarm : -CommandId 'RunShellScript'
For AzureRM commands , refer to here
I have tested on my side and it works perfectly for me .
Related
I'm trying to figure out what the easiest way of running a "one time job" on Azure Container Apps is.
I know that it can be done with Azure Container Instances or Azure Kubernetes Service, but is there something similar for ACA?
Ideally, I'm looking for some kind of cli command like az containerapps run --image <some-image> that can run in an environment and is cleaned up when it has finished running.
Is that possible?
How am I able to run PowerShell commands against a Windows 2016 vm in Azure without using winrm or pssession where I have to install\configure a self signed cert?
Does anyone know
If I understand your query correctly, you want to run PowerShell commands on Azure VM
In such a case, Azure DevOps service will not know where to direct the request, hence out of the box this is not possible.
However what you can do is, you can setup a Self hosted agent on this machine(as long it is connected to the internet).
Then you can run the task on a self hosted agent, and choose to run a powershell task. (This might need you to whitelist some devops ranges on your firewall).
Other than that, there are several ways to run commands and scripts on Azure VMs depending on the design you need to implement.
You can use Azure portal to execute predefined, custom PowerShell commands on a VM using Virtual Machine blade in Azure portal.
Also please take a look at these docs for more reference :
Run Powershell Cmds
Run Cmd
Better to use Azure Automation account than Azure DevOps if you want to do PowerShell.
Read this on how to create an automation account.
See here how to create a Runbook in Azure Automation using PowerShell.
If you are not required to run only one command frequently, you can simply run any PowerShell command just from the portal using Run command, please take a look at the screenshot.
Can I run powershell commands from Azure DevOps to vm's in Azure?
Run PowerShell scripts in your Windows VM by using Run Command provides two possible directions:
1.Azure CLI way:
You can use Azure CLI task in azure devops pipeline to run az vm start
and az vm run-command invoke commands. The az vm start will start the VM, the second command will then run the PS commands.
2.Powershell way:
You can use Azure Powershell task in azure devops pipeline to run Start-AzVM and Invoke-AzVMRunCommand commands.
Note:
It requires several seconds even when starting VM in web portal, so it's recommended to have 10~15 seconds as interval between these two commands(Start VM and Run PS). eg:
Try Start-Sleep -s 15.
We have a Virtual Machine which scheduled to stop automatically on every end of the day.
Next day when we want to connect to it, we need to explicitly login to Azure portal, select Virtual machine and click on start.
I know we can schedule it using automation account (due to some restrictions in office environment cant use automation account) but is there any way we can achieve this?
Thinking of any way of -
Creating a batch file on my local machine which just start the VM
when I run it without logging into Azure portal?
Any other simple setting like we have to schedule auto stop?
Any other hint will be a great help.
Thank you!
you can just use az cli and\or powershell and script the vm to start, its extremely straight forward (something like az vm start -g rg_name -n vm_name).
There are no built-in startup solutions yet.
I have a number of VMs provisioned in my Azure subscription, protected by an NSG, so I don't have WinRM available remotely.
I'd like to execute a PowerShell script on these VMs as part of my TFS release definition.
Is there any way to execute arbitrary PowerShell scripts on Azure VM resources?
I'm aware of the Custom Script extension for Azure VM resources, but I don't know if this is what I'm after, or if it plays nicely with TFS.
Custom Script Extension would work fine for running scripts on the VM - but it would have to be done as part of "adding" the extension. Meaning you'd have to deploy the extension to the VM to trigger running the script. So you'd do it when the VM is provisioned or submit another deployment to run the script after it's provision.
Either is doable from TFS/VSTS...
I'm able to register my existing Azure VM as build agent in VSTS. Build works fine but I would like to turn the machine on and off only if there is any work for it. Is there any way how I can turn on the VM if I see there is some work for it in a queue and then turn it off if the queue is idle for let's say 5 minutes?
Add a hosted agent job to the start to start the VM and add an agent job or additional step to the end to shut down the VM when finished.
Add an Agent job to run this as hosted to start the VM. Then use all build steps as self-hosted(private).
Add your Azure subscription
Choose Inline Script to add the following to start it with VM name and resource group
start-AzureRmVM -Name ""-ResourceGroupName ""
Add another Agent Job or additional step as the last Step
To shut down the VM when the build has finished.
With the following Script
stop-AzureRmVM -Name ""-ResourceGroupName "" -Force
I'm wondering if this could be helpful for you because this is not exactly what you asked for. I used REST API calls before the build and after the build to start and then stop a specific VM. See how to start and power off a VM in the API documentation.
I created an agentless job as the first step, with a task entitled Invoke REST API. The task lets you authenticate to your Azure account, so you don't have to handle that manually. All you have to do is specify the URL suffix. For instance, to start a VM named MyVm, you add a suffix similar to the following one:
/subscriptions/subscription-id/resourceGroups/resource-group-name/providers/Microsoft.Compute/virtualMachines/MyVm/start?api-version=2018-06-01
where the subscription ID and resource group can be verified in the Overview page of your virtual machine in Azure.
After the build you can add another agentless job, but this time with a REST call of the powerOff endpoint:
/subscriptions/subscription-id/resourceGroups/resource-group-name/providers/Microsoft.Compute/virtualMachines/MyVm/powerOff?api-version=2018-06-01
There isn’t the good way to start/stop azure VM when build in VSTS. Regarding WebHook, there isn’t the event for queue build, for other events, they aren’t meet many requirements (e.g. non-CI build, queue build manually)
I recommend you use Hosted agent, with free Hosted Pipeline, you get 4 hours (240 minutes) per month and maximum duration of 30 minutes per build or deployment in Team Services.
How to buy more pipeline capacity for builds and releases in Visual Studio Team Services
Workaround:
Queue build at the specify time, for example, 7:00 am on Monday and Thursday, then you can auto-start and auto-shutdown the VM
Run on Hosted agent (Execute Powershell to start VM)=> Run on private agent
More information, you can refer to: How to Create a Monster Build Agent in Azure for Cheap
You could try using a WebHook in VSTS, and select the most appropriate event (like code pushed, or release created). Open the admin page for the team project in Visual Studio Team Services, and on the Service Hooks tab, run the subscription wizard, select the event you're interested in, and fill in the relevant criteria.
You could then use an Azure Automation Runbook to subscribe to the WebHook and, using PowerShell, either use the build VM if it's already started or else start it up. You could use another WebHook to signal that a build has been completed, or poll VSTS from the VM via its various APIs.
You might want to delay shutting the VM down after a build, as there is a time cost associated with starting up / shutting down.