Skip to content

Commit

Permalink
End to end working, from providing a import path till it is saved to …
Browse files Browse the repository at this point in the history
….lookup_file
  • Loading branch information
harshit-soora committed Feb 21, 2025
1 parent d1f5369 commit 905c440
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 23 deletions.
7 changes: 2 additions & 5 deletions apps/dashboard/app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,11 @@ def create

# POST /projects/import
def import_save
success = Project.add_shared_to_lookup(:import)
success = Project.import_to_lookup(params[:project][:directory])
if success
redirect_to projects_path, notice: I18n.t('dashboard.jobs_project_imported')
else
message = @project.errors[:save].empty? ? I18n.t('dashboard.jobs_project_validation_error') : I18n.t('dashboard.jobs_project_generic_error', error: @project.collect_errors)
flash.now[:alert] = message
@templates = templates if project_params.key?(:template)
render :new
redirect_to project_import_path, alert: I18n.t('dashboard.jobs_project_generic_error')
end
end

Expand Down
28 changes: 14 additions & 14 deletions apps/dashboard/app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ def templates
Project.new(**opts)
end
end

def import_to_lookup(dir)
# fetch _id by opening .ondemand/manifest.yml
manifest_path = Pathname("#{dir.to_s}/.ondemand/manifest.yml")
contents = File.read(manifest_path)
raw_opts = YAML.safe_load(contents)
id = raw_opts["id"]
new_table = Project.lookup_table.merge(Hash[id, dir.to_s])
File.write(Project.lookup_file, new_table.to_yaml)
true
rescue StandardError => e
Rails.logger.warn("Cannot import project #{dir} to lookup file due to error #{e}")
false
end
end

attr_reader :id, :name, :description, :icon, :directory, :template
Expand Down Expand Up @@ -171,20 +185,6 @@ def add_to_lookup(operation)
false
end

def add_shared_to_lookup(operation, dir)
# fetch _id by opening .ondemand/manifest.yml
manifest_path = Pathname("#{dir.to_s}/.ondemand/manifest.yml")
contents = File.read(manifest_path)
raw_opts = YAML.safe_load(contents)
id = raw_opts["id"]
new_table = Project.lookup_table.merge(Hash[id, dir.to_s])
File.write(Project.lookup_file, new_table.to_yaml)
true
rescue StandardError => e
errors.add(operation, "Cannot update lookup file with error #{e.class}:#{e.message}")
false
end

def remove_from_lookup
new_table = Project.lookup_table.except(id)
File.write(Project.lookup_file, new_table.to_yaml)
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/app/views/projects/import.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
</h1>
</div>

<%= bootstrap_form_for(@project, url: import_projects_path, method: :patch) do |form| %>
<%= bootstrap_form_for(@project, url: project_import_save_path, method: :post) do |form| %>
<%= render partial: "form", locals: { form: form }%>
<% end %>
2 changes: 1 addition & 1 deletion apps/dashboard/app/views/projects/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<%- end -%>

<div class="mt-5 justify-content-center text-center project-icon">
<%= link_to(import_projects_path,
<%= link_to(project_import_path,
title: I18n.t('dashboard.jobs_import_shared_project'),
class: 'text-dark btn btn-link') do
%>
Expand Down
5 changes: 3 additions & 2 deletions apps/dashboard/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

Rails.application.routes.draw do
if Configuration.can_access_projects?
get 'projects/import' => 'projects#import', :as => 'project_import'
post 'projects/import' => 'projects#import_save', :as => 'project_import_save'

resources :projects do
root 'projects#index'
get '/jobs/:cluster/:jobid' => 'projects#job_details', :defaults => { :format => 'turbo_stream' }, :as => 'job_details'
delete '/jobs/:cluster/:jobid' => 'projects#delete_job', :as => 'delete_job'
post '/jobs/:cluster/:jobid/stop' => 'projects#stop_job', :as => 'stop_job'
get '/import' => 'projects#import', :as => 'import'
post '/import' => 'projects#import_save', :as => 'import_save'

resources :launchers do
post 'submit', on: :member
Expand Down

0 comments on commit 905c440

Please sign in to comment.