How to restart Azure Web App programmatically - c#

Is any way to restart the Azure Web App programmatically, i.e. Kudu or some another kind?
I'v found that it is possible using Management Libraries, but it is not applicable for me since I can't create application in AD.

Powershell:
Restart-AzureRmWebApp -ResourceGroupName xxx -Name xxx
or using the provider operation:
Invoke-AzureRmResourceAction -ResourceGroupName xxx -ResourceType 'Microsoft.Web/sites' -ResourceName xxx `
-ApiVersion '2015-08-01' -Action 'Restart' -Force
Azure Cli (nodejs, depreciated):
azure webapp restart --resource-group xxx --name xxx
Azure Cli (python):
az appservice web restart --resource-group xxx --name xxx
or you can use Rest Api

I'm assuming you're looking for this feature because you or folks on your team don't have elevated permissions on the Azure Portal.
Creating scripts will work, but it still requires someone to be comfortable with running command lines.
The best way I found to do this is with VSTS pipeline. A few clicks and it's restarted. A simple and awesome way to build a devops culture.

Check out this answer, which uses a LogicApp to make it a nice lightweight way to restart your web-app: https://stackoverflow.com/a/59633629/44815

Related

Azure DevOps Pipeline with selfhosted agent fails to run tasks

I am trying to set up an Azure devops pipeline that runs on a virtual machine in Azure. I'm doing the build in azure and the download the artifact to the VM for deployment. However, when I try to run tasks like web.config transform or file copy, I get an error like this:
##[error]The current operating system is not capable of running this task. That typically means the task was written for Windows only. For example, written for Windows Desktop PowerShell.
##[debug]System.InvalidOperationException: The current operating system is not capable of running this task. That typically means the task was written for Windows only. For example, written for Windows Desktop PowerShell.
This quite odd since the VM is running windows.
I have made sure to install the latest version of powershell
After you use a self-hosted agent for Windows, Make sure you have referenced windows as a host pool in your YAML pipeline. Also, If you’re using the azure PowerShell task, Make sure you have installed the Powershell module in your VM acting as a self-hosted agent.
I created one windows VM (Windows Server 2019 Datacenter Gen2) in Azure Portal and authorized it to work as a self-hosted agent in Azure DevOps with the PAT token like below:-
Self hosted agent is online in Azure DevOps:-
Note- It is a part of Default Pool in my Azure DevOps org
Now, I ran an Azure-Powershell inline script with this Agent in Azure DevOps :-
Yaml pipeline code:-
# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
trigger:
- main
pool:
name: Default
steps:
- script: echo Hello, world!
displayName: 'Run a one-line script'
- script: |
echo Add other tasks to build, test, and deploy your project.
echo See https://aka.ms/yaml
displayName: 'Run a multi-line script'
- task: AzurePowerShell#5
inputs:
azureSubscription: 'SID subscription(xxxxxx365-f598-44d6-xxxx-e2b6e97cb2a7)'
ScriptType: 'InlineScript'
Inline: 'Get-AzResource | Format-Table'
azurePowerShellVersion: 'LatestVersion'
Note:- I have added pool as Default where my agent pool is connected
in Azure DevOps, Make sure you use the correct pool here in your YAML
code:-
pool:
name: Default
The Azure Powershell script ran successfully like below:-
Now, when I ran another SQL Inline script with my pool set to another agent OR ubuntu-image, I got same error code as yours:-
trigger:
- main
pool:
vmImage: ubuntu-latest
Pipeline Output:-
When I changed the pool to windows, the error was resolved and the task ran successfully like below:-
trigger:
- main
pool:
vmImage: windows-latest
Pipeline Output:-
Make sure you’re selecting the correct pool and the Powershell module is installed properly inside that VM which is acting as an Azure DevOps agent.
Reference:-
The current operating system is not capable of running this task. That typically means the task was written for Windows only. For example, written for Windows Desktop PowerShell. – AzureFileCopy#4 | What I Broke – Programming and Web Development
Try installing Windows Desktop PowerShell on the virtual machine and see if that resolves the issue.

ASP.NET Core 3.1 web app publishes from Visual Studio does not work when deployed from Azure DevOps release pipeline

I have a fully working web api written in dotnet core 3.1. I've been following the TimCoRetailManager series.
The application builds with no errors and works as intended when running in Visual Studio. I have a free azure subscription. I setup a F1 free web app service on linux and two basic SQL server databases, all in the same resource group with the firewall set to allow access to other azure services to connect. I can publish the web api and the databases. I can connect to the databases with azure data studio and things look fine. I updated the config on the app service so it's using the azure secrets instead of my dev environment secrets. The API works as intended when published from Visual Studio, i can get to the swagger ui, and life is good.
On to Azure DevOps. When I use a release pipeline from Azure DevOps, the pipeline gives a success message. However, when I navigate to the API (has a simple MVC landing page + swagger) it just has an "Application Error :(" page. I found the logs on the Azure App service and they have this...
Ok 2022-03-01T21:44:43.8098649 \/ \/ \/
Ok 2022-03-01T21:44:43.8098677 A P P S E R V I C E O N L I N U X
Ok 2022-03-01T21:44:43.8098704
Ok 2022-03-01T21:44:43.809873 Documentation: http://aka.ms/webapp-linux
Ok 2022-03-01T21:44:43.8098757 Dotnet quickstart: https://aka.ms/dotnet-qs
Ok 2022-03-01T21:44:43.8098784 ASP .NETCore Version: 3.1.21
Ok 2022-03-01T21:44:43.809881 Note: Any data outside '/home' is not persisted
Ok 2022-03-01T21:44:44.3985103 Running oryx create-script -appPath /home/site/wwwroot -output /opt/startup/startup.sh -defaultAppFilePath /defaulthome/hostingstart/hostingstart.dll -bindPort 8080 -userStartupCommand 'dotnet TRMApi.dll'
Ok 2022-03-01T21:44:44.4498212 Cound not find build manifest file at '/home/site/wwwroot/oryx-manifest.toml'
Ok 2022-03-01T21:44:44.4499139 Could not find operation ID in manifest. Generating an operation id...
Ok 2022-03-01T21:44:44.4500119 Build Operation ID: 74cf02f2-a6c2-44a8-9077-95f1bab9d974
Ok 2022-03-01T21:44:45.5344493
Ok 2022-03-01T21:44:45.5358264 Agent extension
Ok 2022-03-01T21:44:45.5358438 Before if loop >> DotNet Runtime
Ok 2022-03-01T21:44:46.2152908 DotNet Runtime 3.1Writing output script to '/opt/startup/startup.sh'
Ok 2022-03-01T21:44:46.6953451 Running user provided startup command...
Ok 2022-03-01T21:44:46.7448258 It was not possible to find any installed .NET Core SDKs
Ok 2022-03-01T21:44:46.7453653 Did you mean to run .NET Core SDK commands? Install a .NET Core SDK from:
Ok 2022-03-01T21:44:46.7457829 https://aka.ms/dotnet-download
The build pipeline appears to be working as intended. It produces four artifacts: a folder for the web api with a zip for deployment, a folder for the database stuff notably with a dacpac, and a folder for the front end with a zip file in there, too. The release pipeline isn't publishing all of this though. I set the package to $(System.DefaultWorkingDirectory)/_Standard Continuous Integration Build/ApiDrop/TRMApi.zip.
The release pipeline has the following settings:
Azure subscription: selected free trial and authenticated it
App Type: Web App on Linux
App Service name: selected api's name from drop down
Startup command: blank
Agent Job
Agent: Azure Pipelines
Agent Specification: windows-2019
Artifact download: continuous integration build > selected all artifacts
Deploy Azure App Service step
Task version: 4.*
Connection type: Azure Resource Manager
Azure subcription: read only, prefilled by app service selection earlier
App service type: also read only, prefilled
App service name: also read only, prefilled
Package or folder: $(System.DefaultWorkingDirectory)/_Standard Continuous Integration Build/ApiDrop/TRMApi.zip
Runtime Stack: 3.1 (DOTNETCORE|3.1)
Any tips or suggestions? I'm really scratching my head on this. I'm watching videos on pluralsight and youtube and it seems like the process "just works" for the content creators, but I'm not able to repeat the results.
Edit: Including screenshot of how artifact is configured in release pipeline.
FWIW, I have the exact same problem from the exact same code base.
I also have the build pipeline artifact set up correctly.
I initially got caught up on the message "It was not possible to find any installed .NET Core SDKs".
I went to the app service "Advanced tools" and then used Bash and issued the commands:
dotnet --list-sdks
dotnet --list-runtimes
Initially, it wasnt reporting any sdk's.
This led to me adding an additional Use.NET Core pipeline task at the beginning of the pipeline with the following parameters
Display Name:
Use .NET Code sdk 3.1.415
Package to install:
SDK (contains runtime)
Version:
3.1.415
Compatible Visual Studio version:
16.7.21
Path to Install .Net core:
$(Agent.ToolsDirectory)/dotnet
I chose the above versions which seemed to match what was reported being available on the linux box.
However, none of this seemed to make any difference, as it still fails with the same error as you. I did want to share what I have tried so far, but wasnt enough to solve it.
On a whim, I spun up a Windows-based App server (Free tier) to deploy to as well. I modified my Release pipeline and added a second "Deploy to App Service" step. This second one deploys to the Windows App Service.
Once I did this, the app runs successfully on the Windows App Service after the second deployment step, but not on the Linux app service after the first deployment step.
It turns out the issue was due to a Build pipeline issue.
It was building the API project using a Visual Studio Build (i.e. MsBuild) project step.
In order to target linux, this needs to be replaced with a
dotnet publish (or a dotnet build followed by a publish) with the parameters "-r linux-x64" plus the appropriate output folder.
Build pipelines and release pipelines do not use the same system default working directory. So I think your release pipeline is probably not getting the artifacts in the build pipeline.
There is an easy way to pass artifacts generated in build pipelines to release pipelines:
In your build pipeline, Use publish build artifact task or publish pipeline artifact task to publish your artfact.
In your release pipeline, click "Add an artifact". Then select "Build" as source type and fill in the information about your build. Note the value of "Source alias", which you need to use as the name for artifacts in release pipeline.
Go to the "Azure App Service Deploy" task. In "Package or folder", click browse button, and you can find your artifacts path there.

How to use Azure CloudShell for Continuous Deployment

I am using CloudShell to deploy Azure Services using PowerShell Scripts. It's a great experience.Now I want to use CloudShell to automate deployment. I am maintaining a solution in TFS/VSTS.
Is there any way or workaround to call OR to get the CloudShell instance or to run Powershell on CloudShell using some Batch command or C# or else.
Is any CloudShell API exist to use CloudShell service from .Net
No, there's no API behind the CloudShell, but its just a bash shell that's using Azure CLI and Azure Powershell. You can use those on your own.
You can access the open source project for Azure Cloud Shell on github and run the Azure Cloud Shell container locally using docker. Not sure this meets your need, but it's a cool option nonetheless.
As per my knowledge, Azure Cloud is an interactive, browser-accessible for managing Azure resources. You cannot use Azure Cloud Shell for Continuous Deployment.
Note: Azure Cloud Shell is an interactive, browser-accessible shell for managing Azure resources. It gives you the flexibility of choosing the shell experience that best suits the way you work. Linux users can opt for a Bash experience, while Windows users can opt for PowerShell.
For more details, refer "Overview of Azure Cloud Shell (Preview)"

How to run already developed ASP.NET Core app on Ubuntu?

What is the easiest way to run my existing ASP.NET Core application on Ubuntu? I have found this: https://learn.microsoft.com/en-us/aspnet/core/publishing/linuxproduction but I am stuck on this:
I have published the application and copied it to my Ubuntu, but I have no idea how can I "run the app". Any help will be really appreciated.
It's really as simple as executing:
dotnet path/to/your/application.dll
However, for a website you really want to manage that with some sort of init system. The doc file you link to tells you how to start your application using Systemd.
Create a service definition file e.g. /etc/systemd/system/myapp.service
Edit the file to look like this, replacing the relevant parts where necessary:
[Unit]
Description=Example .NET Web API Application running on Ubuntu
[Service]
WorkingDirectory=/var/path/to/your/app
ExecStart=/usr/bin/dotnet /var/path/to/your/app/hellomvc.dll
Restart=always
RestartSec=10 # Restart service after 10 seconds if dotnet service crashes
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.target
Enable the service like this:
systemctl enable myapp.service
Start the service:
systemctl start myapp.service
Check if your service is running:
systemctl status myapp.service
If you have another init system, the instructions will of course be quite different.
Note: This only starts the app running on your machine. If you intend to serve it to the public, then it is highly recommended that you use a proxy such as Nginx as Microsoft has not yet certified Kestrel as an edge server.

How to run Office365 powershell in Azure Webjob?

I have a program which uses Office 365 Powershell to query O365 information. it works fine. Now, we want to use Azure and put this part into a webjob.
Now, we can pass the credentail into the powershell (using C# code), but when it runs as a webjob, it always shows error, see the screen shot. It says that the cmdlet is not recognized.
I know even on our local machine, we will need to install Windows Azure Active Directory Module to run Office365 powershell. Could the error be because in the Azure, it does not have this module?
So the question is: is there a way to install/integrate this into Azure webjob? or, is there other way that we could run Office 365 powershell in Azure webjob?
Thanks

Categories

Resources