Skip to content

Commit

Permalink
refactor: Cloud build service invocation (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
naqvis authored Nov 30, 2023
1 parent 1014c12 commit 206698a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 34 deletions.
1 change: 1 addition & 0 deletions src/placeos-build/api/driver.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
35 changes: 1 addition & 34 deletions src/placeos-build/cli/build.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
require "placeos-log-backend"
require "http/client"
require "uri"
require "http/headers"
require "../driver_store/s3"

module PlaceOS::Build
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Check notice on line 106 in src/placeos-build/cli/build.cr

View workflow job for this annotation

GitHub Actions / Ameba

Style/PredicateName

Favour method name 'driver?' over 'is_driver?'
Raw output
> protected def self.is_driver?(path : Path)
            ^

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
35 changes: 35 additions & 0 deletions src/placeos-build/cli/cloud_builder.cr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 206698a

Please sign in to comment.