From f5741c3426868660e8bc677993803893385de6de Mon Sep 17 00:00:00 2001 From: Ali Naqvi Date: Mon, 20 Nov 2023 09:27:05 +0800 Subject: [PATCH] feat: PPT-524 Integrate the build service into core (#367) --- Dockerfile | 15 +++++---- OPENAPI_DOC.yml | 28 ++++++++++++++++ shard.lock | 4 +-- src/placeos-rest-api/controllers/drivers.cr | 10 +++++- .../controllers/repositories.cr | 33 ++++++++++++++----- 5 files changed, 73 insertions(+), 17 deletions(-) diff --git a/Dockerfile b/Dockerfile index 2fa2a651..75ca7290 100644 --- a/Dockerfile +++ b/Dockerfile @@ -43,13 +43,16 @@ SHELL ["/bin/ash", "-eo", "pipefail", "-c"] # Extract binary dependencies RUN for binary in "/bin/ping" "/bin/ping6" "/usr/bin/git" /app/bin/* /usr/libexec/git-core/*; do \ - ldd "$binary" | \ - tr -s '[:blank:]' '\n' | \ - grep '^/' | \ - xargs -I % sh -c 'mkdir -p $(dirname deps%); cp % deps%;' || true; \ - done + ldd "$binary" | \ + tr -s '[:blank:]' '\n' | \ + grep '^/' | \ + xargs -I % sh -c 'mkdir -p $(dirname deps%); cp % deps%;' || true; \ + done # Build a minimal docker image + +RUN mkdir /repositories && chown -R appuser /repositories + FROM scratch WORKDIR / ENV PATH=$PATH:/ @@ -80,7 +83,7 @@ COPY --from=build /usr/libexec/git-core/ /usr/libexec/git-core/ # Copy the app into place COPY --from=build /app/deps / COPY --from=build /app/bin / - +COPY --chown=appuser:appuser --from=build /repositories /repositories # Use an unprivileged user. USER appuser:appuser diff --git a/OPENAPI_DOC.yml b/OPENAPI_DOC.yml index 47a82696..18ec40cc 100644 --- a/OPENAPI_DOC.yml +++ b/OPENAPI_DOC.yml @@ -5438,6 +5438,14 @@ paths: - websocket - logic nullable: true + - name: update_available + in: query + description: list only drivers for which update is available + example: "true" + required: false + schema: + type: boolean + nullable: true - name: q in: query description: returns results based on a [simple query string](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html) @@ -19165,6 +19173,26 @@ components: ignore_connected: type: boolean nullable: true + update_available: + type: boolean + nullable: true + update_info: + type: object + properties: + commit: + type: string + message: + type: string + author: + type: string + nullable: true + date: + type: string + nullable: true + required: + - commit + - message + nullable: true repository_id: type: string nullable: true diff --git a/shard.lock b/shard.lock index 711052cb..5b67a59b 100644 --- a/shard.lock +++ b/shard.lock @@ -7,7 +7,7 @@ shards: action-controller: git: https://github.com/spider-gazelle/action-controller.git - version: 7.2.3 + version: 7.3.0 active-model: git: https://github.com/spider-gazelle/active-model.git @@ -267,7 +267,7 @@ shards: placeos-models: git: https://github.com/placeos/models.git - version: 9.26.0 + version: 9.27.0 placeos-resource: git: https://github.com/place-labs/resource.git diff --git a/src/placeos-rest-api/controllers/drivers.cr b/src/placeos-rest-api/controllers/drivers.cr index f020310f..fdedb1c8 100644 --- a/src/placeos-rest-api/controllers/drivers.cr +++ b/src/placeos-rest-api/controllers/drivers.cr @@ -38,7 +38,9 @@ module PlaceOS::Api @[AC::Route::GET("/")] def index( @[AC::Param::Info(description: "filter by the type of driver", example: "Logic")] - role : Model::Driver::Role? = nil + role : Model::Driver::Role? = nil, + @[AC::Param::Info(description: "list only drivers for which update is available", example: "true")] + update_available : Bool? = nil ) : Array(Model::Driver) elastic = Model::Driver.elastic query = elastic.query(search_params) @@ -49,6 +51,12 @@ module PlaceOS::Api }) end + if update_available + query.filter({ + "update_available" => [update_available.as(Bool)], + }) + end + query.search_field "name" query.sort(NAME_SORT_ASC) paginate_results(elastic, query) diff --git a/src/placeos-rest-api/controllers/repositories.cr b/src/placeos-rest-api/controllers/repositories.cr index 8648ef36..d21f1980 100644 --- a/src/placeos-rest-api/controllers/repositories.cr +++ b/src/placeos-rest-api/controllers/repositories.cr @@ -33,6 +33,7 @@ module PlaceOS::Api end getter! current_repo : Model::Repository + class_property repository_dir : String = File.expand_path("./repositories") ############################################################################################### @@ -118,16 +119,32 @@ module PlaceOS::Api # lists the drivers in a repository @[AC::Route::GET("/:id/drivers")] def drivers : Array(String) - repository_folder = current_repo.folder_name + Dir.cd(fetch_repo) do + Dir.glob("drivers/**/*.cr").select do |file| + !file.ends_with?("_spec.cr") && File.open(file) do |f| + f.each_line.any? &.includes?("PlaceOS::Driver") + rescue + false + end + end + end + end - # Request to core: - # "/api/core/v1/drivers/?repository=#{repository}" - # Returns: `["path/to/file.cr"]` - drivers = Api::Systems.core_for(repository_folder, request_id) do |core_client| - core_client.drivers(repository_folder) + private def fetch_repo + folder = Path.new(self.class.repository_dir, current_repo.id.as(String), current_repo.folder_name) + downloaded = Dir.exists?(folder) + Dir.mkdir_p(folder) unless downloaded + password = current_repo.decrypt_password if current_repo.password.presence + repo = GitRepository.new(current_repo.uri, current_repo.username, password) + git = GitRepository::Commands.new(folder.to_s) + unless downloaded + git.init + git.add_origin repo.repository end + git.run_git("fetch", {"--all"}) + git.checkout current_repo.branch rescue git.checkout repo.default_branch - drivers + folder end # Returns the commits for a repository or file @@ -169,7 +186,7 @@ module PlaceOS::Api # Request to core: # "/api/core/v1/drivers/#{file_name}/details?repository=#{repository}&commit=#{commit_hash}" details = Api::Systems.core_for(driver_filename, request_id) do |core_client| - core_client.driver_details(driver_filename, commit, current_repo.folder_name, current_repo.branch) + core_client.driver_details(driver_filename, commit, current_repo.id.as(String), current_repo.branch) end # The raw JSON string is returned and we proxy that (no need to encode and decode)