static int Main(string[] args)
{
var consoleTraceListener = new OurConsoleTraceListener();
OurTrace.AddListener(consoleTraceListener);
OurTrace.Source.Switch.Level = SourceLevels.All;
if(args.Length < 1)
{
Usage();
return 0;
}
var tryToUseUpgradeDeployment = false;
var fallbackToReplaceDeployment = false;
var doNotRedeploy = false;
if (args.Length > 1)
{
if (args.Contains("--try-to-use-upgrade-deployment"))
tryToUseUpgradeDeployment = true;
if (args.Contains("--fallback-to-replace-deployment"))
fallbackToReplaceDeployment = true;
if (args.Contains("--delete"))
doNotRedeploy = true;
}
var configuration = ConfigurationParser.ParseConfiguration(args[0]);
var certificate = new X509Certificate2(configuration.CertFileName, configuration.CertPassword);
var http = new Http(certificate);
var azureDeploymentDeploymentLowLevelApi = new AzureManagementLowLevelApi(http);
var managementApiWithRetries = new AzureManagementApiWithRetries(azureDeploymentDeploymentLowLevelApi, configuration.MaxRetries, TimeSpan.FromSeconds(configuration.RetryIntervalInSeconds));
try
{
var deploymentSlotManager = new AzureDeploymentSlot(managementApiWithRetries, configuration.DeploymentSlotUri);
if (doNotRedeploy)
{
deploymentSlotManager.DeleteDeployment();
return 0;
}
var subscriptionId = configuration.DeploymentSlotUri.SubscriptionId;
if (configuration.StorageAccountKey == null || configuration.StorageAccountName == null)
{
OurTrace.TraceInfo("Attempting to guess account name and key based on certificate.");
if (string.IsNullOrWhiteSpace(configuration.StorageAccountName))
{
OurTrace.TraceInfo("Looking up storage accounts for the subscription.");
var storageAccounts = azureDeploymentDeploymentLowLevelApi.ListStorageAccounts(subscriptionId);
configuration.StorageAccountName = storageAccounts.FirstOrDefault();
if (string.IsNullOrWhiteSpace(configuration.StorageAccountName))
{
OurTrace.TraceError("Couldn't find any suitable storage accounts.");
throw new InvalidOperationException("No suitable storage accounts.");
}
if (!string.IsNullOrWhiteSpace(configuration.BlobPathToDeploy))
{
// don't allow BlobPathToDeploy if we're guessing the storage account.
OurTrace.TraceInfo("Ignoring BlobPathToDeploy because we're guessing the storage account.");
configuration.BlobPathToDeploy = null;
}
}
if (string.IsNullOrWhiteSpace(configuration.StorageAccountKey))
{
OurTrace.TraceInfo(string.Format("Looking up storage keys for account: {0}", configuration.StorageAccountName));
var storageKeys = azureDeploymentDeploymentLowLevelApi.GetStorageAccountKeys(subscriptionId, configuration.StorageAccountName);
configuration.StorageAccountKey = storageKeys.FirstOrDefault();
if (string.IsNullOrWhiteSpace(configuration.StorageAccountKey))
{
OurTrace.TraceError(string.Format("Couldn't find any keys for storage account: {0}", configuration.StorageAccountName));
throw new InvalidOperationException("No suitable storage account keys.");
}
}
}
var csPkg = configuration.PackageFileName;
if (!string.IsNullOrWhiteSpace(configuration.ChangeVMSize))
{
csPkg = Path.GetTempFileName();
File.Copy(configuration.PackageFileName, csPkg, true);
ChangeVmSize(csPkg, configuration.ChangeVMSize);
}
UploadBlob(csPkg, configuration.PackageUrl, configuration.StorageAccountName, configuration.StorageAccountKey);
if(!string.IsNullOrWhiteSpace(configuration.BlobPathToDeploy))
DeployBlobs(configuration.BlobPathToDeploy, configuration.StorageAccountName, configuration.StorageAccountKey);
if (tryToUseUpgradeDeployment && managementApiWithRetries.DoesDeploymentExist(configuration.DeploymentSlotUri))
{
try
{
deploymentSlotManager.UpgradeDeployment(configuration);
}
catch(BadRequestException ex)
{
OurTrace.TraceError(string.Format("Upgrade failed with message: {0}\r\n, **** {1}", ex, fallbackToReplaceDeployment ? "falling back to replace." : "exiting."));
// retry using CreateOrReplaceDeployment, since we might have tried to do something that isn't allowed with UpgradeDeployment.
if (fallbackToReplaceDeployment)
deploymentSlotManager.CreateOrReplaceDeployment(configuration);
else
throw;
}
}
else
{
deploymentSlotManager.CreateOrReplaceDeployment(configuration);
}
DeleteBlob(configuration.PackageUrl, configuration.StorageAccountName, configuration.StorageAccountKey);
}
catch(Exception ex)
{
OurTrace.TraceError(string.Format("exception!\n{0}", ex));
return -1;
}
return 0;
}