Class: Luban::Deployment::Command

Inherits:
CLI::Command
  • Object
show all
Includes:
Helpers::Configuration, Parameters::General
Defined in:
lib/luban/deployment/cli/command.rb

Direct Known Subclasses

Application, Package::Base, Project

Defined Under Namespace

Modules: Tasks

Constant Summary

Constants included from Parameters::General

Parameters::General::DefaultLubanRootPath

Class Attribute Summary collapse

Attributes included from Helpers::Configuration

#config

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Parameters::General

included

Methods included from Parameters::Base

#parameter

Methods included from Helpers::Configuration

#ask, #fetch, #find_template_file, #load_configuration_file, #primary, #release_roles, #role, #roles, #server, #set, #set_default, #syntax_error?

Class Attribute Details

.default_templates_pathsObject (readonly)

Returns the value of attribute default_templates_paths.



277
278
279
# File 'lib/luban/deployment/cli/command.rb', line 277

def default_templates_paths
  @default_templates_paths
end

Class Method Details

.default_worker_classObject



279
280
281
# File 'lib/luban/deployment/cli/command.rb', line 279

def default_worker_class
  Luban::Deployment::Worker::Base
end

.dispatch_task(task, to:, as: task, locally: false, &blk) ⇒ Object



292
293
294
295
296
297
298
299
# File 'lib/luban/deployment/cli/command.rb', line 292

def dispatch_task(task, to:, as: task, locally: false, &blk)
  define_method(task) do |args:, opts:|
    run_task(cmd: as, args: args, opts: opts, locally: locally,
             worker_class: self.class.worker_class(to), &blk)
  end

  protected task
end

.inherited(subclass) ⇒ Object



269
270
271
272
273
274
275
# File 'lib/luban/deployment/cli/command.rb', line 269

def inherited(subclass)
  super
  # Ensure default_templates_paths from base class
  # got inherited to its subclasses
  paths = instance_variable_get('@default_templates_paths')
  subclass.instance_variable_set('@default_templates_paths', paths.nil? ? [] : paths.clone)
end

.worker_class(worker) ⇒ Object



283
284
285
286
287
288
289
290
# File 'lib/luban/deployment/cli/command.rb', line 283

def worker_class(worker)
  class_name = worker.camelcase
  if const_defined?(class_name)
    const_get(class_name)
  else
    abort "Aborted! #{name}::#{class_name} is NOT defined."
  end
end

Instance Method Details

#base_templates_path(base_path) ⇒ Object



330
331
332
333
# File 'lib/luban/deployment/cli/command.rb', line 330

def base_templates_path(base_path)
  path = Pathname.new(base_path).dirname.join('templates')
  path.exist? ? path.realpath : nil
end

#controllable?Boolean

Returns:

  • (Boolean)


265
# File 'lib/luban/deployment/cli/command.rb', line 265

def controllable?;  false; end

#default_templatesObject



335
336
337
338
339
340
# File 'lib/luban/deployment/cli/command.rb', line 335

def default_templates
  return @default_templates unless @default_templates.nil?
  (@default_templates = []).tap do |t|
    default_templates_paths.each { |p| t.concat(p.children).uniq! }
  end
end

#default_templates_pathsObject



342
# File 'lib/luban/deployment/cli/command.rb', line 342

def default_templates_paths; self.class.default_templates_paths; end

#deployable?Boolean

Returns:

  • (Boolean)


264
# File 'lib/luban/deployment/cli/command.rb', line 264

def deployable?;    false; end

#display_nameObject



261
# File 'lib/luban/deployment/cli/command.rb', line 261

def display_name; @display_name ||= name.camelcase; end

#monitorable?Boolean

Returns:

  • (Boolean)


266
# File 'lib/luban/deployment/cli/command.rb', line 266

def monitorable?;   false; end

#provisionable?Boolean

Returns:

  • (Boolean)


263
# File 'lib/luban/deployment/cli/command.rb', line 263

def provisionable?; false; end

#run_task(cmd: nil, args:, opts:, locally: false, worker_class: self.class.default_worker_class, &blk) ⇒ Object



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
# File 'lib/luban/deployment/cli/command.rb', line 302

def run_task(cmd: nil, args:, opts:, locally: false,
             worker_class: self.class.default_worker_class, &blk)
  backtrace = opts[:backtrace]
  task_args = compose_task_arguments(args)
  task_opts = compose_task_options(opts)
  run_opts = extract_run_options(task_opts)
  run_opts[:hosts] = :local if locally
  task_msg = { cmd: cmd, config: config, local: locally,
               args: task_args, opts: task_opts}
  result = []
  mutex = Mutex.new
  run(**run_opts) do |backend|
    begin 
      r = worker_class.new(task_msg.merge(backend: backend), &blk).run
    rescue StandardError => e
      r = {
        hostname: backend.host.hostname,
        status: :failed,
        message: backtrace ? "#{e.message}\n#{e.backtrace.join("\n")}" : e.message,
        error: e
      }
    end
    mutex.synchronize { result << r }
  end
  print_task_result(result) if opts[:format] == :blackhole
  locally ? result.first[:__return__] : result
end