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.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pwrake/option/option.rb', line 17

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.



35
36
37
# File 'lib/pwrake/option/option.rb', line 35

def counter
  @counter
end

#host_mapObject (readonly)

Returns the value of attribute host_map.



370
371
372
# File 'lib/pwrake/option/option.rb', line 370

def host_map
  @host_map
end

#queue_classObject (readonly)

Returns the value of attribute queue_class.



74
75
76
# File 'lib/pwrake/option/option.rb', line 74

def queue_class
  @queue_class
end

#total_coresObject

Returns the value of attribute total_cores.



36
37
38
# File 'lib/pwrake/option/option.rb', line 36

def total_cores
  @total_cores
end

#worker_optionObject (readonly)

Returns the value of attribute worker_option.



73
74
75
# File 'lib/pwrake/option/option.rb', line 73

def worker_option
  @worker_option
end

#worker_progsObject (readonly)

Returns the value of attribute worker_progs.



72
73
74
# File 'lib/pwrake/option/option.rb', line 72

def worker_progs
  @worker_progs
end

Instance Method Details

#cwd_relative_if_under_homeObject



309
310
311
312
313
314
315
316
317
318
319
# File 'lib/pwrake/option/option.rb', line 309

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



305
306
307
# File 'lib/pwrake/option/option.rb', line 305

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

#feedback_optionsObject



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/pwrake/option/option.rb', line 243

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



239
240
241
# File 'lib/pwrake/option/option.rb', line 239

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

#init_filesystemObject




63
64
65
66
67
68
69
70
71
# File 'lib/pwrake/option/option.rb', line 63

def init_filesystem
  @filesystem = Rake.application.options.filesystem || mount_type
  case @filesystem
  when 'gfarm2fs'
    require "pwrake/option/option_gfarm2fs"
  else
    require "pwrake/option/option_default_filesystem"
  end
end

#init_optionsObject




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

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




323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/pwrake/option/option.rb', line 323

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 —–



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pwrake/option/option.rb', line 42

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(dir = nil) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/pwrake/option/option.rb', line 76

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

#mountpoint_of_cwdObject



92
93
94
95
96
97
98
# File 'lib/pwrake/option/option.rb', line 92

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

#option_dataObject



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
231
232
233
234
235
236
237
# File 'lib/pwrake/option/option.rb', line 131

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', # LIHR(default), FIFO, LIFO, RANK
    'NOACTION_QUEUE_PRIORITY', # FIFO(default), LIFO, RAND
    'DISABLE_RANK_PRIORITY',
    ['RESERVE_NODE','RESERVE_HOST'],
    'GRAPH_PARTITION',
    'PLOT_PARTITION',

    ['HOSTFILE','HOSTS'],
    ['LOG_DIR',
      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 && v.to_i}],
    ['RETRY', proc{|v| (v || 1).to_i}],
    ['HOST_FAILURE', 'HOST_FAIL', proc{|v| (v || 2).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



290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/pwrake/option/option.rb', line 290

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

#put_logObject




374
375
376
377
378
379
380
381
382
# File 'lib/pwrake/option/option.rb', line 374

def put_log
  Log.info "Pwrake::VERSION=#{Pwrake::VERSION}"
  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


273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/pwrake/option/option.rb', line 273

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




356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/pwrake/option/option.rb', line 356

def setup_hosts
  if f = ENV['PBS_NODEFILE']
    if @hostfile
      Log.info "HOSTFILE=#{@hostfile} overrides PBS_NODEFILE=#{f}"
    else
      Log.info "use PBS_NODEFILE=#{f}"
      @hostfile = f
    end
  end
  if @hostfile && @num_threads
    raise "Cannot set `hostfile' and `num_threads' simultaneously"
  end
  @host_map = HostMap.new(@hostfile || @num_threads)
end