Skip to content

Commit

Permalink
rename Script model to Launcher (#3397)
Browse files Browse the repository at this point in the history
First pass at renaming the Script model to Launcher. Lots of variables still
use script, but this was all that was required to rename the model.
  • Loading branch information
johrstrom authored Mar 5, 2024
1 parent 65b637d commit 215b389
Show file tree
Hide file tree
Showing 21 changed files with 176 additions and 176 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

# The controller for apps pages /dashboard/projects/:project_id/scripts
class ScriptsController < ApplicationController
class LaunchersController < ApplicationController

before_action :find_project
before_action :find_script, only: [:show, :edit, :destroy, :submit, :save]
Expand All @@ -16,13 +16,13 @@ class ScriptsController < ApplicationController
].freeze

def new
@script = Script.new(project_dir: @project.directory)
@script = Launcher.new(project_dir: @project.directory)
end

# POST /dashboard/projects/:project_id/scripts
def create
opts = { project_dir: @project.directory }.merge(create_script_params[:script])
@script = Script.new(opts)
opts = { project_dir: @project.directory }.merge(create_script_params[:launcher])
@script = Launcher.new(opts)
default_script_created = @script.create_default_script

if @script.save
Expand Down Expand Up @@ -51,7 +51,7 @@ def destroy
# POST /projects/:project_id/scripts/:id/save
# save the script after editing
def save
@script.update(save_script_params[:script])
@script.update(save_script_params[:launcher])

if @script.save
redirect_to project_path(params[:project_id]), notice: I18n.t('dashboard.jobs_scripts_updated')
Expand All @@ -63,7 +63,7 @@ def save
# POST /projects/:project_id/scripts/:id/submit
# submit the job
def submit
opts = submit_script_params[:script].to_h.symbolize_keys
opts = submit_script_params[:launcher].to_h.symbolize_keys

if (job_id = @script.submit(opts))
redirect_to(project_path(params[:project_id]), notice: I18n.t('dashboard.jobs_scripts_submitted', job_id: job_id))
Expand All @@ -75,12 +75,12 @@ def submit
private

def find_script
@script = Script.find(show_script_params[:id], @project.directory)
@script = Launcher.find(show_script_params[:id], @project.directory)
redirect_to(project_path(@project.id), alert: "Cannot find script #{show_script_params[:id]}") if @script.nil?
end

def create_script_params
params.permit({ script: [:title] }, :project_id)
params.permit({ launcher: [:title] }, :project_id)
end

def show_script_params
Expand All @@ -89,11 +89,11 @@ def show_script_params

def submit_script_params
keys = @script.smart_attributes.map { |sm| sm.id.to_s }
params.permit({ script: keys }, :project_id, :id)
params.permit({ launcher: keys }, :project_id, :id)
end

def save_script_params
params.permit({ script: SAVE_SCRIPT_KEYS }, :project_id, :id)
params.permit({ launcher: SAVE_SCRIPT_KEYS }, :project_id, :id)
end

def find_project
Expand Down
6 changes: 3 additions & 3 deletions apps/dashboard/app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ def show
if @project.nil?
redirect_to(projects_path, alert: I18n.t('dashboard.jobs_project_not_found', project_id: project_id))
else
@scripts = Script.all(@project.directory)
@valid_project = Script.clusters?
@valid_scripts = Script.scripts?(@project.directory)
@scripts = Launcher.all(@project.directory)
@valid_project = Launcher.clusters?
@valid_scripts = Launcher.scripts?(@project.directory)

alert_messages = []
alert_messages << I18n.t('dashboard.jobs_project_invalid_configuration_clusters') unless @valid_project
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

# ScriptsHelper is the helper module for scripts pages.
module ScriptsHelper
module LaunchersHelper
def create_editable_widget(form, attrib, format: nil)
widget = attrib.widget
attrib.html_options = { class: 'real-field', autocomplete: 'off' }
Expand All @@ -22,7 +22,7 @@ def create_editable_widget(form, attrib, format: nil)
end

def editable_partial(partial)
"scripts/editable_form_fields/#{partial}"
"launchers/editable_form_fields/#{partial}"
end

def parse_select_data(select_data)
Expand Down Expand Up @@ -58,10 +58,10 @@ def auto_accounts_template
# Otherwise you'd have required fields that you cannot actually edit
# because they're hidden.
def script_form_double
BootstrapForm::FormBuilder.new('script', nil, self, {})
BootstrapForm::FormBuilder.new('launcher', nil, self, {})
end

def script_removable_field?(id)
['script_auto_scripts', 'script_auto_batch_clusters'].exclude?(id.to_s)
['launcher_auto_scripts', 'launcher_auto_batch_clusters'].exclude?(id.to_s)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function addNewFieldButton() {
function enableNewFieldButton() {
const newFieldButton = addNewFieldButton();
for(let newField in newFieldData) {
const field = document.getElementById(`script_${newField}`);
const field = document.getElementById(`launcher_${newField}`);
if(field === null) {
// There is at least one field to be added.
// Enabled add button.
Expand Down Expand Up @@ -62,7 +62,7 @@ function addNewField(_event) {

function updateNewFieldOptions(selectMenu) {
for(let newField in newFieldData) {
const field = document.getElementById(`script_${newField}`);
const field = document.getElementById(`launcher_${newField}`);

// if the field doesn't already exist, it's an option for a new field.
if(field === null) {
Expand Down Expand Up @@ -318,12 +318,12 @@ function initFixedFields(){
jQuery(() => {
newFieldTemplate = $('#new_field_template');
$('#add_new_field_button').on('click', (event) => { addNewField(event) });
$('.new_script')
$('.new_launcher')
.find('.editable-form-field')
.find('.btn-danger')
.on('click', (event) => { removeField(event) });

$('.new_script')
$('.new_launcher')
.find('.editable-form-field')
.find('.btn-primary')
.on('click', (event) => { showEditField(event) });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class Script
class Launcher
include ActiveModel::Model

class ClusterNotFound < StandardError; end
Expand All @@ -15,14 +15,14 @@ def scripts_dir(project_dir)
end

def find(id, project_dir)
script_path = Script.script_path(project_dir, id)
script_path = Launcher.script_path(project_dir, id)
file = script_form_file(script_path)
Script.from_yaml(file, project_dir)
Launcher.from_yaml(file, project_dir)
end

def all(project_dir)
Dir.glob("#{scripts_dir(project_dir).to_s}/*/form.yml").map do |file|
Script.from_yaml(file, project_dir)
Launcher.from_yaml(file, project_dir)
end.compact.sort_by do |s|
s.created_at
end
Expand Down Expand Up @@ -137,11 +137,11 @@ def []=(_id, value)
end

def save
@id = Script.next_id if @id.nil?
@id = Launcher.next_id if @id.nil?
@created_at = Time.now.to_i if @created_at.nil?
script_path = Script.script_path(project_dir, id)
script_path = Launcher.script_path(project_dir, id)
script_path.mkpath unless script_path.exist?
File.write(Script.script_form_file(script_path), to_yaml)
File.write(Launcher.script_form_file(script_path), to_yaml)

true
rescue StandardError => e
Expand All @@ -152,8 +152,8 @@ def save

def destroy
return true unless id
script_path = Script.script_path(project_dir, id)
FileUtils.remove_dir(Script.script_path(project_dir, id)) if script_path.exist?
script_path = Launcher.script_path(project_dir, id)
FileUtils.remove_dir(Launcher.script_path(project_dir, id)) if script_path.exist?
true
rescue StandardError => e
errors.add(:destroy, e.message)
Expand Down Expand Up @@ -199,7 +199,7 @@ def most_recent_job_cluster
end

def create_default_script
return false if Script.scripts?(project_dir) || default_script_path.exist?
return false if Launcher.scripts?(project_dir) || default_script_path.exist?

script_content = <<~DEFAULT_SCRIPT
#!/bin/bash
Expand All @@ -213,7 +213,7 @@ def create_default_script
private

def self.script_path(root_dir, script_id)
Pathname.new(File.join(Script.scripts_dir(root_dir), script_id.to_s))
Pathname.new(File.join(Launcher.scripts_dir(root_dir), script_id.to_s))
end

def default_script_path
Expand Down Expand Up @@ -271,7 +271,7 @@ def write_job_options_to_cache(opts)
end

def cache_file_path
Pathname.new(File.join(Script.script_path(project_dir, id), "cache.json"))
Pathname.new(File.join(Launcher.script_path(project_dir, id), "cache.json"))
end

def cache_file_exists?
Expand All @@ -280,7 +280,7 @@ def cache_file_exists?

def cached_values
@cached_values ||= begin
cache_file_path = OodAppkit.dataroot.join(Script.scripts_dir("#{project_dir}"), "#{id}_opts.json")
cache_file_path = OodAppkit.dataroot.join(Launcher.scripts_dir("#{project_dir}"), "#{id}_opts.json")
cache_file_content = File.read(cache_file_path) if cache_file_path.exist?

File.exist?(cache_file_path) ? JSON.parse(cache_file_content) : {}
Expand Down Expand Up @@ -311,7 +311,7 @@ def job_data
end

def job_log_file
@job_log_file ||= Pathname.new(File.join(Script.script_path(project_dir, id), "job_history.log")).tap do |path|
@job_log_file ||= Pathname.new(File.join(Launcher.script_path(project_dir, id), "job_history.log")).tap do |path|
FileUtils.touch(path.to_s)
end
end
Expand Down
6 changes: 3 additions & 3 deletions apps/dashboard/app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,13 @@ def sync_template
false
end

# When copying a project from a template, we need new Script objects
# When copying a project from a template, we need new Launcher objects
# that point to the _new_ project directory, not the template's directory.
# This creates them _and_ serializes them to yml in the new directory.
def save_new_scripts
dir = Script.scripts_dir(template)
dir = Launcher.scripts_dir(template)
Dir.glob("#{dir}/*/form.yml").map do |script_yml|
Script.from_yaml(script_yml, project_dataroot)
Launcher.from_yaml(script_yml, project_dataroot)
end.map do |script|
saved_successfully = script.save
errors.add(:save, script.errors.full_messages) unless saved_successfully
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<h1>Editing <%= @script.title %></h1>

<%= javascript_include_tag 'script_edit', nonce: true %>
<%= javascript_include_tag 'launcher_edit', nonce: true %>

<%= link_to 'Back', project_path(params[:project_id]), class: 'btn btn-default my-3', title: 'Return to projects page' %>

<%= bootstrap_form_for(@script, url: save_project_script_path, html: { autocomplete: "off" }) do |f| %>
<%= bootstrap_form_for(@script, url: save_project_launcher_path, html: { autocomplete: "off" }) do |f| %>
<% @script.smart_attributes.each do |attrib| %>
<%# TODO generate render_format %>
<%= create_editable_widget(f, attrib, format: nil) %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

<div class="d-none edit-group mb-3">

<%= render(partial: 'scripts/editable_form_fields/edit_fixed_field', locals: { form: form, attrib: attrib, fixed: fixed }) %>
<%= render(partial: 'launchers/editable_form_fields/edit_fixed_field', locals: { form: form, attrib: attrib, fixed: fixed }) %>

<label for="<%= min_edit_id %>">Minimum</label>
<input min="1" step="1"
Expand All @@ -32,5 +32,5 @@

</div>

<%= render(partial: 'scripts/editable_form_fields/edit_field_buttons', locals: { field_id: field_id }) %>
<%= render(partial: 'launchers/editable_form_fields/edit_field_buttons', locals: { field_id: field_id }) %>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<div class="d-none edit-group">

<%= render(partial: 'scripts/editable_form_fields/edit_fixed_field', locals: { form: form, attrib: attrib, fixed: fixed }) %>
<%= render(partial: 'launchers/editable_form_fields/edit_fixed_field', locals: { form: form, attrib: attrib, fixed: fixed }) %>

<ol class="list-group text-center col-md-4 mb-3">
<%- attrib.select_choices(hide_excludable: false).each do |select_data| %>
Expand Down Expand Up @@ -48,5 +48,5 @@
value="<%= attrib.exclude_select_choices.join(',') %>">
</input>

<%= render(partial: 'scripts/editable_form_fields/edit_field_buttons', locals: { field_id: field_id }) %>
<%= render(partial: 'launchers/editable_form_fields/edit_field_buttons', locals: { field_id: field_id }) %>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
<div class="d-none edit-group">
</div>

<%= render(partial: 'scripts/editable_form_fields/edit_field_buttons', locals: { field_id: field_id }) %>
<%= render(partial: 'launchers/editable_form_fields/edit_field_buttons', locals: { field_id: field_id }) %>
</div>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

<%= bootstrap_form_for(@script, url: project_scripts_path) do |form| %>
<%= bootstrap_form_for(@script, url: project_launchers_path) do |form| %>

<div class="field">
<%= form.text_field :title, label: "Name" %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<%= link_to 'Back', project_path(params[:project_id]), class: 'btn btn-default my-3', title: 'Return to projects page' %>

<%= bootstrap_form_for(@script, url: submit_project_script_path) do |f| %>
<%= bootstrap_form_for(@script, url: submit_project_launcher_path) do |f| %>
<% @script.smart_attributes.each do |attrib| %>
<%# TODO generate render_format %>
<%= create_widget(f, attrib, format: nil, hide_fixed: false) %>
Expand Down
18 changes: 9 additions & 9 deletions apps/dashboard/app/views/projects/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
</div>

<div class='card'>
<h3 class='card-header'>Scripts</h3>
<h3 class='card-header'>Launchers</h3>
<div class='card-body'>
<table class='table table-bordered table-hover'>
<thead>
<tr>
<th scope='col' class='text-center'>Job Info</th>
<th scope='col' class='text-center'>Script Name</th>
<th scope='col' class='text-center'>Launcher Name</th>
<th scope='col' class='text-center'>Actions</th>
</tr>
</thead>
Expand All @@ -51,34 +51,34 @@
<div class="d-inline-flex">
<%= link_to(
t('dashboard.show'),
project_script_path(@project.id, script.id),
project_launcher_path(@project.id, script.id),
class: "btn btn-success mx-1 #{disabled_class}"
)
%>

<%= link_to(
t('dashboard.edit'),
edit_project_script_path(@project.id, script.id),
edit_project_launcher_path(@project.id, script.id),
class: "btn btn-primary mx-1 #{disabled_class}",
)
%>

<%= link_to(
t('dashboard.delete'),
project_script_path(@project.id, script.id),
project_launcher_path(@project.id, script.id),
class: "btn btn-danger mx-1 #{disabled_class}",
method: 'delete',
data: { confirm: I18n.t('dashboard.jobs_scripts_delete_script_confirmation') },
)
%>

<%-
params = script.smart_attributes.map { |attr| ["script[#{attr.id}]", attr.value] }.to_h
params = script.smart_attributes.map { |attr| ["launcher[#{attr.id}]", attr.value] }.to_h
-%>

<%= button_to(
t('dashboard.batch_connect_form_launch'),
submit_project_script_path(@project.id, script.id),
submit_project_launcher_path(@project.id, script.id),
class: "btn btn-secondary mx-1",
title: 'Launch script with cached values',
data: {:method => "post"},
Expand All @@ -93,8 +93,8 @@
</tbody>
</table>

<%= link_to('New Script',
new_project_script_path(@project.id),
<%= link_to('New Launcher',
new_project_launcher_path(@project.id),
class: "btn btn-info #{disabled_class}",
title: I18n.t('dashboard.jobs_project_create_new_project_directory'))
%>
Expand Down
Loading

0 comments on commit 215b389

Please sign in to comment.