Example #1
0
        public static async Task InitApiApps(this ResourceGroup resourceGroup)
        {
            if (!resourceGroup.Tags.ContainsKey(Constants.CommonApiAppsDeployed) ||
                !resourceGroup.Tags[Constants.CommonApiAppsDeployed].Equals(Constants.CommonApiAppsDeployedVersion))
            {
                var csmTemplateString = string.Empty;

                using (var reader = new StreamReader(SimpleSettings.CommonApiAppsCsmTemplatePath))
                {
                    csmTemplateString = await reader.ReadToEndAsync();
                }

                var gatewayName = resourceGroup.Gateways.Count() != 0
                    ? resourceGroup.Gateways.Select(s => s.GatewayName).First()
                    : Guid.NewGuid().ToString().Replace("-", "");
                csmTemplateString = csmTemplateString.Replace("{{gatewayName}}", gatewayName);

                var deployment = new CsmDeployment
                {
                    DeploymentName    = resourceGroup.ResourceUniqueId,
                    SubscriptionId    = resourceGroup.SubscriptionId,
                    ResourceGroupName = resourceGroup.ResourceGroupName,
                    CsmTemplate       = JsonConvert.DeserializeObject <JToken>(csmTemplateString),
                };

                await RetryHelper.Retry(() => deployment.Deploy(block: true), 3);

                resourceGroup.Tags[Constants.CommonApiAppsDeployed] = Constants.CommonApiAppsDeployedVersion;
                await resourceGroup.Update();

                await resourceGroup.Load();
            }
        }
Example #2
0
 public Task <JToken> CreateDeployment(object template, bool block, SubscriptionType subscriptionType)
 {
     Deployment = new CsmDeployment
     {
         DeploymentName    = ResourceGroup.ResourceUniqueId,
         SubscriptionId    = ResourceGroup.SubscriptionId,
         ResourceGroupName = ResourceGroup.ResourceGroupName,
         CsmTemplate       = template,
     };
     return(Deployment.Deploy(block: block, subscriptionType: subscriptionType));
 }
Example #3
0
        public Task <JToken> CreateDeployment(object template, bool block)
        {
            Deployment = new CsmDeployment
            {
                DeploymentName    = ResourceGroup.ResourceUniqueId,
                SubscriptionId    = ResourceGroup.SubscriptionId,
                ResourceGroupName = ResourceGroup.ResourceGroupName,
                CsmTemplate       = template
            };

            return(RetryHelper.Retry(() => Deployment.Deploy(block: block), 3));
        }
 public static Task <string> GetStatus(this CsmDeployment deployment)
 {
     return(Task.FromResult("In progress"));
 }
        public static async Task <JToken> Deploy(this CsmDeployment csmDeployment, bool block = false, SubscriptionType subscriptionType = SubscriptionType.AppService)
        {
            var csmResponse = await GetClient(subscriptionType).HttpInvoke(HttpMethod.Put, ArmUriTemplates.CsmTemplateDeployment.Bind(csmDeployment), csmDeployment.CsmTemplate);

            await csmResponse.EnsureSuccessStatusCodeWithFullError();

            var content = await csmResponse.Content.ReadAsAsync <JToken>();

            if (!block)
            {
                return(content);
            }

            Func <JToken, string> getProvisioningState = (jtoken) =>
            {
                return(jtoken["properties"] != null && jtoken["properties"]["provisioningState"] != null
                ? jtoken["properties"]["provisioningState"].ToString()
                : string.Empty);
            };

            var result = string.Empty;
            var count  = 0;

            do
            {
                result = getProvisioningState(content);

                if (string.IsNullOrEmpty(result))
                {
                    throw new Exception(string.Format("Response doesn't have properties or provisioningState: {0}", content.ToString(Newtonsoft.Json.Formatting.None)));
                }
                else if (result.Equals("Accepted") || result.Equals("Running"))
                {
                    await Task.Delay(10000);

                    count++;
                }
                else if (result.Equals("Succeeded"))
                {
                    return(content);
                }
                else if (result.Equals("Failed"))
                {
                    throw new Exception(
                              string.Format("Deploying CSM template failed, ID: {0}: {1}: {2}: {3}",
                                            content["properties"]["timestamp"],
                                            content["properties"]["correlationid"],
                                            content["id"],
                                            content["properties"]["error"]));
                }
                else
                {
                    SimpleTrace.Diagnostics.Error("Unknown status code from CSM {provisioningState}", result);
                    throw new Exception("Unknown status " + result);
                }

                csmResponse = await GetClient(subscriptionType).HttpInvoke(HttpMethod.Get, ArmUriTemplates.CsmTemplateDeployment.Bind(csmDeployment));

                await csmResponse.EnsureSuccessStatusCodeWithFullError();

                content = await csmResponse.Content.ReadAsAsync <JToken>();
            } while (block && count < 30 * 6);
            if (count >= 179 && !result.Equals("Succeeded"))
            {
                throw new Exception(string.Format("Deploying CSM template taking too long, ID: {0}", content["id"]));
            }
            return(content);
        }