Exemplo n.º 1
0
        public async Task Construct()
        {
            var request = new StartBuildRequest("carbon");

            var result = CodeBuildClient.GetRequestMessage("https://google.com/", request);

            Assert.Equal(@"{""projectName"":""carbon""}", await result.Content.ReadAsStringAsync());
        }
        public async Task <StartBuildResponse> StartBuildAsync(string projectName, string buildSpec)
        {
            var request = new StartBuildRequest
            {
                ProjectName       = projectName,
                BuildspecOverride = buildSpec
            };

            return(await Client.StartBuildAsync(request));
        }
Exemplo n.º 3
0
        private static int StartBuild(string inputFile)
        {
            Console.WriteLine($"Read request from '{inputFile}'");

            StartBuildRequest request = VmSerializer.DeserializeFromFile <StartBuildRequest>(inputFile);

            Console.WriteLine($"Start build request: ");
            Console.WriteLine($"    - HostLowPrivilegeUsername: {request.HostLowPrivilegeUsername ?? string.Empty}");
            Console.WriteLine($"    - HostLowPrivilegePassword: {request.HostLowPrivilegePassword ?? string.Empty}");

            return(0);
        }
        private async Task <object> Create(CloudFormationEvent <RunTerraformRequest> input, ILambdaContext context)
        {
            var client  = new AmazonCodeBuildClient();
            var request = new StartBuildRequest
            {
                EnvironmentVariablesOverride = BuildEnvironmentVariables(input)
            };

            var response = await client.StartBuildAsync(request);

            return(null);
        }
Exemplo n.º 5
0
        private async Task InitVmAsync()
        {
            // (1) Create and serialize 'StartBuild' request.
            string startBuildRequestPath = Path.GetTempFileName();

            using (var password = LowPrivilegeAccountUtils.GetLowPrivilegeBuildPassword())
            {
                //This will be temporary, will fix the problem exposing the password
                var startBuildRequest = new StartBuildRequest
                {
                    HostLowPrivilegeUsername = LowPrivilegeAccountUtils.GetLowPrivilegeBuildAccount(),
                    HostLowPrivilegePassword = LowPrivilegeAccountUtils.GetUnsecuredString(password)
                };
                VmSerializer.SerializeToFile(startBuildRequestPath, startBuildRequest);
            }


            // (2) Create a process to execute VmCommandProxy.
            string arguments = $"{VmCommand.StartBuild} /{VmCommand.Param.InputJsonFile}:\"{startBuildRequestPath}\"";
            var    process   = CreateVmCommandProxyProcess(arguments, Path.GetDirectoryName(startBuildRequestPath));

            var stdOutForStartBuild = new StringBuilder();
            var stdErrForStartBuild = new StringBuilder();

            // (3) Run VmCommandProxy to start build.
            using (var executor = new AsyncProcessExecutor(
                       process,
                       TimeSpan.FromMilliseconds(-1),
                       line => { if (line != null)
                                 {
                                     stdOutForStartBuild.AppendLine(line);
                                 }
                       },
                       line => { if (line != null)
                                 {
                                     stdErrForStartBuild.AppendLine(line);
                                 }
                       }))
            {
                executor.Start();
                await executor.WaitForExitAsync();

                await executor.WaitForStdOutAndStdErrAsync();

                if (executor.Process.ExitCode != 0)
                {
                    string stdOut = $"{Environment.NewLine}StdOut:{Environment.NewLine}{stdOutForStartBuild.ToString()}";
                    string stdErr = $"{Environment.NewLine}StdErr:{Environment.NewLine}{stdErrForStartBuild.ToString()}";
                    throw new BuildXLException($"Failed to init VM '{VmCommandProxy} {arguments}', with exit code {executor.Process.ExitCode}{stdOut}{stdErr}");
                }
            }
        }
Exemplo n.º 6
0
        public async Task Execute(JObject input, ILambdaContext context)
        {
            LambdaLogger.Log($"Received input as {input.ToString()}");

            var request  = input.ToObject <CustomResourceRequest>();
            var response = new CustomResourceResponse();

            // init the response
            response.StackId           = request.stackId;
            response.RequestId         = request.requestId;
            response.LogicalResourceId = request.logicalResourceId;
            response.NoEcho            = false;
            if (string.IsNullOrEmpty(request.physicalResourceId))
            {
                response.PhysicalResourceId = "";
            }
            else
            {
                response.PhysicalResourceId = request.physicalResourceId;
            }
            response.PhysicalResourceId = "CodeBuildAndWait";
            response.Reason             = "See CloudWatch logs for detail";
            response.Status             = "FAILED";
            var resourceProperties = request.resourceProperties.ToObject <CustomResourceProperties>();

            try
            {
                switch (request.requestType.ToLower())
                {
                case "create":
                case "update":
                    var buildRequest = new StartBuildRequest();
                    buildRequest.ProjectName = resourceProperties.CodeBuildProjectName;

                    var buildClient = new AmazonCodeBuildClient();

                    LambdaLogger.Log($"Starting build: {buildRequest.ProjectName}");
                    var buildResponse = await buildClient.StartBuildAsync(buildRequest);

                    if (buildResponse.HttpStatusCode == System.Net.HttpStatusCode.OK)
                    {
                        var statusRequest = new BatchGetBuildsRequest();
                        statusRequest.Ids.Add(buildResponse.Build.Id);

                        while (true)
                        {
                            System.Threading.Thread.Sleep(5000);     // Wait 5 seconds between build status checks

                            var statusResponse = await buildClient.BatchGetBuildsAsync(statusRequest);

                            Debug.Assert(statusResponse.Builds.Count == 1);

                            LambdaLogger.Log($"Build Id: {statusResponse.Builds[0].Id}; Build Status: {statusResponse.Builds[0].BuildStatus}");
                            var buildStatus = statusResponse.Builds[0].BuildStatus;
                            if (buildStatus == StatusType.SUCCEEDED)
                            {
                                response.Status = "SUCCESS";
                                break;
                            }
                            else if (buildStatus == StatusType.IN_PROGRESS)
                            {
                                continue;
                            }
                            else
                            {
                                response.Status = "FAILED";
                                break;
                            }
                        }
                    }
                    else
                    {
                        LambdaLogger.Log($"StartBuild Failed: {buildResponse.HttpStatusCode}: {buildResponse}");
                    }
                    break;

                case "delete":
                    LambdaLogger.Log("Received DELETE request");
                    response.Status = "SUCCESS";
                    break;
                }
            }
            catch (Exception e)
            {
                // Swallow exception, we will just pass it back as-is to CloudFormation
                response.Reason = e.Message;
            }

            context.Logger.LogLine($"Calling CloudFormation callback; status: {response.Status}");
            var http         = new HttpClient();
            var httpResponse = await http.PutAsJsonAsync(request.responseUrl, response.ToJson());

            context.Logger.LogLine($"Callback Response: {httpResponse.StatusCode}");

            await Task.CompletedTask;
        }