Skip to content

Commit

Permalink
feat: PPT-524 Integrate the build service into core (#367)
Browse files Browse the repository at this point in the history
  • Loading branch information
naqvis authored Nov 20, 2023
1 parent 664ff7a commit f5741c3
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 17 deletions.
15 changes: 9 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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:/
Expand Down Expand Up @@ -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

Expand Down
28 changes: 28 additions & 0 deletions OPENAPI_DOC.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions shard.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion src/placeos-rest-api/controllers/drivers.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
33 changes: 25 additions & 8 deletions src/placeos-rest-api/controllers/repositories.cr
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module PlaceOS::Api
end

getter! current_repo : Model::Repository
class_property repository_dir : String = File.expand_path("./repositories")

###############################################################################################

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f5741c3

Please sign in to comment.