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

Instance Attribute Summary

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, #default_templates_paths, #fetch, #find_template_file, #load_configuration_file, #primary, #release_roles, #role, #roles, #server, #set, #set_default, #syntax_error?

Class Method Details

.default_worker_classObject



168
169
170
# File 'lib/luban/deployment/cli/command.rb', line 168

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

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



181
182
183
184
185
186
187
188
# File 'lib/luban/deployment/cli/command.rb', line 181

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

.worker_class(worker) ⇒ Object



172
173
174
175
176
177
178
179
# File 'lib/luban/deployment/cli/command.rb', line 172

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



219
220
221
222
# File 'lib/luban/deployment/cli/command.rb', line 219

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

#controllable?Boolean

Returns:

  • (Boolean)


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

def controllable?; false; end

#default_templatesObject



224
225
226
227
228
229
# File 'lib/luban/deployment/cli/command.rb', line 224

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

#deployable?Boolean

Returns:

  • (Boolean)


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

def deployable?;   false; end

#display_nameObject



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

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

#installable?Boolean

Returns:

  • (Boolean)


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

def installable?;  false; end

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



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

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

#task(cmd, **opts, &blk) ⇒ Object



156
157
158
159
160
161
162
163
# File 'lib/luban/deployment/cli/command.rb', line 156

def task(cmd, **opts, &blk)
  command(cmd, **opts, &blk).tap do |c|
    add_common_task_options(c)
    if !c.summary.nil? and c.description.empty?
      c.long_desc "#{c.summary} in #{self.class.name}"
    end
  end
end