class Ncio::App

The main Application class. Intended to be instantiated and called with using the `run` method by the `ncio` bin script.

Public Class Methods

new(argv = ARGV.dup, env = ENV.to_hash) click to toggle source

@param [Array] argv The argument vector, passed to the option parser.

@param [Hash] env The environment hash, passed to the option parser to

supply defaults not specified on the command line argument vector.

@return [Ncio::App] the application instance.

# File lib/ncio/app.rb, line 31
def initialize(argv = ARGV.dup, env = ENV.to_hash)
  @argv = argv
  @env = env
  reset!
end

Public Instance Methods

backup_groups() click to toggle source

Backup all groups in a manner suitable for the node classification hierarchy import. See: [NC Groups](docs.puppet.com/pe/2016.1/nc_groups.html#get-v1groups) rubocop:disable Metrics/AbcSize

# File lib/ncio/app.rb, line 79
def backup_groups
  warn "Starting Node Classification Backup using GET #{uri}/groups"
  groups = api.groups
  debug "Number of groups retrieved: #{groups.size}"
  str = JSON.pretty_generate(groups)
  debug "Write #{str.bytesize} bytes of JSON to #{file} ..."
  write_output(str, map_file_option(file))
  warn 'Finished Node Classification Backup '\
    "STATUS=OK BYTESIZE=#{str.bytesize} GROUPCOUNT=#{groups.size} "\
    "OUTPUT=#{file}"
rescue Exception => e
  fatal "ERROR Obtaining backup: #{format_error e}"
  raise e
end
reset!() click to toggle source

Reset all state associated with this application instance.

# File lib/ncio/app.rb, line 39
def reset!
  reset_options!
  reset_logging!(opts)
  @api = nil
end
restore_groups() click to toggle source

Restore all groups in a manner suitable for the node classification hierarchy import. See: [NC Import Hierarchy](docs.puppet.com/pe/2016.1/nc_import-hierarchy.html) rubocop:disable Lint/RescueException

# File lib/ncio/app.rb, line 100
def restore_groups
  warn 'Starting Node Classification Restore using '\
    "POST #{uri}/import-hierarchy"
  api = self.api
  debug "Open #{file} for streaming ..."
  input_stream(map_file_option(file)) do |stream|
    debug "POST #{uri}/import-hierarchy"
    api.import_hierarchy(stream)
  end
  warn 'Finished Node Classification Restore '\
    "STATUS=OK INPUT=#{file}"
rescue Exception => e
  fatal "ERROR Restoring backup: #{format_error e}"
  raise e
end
run() click to toggle source

Run the application instance. This method is responsible for the application lifecycle. Command line arguments are parsed, unspecified options are read from the environment, and the specified subcommand is executed.

@return [Fixnum] the exit code to pass to Kernel.exit in the calling

script.

rubocop:disable Metrics/MethodLength

# File lib/ncio/app.rb, line 54
def run
  case opts[:subcommand]
  when 'backup'
    backup_groups if opts[:groups]
    return 0
  when 'restore'
    restore_groups if opts[:groups]
    return 0
  when 'transform'
    transform_groups
    return 0
  end
rescue Exception => e
  msg = "ERROR: #{friendly_error(e)}"
  fatal msg
  $stderr.puts msg
  return 1
end
transform_groups() click to toggle source

Transform a backup produced with backup_groups. The transformation is intended to allow restoration of the backup on PE Infrastructure cluster different from the one the backup was produced on.

Currently only one PE cluster type is supported, the Monolithic master type. rubocop:disable Metrics/AbcSize

# File lib/ncio/app.rb, line 124
def transform_groups
  # Read input
  groups = JSON.parse(input_stream(map_file_option(opts[:input]), &:read))
  groups.map! do |group|
    group_matches?(group) ? transform_group(group) : group
  end
  str = JSON.pretty_generate(groups)
  debug "Write #{str.bytesize} bytes to #{opts[:output]} ..."
  write_output(str, map_file_option(opts[:output]))
  info 'Transformation completed successful!'
end