protected override async Task <TaskResult> ExecuteAsync(TaskContext context, WaitDependsOnActivityInput input)
        {
            DeploymentOperation deploymentOperation = new DeploymentOperation(input.DeploymentContext, this.infrastructure, input.Resource)
            {
                InstanceId  = context.OrchestrationInstance.InstanceId,
                ExecutionId = context.OrchestrationInstance.ExecutionId,
                Stage       = input.ProvisioningStage,
                Input       = DataConverter.Serialize(input)
            };

            templateHelper.SaveDeploymentOperation(deploymentOperation);
            using (var db = new DbAccess(this.options.Database.ConnectionString))
            {
                foreach (var item in input.DependsOn)
                {
                    db.AddStatement(this.commandText, new
                    {
                        input.DeploymentContext.RootId,
                        input.DeploymentContext.DeploymentId,
                        context.OrchestrationInstance.InstanceId,
                        context.OrchestrationInstance.ExecutionId,
                        EventName     = input.ProvisioningStage.ToString(),
                        DependsOnName = item
                    });
                }
                await db.ExecuteNonQueryAsync();
            }
            return(new TaskResult()
            {
                Code = 200
            });
        }
示例#2
0
 protected override TaskResult Execute(TaskContext context, ResourceOrchestrationInput input)
 {
     try
     {
         input.Resource.Properties = input.Resource.ExpandProperties(input.Context, functions, infrastructure);
         helper.SaveDeploymentOperation(new DeploymentOperation(input.Context, infrastructure, input.Resource)
         {
             InstanceId  = context.OrchestrationInstance.InstanceId,
             ExecutionId = context.OrchestrationInstance.ExecutionId,
             Stage       = ProvisioningStage.ExpandResourceProperties,
             Input       = this.DataConverter.Serialize(input)
         });
     }
     catch (Exception ex)
     {
         return(new TaskResult()
         {
             Code = 500, Content = ex.Message
         });
     }
     return(new TaskResult()
     {
         Code = 200, Content = this.DataConverter.Serialize(input.Resource)
     });
 }
        protected override TaskResult Execute(TaskContext context, DeploymentOrchestrationInput input)
        {
            TaskResult tr;

            try
            {
                var Deployment = DeploymentOrchestrationInput.Validate(input, templateHelper.ARMfunctions, infrastructure);
                tr = new TaskResult(200, DataConverter.Serialize(Deployment));
            }
            catch (Exception ex)
            {
                tr = new TaskResult()
                {
                    Code = 400, Content = ex.Message
                };
            }
            DeploymentOperation deploymentOperation = new DeploymentOperation(input, this.infrastructure)
            {
                InstanceId  = context.OrchestrationInstance.InstanceId,
                ExecutionId = context.OrchestrationInstance.ExecutionId,
                Stage       = tr.Code == 200 ? ProvisioningStage.ValidateTemplate : ProvisioningStage.ValidateTemplateFailed,
                Input       = DataConverter.Serialize(input),
                Result      = DataConverter.Serialize(tr)
            };

            templateHelper.SaveDeploymentOperation(deploymentOperation);
            return(tr);
        }
 protected override TaskResult Execute(TaskContext context, DeploymentOperation input)
 {
     templateHelper.SaveDeploymentOperation(input);
     return(new TaskResult()
     {
         Code = 200
     });
 }
示例#5
0
        public override async Task <TaskResult> RunTask(OrchestrationContext context, AsyncRequestActivityInput input)
        {
            this.eventName   = input.ProvisioningStage.ToString();
            this.waitHandler = new TaskCompletionSource <string>();
            await context.ScheduleTask <TaskResult>(AsyncRequestActivity.Name, "1.0", input);

            await waitHandler.Task;
            var r = DataConverter.Deserialize <TaskResult>(waitHandler.Task.Result);

            templateHelper.SaveDeploymentOperation(new DeploymentOperation(input.DeploymentContext, infrastructure, input.Resource)
            {
                InstanceId  = input.InstanceId,
                ExecutionId = input.ExecutionId,
                Stage       = r.Code == 200 ? input.ProvisioningStage : (ProvisioningStage)(0 - input.ProvisioningStage),
                Result      = waitHandler.Task.Result
            });
            return(r);
        }
示例#6
0
        protected override async Task <TaskResult> ExecuteAsync(TaskContext context, AsyncRequestActivityInput input)
        {
            templateHelper.SaveDeploymentOperation(new DeploymentOperation(input.DeploymentContext, infrastructure, input.Resource)
            {
                InstanceId  = input.InstanceId,
                ExecutionId = input.ExecutionId,
                Stage       = input.ProvisioningStage
            });
            await asyncRequestActivity.SaveRequest(infrastructure.GetRequestInput(input),
                                                   new OrchestrationInstance()
            {
                InstanceId  = context.OrchestrationInstance.InstanceId,
                ExecutionId = context.OrchestrationInstance.ExecutionId
            });

            return(new TaskResult()
            {
                Code = 200
            });
        }
        public override async Task <TaskResult> RunTask(OrchestrationContext context, CopyOrchestrationInput input)
        {
            var copy = input.Resource as CopyResource;
            ConcurrentBag <string>    msg   = new ConcurrentBag <string>();
            List <Task <TaskResult> > tasks = new List <Task <TaskResult> >();

            foreach (var resource in input.Resources)
            {
                tasks.Add(context.CreateSubOrchestrationInstance <TaskResult>(
                              ResourceOrchestration.Name,
                              "1.0",
                              new ResourceOrchestrationInput()
                {
                    Resource = resource,
                    Context  = input.Context,
                }));
                if (copy.BatchSize > 0 && tasks.Count >= copy.BatchSize)
                {
                    await Task.WhenAny(tasks);

                    List <Task <TaskResult> > temp = new List <Task <TaskResult> >();
                    foreach (var item in tasks)
                    {
                        if (item.IsCompleted)
                        {
                            if (item.Result.Code != 200)
                            {
                                msg.Add(item.Result.Content);
                            }
                        }
                        else
                        {
                            temp.Add(item);
                        }
                    }
                    tasks = temp;
                }
            }
            await Task.WhenAll(tasks);

            foreach (var item in tasks)
            {
                if (item.Result.Code != 200)
                {
                    msg.Add(item.Result.Content);
                }
            }
            if (msg.Count > 0)
            {
                helper.SaveDeploymentOperation(new DeploymentOperation(input.Context, infrastructure, copy)
                {
                    InstanceId  = context.OrchestrationInstance.InstanceId,
                    ExecutionId = context.OrchestrationInstance.ExecutionId,
                    Stage       = ProvisioningStage.Failed,
                    Input       = DataConverter.Serialize(input),
                    Result      = string.Join(Environment.NewLine, msg.ToArray())
                });
                return(new TaskResult()
                {
                    Code = 500, Content = string.Join(Environment.NewLine, msg.ToArray())
                });
            }
            else
            {
                helper.SaveDeploymentOperation(new DeploymentOperation(input.Context, infrastructure, copy)
                {
                    InstanceId  = context.OrchestrationInstance.InstanceId,
                    ExecutionId = context.OrchestrationInstance.ExecutionId,
                    Stage       = ProvisioningStage.Successed,
                    Input       = DataConverter.Serialize(input)
                });
                return(new TaskResult()
                {
                    Code = 200
                });
            }
        }
        public override async Task <TaskResult> RunTask(OrchestrationContext context, string arg)
        {
            DeploymentOrchestrationInput input = this.DataConverter.Deserialize <DeploymentOrchestrationInput>(arg);

            if (string.IsNullOrEmpty(input.RootId))
            {
                input.RootId = input.DeploymentId;
            }

            #region validate template

            // when Template had value, this orchestration call by internal, the template string content already be parsed
            if (input.Template == null || input.Deployments.Count > 0)
            {
                var valid = await context.ScheduleTask <TaskResult>(ValidateTemplateActivity.Name, "1.0", input);

                if (valid.Code != 200)
                {
                    return(valid);
                }
                input = DataConverter.Deserialize <DeploymentOrchestrationInput>(valid.Content);
            }
            else
            {
                helper.SaveDeploymentOperation(new DeploymentOperation(input, infrastructure)
                {
                    InstanceId  = context.OrchestrationInstance.InstanceId,
                    ExecutionId = context.OrchestrationInstance.ExecutionId,
                    Input       = arg,
                    Stage       = ProvisioningStage.ValidateTemplate
                });;
            }

            #endregion validate template

            #region InjectBeforeDeployment

            if (infrastructure.InjectBeforeDeployment)
            {
                var injectBeforeDeploymenteResult = await context.CreateSubOrchestrationInstance <TaskResult>(
                    RequestOrchestration.Name,
                    "1.0",
                    new AsyncRequestActivityInput()
                {
                    InstanceId        = context.OrchestrationInstance.InstanceId,
                    ExecutionId       = context.OrchestrationInstance.ExecutionId,
                    ProvisioningStage = ProvisioningStage.InjectBeforeDeployment,
                    DeploymentContext = input,
                    Resource          = null
                });

                if (injectBeforeDeploymenteResult.Code != 200)
                {
                    helper.SaveDeploymentOperation(new DeploymentOperation(input, infrastructure, null)
                    {
                        InstanceId  = context.OrchestrationInstance.InstanceId,
                        ExecutionId = context.OrchestrationInstance.ExecutionId,
                        Stage       = ProvisioningStage.BeforeDeploymentFailed,
                        Result      = DataConverter.Serialize(injectBeforeDeploymenteResult)
                    });
                    return(injectBeforeDeploymenteResult);
                }
            }

            #endregion InjectBeforeDeployment

            #region Before Deployment

            if (infrastructure.BeforeDeploymentOrchestration != null)
            {
                foreach (var t in infrastructure.BeforeDeploymentOrchestration)
                {
                    var r = await context.CreateSubOrchestrationInstance <TaskResult>(t.Name, t.Version, input);

                    if (r.Code != 200)
                    {
                        helper.SaveDeploymentOperation(new DeploymentOperation(input, infrastructure, null)
                        {
                            InstanceId  = context.OrchestrationInstance.InstanceId,
                            ExecutionId = context.OrchestrationInstance.ExecutionId,
                            Stage       = ProvisioningStage.BeforeDeploymentFailed,
                            Result      = DataConverter.Serialize(r)
                        });
                        return(r);
                    }

                    input = DataConverter.Deserialize <DeploymentOrchestrationInput>(r.Content);
                }
            }

            #endregion Before Deployment

            #region DependsOn

            if (input.DependsOn.Count > 0)
            {
                waitHandler = new TaskCompletionSource <string>();
                await context.ScheduleTask <TaskResult>(WaitDependsOnActivity.Name, "1.0",
                                                        new WaitDependsOnActivityInput()
                {
                    ProvisioningStage = ProvisioningStage.DependsOnWaited,
                    DeploymentContext = input,
                    Resource          = null,
                    DependsOn         = input.DependsOn
                });

                await waitHandler.Task;
                var r = DataConverter.Deserialize <TaskResult>(waitHandler.Task.Result);
                if (r.Code != 200)
                {
                    helper.SaveDeploymentOperation(new DeploymentOperation(input, infrastructure, null)
                    {
                        InstanceId  = context.OrchestrationInstance.InstanceId,
                        ExecutionId = context.OrchestrationInstance.ExecutionId,
                        Stage       = ProvisioningStage.DependsOnWaitedFailed,
                        Result      = DataConverter.Serialize(r)
                    });
                    return(r);
                }
            }

            #endregion DependsOn

            bool hasFailResource = false;

            #region Provisioning resources
            ConcurrentBag <Task <TaskResult> > tasks = new ConcurrentBag <Task <TaskResult> >();

            Dictionary <string, List <Resource> > copyDic = new Dictionary <string, List <Resource> >();
            foreach (var resource in input.Template.Resources)
            {
                if (resource.FullType == infrastructure.BuiltinServiceTypes.Deployments ||
                    resource.Type == Copy.ServiceType)
                {
                    continue;
                }
                if (!string.IsNullOrEmpty(resource.CopyId))
                {
                    if (!copyDic.TryGetValue(resource.CopyName, out List <Resource> rList))
                    {
                        rList = new List <Resource>();
                        copyDic.Add(resource.CopyName, rList);
                    }
                    rList.Add(resource);
                    continue;
                }
                tasks.Add(context.CreateSubOrchestrationInstance <TaskResult>(
                              ResourceOrchestration.Name,
                              "1.0",
                              new ResourceOrchestrationInput()
                {
                    Resource = resource,
                    Context  = input,
                }));
            }
            foreach (var deploy in input.Deployments)
            {
                tasks.Add(context.CreateSubOrchestrationInstance <TaskResult>(
                              DeploymentOrchestration.Name,
                              "1.0",
                              DataConverter.Serialize(deploy.Value)));
            }
            foreach (var key in copyDic.Keys)
            {
                var c = input.Template.Resources[key] as CopyResource;
                tasks.Add(context.CreateSubOrchestrationInstance <TaskResult>(CopyOrchestration.Name, "1.0", new CopyOrchestrationInput()
                {
                    Resource  = c,
                    Context   = input,
                    Resources = copyDic[key]
                }));
            }

            await Task.WhenAll(tasks.ToArray());

            foreach (var t in tasks)
            {
                if (t.Result.Code != 200)
                {
                    hasFailResource = true;
                    break;
                }
            }

            #endregion Provisioning resources

            if (input.Mode == DeploymentMode.Complete)
            {
                // TODO: complete mode, delete resource not exist in template
            }
            string rtv = null;

            #region After Deployment

            if (infrastructure.AfterDeploymentOrhcestration != null)
            {
                foreach (var t in infrastructure.AfterDeploymentOrhcestration)
                {
                    var r = await context.CreateSubOrchestrationInstance <TaskResult>(t.Name, t.Version, input);

                    if (r.Code != 200)
                    {
                        helper.SaveDeploymentOperation(new DeploymentOperation(input, infrastructure, null)
                        {
                            InstanceId  = context.OrchestrationInstance.InstanceId,
                            ExecutionId = context.OrchestrationInstance.ExecutionId,
                            Stage       = ProvisioningStage.AfterDeploymentOrhcestrationFailed,
                            Result      = DataConverter.Serialize(r)
                        });
                        return(r);
                    }
                    input = DataConverter.Deserialize <DeploymentOrchestrationInput>(r.Content);
                }
            }

            #endregion After Deployment

            if (infrastructure.InjectAfterDeployment)
            {
                var injectAfterDeploymenteResult = await context.CreateSubOrchestrationInstance <TaskResult>(
                    RequestOrchestration.Name,
                    "1.0",
                    new AsyncRequestActivityInput()
                {
                    InstanceId        = context.OrchestrationInstance.InstanceId,
                    ExecutionId       = context.OrchestrationInstance.ExecutionId,
                    ProvisioningStage = ProvisioningStage.InjectAfterDeployment,
                    DeploymentContext = input,
                    Resource          = null
                });

                if (injectAfterDeploymenteResult.Code != 200)
                {
                    return(injectAfterDeploymenteResult);
                }
            }

            #region get template outputs

            if (!string.IsNullOrEmpty(input.Template.Outputs))
            {
                try
                {
                    rtv = this.GetOutputs(input);
                }
                catch (Exception ex)
                {
                    hasFailResource = true;
                    rtv             = ex.Message;
                }
            }

            #endregion get template outputs

            helper.SaveDeploymentOperation(new DeploymentOperation(input, infrastructure, null)
            {
                InstanceId  = context.OrchestrationInstance.InstanceId,
                ExecutionId = context.OrchestrationInstance.ExecutionId,
                Stage       = hasFailResource ? ProvisioningStage.Failed : ProvisioningStage.Successed,
                Result      = rtv
            });
            return(new TaskResult()
            {
                Code = hasFailResource ? 500 : 200, Content = rtv
            });
        }
示例#9
0
        public override async Task <TaskResult> RunTask(OrchestrationContext context, ResourceOrchestrationInput input)
        {
            #region DependsOn

            if (input.Resource.DependsOn.Count > 0)
            {
                dependsOnWaitHandler = new TaskCompletionSource <string>();
                await context.ScheduleTask <TaskResult>(WaitDependsOnActivity.Name, "1.0",
                                                        new WaitDependsOnActivityInput()
                {
                    ProvisioningStage = ProvisioningStage.DependsOnWaited,
                    DeploymentContext = input.Context,
                    Resource          = input.Resource,
                    DependsOn         = input.Resource.DependsOn
                });

                await dependsOnWaitHandler.Task;
                var r = DataConverter.Deserialize <TaskResult>(dependsOnWaitHandler.Task.Result);
                if (r.Code != 200)
                {
                    templateHelper.SaveDeploymentOperation(new DeploymentOperation(input.Context, infrastructure, input.Resource)
                    {
                        InstanceId  = context.OrchestrationInstance.InstanceId,
                        ExecutionId = context.OrchestrationInstance.ExecutionId,
                        Stage       = ProvisioningStage.DependsOnWaitedFailed,
                        Input       = DataConverter.Serialize(input),
                        Result      = DataConverter.Serialize(r)
                    });
                    return(r);
                }
            }

            #endregion DependsOn

            #region Evaluate functions
            var expandResult = await context.ScheduleTask <TaskResult>(ExpandResourcePropertiesActivity.Name, "1.0", input);

            if (expandResult.Code != 200)
            {
                templateHelper.SaveDeploymentOperation(new DeploymentOperation(input.Context, infrastructure, input.Resource)
                {
                    InstanceId  = context.OrchestrationInstance.InstanceId,
                    ExecutionId = context.OrchestrationInstance.ExecutionId,
                    Stage       = ProvisioningStage.ExpandResourcePropertiesFailed,
                    Input       = this.DataConverter.Serialize(input)
                });
                return(expandResult);
            }

            #endregion Evaluate functions

            input.Resource = DataConverter.Deserialize <Resource>(expandResult.Content);

            #region plug-in


            if (infrastructure.InjectBefroeProvisioning)
            {
                var injectBefroeProvisioningResult = await context.CreateSubOrchestrationInstance <TaskResult>(
                    RequestOrchestration.Name,
                    "1.0",
                    new AsyncRequestActivityInput()
                {
                    InstanceId        = context.OrchestrationInstance.InstanceId,
                    ExecutionId       = context.OrchestrationInstance.ExecutionId,
                    ProvisioningStage = ProvisioningStage.InjectBefroeProvisioning,
                    DeploymentContext = input.Context,
                    Resource          = input.Resource
                });

                if (injectBefroeProvisioningResult.Code != 200)
                {
                    return(injectBefroeProvisioningResult);
                }
            }

            if (infrastructure.BeforeResourceProvisioningOrchestation != null)
            {
                foreach (var t in infrastructure.BeforeResourceProvisioningOrchestation)
                {
                    var r = await context.CreateSubOrchestrationInstance <TaskResult>(t.Name, t.Version, input);

                    if (r.Code != 200)
                    {
                        templateHelper.SaveDeploymentOperation(new DeploymentOperation(input.Context, infrastructure, input.Resource)
                        {
                            InstanceId  = context.OrchestrationInstance.InstanceId,
                            ExecutionId = context.OrchestrationInstance.ExecutionId,
                            Stage       = ProvisioningStage.BeforeResourceProvisioningFailed,
                            Input       = DataConverter.Serialize(input),
                            Result      = DataConverter.Serialize(r)
                        });
                        return(r);
                    }

                    input = DataConverter.Deserialize <ResourceOrchestrationInput>(r.Content);
                }
            }


            #endregion plug-in

            #region Provisioning Resource
            var createResourceResult = await context.CreateSubOrchestrationInstance <TaskResult>(
                RequestOrchestration.Name,
                "1.0",
                new AsyncRequestActivityInput()
            {
                InstanceId        = context.OrchestrationInstance.InstanceId,
                ExecutionId       = context.OrchestrationInstance.ExecutionId,
                ProvisioningStage = ProvisioningStage.ProvisioningResource,
                DeploymentContext = input.Context,
                Resource          = input.Resource
            });

            if (createResourceResult.Code != 200)
            {
                return(createResourceResult);
            }


            #endregion Provisioning Resource

            #region After Resource Provisioning

            if (infrastructure.AfterResourceProvisioningOrchestation != null)
            {
                foreach (var t in infrastructure.AfterResourceProvisioningOrchestation)
                {
                    var r = await context.CreateSubOrchestrationInstance <TaskResult>(t.Name, t.Version, input);

                    if (r.Code != 200)
                    {
                        templateHelper.SaveDeploymentOperation(new DeploymentOperation(input.Context, infrastructure, input.Resource)
                        {
                            InstanceId  = context.OrchestrationInstance.InstanceId,
                            ExecutionId = context.OrchestrationInstance.ExecutionId,
                            Stage       = ProvisioningStage.AfterResourceProvisioningOrchestationFailed,
                            Input       = DataConverter.Serialize(input),
                            Result      = DataConverter.Serialize(r)
                        });
                        return(r);
                    }

                    input = DataConverter.Deserialize <ResourceOrchestrationInput>(r.Content);
                }
            }

            #endregion After Resource Provisioning

            if (infrastructure.InjectAfterProvisioning)
            {
                if (infrastructure.InjectBeforeDeployment)
                {
                    var injectAfterProvisioningResult = await context.CreateSubOrchestrationInstance <TaskResult>(
                        RequestOrchestration.Name,
                        "1.0",
                        new AsyncRequestActivityInput()
                    {
                        InstanceId        = context.OrchestrationInstance.InstanceId,
                        ExecutionId       = context.OrchestrationInstance.ExecutionId,
                        ProvisioningStage = ProvisioningStage.InjectAfterProvisioning,
                        DeploymentContext = input.Context,
                        Resource          = input.Resource
                    });

                    if (injectAfterProvisioningResult.Code != 200)
                    {
                        return(injectAfterProvisioningResult);
                    }
                }
            }


            #region Ready Resource

            templateHelper.SaveDeploymentOperation(new DeploymentOperation(input.Context, infrastructure, input.Resource)
            {
                InstanceId  = context.OrchestrationInstance.InstanceId,
                ExecutionId = context.OrchestrationInstance.ExecutionId,
                Stage       = ProvisioningStage.Successed,
                Input       = DataConverter.Serialize(input)
            });

            #endregion Ready Resource

            return(new TaskResult()
            {
                Code = 200
            });
        }