From 6a602f960c05862c6fca937af3e7780d449151ac Mon Sep 17 00:00:00 2001 From: Ali Naqvi Date: Thu, 30 Nov 2023 22:42:28 +0800 Subject: [PATCH] refactor: Cloud build service invocation --- src/placeos-build/api/driver.cr | 1 + src/placeos-build/cli/build.cr | 35 +------------------------- src/placeos-build/cli/cloud_builder.cr | 35 ++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 34 deletions(-) create mode 100644 src/placeos-build/cli/cloud_builder.cr diff --git a/src/placeos-build/api/driver.cr b/src/placeos-build/api/driver.cr index 1ed6085..28bac38 100644 --- a/src/placeos-build/api/driver.cr +++ b/src/placeos-build/api/driver.cr @@ -60,6 +60,7 @@ module PlaceOS::Build::Api in Build::Compilation::NotFound head code: :not_found in Build::Compilation::Success + PlaceOS::Build.call_cloud_build_service(repository_uri, branch || "HEAD", file, commit, username: username, password: password) path = builder.binary_store.path(result.executable) response.content_type = "application/octet-stream" diff --git a/src/placeos-build/cli/build.cr b/src/placeos-build/cli/build.cr index e0a45f0..8919c92 100644 --- a/src/placeos-build/cli/build.cr +++ b/src/placeos-build/cli/build.cr @@ -1,7 +1,4 @@ require "placeos-log-backend" -require "http/client" -require "uri" -require "http/headers" require "../driver_store/s3" module PlaceOS::Build @@ -71,7 +68,7 @@ module PlaceOS::Build password: password, ) end - call_cloud_build_service(entrypoint, ref, username: username, password: password) + PlaceOS::Build.call_cloud_build_service(repository_uri, branch, entrypoint, ref, username: username, password: password) rescue e Log.warn(exception: e) { "failed to compile #{entrypoint}" } end @@ -107,36 +104,6 @@ module PlaceOS::Build protected def self.is_driver?(path : Path) !path.to_s.ends_with?("_spec.cr") && File.read_lines(path).any? &.includes?("< PlaceOS::Driver") end - - private def call_cloud_build_service(entrypoint, commit, username, password) - headers = HTTP::Headers.new - if token = BUILD_GIT_TOKEN - headers["X-Git-Token"] = token - elsif (user = username) && (pwd = password) - headers["X-Git-Username"] = user - headers["X-Git-Password"] = pwd - end - uri = URI.encode_www_form(entrypoint) - params = HTTP::Params{ - "url" => repository_uri, - "branch" => branch, - "commit" => commit, - } - - client = HTTP::Client.new(URI.parse(BUILD_SERVICE_URL)) - begin - ["amd64", "arm64"].each do |arch| - Log.debug { "Sending #{entrypoint} compilation request for architecture #{arch}" } - resp = client.post("/api/build/v1/#{arch}/#{uri}?#{params}", headers: headers) - unless resp.status_code == 202 - Log.warn { "Compilation request for #{arch} returned status code #{resp.status_code}, while 202 expected" } - Log.debug { "Cloud build service returned with response: #{resp.body}" } - end - end - rescue e - Log.warn(exception: e) { "failed to invoke cloud build service #{entrypoint}" } - end - end end end end diff --git a/src/placeos-build/cli/cloud_builder.cr b/src/placeos-build/cli/cloud_builder.cr new file mode 100644 index 0000000..ae382cf --- /dev/null +++ b/src/placeos-build/cli/cloud_builder.cr @@ -0,0 +1,35 @@ +require "http/client" +require "uri" +require "http/headers" + +module PlaceOS::Build + def self.call_cloud_build_service(repository_uri : String, branch : String, entrypoint : String, commit : String, username : String?, password : String?) + headers = HTTP::Headers.new + if token = BUILD_GIT_TOKEN + headers["X-Git-Token"] = token + elsif (user = username) && (pwd = password) + headers["X-Git-Username"] = user + headers["X-Git-Password"] = pwd + end + uri = URI.encode_www_form(entrypoint) + params = HTTP::Params{ + "url" => repository_uri, + "branch" => branch, + "commit" => commit, + } + + client = HTTP::Client.new(URI.parse(BUILD_SERVICE_URL)) + begin + ["amd64", "arm64"].each do |arch| + Log.debug { "Sending #{entrypoint} compilation request for architecture #{arch}" } + resp = client.post("/api/build/v1/#{arch}/#{uri}?#{params}", headers: headers) + unless resp.status_code == 202 + Log.warn { "Compilation request for #{arch} returned status code #{resp.status_code}, while 202 expected" } + Log.debug { "Cloud build service returned with response: #{resp.body}" } + end + end + rescue e + Log.warn(exception: e) { "failed to invoke cloud build service #{entrypoint}" } + end + end +end