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

Attributes included from MiGA::Common::Net

#remote_connection_uri

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, rc_path, #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, #http_request, #known_hosts, #main_server, #net_method, #normalize_encoding, #remote_connection

Methods included from MiGA::Common::SystemCall

#run_cmd, #run_cmd_opts

Constructor Details

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

Instance Method Details

#parse_cliObject



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

def parse_cli
  cli.defaults = { try_load: false, thr: 1, env: false, check_first: 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 }
    opt.on(
      '--check-first',
      'Check if the result exists, and run only if it does not'
    ) { |v| cli[:check_first] = v }
  end
end

#performObject



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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/miga/cli/action/run.rb', line 35

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

  # Check if result already exists
  if cli[:check_first]
    obj = cli[:dataset] ? p.dataset(cli[:dataset]) : p
    if obj.result(cli[:result])
      cli.say('Result already exists')
      return
    end
  end

  # Prepare command
  miga = MiGA.root_path
  opts = {}
  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

  if cli[:log]
    opts[:stdout] = cli[:log]
    opts[:err2out] = true
  end

  # Launch
  # note that all elements were carefully escaped in advace, so this has to be
  # passed as String to avoid double-escaping or unintentionally escaping
  # characters such as `=` and `>`
  MiGA::MiGA.run_cmd(cmd.join(' '), opts)
end