Class: MiGA::Cli::Action::Run

Inherits:
MiGA::Cli::Action show all
Defined in:
lib/miga/cli/action/run.rb

Constant Summary

Constants included from MiGA

MiGA::CITATION, VERSION, VERSION_DATE, VERSION_NAME

Instance Attribute Summary

Attributes inherited from MiGA::Cli::Action

#cli

Instance Method Summary collapse

Methods inherited from MiGA::Cli::Action

#complete, #empty_action, #initialize, #launch, load, #name

Methods inherited from MiGA

CITATION, CITATION_ARRAY, DEBUG, DEBUG_OFF, DEBUG_ON, DEBUG_TRACE_OFF, DEBUG_TRACE_ON, FULL_VERSION, LONG_VERSION, VERSION, VERSION_DATE, #advance, debug?, debug_trace?, initialized?, #like_io?, #num_suffix, #result_files_exist?, #say

Methods included from MiGA::Common::Path

#root_path, #script_path

Methods included from MiGA::Common::Format

#clean_fasta_file, #seqs_length, #tabulate

Methods included from MiGA::Common::Net

#download_file_ftp, #known_hosts, #remote_connection

Constructor Details

This class inherits a constructor from MiGA::Cli::Action

Instance Method Details

#parse_cliObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/miga/cli/action/run.rb', line 8

def parse_cli
  cli.defaults = { try_load: false, thr: 1, env: false }
  cli.parse do |opt|
    cli.opt_object(opt, [:project, :dataset_opt, :result_opt])
    opt.on(
      '-t', '--threads INT', Integer,
      "Threads to use in the local run (by default: #{cli[:thr]})"
    ) { |v| cli[:thr] = v }
    opt.on(
      '-R', '--remote STR',
      'Description of remote SSH node to launch the job, as "user@hostname"',
      'By default, the job is executed locally'
    ) { |v| cli[:remote] = v }
    opt.on(
      '-l', '--log PATH',
      'Path to the output log file to be created. If not set, STDOUT'
    ) { |v| cli[:log] = v }
    opt.on(
      '-e', '--environment',
      'Load PROJECT, DATASET, and CORES from the environment'
    ) { |v| cli[:env] = v }
  end
end

#performObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/miga/cli/action/run.rb', line 32

def perform
  # Load environment variables if requested (typically by the daemon)
  if cli[:env]
    cli[:project] ||= ENV['PROJECT']
    cli[:dataset] ||= ENV['DATASET']
    cli[:thr] = ENV['CORES'].to_i unless ENV['CORES'].nil?
    cli[:result] = File.basename(cli[:result].to_s, '.bash').to_sym
  end
  %i[project dataset result].each do |i|
    cli[i] = nil if cli[i].nil? || cli[i].empty?
  end

  # Unset dataset if the requested result is for projects
  if (MiGA::Project.RESULT_DIRS.keys + [:p]).include? cli[:result]
    cli[:dataset] = nil
  end

  # Use virtual result if not explicitly passed
  cli[:result] ||= cli[:dataset] ? :d : :p

  # Load project
  p = cli.load_project

  # Prepare command
  miga = MiGA.root_path
  cmd = ["PROJECT=#{p.path.shellescape}",
         "RUNTYPE=#{cli[:remote] ? 'ssh' : 'bash'}",
         "MIGA=#{miga.shellescape}", "CORES=#{cli[:thr]}"]
  obj = cli.load_project_or_dataset
  klass = obj.class
  virtual_task = %i[p d maintenance].include?(cli[:result])
  cmd << "DATASET=#{obj.name.shellescape}" if obj.is_a? MiGA::Dataset
  if klass.RESULT_DIRS[cli[:result]].nil? and not virtual_task
    raise "Unsupported #{klass.to_s.sub(/.*::/, '')} result: #{cli[:result]}."
  end

  cmd << MiGA.script_path(cli[:result], miga: miga, project: p).shellescape
  if cli[:remote]
    cmd = ['ssh', '-t', '-t', cli[:remote].shellescape,
           cmd.join(' ').shellescape]
  end
  cmd << ['>', cli[:log].shellescape, '2>&1'] if cli[:log]

  # Launch
  pid = spawn cmd.join(' ')
  Process.wait pid
end