Exemplo n.º 1
0
        public async Task Handle(NewPayload newPayload, string replyTo, string correlationId)
        {
            Console.WriteLine($"[i] Got New Payload Message.");
            Payload payload = new Payload();

            payload.AgentType                  = _taskRepository.GetAgentType(newPayload.AgentTypeId);
            payload.AgentTransportType         = _taskRepository.GetAgentTransportType(newPayload.AgentTransportTypeId);
            payload.Transport                  = _taskRepository.GetTransport(newPayload.TransportId);
            payload.AgentTypeArchitectureId    = newPayload.ArchitectureId;
            payload.AgentTypeFormatId          = newPayload.FormatId;
            payload.AgentTypeVersionId         = newPayload.VersionId;
            payload.AgentTypeConfigurationId   = newPayload.AgentTypeConfigurationId;
            payload.AgentTypeOperatingSystemId = newPayload.OperatingSystemId;
            payload.Debug = newPayload.Debug;

            payload.Name           = newPayload.Name;
            payload.Description    = newPayload.Description;
            payload.Jitter         = newPayload.Jitter;
            payload.BeaconInterval = newPayload.BeaconInterval;
            payload.ExpirationDate = newPayload.ExpirationDate;
            payload.BuildToken     = newPayload.BuildToken;

            payload.Created    = DateTime.UtcNow;
            payload.Enabled    = true;
            payload.Visible    = true;
            payload.Built      = false;
            payload.Key        = Utility.GenerateSecureString(32);
            payload.LanguageId = Settings.LanguageId;
            _taskRepository.Add(payload);
            _eventBus.Publish(payload, replyTo, correlationId);

            string workingDir = Path.Join(Settings.AgentsPath, payload.AgentType.Name);

            // Create build config file
            BuildConfig buildConfig = CreateBuildConfig(payload);

            string buildConfigFile = Path.GetTempFileName();

            File.AppendAllText(buildConfigFile, JsonConvert.SerializeObject(buildConfig, Formatting.Indented));

            // Build transport first
            File.Delete(Path.Join(workingDir, payload.AgentTransportType.BuildLocation));

            string transportBuildCommand = $"{payload.AgentTransportType.BuildCommand} {buildConfigFile}";

            Dictionary <string, string> cmdResult = RunCommand(workingDir, transportBuildCommand);
            string transportB64 = "";

            if (cmdResult["ExitCode"] == "0")
            {
                byte[] transportBytes = File.ReadAllBytes(Path.Join(workingDir, payload.AgentTransportType.BuildLocation));
                transportB64 = Convert.ToBase64String(transportBytes);
                File.Delete(buildConfigFile);
            }
            else
            {
                Console.WriteLine($"ERROR DURING TRANSPORT BUILD: \nStdout: {cmdResult["Output"]}\n Stderr: {cmdResult["Error"]}");
                NewErrorMessage response = new NewErrorMessage();
                response.Source  = ".NET Build Server";
                response.Message = $"Error building {payload.AgentType.Name}";
                response.Details = $"Stdout: {cmdResult["Output"]}\n Stderr: {cmdResult["Error"]}";
                _eventBus.Publish(response, replyTo = null, correlationId = null);
            }

            // Build the agent
            if (!String.IsNullOrEmpty(transportB64))
            {
                buildConfig.TransportModule = transportB64;
                File.AppendAllText(buildConfigFile, JsonConvert.SerializeObject(buildConfig, Formatting.Indented));

                File.Delete(Path.Join(workingDir, payload.AgentType.BuildLocation));
                string buildCommand = $"{payload.AgentType.BuildCommand} {buildConfigFile}";
                cmdResult = RunCommand(workingDir, buildCommand);

                if (cmdResult["ExitCode"] == "0")
                {
                    try {
                        Console.WriteLine($"[PayloadBuildService] Build Successful!");
                        string originalPath  = Path.Join(workingDir, payload.AgentType.BuildLocation);
                        string fileExtension = Path.GetExtension(originalPath);
                        string payloadPath   = Path.Join(Settings.AgentsPath, "/build/", $"{payload.AgentType.Name}_{payload.AgentTypeConfiguration.Name}_{payload.Name}_{DateTime.Now.ToString("yyyyMMddHHmmss")}{fileExtension}");
                        Console.WriteLine($"[PayloadBuildService] Moving from {originalPath} to {payloadPath}");
                        File.Move(originalPath, payloadPath);
                        string    uploadUlr = $"{apiUrl}/{payload.Id}/file/";
                        WebClient wc        = new WebClient();
                        wc.Headers.Add("build-token", payload.BuildToken);
                        Console.WriteLine($"[PayloadBuildService] Uploading to {uploadUlr} with token {payload.BuildToken}");
                        byte[] resp = wc.UploadFile(uploadUlr, payloadPath);
                        Console.WriteLine($"[PayloadBuildService] Response: {wc.Encoding.GetString(resp)}");
                        //File.Delete(buildConfigFile);
                    }
                    catch (Exception e) {
                        Console.WriteLine($"ERROR UPLOADING PAYLOAD TO API: \n{e.Message}");
                        NewErrorMessage response = new NewErrorMessage();
                        response.Source  = ".NET Build Server";
                        response.Message = $"Error uploading {payload.AgentType.Name} payload to API";
                        response.Details = $"{e.Message}";
                        _eventBus.Publish(response, replyTo = null, correlationId = null);
                    }
                }
                else
                {
                    Console.WriteLine($"ERROR DURING AGENT BUILD: \nStdout: {cmdResult["Output"]}\n Stderr: {cmdResult["Error"]}");
                    NewErrorMessage response = new NewErrorMessage();
                    response.Source  = ".NET Build Server";
                    response.Message = $"Error building {payload.AgentType.Name}";
                    response.Details = $"Stdout: {cmdResult["Output"]}\n Stderr: {cmdResult["Error"]}";
                    _eventBus.Publish(response, replyTo = null, correlationId = null);
                }
            }
            else
            {
                Console.WriteLine($"ERROR DURING AGENT BUILD: \nStdout: {cmdResult["Output"]}\n Stderr: {cmdResult["Error"]}");
                NewErrorMessage response = new NewErrorMessage();
                response.Source  = ".NET Build Server";
                response.Message = $"Error building {payload.AgentType.Name}";
                response.Details = $"Tried to build an agent without a Base64 encoded transport string. Transport build must have failed.";
                _eventBus.Publish(response, replyTo = null, correlationId = null);
            }
        }