From 6fa2026b6117b0f4a47dd23f1e069ed62698eb94 Mon Sep 17 00:00:00 2001 From: Ali Naqvi Date: Tue, 6 May 2025 14:47:35 +0800 Subject: [PATCH] refactor: PPT-2039 refactored to work with latest awscr shard version --- shard.lock | 2 +- spec/api_spec.cr | 5 ++-- src/ext/url.cr | 25 -------------------- src/placeos-build-api/controllers/driver.cr | 10 ++++---- src/placeos-build-api/controllers/monitor.cr | 4 ++-- src/placeos-build-api/s3.cr | 4 ++-- 6 files changed, 12 insertions(+), 38 deletions(-) delete mode 100644 src/ext/url.cr diff --git a/shard.lock b/shard.lock index 3b25e9d..e0b6864 100644 --- a/shard.lock +++ b/shard.lock @@ -10,7 +10,7 @@ shards: awscr-s3: git: https://github.com/taylorfinnell/awscr-s3.git - version: 0.9.0 + version: 0.10.0 awscr-signer: git: https://github.com/taylorfinnell/awscr-signer.git diff --git a/spec/api_spec.cr b/spec/api_spec.cr index 22daa5b..4884299 100644 --- a/spec/api_spec.cr +++ b/spec/api_spec.cr @@ -59,7 +59,7 @@ module PlaceOS::Api task = TaskStatus.from_json(resp.body) break if task.completed? resp.status_code.should eq 200 - sleep 5 + sleep 5.seconds end resp.status_code.should eq 200 task.state.to_s.should eq("error") @@ -97,7 +97,7 @@ module PlaceOS::Api task = TaskStatus.from_json(resp.body) break if task.completed? resp.status_code.should eq 200 - sleep 5 + sleep 5.seconds end resp.status_code.should eq 303 task.state.to_s.should eq("done") @@ -196,7 +196,6 @@ module PlaceOS::Api resp = client.post("#{namespace}/#{Api.arch}/#{uri}?#{prms}") resp.status_code.should eq 202 - task = TaskStatus.from_json(resp.body) code = TOTP.generate_number_string(Api::TOTP_SECRET) diff --git a/src/ext/url.cr b/src/ext/url.cr deleted file mode 100644 index 0005457..0000000 --- a/src/ext/url.cr +++ /dev/null @@ -1,25 +0,0 @@ -module Awscr - module S3 - module Presigned - class Url - def for(method : Symbol, scheme = "https://") - raise S3::Exception.new("unsupported method #{method}") unless allowed_methods.includes?(method) - - request = build_request(method.to_s.upcase) - - @options.additional_options.each do |k, v| - request.query_params.add(k, v) - end - - presign_request(request) - - String.build do |str| - str << scheme - str << host - str << request.resource - end - end - end - end - end -end diff --git a/src/placeos-build-api/controllers/driver.cr b/src/placeos-build-api/controllers/driver.cr index 454ca78..8b23c81 100644 --- a/src/placeos-build-api/controllers/driver.cr +++ b/src/placeos-build-api/controllers/driver.cr @@ -36,7 +36,7 @@ module PlaceOS::Api @[AC::Param::Info(description: "Branch to return driver binary for, defaults to master", example: "main")] branch : String?, @[AC::Param::Info(description: "the commit hash of the driver to check is compiled", example: "e901494")] - commit : String + commit : String, ) : S3::LinkData? Log.context.set(driver: file_name, arch: arch, repository: url, branch: branch, commit: commit) if ret = Api.with_s3 &.compiled?(file_name, arch, url, commit, branch) @@ -57,7 +57,7 @@ module PlaceOS::Api @[AC::Param::Info(description: "Branch to return driver binary for, defaults to master", example: "main")] branch : String?, @[AC::Param::Info(description: "the commit hash of the driver to check is compiled, defaults to latest commit on branch", example: "e901494362f6859100b8f3")] - commit : String + commit : String, ) : String? Log.context.set(driver: file_name, repository: url, branch: branch, commit: commit) if ret = Api.with_s3 &.compiled?(file_name, "meta", url, commit, branch) @@ -78,7 +78,7 @@ module PlaceOS::Api @[AC::Param::Info(description: "Branch to return driver binary for, defaults to master", example: "main")] branch : String?, @[AC::Param::Info(description: "the commit hash of the driver to check is compiled, defaults to latest commit on branch", example: "e901494362f6859100b8f3")] - commit : String + commit : String, ) : String? Log.context.set(driver: file_name, repository: url, branch: branch, commit: commit) if ret = Api.with_s3 &.compiled?(file_name, "meta", url, commit, branch) @@ -104,7 +104,7 @@ module PlaceOS::Api @[AC::Param::Info(description: "the commit hash of the driver to check is compiled", example: "e901494")] commit : String, @[AC::Param::Info(description: "Whether to re-compile driver using the latest shards? default is false", example: "true")] - force : Bool = false + force : Bool = false, ) : TaskStatus Log.context.set(driver: file_name, arch: arch, repository: url, branch: branch, commit: commit, force: force) Log.info { "Processing compile driver request" } @@ -124,7 +124,7 @@ module PlaceOS::Api @[AC::Param::Info(description: "the system architecture, defaults to architecutre of system where this service is running", example: "amd64 | arm64")] arch : String, @[AC::Param::Info(description: "Submitted Job ID returned by POST request")] - id : String + id : String, ) : TaskStatus? if task = Api.task_status(id) if task.success? diff --git a/src/placeos-build-api/controllers/monitor.cr b/src/placeos-build-api/controllers/monitor.cr index d5e1e3c..aa25c45 100644 --- a/src/placeos-build-api/controllers/monitor.cr +++ b/src/placeos-build-api/controllers/monitor.cr @@ -9,7 +9,7 @@ module PlaceOS::Api @[AC::Route::GET("/monitor")] def monitor( @[AC::Param::Info(name: "state", description: "state of job to return. One of [pending,running,cancelled error,done]. Defaults to 'pending'", example: "pending")] - state : Api::State = Api::State::Pending + state : Api::State = Api::State::Pending, ) : Array(TaskStatus) render json: Api.get_job_queue(state) end @@ -17,7 +17,7 @@ module PlaceOS::Api @[AC::Route::DELETE("/cancel/:job")] def cancel( @[AC::Param::Info(name: "job", description: "ID of previously submitted compilation job")] - job : String + job : String, ) : String? if Api.cancel_task(job) render json: {status: "success", message: "Job with id #{job} cancelled successfully"}.to_json diff --git a/src/placeos-build-api/s3.cr b/src/placeos-build-api/s3.cr index a6f80b8..fb3b8a4 100644 --- a/src/placeos-build-api/s3.cr +++ b/src/placeos-build-api/s3.cr @@ -1,7 +1,6 @@ require "awscr-s3" require "http/headers" require "./utils" -require "../ext/url" module PlaceOS::Api def self.with_s3(&) @@ -124,13 +123,14 @@ module PlaceOS::Api object: "/#{@name}", bucket: AWS_S3_BUCKET, host_name: host, + scheme: scheme, expires: AWS_S3_LINK_EXPIRY.to_i.to_i32, additional_options: { "Content-Type" => "binary/octet-stream", }) url = Awscr::S3::Presigned::Url.new(options) Log.debug { "Generating signed URL}" } - url.for(:get, scheme) + url.for(:get) end def get_resp