class Mamiya::CLI

Public Instance Methods

agent() click to toggle source
# File lib/mamiya/cli.rb, line 202
def agent
  prepare_agent_behavior!
  merge_serf_option!
  override_labels!

  @agent = Agent.new(config, logger: logger)
  @agent.run!
end
build() click to toggle source
# File lib/mamiya/cli.rb, line 93
def build
  # TODO: overriding name
  %i(build_from build_to).each { |k| script.set(k, File.expand_path(options[k])) if options[k] }

  if options[:force_prepare_build] && options[:skip_prepare_build]
    logger.warn 'Both force_prepare_build and skip_prepare_build are enabled. ' \
      'This results skipping prepare_build...'
  end

  if options[:force_prepare_build]
    script.set :skip_prepare_build, false
  end

  if options[:skip_prepare_build]
    script.set :skip_prepare_build, true
  end

  builder = Mamiya::Steps::Build.new(script: script, logger: logger)
  builder.run!

  if options[:push]
    package = builder.package
    push(package.name)
  end
end
distribute() click to toggle source
# File lib/mamiya/cli.rb, line 167
def distribute
end
extract(package_atom, destination) click to toggle source
# File lib/mamiya/cli.rb, line 157
def extract(package_atom, destination)
  package_path = package_path_from_atom(package_atom)

  Mamiya::Steps::Extract.new(
    package: package_path,
    destination: destination
  ).run!
end
fetch(package_atom, destination) click to toggle source
# File lib/mamiya/cli.rb, line 146
def fetch(package_atom, destination)
  Mamiya::Steps::Fetch.new(
    script: script(:no_error),
    config: config,
    package: package_atom,
    application: application,
    destination: destination,
  ).run!
end
invoke_command(*) click to toggle source
Calls superclass method
# File lib/mamiya/cli.rb, line 32
def invoke_command(*)
  super
rescue SystemExit
  raise
rescue Exception => e
  logger.fatal "#{e.class}: #{e.message}"

  use_fatal = config(:no_error) && config[:show_backtrace_in_fatal]
  e.backtrace.map{ |_| _.prepend("\t") }.each do |line|
    if use_fatal
      logger.fatal line
    else
      logger.debug line
    end
  end
  exit 1
end
list_applications() click to toggle source
# File lib/mamiya/cli.rb, line 63
def list_applications
  puts _applications.keys
end
list_packages() click to toggle source
# File lib/mamiya/cli.rb, line 53
def list_packages
  unless options[:name_only]
    puts "Available packages in #{application}:"
    puts ""
  end

  puts storage.packages.sort
end
master() click to toggle source
# File lib/mamiya/cli.rb, line 216
def master
  prepare_agent_behavior!
  merge_serf_option!

  @agent = Master.new(config, logger: logger)
  @agent.run!
end
prepare(target) click to toggle source
# File lib/mamiya/cli.rb, line 172
def prepare(target)
  Mamiya::Steps::Prepare.new(
    script: nil,
    config: config,
    target: target,
    labels: labels,
  ).run!
end
prune(nums_to_keep) click to toggle source
# File lib/mamiya/cli.rb, line 136
def prune(nums_to_keep)
  puts "Pruning packages from #{application} (keeping last #{nums_to_keep.to_i} packages)..."

  removed = storage.prune(nums_to_keep.to_i)

  puts "Pruned #{removed.size} packages:"
  puts removed.join(?\n)
end
push(package_atom) click to toggle source
# File lib/mamiya/cli.rb, line 120
def push(package_atom)
  package_path = package_path_from_atom(package_atom)

  if options[:application]
    logger.warn "Overriding package's application name with given one: #{options[:application]}"
    sleep 2
  end

  Mamiya::Steps::Push.new(
    config: config,
    package: package_path,
    application: options[:application],
  ).run!
end
show(package) click to toggle source
# File lib/mamiya/cli.rb, line 69
def show(package)
  meta = storage.meta(package)

  case options[:format]
  when 'pp'
    require 'pp'
    pp meta
  when 'json'
    require 'json'
    puts meta.to_json
  when 'yaml'
    require 'yaml'
    puts meta.to_yaml
  end
end
switch(target) click to toggle source
# File lib/mamiya/cli.rb, line 184
def switch(target)
  Mamiya::Steps::Switch.new(
    script: nil,
    config: config,
    target: target,
    labels: labels,
    no_release: options[:no_release],
  ).run!
end

Private Instance Methods

_applications() click to toggle source
# File lib/mamiya/cli.rb, line 330
def _applications
  config.storage_class.find(config[:storage])
end
application() click to toggle source
# File lib/mamiya/cli.rb, line 313
def application
  @_application ||=
    options[:application] \
    || ENV['MAMIYA_APP'] \
    || ENV['MAMIYA_APPLICATION'] \
    || config[:application] \
    || script.application
end
config(dont_raise_error = false) click to toggle source
# File lib/mamiya/cli.rb, line 247
def config(dont_raise_error = false)
  return @config if @config
  path = [options[:config], './mamiya.conf.rb', './config.rb', '/etc/mamiya/config.rb'].compact.find { |_| File.exists?(_) }

  if path
    logger.debug "Using configuration: #{path}"
    @config = Mamiya::Configuration.new.load!(File.expand_path(path))
  else
    logger.debug "Couldn't find configuration file"
    return nil if dont_raise_error
    fatal! "Configuration File not found (try --config(-C) option or place it at ./config.rb)"
  end
end
fatal!(message) click to toggle source
# File lib/mamiya/cli.rb, line 283
def fatal!(message)
  logger.fatal message
  exit 1
end
labels() click to toggle source
# File lib/mamiya/cli.rb, line 278
def labels
  c = config(:no_error)
  options[:labels] ? options[:labels].split(/,/).map(&:to_sym) : (c ? c.labels[[]] : [])
end
logger() click to toggle source
# File lib/mamiya/cli.rb, line 334
def logger
  @logger ||= begin
    $stdout.sync = ENV["MAMIYA_SYNC_OUT"] == '1'
    outs = [$stdout]
    outs << File.expand_path(options[:log]) if options[:log]
    Mamiya::Logger.new(
      color: options[:no_color] ? false : (options[:color] ? true : nil),
      outputs: outs,
      level: options[:debug] ? Mamiya::Logger::DEBUG : Mamiya::Logger.defaults[:level],
    )
  end
end
merge_serf_option!() click to toggle source
# File lib/mamiya/cli.rb, line 288
def merge_serf_option!
  (config[:serf] ||= {})[:agent] ||= {}

  if options[:serf]
    options[:serf].flat_map{ |_| _.split(/,/) }.each do |conf|
      k,v = conf.split(/=/,2)
      config[:serf][:agent][k.to_sym] = v
    end
  end
end
override_labels!() click to toggle source
# File lib/mamiya/cli.rb, line 299
def override_labels!
  return unless config(:no_error)
  return unless options[:labels]

  labels = options[:labels].flat_map{ |_| _.split(/,/) }.map(&:to_sym)
  return if labels.empty?

  config.labels do
    labels
  end

  logger.info "Overriding labels: #{labels.inspect}"
end
package_path_from_atom(package_atom) click to toggle source
# File lib/mamiya/cli.rb, line 347
def package_path_from_atom(package_atom)
  candidates = [
    package_atom,
    options[:build_to] && File.join(options[:build_to], package_atom),
    options[:build_to] && File.join(options[:build_to], "#{package_atom}.tar.gz"),
    script(:no_error) && script.build_to && File.join(script.build_to, package_atom),
    script(:no_error) && script.build_to && File.join(script.build_to, "#{package_atom}.tar.gz"),
  ]
  logger.debug "Candidates: #{candidates.inspect}"

  package_path = candidates.select { |_| _ }.find { |_| File.exists?(_) }

  unless package_path
    fatal! "Package (#{package_atom}) couldn't find at #{candidates.join(', ')}"
  end

  package_path
end
prepare_agent_behavior!() click to toggle source
# File lib/mamiya/cli.rb, line 226
def prepare_agent_behavior!
  pidfile = File.expand_path(options[:pidfile]) if options[:pidfile]
  logger # insitantiate

  Process.daemon(:nochdir) if options[:daemonize]

  if pidfile
    open(pidfile, 'w') { |io| io.puts $$ }
    at_exit { File.unlink(pidfile) if File.exist?(pidfile) }
  end

  trap(:HUP) do
    logger.reopen
  end

  trap(:TERM) do
    puts "Received SIGTERM..."
    @agent.stop! if @agent
  end
end
script(dont_raise_error = false) click to toggle source
# File lib/mamiya/cli.rb, line 261
def script(dont_raise_error = false)
  return @script if @script
  path = [options[:script], './mamiya.rb', './deploy.rb'].compact.find { |_| File.exists?(_) }

  if path
    logger.debug "Using deploy script: #{path}"
    @script = Mamiya::Script.new.load!(File.expand_path(path)).tap do |s|
      s.set :application, options[:application] if options[:application]
      s.set :logger, logger
    end
  else
    logger.debug "Couldn't find deploy script."
    return nil if dont_raise_error
    fatal! "Deploy Script File not found (try --script(-S) option or place it at ./mamiya.rb or ./deploy.rb)"
  end
end
storage() click to toggle source
# File lib/mamiya/cli.rb, line 322
def storage
  config.storage_class.new(
    config[:storage].merge(
      application: application
    )
  )
end