Archive

Archive for the ‘Azure’ Category

Publish .NET Core 1.0 site to Azure


I’m working to my first Angular2 site , and now that something is working i tried to publish on Azure.
First lesson learned: if you use ASP.NET Identity copy only the table structures(of AspNetRoles table, AspNetUsers and so on) to Azure SQL , but not the data: use a DbSeeder code in Startup .cs to create the initial set of Users, Roles, etc. otherwise you never get successfully the token.
The deployment is simple, as the normal (not Core) ASP.NET sites: right clic on the project then Deploy.
It is recommended to download from Azure the WebApp publish profile

when publish from Visual Studio (in my case, Vs2015)

In order to simplify the process.
Now the site started, but i got an error:

connecting to http://yourazuresite.scm.azurewebsites.net/DebugConsole i noticed that there are not visible .json files… note that opening the web.config it says:
“Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
Oh well, but the Vs2015 publisher does not copy appsettings.json.. and the Core packages? where are? i should try with Vs2017…
So I created with the scm file manager an empty appsettings.json file and an empty package.json file, copied from my pc the text for both files, changed the connection string in appsettings.json.
Then i launched “npm install” in the scm console, that reloads the missing Core packages.
Finally, the site is working.

Advertisements
Categories: .NET Core, Angular 2, Azure

Automated start & stop of Azure vm


An Azure vm is useful but can be expensive, so a typical request of the customer is to reduce the expense by stopping the vm by the night and holidays.
In the new Azure portal there is a different automation system than the one in the old classic Azure portal.
The first thing is to create an Azure Automation account,

in Automation Accounts click Add.

Verify that Yes is selectd for “Create Azure Run As account”.
When the Automation account is successfully created, several resources are automatically created for you: example runbooks, Azure certificates, Azure connections.
With my previous operation i created these items:

Note that the Runbooks are only templates, by default are not linked to a scheduler (that causes a billing, but anyway you have for the basic scheduling 500 minutes free every month).
These tutorial scripts can be used as templates for your activities, and in every case clicking on “Runbooks Gallery” you can find a big collection of examples.

In this case I was managing a classic Azure vm, and the request was to stop the vm not only every night but also Saturday, Sunday and Italian Holidays.
The problem was the Monday after Easter: Easter is also a Sunday, but Easter is not at the same date every year, so the Monday after was to be calculated.
For the first thing, the easy stop task.
I clicked on Add a runbook

and created a stop runbook

As

$ConnectionAssetName = "AzureClassicRunAsConnection"

# Get the connection
$connection = Get-AutomationConnection -Name $connectionAssetName        

# Authenticate to Azure with certificate
Write-Verbose "Get connection asset: $ConnectionAssetName" -Verbose
$Conn = Get-AutomationConnection -Name $ConnectionAssetName
if ($Conn -eq $null)
{
    throw "Could not retrieve connection asset: $ConnectionAssetName. Assure that this asset exists in the Automation account."
}

$CertificateAssetName = $Conn.CertificateAssetName
Write-Verbose "Getting the certificate: $CertificateAssetName" -Verbose
$AzureCert = Get-AutomationCertificate -Name $CertificateAssetName
if ($AzureCert -eq $null)
{
    throw "Could not retrieve certificate asset: $CertificateAssetName. Assure that this asset exists in the Automation account."
}

Write-Verbose "Authenticating to Azure with certificate." -Verbose
Set-AzureSubscription -SubscriptionName $Conn.SubscriptionName -SubscriptionId $Conn.SubscriptionID -Certificate $AzureCert
Select-AzureSubscription -SubscriptionId $Conn.SubscriptionID

Stop-AzureVM -ServiceName "mytestvm" -Name "mytestvm" -Force

In practice until the “Select-AzureSubscription” is all code for environment preparation, after you can write your logic.
In this case there is only a basic Stop-AzureVM: copy, paste, Save and Publish.

Then I can link this runbook to a schedule ,

for example every evening at 20:00

If your customers asks “I would to work by the Saturday, sometimes…” then we can create a Webhook

that gives a URL that MUST be copied now, otherwise you can review it after

So you can create an little utility that make a HTTP POST and your customer is able to stop the vm (in this sample) without the need to access the Azure portal.
But the interesting code is the one of the start runbook:

$ConnectionAssetName = "AzureClassicRunAsConnection"

# Get the connection
$connection = Get-AutomationConnection -Name $connectionAssetName        

# Authenticate to Azure with certificate
Write-Verbose "Get connection asset: $ConnectionAssetName" -Verbose
$Conn = Get-AutomationConnection -Name $ConnectionAssetName
if ($Conn -eq $null)
{
    throw "Could not retrieve connection asset: $ConnectionAssetName. Assure that this asset exists in the Automation account."
}

$CertificateAssetName = $Conn.CertificateAssetName
Write-Verbose "Getting the certificate: $CertificateAssetName" -Verbose
$AzureCert = Get-AutomationCertificate -Name $CertificateAssetName
if ($AzureCert -eq $null)
{
    throw "Could not retrieve certificate asset: $CertificateAssetName. Assure that this asset exists in the Automation account."
}

Write-Verbose "Authenticating to Azure with certificate." -Verbose
Set-AzureSubscription -SubscriptionName $Conn.SubscriptionName -SubscriptionId $Conn.SubscriptionID -Certificate $AzureCert
Select-AzureSubscription -SubscriptionId $Conn.SubscriptionID

function Get-DateOfMondayEaster {
    param(
        [Parameter(ValueFromPipeline)]
        $Year=(Get-Date).Year
    )

    Process {
        [pscustomobject]@{} |
            Add-Member -PassThru -MemberType NoteProperty   C3 $Year |
            Add-Member -PassThru -MemberType ScriptProperty C7  { $this.c3%19 } |
            Add-Member -PassThru -MemberType ScriptProperty C8  { [System.Math]::Truncate($this.c3/100) } |
            Add-Member -PassThru -MemberType ScriptProperty C9  { $this.c3%100 } |
            Add-Member -PassThru -MemberType ScriptProperty C10 { [System.Math]::Truncate($this.c8/4) } |
            Add-Member -PassThru -MemberType ScriptProperty C11 { $this.c8%4 } |
            Add-Member -PassThru -MemberType ScriptProperty C12 { [System.Math]::Truncate(($this.c8+8)/25) } |
            Add-Member -PassThru -MemberType ScriptProperty C13 { [System.Math]::Truncate(($this.c8-$this.c12+1)/3) } |
            Add-Member -PassThru -MemberType ScriptProperty C14 { ((19*$this.c7)+$this.c8-$this.c10-$this.c13+15)%30 } |
            Add-Member -PassThru -MemberType ScriptProperty C15 { [System.Math]::Truncate($this.c9/4)} |
            Add-Member -PassThru -MemberType ScriptProperty C16 { $this.c9%4 } |
            Add-Member -PassThru -MemberType ScriptProperty C17 { (32+2*($this.c11+$this.c15)-$this.c14-$this.c16)%7 } |
            Add-Member -PassThru -MemberType ScriptProperty C18 { [System.Math]::Truncate(($this.c7+(11*$this.c14)+(22*$this.c17))/451) } |
            Add-Member -PassThru -MemberType ScriptProperty C19 { [System.Math]::Truncate(($this.c14+$this.c17-(7*$this.c18)+114)/31) } |
            Add-Member -PassThru -MemberType ScriptProperty C20 { ($this.c14+$this.c17-(7*$this.c18)+114)%31 } |
            Add-Member -PassThru -MemberType ScriptProperty C21 { $this.c20+1 } | # day
            Add-Member -PassThru -MemberType ScriptProperty C22 { $this.c19 }   | # month
            Add-Member -PassThru -MemberType ScriptProperty Easter {
                (Get-Date ("{0}/{1}/{2}" -f $this.c22, $this.c21, $this.c3)).AddDays(1)
            }
    }
}

$boolStart = $true

$year = (Get-Date).Year
$stringyear = [string]$year
$dayall = (Get-Date).ToShortDateString()
$day = (Get-Date).DayOfWeek

$newyeareve = "1/1/" + $stringyear
$befana  = "1/6/" + $stringyear
$liberation = "4/25/" + $stringyear
$laborday = "5/1/" + $stringyear
$republic = "6/2/" + $stringyear
$assunta = "8/15/" + $stringyear
$santi = "11/1/" + $stringyear
$immacolata = "12/8/" + $stringyear
$christmas = "12/25/" + $stringyear
$boxing = "12/26/" + $stringyear

if($dayall -eq $newyeareve){
    $boolStart = $false
}
if($dayall -eq $befana){
    $boolStart = $false
}
if($dayall -eq $liberation){
    $boolStart = $false
}
if($dayall -eq $laborday){
    $boolStart = $false
}
if($dayall -eq $republic){
    $boolStart = $false
}
if($dayall -eq $assunta){
    $boolStart = $false
}
if($dayall -eq $santi){
    $boolStart = $false
}
if($dayall -eq $immacolata){
    $boolStart = $false
}
if($dayall -eq $christmas){
    $boolStart = $false
}
if($dayall -eq $boxing){
    $boolStart = $false
}
if ($day -eq 'Saturday' -or $day -eq 'Sunday'){
    $boolStart = $false
}
# calculate Monday after Easter
$MondayEaster = Get-DateOfMondayEaster
$dayMondayEaster = $MondayEaster.Easter.ToShortDateString()
if($dayall -eq $dayMondayEaster){
    $boolStart = $false
}

if($boolStart -eq $true){
   Start-AzureVM -ServiceName "mytestvm" -Name "mytestvm"
   Write-Output "started"
} else {
   Write-Output "Not started"
}

After the common start code we can see an Powershell function Get-DateOfMondayEaster that calculate the Easter day, and then adding 1 day the Monday after.
Obviously if we need an WebBook for absolute starting we must create an “absstart” RunBook with only an Start-AzureVM, without the holiday logic, and create a WebHook for it.

Categories: Azure

Vb6 software on Azure VM

There are still customers that uses old, aged software: for example, i have customers using my old program for truck transports, that uses a Microsoft Access file as database.
As i wrote in this post, this software in installed on a on-premise server, which was tipically Windows server 2003.
Now, these old servers (often recycled) are dying, the hardware today is more cheaper than in the past but an Windows Server 2012 license is still expensive for a small office.
So the idea: let’s try to migrate to Azure!
So i began an incredible amount of try & catch (errors..)
The application requires to send emails via CDO , with an attached pdf file generated from the Crystal Reports engine (used from the app for the printing activities).
In order to print via CDO is required a working email client; in the old Windows 2003 there was by default Outlook Express, instead from 2008 version in Windows Server there is no more an email client: this can be resolved installing Windows Mail, but it requires .NET 3.5
First error: using an Windows Server 2012 DataCenter NOT R2.
The main problem with the “normal” version is, IMHE (In My Humble Experience) that .NET 3.5 is not installable because you can’t use the downloadable 3.5 installer and this must be done from the Server Manager that complains about missing sources (for example see here, but i was not able to achieve the same result) : could be that attaching an ISO file (of Windows Server 2012) it works.
But i lose no more time and try creating instead an Windows Server 2012 R2 vm: same complain about the missing sources, but this time .NET 3.5 installed without issues.
The installation of Windows Mail was without problems and so for my app.
The only problem is the calendar for date input: my vb6 app uses mscal.ocx (perhaps a wrong choice) which is problematic.
For example in the setup.lst file generated from the vb6 package installer wizard the mscal is generated as

File10=@MSCAL.OCX,$(WinSysPath),$(DLLSelfRegisterEx),$(Shared),5/7/98 12:00:00 AM,90112,8.0.0.5007

But the DLLSelfRegisterEx must be changed in DLLSelfRegister otherwise the setup is not successful.
And in every case the problem is that the calendar was not displayed (interface in Italian):


The solution was to recreate from scratch the VM and create an vb6 installer WITHOUT mscal.ocx: it is already present in the Azure vm and trying to install another mscal.ocx causes big troubles in the registry; now the mscal.ocx is working ok.
Another curious thing was that in the vb6 code

With Flds
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = boolUseSsl
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = intSmtpAuth
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = strUser
    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = strPwd
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSmtpSvr
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = intSendUsing
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = intSvrPort
    .Update
End With

“microsoft” was accidentally written with the initial “m” uppercase, so the email sending was not working in the new 2012 R2 server (instead in Windows Server 2003 yes!); after the correction, emails sent.

Autostart of RabbitMQ service in Ubuntu

I have configured a high availability cluster of RabbitMQ instances, on Azure with 2 Ubuntu LTS 16.04 servers.
With an automation runbook these 2 servers are configured for the shutdown at night, and start on morning.
I was believing that the RabbitMQ service was starting by default as in Windows: but the servers was started, RabbitMQ no.
I was working with Unix systems many years ago, so i’m not so expert and could be that there is a better approach, but i found an working solution.
Typically an Unix user at the login runs the commands listed in the $HOME/.profile file, but this works just only when the user makes an login: in practice .profile act as the auto starting profile commands of today Windows server.
In order to automatically start something at the boot as an Windows service (or the autoexec.bat of the long gone ’80s MSDOS days) the file to change is /etc/rc.sysinit
The user configured while creating the Ubuntu machine is not a super user, so using the good old vi for rc.sysinit editing the command line is

sudo service rabbitmq-server start


in a cluster this must be done on the main cluster node, if you then connect via putty to other nodes you will find the file already changed.
(esc wq! … how many times i launched this sequence in ’80s..)
Rebooting all the vm RabbitMQ is immediately working.

Categories: Azure, RabbitMQ, Ubuntu

Passed Microsoft Exam 70-532 Developing Microsoft Azure Solutions

Today i got certified for 70-532 Developing Microsoft Azure Solutions.
The exam is not easy: on the Internet you can find various sources each promising exam dumps and so on (it seems that is sufficient to memorize questions and answers…), but this morning i fighted against questions absolutely new: difficult if you don’t know very well Azure DevOps and relative programming.

Categories: Azure

Delete Azure AD

I had created an AD directory named alextestad in my Azure subscription in order to do some experiments, after that i tried to delete this temp AD, but i received

After some struggle i found how to do.
First must be added an Global admin user to this AD, in my case I added a user named “tobedeleted”



Clicking on create you get a temporary password:

At this point using another browser in a new Private window point to https://manage.windowsazure.com and login with the new account:

And immediately is requested to change the password:

At this point by clicking the button the login is done, obviously there is no subscription

but is not important.
At this point we need to install 2 things on our pc (found info here):
Microsoft Online Services Sign-In Assistant for IT Professionals RTW
Azure Active Directory Module for Windows PowerShell (64 bit)
Done this in the Azure Powershell we can launch

$objCred = get-credential

This causes the request of login where we use the credentials of the new user:

and then

connect-msolservice -credential $objCred

done this , we can launch in Powershell

Get-MsolServicePrincipal | Select DisplayName

This instructions lists all the applications Service Principals in AD, in my case

Now we can delete all not required apps in AD tenant

Get-MsolServicePrincipal | Remove-MsolServicePrincipal

where I got some errors but it seems no problem

Reissuing Get-MsolServicePrincipal | Select DisplayName in my case we had

Now in Azure portal is needed to delete the temp user, with the DELETE button at the end of this page

Done this, finally no errors while trying to delete the AD:

Categories: Azure

Delete hidden VM Image

I was experimenting with Azure, and i created an VM Windows Server 200 R2, for a customer demo.
Giving the fact that the customer is not ready for the cloud … :-)… , i deleted the vm in order to not leave unuseful objects in the portal.
The problem was that i made an vm image, in order to eventually reset to the initial conditions, and after the vm was deleted in the Azure portal there was still the Storage account:
27-11-2015 09-58-32
Then i tried to delete also this storage, but i received
27-11-2015 09-58-51
Let’s search for this vm image.. but apparently this vm image was a ghost: where to find it? In the CONTAINERS tab for the storage account there is nothing, and even exploring the preview portal i found nothing about vm images.
Ok , i’m a beginner with Azure, but should be provided some interface in order to easily discover these things.
After a while i discovered that when you create a new VM from the Gallery
27-11-2015 09-59-53
In the Gallery there are also your VM images:
27-11-2015 10-00-14
And so you can re-read the VM name
With the Powershell command Get-AzureVMImage , filtered by the name , effectively there were all the info about:
27-11-2015 10-14-12
Fortunately Azure PowerShell gives more opportunities than the portal: at this point i deleted the image with Remove-AzureVMImage
27-11-2015 10-16-14
So finally i was able to get rid of the Storage account from the Azure portal:
27-11-2015 10-17-00

Categories: Azure

Error creating an Azure Cloud Service with 2.7 SDK

From some days is available the Azure 2.7 SDK, and i’m trying it with Visual Studio 2013: when Visual Studio was alerting that were available this update i installed it from inside Visual Studio.
Creating an Azure Cloud Service i got the error “Microsoft Azure Tools: Error: The installed Microsoft Azure Compute Emulator does not support the role binaries. Please install the latest Microsoft Azure Compute Emulator and try again.”.
I don’t know if is the real solution valid for all the cases, but anyway re-downloading the Microsoft Azure SDK for .NET – 2.7  from http://www.microsoft.com/en-us/download/details.aspx?id=48178 but installing only the MicrosoftAzureComputeEmulator-x64.exe (x86 if you are using an 32 bit environment, but who has today an 32 bit pc?) finally the project has started.

Categories: .NET, Azure, Vs2013

Azure Tools 2.6 troubles

Some days ago i noticed that was available the 2.6 update of Azure Tools for Visual Studio 2013, and obviously..immediately installed.
Until now i was working on Windows Phone 8.1 projects, then i decided to try an Universal App, and ..error: “No exports were found that match the constraint…”, with a plethora of other error messages; project not created.
After some struggle, the solution : the uninstallation of the Azure Tools 2.6 for Vs 2013, leaving the previous versions 2.5 and 2.4 installed.
Featured image
After that, the creation of an Universal App is successfully.
I’m using Windows 8.1 in English, Visual Studio 2013 in English, both with the latest updates: how is possible that in Microsoft no one got this error while trying to create an Universal App, the new Microsoft Verb ?

Categories: Azure