Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Cloud build service invocation #55

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 All @@ -26,10 +23,10 @@
getter password : String? = nil

@[Clip::Doc("Extract driver info on build")]
getter strict_driver_info : Bool = true

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

View workflow job for this annotation

GitHub Actions / Ameba

Style/QueryBoolMethods

Consider using 'getter?' for 'strict_driver_info'
Raw output
> getter strict_driver_info : Bool = true
         ^

@[Clip::Doc("Discover drivers and compile them")]
getter discover : Bool = false

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

View workflow job for this annotation

GitHub Actions / Ameba

Style/QueryBoolMethods

Consider using 'getter?' for 'discover'
Raw output
> getter discover : Bool = false
         ^

@[Clip::Doc("URI of the git repository")]
@[Clip::Option]
Expand Down Expand Up @@ -60,7 +57,7 @@
args = {entrypoint: entrypoint, commit: ref, crystal_version: crystal_version}
::Log.with_context(**args) do
begin
if (path = repository_path)

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

View workflow job for this annotation

GitHub Actions / Ameba

Style/ParenthesesAroundCondition

Redundant parentheses
Raw output
> if (path = repository_path)
     ^
Log.debug { "local compile" }
builder.local_compile(Path[path].expand, **args)
else
Expand All @@ -71,7 +68,7 @@
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 All @@ -93,7 +90,7 @@
end

if discover
found = if (path = repository_path)

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

View workflow job for this annotation

GitHub Actions / Ameba

Style/ParenthesesAroundCondition

Redundant parentheses
Raw output
> found = if (path = repository_path)
             ^
builder.local_discover_drivers?(Path[path].expand)
else
builder.discover_drivers?(repository_uri, ref, username, password)
Expand All @@ -104,39 +101,9 @@
valid_driver_entrypoints
end

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
Loading