Class: IronWorkerNG::Code::Base

Direct Known Subclasses

Binary, Builder, Go, Java, Mono, Node, PHP, Perl, Python, Ruby

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Feature::Common::SetEnv::InstanceMethods

#set_env

Methods included from Feature::Common::MergeZip::InstanceMethods

#merge_zip

Methods included from Feature::Common::MergeDeb::InstanceMethods

#merge_deb

Methods included from Feature::Common::MergeDir::InstanceMethods

#merge_dir

Methods included from Feature::Common::MergeFile::InstanceMethods

#merge_file

Constructor Details

#initialize(*args, &block) ⇒ Base

Returns a new instance of Base.



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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/iron_worker_ng/code/base.rb', line 39

def initialize(*args, &block)
  @features = []
  @fixators = []

  @base_dir = ''
  @dest_dir = ''

  @full_remote_build = false

  @runtime = nil

  @name = nil
  @exec = nil

  @zip_package = nil

  @use_local_iron_worker_ng = false

  worker_files = []

  if args.length == 1 && args[0].is_a?(String)
    @name = args[0]
  elsif args.length == 1 && args[0].is_a?(Hash)
    @name = args[0][:name] || args[0]['name']

    worker_file = args[0][:workerfile] || args[0]['workerfile']
    worker_files << worker_file unless worker_file.nil?

    @env = args[0][:env] || args[0]['env']

    exec = args[0][:exec] || args[0]['exec'] || args[0][:worker] || args[0]['worker']
    unless exec.nil?
      merge_exec(exec)
    end
  end

  if @name.is_a?(String) && @name.end_with?('.worker')
    @name = @name.gsub(/\.worker$/, '')
  end

  if @name.is_a?(String) && @name.end_with?('.zip')
    @zip_package = @name
    @name = @name.gsub(/\.zip/, '')
  end

  if @name.nil? and (not @exec.nil?)
    @name = guess_name_for_path(@exec.path)
  end

  if (not @name.nil?) && @zip_package.nil?
    worker_files << @name + '.worker'
  end

  worker_files.each do |worker_file|
    IronWorkerNG::Fetcher.fetch(worker_file) do |content|
      unless content.nil?
        IronCore::Logger.info 'IronWorkerNG', "Found workerfile with path='#{worker_file}'"

        if IronWorkerNG::Fetcher.remote?(worker_file)
          @full_remote_build = true
        end

        @base_dir = File.dirname(worker_file) == '.' ? '' : File.dirname(worker_file) + '/'

        eval(content)

        break
      end
    end
  end

  unless block.nil?
    instance_eval(&block)
  end

  if @name.nil? and (not @exec.nil?)
    @name = guess_name_for_path(@exec.path)
  end

  unless @name.nil?
    @name = File.basename(@name)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/iron_worker_ng/code/base.rb', line 123

def method_missing(name, *args, &block)
  if @runtime.nil?
    runtime(:ruby)
    send(name, *args, &block)
  else
    super(name, *args, &block)
  end
end

Instance Attribute Details

#base_dirObject

Returns the value of attribute base_dir.



20
21
22
# File 'lib/iron_worker_ng/code/base.rb', line 20

def base_dir
  @base_dir
end

#dest_dirObject

Returns the value of attribute dest_dir.



21
22
23
# File 'lib/iron_worker_ng/code/base.rb', line 21

def dest_dir
  @dest_dir
end

#envObject

Returns the value of attribute env.



22
23
24
# File 'lib/iron_worker_ng/code/base.rb', line 22

def env
  @env
end

#featuresObject

Returns the value of attribute features.



17
18
19
# File 'lib/iron_worker_ng/code/base.rb', line 17

def features
  @features
end

#fix_paramsObject

Returns the value of attribute fix_params.



28
29
30
# File 'lib/iron_worker_ng/code/base.rb', line 28

def fix_params
  @fix_params
end

#fixatorsObject

Returns the value of attribute fixators.



18
19
20
# File 'lib/iron_worker_ng/code/base.rb', line 18

def fixators
  @fixators
end

#use_build_cacheObject

Returns the value of attribute use_build_cache.



27
28
29
# File 'lib/iron_worker_ng/code/base.rb', line 27

def use_build_cache
  @use_build_cache
end

#use_local_iron_worker_ngObject

Returns the value of attribute use_local_iron_worker_ng.



26
27
28
# File 'lib/iron_worker_ng/code/base.rb', line 26

def use_local_iron_worker_ng
  @use_local_iron_worker_ng
end

#zip_packageObject

Returns the value of attribute zip_package.



24
25
26
# File 'lib/iron_worker_ng/code/base.rb', line 24

def zip_package
  @zip_package
end

Instance Method Details

#build_workerfileObject



357
358
359
360
361
362
363
364
365
366
# File 'lib/iron_worker_ng/code/base.rb', line 357

def build_workerfile
  commands = []

  commands << "runtime '#{runtime}'"
  commands << "name '#{name}'"

  commands += @features.map { |f| f.build_command }

  commands.compact.uniq.join("\n")
end

#bundle(container, local = false) ⇒ Object



233
234
235
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/iron_worker_ng/code/base.rb', line 233

def bundle(container, local = false)
  @features.each do |feature|
    feature.bundle(container)
  end

  if local || ((not remote_build_command) && (not full_remote_build))
    params = "\"$@\""

    if @fix_params
      params="-`echo \"$@\" | sed \"s/ -/ --/g\"`"
    end

    container.get_output_stream(@dest_dir + '__runner__.sh') do |runner|
      runner.write <<RUNNER
#!/bin/sh
# #{IronWorkerNG.full_version}

root() {
  while [ $# -gt 1 ]; do
    if [ "$1" = "-d" ]; then
printf "%s" "$2"
break
    fi

    shift
  done
}

cd "$(root "$@")"

LD_LIBRARY_PATH=.:./lib:./__debs__/usr/lib:./__debs__/usr/lib/x86_64-linux-gnu:./__debs__/lib:./__debs__/lib/x86_64-linux-gnu
export LD_LIBRARY_PATH

PATH=.:./bin:./__debs__/usr/bin:./__debs__/bin:$PATH
export PATH

#{container.runner_additions}

#{runtime_run_code(local, params)}
RUNNER
    end

    runtime_bundle(container, local)
  end
end

#create_container(local = false) ⇒ Object



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/iron_worker_ng/code/base.rb', line 279

def create_container(local = false)
  if @exec.nil?
    IronCore::Logger.error 'IronWorkerNG', 'No exec specified, check if worker file name is correct and/or exec command is present', IronCore::Error
  end

  if @name.nil?
    @name = guess_name_for_path(@exec.path)
  end

  fixate

  container = local ? IronWorkerNG::Code::Container::Dir.new : IronWorkerNG::Code::Container::Zip.new

  IronCore::Logger.debug 'IronWorkerNG', "Creating #{local ? 'local ' : ''}code container '#{container.name}'"

  if local
    bundle(container, local)
  else
    if @remote_build_command || @full_remote_build
      @dest_dir = '__build__/'
    end

    bundle(container, local)

    if @remote_build_command || @full_remote_build
      IronCore::Logger.info 'IronWorkerNG', 'Remote building worker'

      builder = IronWorkerNG::Code::Builder.new
      builder.builder_remote_build_command = @remote_build_command

      builder.use_local_iron_worker_ng = @use_local_iron_worker_ng
      builder.stack(@stack)

      if @use_local_iron_worker_ng
        builder.gem('iron_worker_ng')
        builder.fixate
      end

      builder.bundle(container, local)

      container.get_output_stream(@name + '.worker') do |wf|
        wf.write(build_workerfile)
      end
    end

    if @remote_build_command || @full_remote_build
      @dest_dir = ''
    end
  end

  container.close

  container.name
end

#fixateObject



220
221
222
223
224
# File 'lib/iron_worker_ng/code/base.rb', line 220

def fixate
  @fixators.each do |f|
    send(f)
  end
end

#full_remote_build(full_remote_build = nil) ⇒ Object



168
169
170
171
172
# File 'lib/iron_worker_ng/code/base.rb', line 168

def full_remote_build(full_remote_build = nil)
  @full_remote_build = full_remote_build unless full_remote_build.nil?

  @full_remote_build
end

#full_remote_build=(full_remote_build) ⇒ Object



174
175
176
# File 'lib/iron_worker_ng/code/base.rb', line 174

def full_remote_build=(full_remote_build)
  @full_remote_build = full_remote_build
end

#guess_name_for_path(path) ⇒ Object



132
133
134
# File 'lib/iron_worker_ng/code/base.rb', line 132

def guess_name_for_path(path)
  File.basename(path).gsub(/#{File.extname(path)}$/, '')
end

#install(standalone = false) ⇒ Object



354
355
# File 'lib/iron_worker_ng/code/base.rb', line 354

def install(standalone = false)
end

#name(name = nil) ⇒ Object



136
137
138
139
140
# File 'lib/iron_worker_ng/code/base.rb', line 136

def name(name = nil)
  @name = name if name

  @name
end

#name=(name) ⇒ Object



142
143
144
# File 'lib/iron_worker_ng/code/base.rb', line 142

def name=(name)
  @name = name
end

#remoteObject



178
179
180
# File 'lib/iron_worker_ng/code/base.rb', line 178

def remote
  @full_remote_build = true
end

#remote_build_command(remote_build_command = nil) ⇒ Object Also known as: build



155
156
157
158
159
# File 'lib/iron_worker_ng/code/base.rb', line 155

def remote_build_command(remote_build_command = nil)
  @remote_build_command = remote_build_command unless remote_build_command.nil?

  @remote_build_command
end

#remote_build_command=(remote_build_command) ⇒ Object Also known as: build=



161
162
163
# File 'lib/iron_worker_ng/code/base.rb', line 161

def remote_build_command=(remote_build_command)
  @remote_build_command = remote_build_command
end

#run(params = {}, config = {}) ⇒ Object



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/iron_worker_ng/code/base.rb', line 334

def run(params = {}, config = {})
  container_name = create_container(true)

  payload = File.open("#{container_name}/__payload__", 'wb')
  payload.write(params.is_a?(String) ? params : params.to_json)
  payload.close

  config_file = File.open("#{container_name}/__config__", 'wb')
  config_file.write(config.is_a?(String) ? config : config.to_json)
  config_file.close

  if @remote_build_command
    system("cd #{container_name} && #{@remote_build_command}")
  end

  system("sh #{container_name}/__runner__.sh -d #{container_name} -payload #{container_name}/__payload__ -config #{container_name}/__config__ -id 0")

  FileUtils.rm_rf(container_name)
end

#runtime(runtime = nil, version = nil) ⇒ Object



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
# File 'lib/iron_worker_ng/code/base.rb', line 182

def runtime(runtime = nil, version = nil)
  return @runtime unless runtime

  unless @runtime.nil?
    IronCore::Logger.error 'IronWorkerNG', "Runtime is already set to '#{@runtime}'", IronCore::Error
  end

  runtime_module = nil
  runtime = runtime.to_s

  if version
    s = "#{runtime}-#{version}"
    IronCore::Logger.info 'IronWorkerNG', "Trying to set stack to:#{s}'"
    stack(s)
  end

  begin
    runtime_module = IronWorkerNG::Code::Runtime.const_get(runtime.capitalize)
  rescue
    begin
      runtime_module = IronWorkerNG::Code::Runtime.const_get(runtime.upcase)
    rescue
    end
  end

  if runtime_module.nil?
    IronCore::Logger.error 'IronWorkerNG', "Can't find runtime '#{runtime}'"
  end

  self.extend(runtime_module)

  @runtime = runtime
end

#runtime=(runtime) ⇒ Object



216
217
218
# File 'lib/iron_worker_ng/code/base.rb', line 216

def runtime=(runtime)
  runtime(runtime)
end

#runtime_bundle(container, local = false) ⇒ Object



226
227
# File 'lib/iron_worker_ng/code/base.rb', line 226

def runtime_bundle(container, local = false)
end

#runtime_run_code(local, params) ⇒ Object



229
230
231
# File 'lib/iron_worker_ng/code/base.rb', line 229

def runtime_run_code(local, params)
  ''
end

#stack(stack_name = nil) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/iron_worker_ng/code/base.rb', line 146

def stack(stack_name = nil)
  @stack = stack_name if stack_name

  @use_local_iron_worker_ng = true if @stack == 'ruby-2.1'

  @stack
end

#to_sObject



368
369
370
# File 'lib/iron_worker_ng/code/base.rb', line 368

def to_s
  "runtime='#{@runtime}', name='#{@name}', exec='#{@exec.nil? ? '' : @exec.path}'"
end