Class: Ncio::App

Inherits:
Object
  • Object
show all
Includes:
Support, Support::OptionParsing, Support::Transform
Defined in:
lib/ncio/app.rb

Overview

using the run method by the ncio bin script.

Constant Summary

Constants included from Support::OptionParsing

Support::OptionParsing::BANNER, Support::OptionParsing::CACERT_DEFAULT, Support::OptionParsing::CACERT_MSG, Support::OptionParsing::CERT_MSG, Support::OptionParsing::CONNECT_TIMEOUT_DEFAULT, Support::OptionParsing::FILE_DEFAULT_MAP, Support::OptionParsing::KEY_MSG, Support::OptionParsing::NAMES, Support::OptionParsing::SSLDIR

Instance Attribute Summary

Attributes included from Support::OptionParsing

#argv, #env, #opts

Attributes included from Support

#opts

Instance Method Summary collapse

Methods included from Support::Transform

#class_matches?, #group_matches?, #transform_group, #transform_params, #transform_rules

Methods included from Support::OptionParsing

#cert_default, #certname, #key_default, #map_hostnames, #parse_global_options!, #parse_options, #parse_subcommand!, #parse_subcommand_options!, pem_exists?, #reset_options!

Methods included from Support

#api, #debug, #error, #fatal, #file, #format_error, #friendly_error, #friendly_ssl_error, #info, #input_stream, #log, log, #map_file_option, map_file_option, #reset_logging!, reset_logging!, stream_logger, syslog_logger, #uri, #version, #warn, #write_output

Constructor Details

#initialize(argv = ARGV.dup, env = ENV.to_hash) ⇒ Ncio::App

Returns the application instance.

Parameters:

  • argv (Array) (defaults to: ARGV.dup)

    The argument vector, passed to the option parser.

  • env (Hash) (defaults to: ENV.to_hash)

    The environment hash, passed to the option parser to supply defaults not specified on the command line argument vector.



31
32
33
34
35
# File 'lib/ncio/app.rb', line 31

def initialize(argv = ARGV.dup, env = ENV.to_hash)
  @argv = argv
  @env = env
  reset!
end

Instance Method Details

#backup_groupsObject

Backup all groups in a manner suitable for the node classification hierarchy import. See: NC Groups rubocop:disable Metrics/AbcSize



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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!Object

Reset all state associated with this application instance.



39
40
41
42
43
# File 'lib/ncio/app.rb', line 39

def reset!
  reset_options!
  reset_logging!(opts)
  @api = nil
end

#restore_groupsObject

Restore all groups in a manner suitable for the node classification hierarchy import. See: NC Import Hierarchy rubocop:disable Lint/RescueException



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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

#runFixnum

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.

rubocop:disable Metrics/MethodLength

Returns:

  • (Fixnum)

    the exit code to pass to Kernel.exit in the calling script.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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_groupsObject

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



124
125
126
127
128
129
130
131
132
133
134
# 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