Class: Pwrake::Option

Inherits:
Hash
  • Object
show all
Includes:
DefaultFileSystemOption, GfarmFileSystemOption
Defined in:
lib/pwrake/option/option.rb,
lib/pwrake/option/option_gfarm2fs.rb,
lib/pwrake/option/option_default_filesystem.rb

Constant Summary collapse

DEFAULT_CONFFILES =
["pwrake_conf.yaml","PwrakeConf.yaml"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DefaultFileSystemOption

#max_postprocess_pool, #option_data_filesystem, #postprocess, #set_filesystem_option

Methods included from GfarmFileSystemOption

#clear_gfarm2fs, #max_postprocess_pool, #option_data_filesystem, #postprocess, #set_filesystem_option

Constructor Details

#initializeOption

Returns a new instance of Option.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pwrake/option/option.rb', line 13

def initialize
  load_pwrake_conf
  init_filesystem
  init_options
  init_pass_env
  if self['SHOW_CONF']
    require "yaml"
    YAML.dump(self,$stdout)
    exit
  elsif self['REPORT_DIR']
    require 'pwrake/report'
    Report.new(self,[]).report_html
    exit
  end
  setup_hosts
  set_filesystem_option
end

Instance Attribute Details

#counterObject (readonly)

Returns the value of attribute counter.



31
32
33
# File 'lib/pwrake/option/option.rb', line 31

def counter
  @counter
end

#host_mapObject (readonly)

Returns the value of attribute host_map.



355
356
357
# File 'lib/pwrake/option/option.rb', line 355

def host_map
  @host_map
end

#queue_classObject (readonly)

Returns the value of attribute queue_class.



70
71
72
# File 'lib/pwrake/option/option.rb', line 70

def queue_class
  @queue_class
end

#total_coresObject

Returns the value of attribute total_cores.



32
33
34
# File 'lib/pwrake/option/option.rb', line 32

def total_cores
  @total_cores
end

#worker_optionObject (readonly)

Returns the value of attribute worker_option.



69
70
71
# File 'lib/pwrake/option/option.rb', line 69

def worker_option
  @worker_option
end

#worker_progsObject (readonly)

Returns the value of attribute worker_progs.



68
69
70
# File 'lib/pwrake/option/option.rb', line 68

def worker_progs
  @worker_progs
end

Instance Method Details

#cwd_relative_if_under_homeObject



302
303
304
305
306
307
308
309
310
311
312
# File 'lib/pwrake/option/option.rb', line 302

def cwd_relative_if_under_home
  home = Pathname.new(ENV['HOME']).realpath
  path = pwd = Pathname.pwd.realpath
  while path != home
    if path.root?
      return pwd.to_s
    end
    path = path.parent
  end
  return pwd.relative_path_from(home).to_s
end

#cwd_relative_to_homeObject



298
299
300
# File 'lib/pwrake/option/option.rb', line 298

def cwd_relative_to_home
  Pathname.pwd.relative_path_from(Pathname.new(ENV['HOME'])).to_s
end

#feedback_optionsObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/pwrake/option/option.rb', line 236

def feedback_options
  opts = Rake.application.options
  ['DRYRUN',
   'IGNORE_SYSTEM',
   'IGNORE_DEPRECATE',
   'LOAD_SYSTEM',
   'NOSEARCH',
   'RAKELIB',
   'SHOW_PREREQS',
   'SILENT',
   'TRACE',
   'BACKTRACE',
   'TRACE_OUTPUT',
   'TRACE_RULES'
  ].each do |k|
    if v=self[k]
      m = (k.downcase+"=").to_sym
      opts.send(m,v)
    end
  end
  case opts.trace_output
  when 'stdout'
    opts.trace_output = $stdout
  when 'stderr', nil
    opts.trace_output = $stderr
  end
end

#format_time_pid(v) ⇒ Object



232
233
234
# File 'lib/pwrake/option/option.rb', line 232

def format_time_pid(v)
  START_TIME.strftime(v).sub("%$","%05d"%Process.pid)
end

#init_filesystemObject




59
60
61
62
63
64
65
66
67
# File 'lib/pwrake/option/option.rb', line 59

def init_filesystem
  @filesystem = Rake.application.options.filesystem
  @filesystem ||= mount_type.sub(/fuse\./,"")
  begin
    require "pwrake/option/option_#{@filesystem}"
  rescue LoadError
    require "pwrake/option/option_default_filesystem"
  end
end

#init_optionsObject




98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/pwrake/option/option.rb', line 98

def init_options
  option_data.each do |a|
    prc = nil
    keys = []
    case a
    when String
      keys << a
    when Array
      a.each do |x|
        case x
        when String
          keys << x
        when Proc
          prc = x
        end
      end
    end
    key = keys[0]
    val = search_opts(keys)
    val = prc.call(val) if prc
    self[key] = val if !val.nil?
    instance_variable_set("@"+key.downcase, val)
  end

  feedback_options

  Rake.verbose(false) if Rake.application.options.silent
end

#init_pass_envObject




316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/pwrake/option/option.rb', line 316

def init_pass_env
  if envs = self['PASS_ENV']
    pass_env = {}

    case envs
    when Array
      envs.each do |k|
        k = k.to_s
        if v = ENV[k]
          pass_env[k] = v
        end
      end
    when Hash
      envs.each do |k,v|
        k = k.to_s
        if v = ENV[k] || v
          pass_env[k] = v
        end
      end
    else
      raise "invalid option for PASS_ENV in pwrake_conf.yaml"
    end

    if pass_env.empty?
      self.delete('PASS_ENV')
    else
      self['PASS_ENV'] = pass_env
    end
  end
end

#load_pwrake_confObject

—– init —–



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/pwrake/option/option.rb', line 38

def load_pwrake_conf
  # Read pwrake_conf
  pwrake_conf = Rake.application.options.pwrake_conf
  if pwrake_conf
    if !File.exist?(pwrake_conf)
      raise "Configuration file not found: #{pwrake_conf}"
    end
  else
    pwrake_conf = DEFAULT_CONFFILES.find{|fn| File.exist?(fn)}
  end
  self['PWRAKE_CONF'] = pwrake_conf
  if pwrake_conf.nil?
    @yaml = {}
  else
    require "yaml"
    @yaml = open(pwrake_conf){|f| YAML.load(f) }
  end
end

#mount_type(d = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/pwrake/option/option.rb', line 72

def mount_type(d=nil)
  mtab = '/etc/mtab'
  if File.exist?(mtab)
    d ||= mountpoint_of_cwd
    open(mtab,'r') do |f|
      f.each_line do |l|
        a = l.split
        if a[1] == d
          return a[2]
        end
      end
    end
  end
  nil
end

#mountpoint_of_cwdObject



88
89
90
91
92
93
94
# File 'lib/pwrake/option/option.rb', line 88

def mountpoint_of_cwd
  d = Pathname.pwd
  while !d.mountpoint?
    d = d.parent
  end
  d.to_s
end

#option_dataObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/pwrake/option/option.rb', line 127

def option_data
  [
    'DRYRUN',
    'IGNORE_SYSTEM',
    'IGNORE_DEPRECATE',
    'LOAD_SYSTEM',
    'NOSEARCH',
    'RAKELIB',
    'SHOW_PREREQS',
    'SILENT',
    'TRACE',
    'BACKTRACE',
    'TRACE_OUTPUT',
    'TRACE_RULES',

    'SSH_OPTION',
    'PASS_ENV',
    'GNU_TIME',
    'DEBUG',
    'PLOT_PARALLELISM',
    'SHOW_CONF',
    ['SUBDIR','SUBDIRS',
      proc{|v|
        if Array===v
          v.each do |d|
            if !File.directory?(d)
              raise "directory #{d.inspect} does not exist"
            end
          end
        elsif !v.nil?
          raise "invalid argument for SUBDIR: #{v.inspect}"
        end
      }
    ],
    ['REPORT_DIR','REPORT'],
    'REPORT_IMAGE',
    'FAILED_TARGET', # rename(default), delete, leave
    'FAILURE_TERMINATION', # wait, kill, continue
    'QUEUE_PRIORITY', # RANK(default), FIFO, LIFO, DFS
    'NOACTION_QUEUE_PRIORITY', # FIFO(default), LIFO, RAND
    'GRAPH_PARTITION',
    'PLOT_PARTITION',

    ['HOSTFILE','HOSTS'],
    ['LOG_DIR','LOG',
      proc{|v|
        if v
          if v == "" || !v.kind_of?(String)
            v = "Pwrake%Y%m%d-%H%M%S"
          end
          d = v = format_time_pid(v)
          i = 1
          while File.exist?(d)
            d = "#{v}.#{i}"
            i += 1
          end
          d
        end
      }],
    ['LOG_FILE',
      proc{|v|
        if v.kind_of?(String) && v != ""
          v
        else
          "pwrake.log"
        end
      }],
    ['TASK_CSV_FILE',
      proc{|v|
        if v.kind_of?(String) && v != ""
          v
        else
          "task.csv"
        end
      }],
    ['COMMAND_CSV_FILE',
      proc{|v|
        if v.kind_of?(String) && v != ""
          v
        else
          "command.csv"
        end
      }],
    ['GC_LOG_FILE',
     proc{|v|
       if v
         if v.kind_of?(String) && v != ""
           v
         else
           "gc.log"
         end
       end
     }],
    ['NUM_THREADS', proc{|v| v && v.to_i}],
    ['SHELL_START_INTERVAL', proc{|v| (v || 0.012).to_f}],
    ['HEARTBEAT', proc{|v| (v || 240).to_i}],
    ['RETRY', proc{|v| (v || 1).to_i}],
    ['MASTER_HOSTNAME', proc{|v| (v || Socket.gethostname).chomp}],
    ['WORK_DIR', proc{|v|
       v ||= '%CWD_RELATIVE_TO_HOME'
       v.sub('%CWD_RELATIVE_TO_HOME',cwd_relative_if_under_home)
     }],
  ].concat(option_data_filesystem)
end

#parse_opt(s) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/pwrake/option/option.rb', line 283

def parse_opt(s)
  case s
  when /^(false|nil|off)$/i
    false
  when /^(true|on)$/i
    true
  when $stdout
    "stdout"
  when $stderr
    "stderr"
  else
    s
  end
end

#put_logObject




359
360
361
362
363
364
365
366
# File 'lib/pwrake/option/option.rb', line 359

def put_log
  Log.info "Options:"
  self.each do |k,v|
    Log.info " #{k} = #{v.inspect}"
  end
  Log.debug "@queue_class=#{@queue_class}"
  Log.debug "@filesystem=#{@filesystem}"
end

#search_opts(keys) ⇒ Object

Priority of Option:

command_option > ENV > pwrake_conf > DEFAULT_OPTIONS


266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/pwrake/option/option.rb', line 266

def search_opts(keys)
  val = Rake.application.options.send(keys[0].downcase.to_sym)
  return parse_opt(val) if !val.nil?
  #
  keys.each do |k|
    val = ENV[k.upcase]
    return parse_opt(val) if !val.nil?
  end
  #
  return nil if !@yaml
  keys.each do |k|
    val = @yaml[k.upcase]
    return val if !val.nil?
  end
  nil
end

#setup_hostsObject




349
350
351
352
353
354
# File 'lib/pwrake/option/option.rb', line 349

def setup_hosts
  if @hostfile && @num_threads
    raise "Cannot set `hostfile' and `num_threads' simultaneously"
  end
  @host_map = HostMap.new(@hostfile || @num_threads)
end