3

I have found how to upload/manage Azure Batch job Application Packages through the UI:

https://learn.microsoft.com/en-us/azure/batch/batch-application-packages

And how to upload and manage Resource Packages programmatically:

https://github.com/Azure/azure-batch-samples/tree/master/CSharp/GettingStarted/02_PoolsAndResourceFiles

But I can't quite seem to put 2 and 2 together on how to manage Application Packages programmatically. Is there an API endpoint we can call to upload/manage an Application Package when setting up a batch job?

Lane Goolsby
  • 594
  • 1
  • 8
  • 26
  • Hi, have you found a way to upload the Jop Application package? – Robar Oct 19 '17 at 14:46
  • I did but I do not remember how and am no longer on that project so I can't reference the code. I think @fpark's comments below helped me. Sorry I couldn't be more help. :( – Lane Goolsby Oct 19 '17 at 15:31

2 Answers2

10

Since this is not quite straightforward, I'll write down my findings. These are the steps to programmatically upload Application Packages via an application that is unattended - no user input (e.g. Azure credentials) is needed.

In Azure Portal:

  • Create the Azure Batch application
  • Create a new Azure AD application (as Application Type use Web app / API)
  • Follow these steps to create the secret key and assign the role to the Azure Batch account
  • Note down the following credentials/ids:
    • Azure AD application id
    • Azure AD application secret key
    • Azure AD tenant id
    • Subscription id
    • Batch account name
    • Batch account resource group name

In your code:

Put together the whole code looks something like this:

private const string ResourceUri = "https://management.core.windows.net/";
private const string AuthUri = "https://login.microsoftonline.com/" + "{TenantId}";
private const string ApplicationId = "{ApplicationId}";
private const string ApplicationSecretKey = "{ApplicationSecretKey}";
private const string SubscriptionId = "{SubscriptionId}";
private const string ResourceGroupName = "{ResourceGroupName}";
private const string BatchAccountName = "{BatchAccountName}";

private async Task UploadApplicationPackageAsync() {
    // get the access token
    var authContext = new AuthenticationContext(AuthUri);
    var authResult = await authContext.AcquireTokenAsync(ResourceUri, new ClientCredential(ApplicationId, ApplicationSecretKey)).ConfigureAwait(false);

    // create the BatchManagementClient and set the subscription id
    var bmc = new BatchManagementClient(new TokenCredentials(authResult.AccessToken)) {
        SubscriptionId = SubscriptionId
    };

    // create the application package
    var createResult = await bmc.ApplicationPackage.CreateWithHttpMessagesAsync(ResourceGroupName, BatchAccountName, "MyPackage", "1.0").ConfigureAwait(false);

    // upload the package to the blob storage
    var cloudBlockBlob = new CloudBlockBlob(new Uri(createResult.Body.StorageUrl));
    cloudBlockBlob.Properties.ContentType = "application/x-zip-compressed";
    await cloudBlockBlob.UploadFromFileAsync("myZip.zip").ConfigureAwait(false);

    // create the application package
    var activateResult = await bmc.ApplicationPackage.ActivateWithHttpMessagesAsync(ResourceGroupName, BatchAccountName, "MyPackage", "1.0", "zip").ConfigureAwait(false);
}
Robar
  • 1,929
  • 2
  • 30
  • 61
1

Azure Batch Application Packages management operations occur on the management plane. The MSDN docs for this namespace are here:

https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.management.batch

The nuget package for Microsoft.Azure.Management.Batch is here:

https://www.nuget.org/packages/Microsoft.Azure.Management.Batch/

And the following sample shows management plane operations in C#, although it is for non-application package operations:

https://github.com/Azure/azure-batch-samples/tree/master/CSharp/AccountManagement

fpark
  • 2,304
  • 2
  • 14
  • 21