ncio 0.2.2 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7f4228a74a79a10e47ffce674748ec755eae37ec
4
- data.tar.gz: 5c80ffcca70d9ca50174b4f76cf72fed453e3794
3
+ metadata.gz: 6a605a5b8f5fe218bf929a616865beefdee6e498
4
+ data.tar.gz: 64f6c604eead3c2f171b4e257aefafd6557284fb
5
5
  SHA512:
6
- metadata.gz: bc29b5864c421b08faf4609baa7560e2e5e8be82c6210522e82df39338ca7c5e2290ff53f4dd6ee9b34e3e79b4fd5bb1e21a7980f4bd3f347c0723d23727884d
7
- data.tar.gz: b31ba443b49e416b5980e9a78996a0c9d8f53b7836c8d7b608f5e3d65b420929999acc34de959e7b92060e0255b05a274f45d389e6faf1ed9c973930ab1b4c80
6
+ metadata.gz: bda029e4149c95969a8ab4692a955f2d2fd16fbb53d649bbcc95371dc16acf9d7adbf9545308c466aa6a7d1f2566f9f0b1fb8b72cb65f7532cdd13fa28451ba7
7
+ data.tar.gz: 65f027f5b97bb2b57e5869340ce61b23d40030495b0e79b7d5094bc9682e819eac9572c0fd01c7c71a8088f95296a1858e277e20c9d37eb9051c74d217ca4743
data/README.md CHANGED
@@ -84,6 +84,33 @@ white-list of certificates is located at
84
84
  backup > /var/tmp/backup.json
85
85
  I, [2016-06-28T19:28:48.236257 #3148] INFO -- : Backup completed successfully!
86
86
 
87
+ ## Logging
88
+
89
+ The status of backup and restore operations are logged to the syslog by default.
90
+ The `daemon` facility is used to ensure messages are written to files on a wide
91
+ variety of systems that log daemon messages by default. A general exception
92
+ handler will log a backtrace in JSON format to help log processors and
93
+ notification systems like Splunk and Logstash.
94
+
95
+ Here's an example of a failed restore triggering the catch all handler:
96
+
97
+ Jun 29 12:12:21 Jeff-McCune ncio[51474]: ERROR Restoring backup: {
98
+ "error": "RuntimeError",
99
+ "message": "Some random error",
100
+ "backtrace": [
101
+ "/Users/jeff/projects/puppet/ncio/lib/ncio/app.rb:94:in `restore_groups'",
102
+ "/Users/jeff/projects/puppet/ncio/lib/ncio/app.rb:59:in `run'",
103
+ "/Users/jeff/projects/puppet/ncio/exe/ncio:5:in `<top (required)>'"
104
+ ]
105
+ }
106
+
107
+ Log to the console using the `--no-syslog` command line option.
108
+
109
+ ncio --no-syslog restore --file backup.json
110
+
111
+ The tool can only log to either syslog or the console at this time. Multiple
112
+ log destinations are not currently supported.
113
+
87
114
  ## Contributing
88
115
 
89
116
  Bug reports and pull requests are welcome on GitHub at
@@ -69,26 +69,42 @@ class App
69
69
  # Backup all groups in a manner suitable for the node classification hierarchy
70
70
  # import. See:
71
71
  # [NC Groups](https://docs.puppet.com/pe/2016.1/nc_groups.html#get-v1groups)
72
+ # rubocop:disable Metrics/AbcSize
72
73
  def backup_groups
73
- debug "GET #{uri}/groups"
74
- str = JSON.pretty_generate(api.groups)
75
- debug "Write #{str.bytesize} bytes to #{file} ..."
74
+ warn "Starting Node Classification Backup using GET #{uri}/groups"
75
+ groups = api.groups
76
+ debug "Number of groups retrieved: #{groups.size}"
77
+ str = JSON.pretty_generate(groups)
78
+ debug "Write #{str.bytesize} bytes of JSON to #{file} ..."
76
79
  write_output(str, map_file_option(file))
77
- info 'Backup completed successfully!'
80
+ warn 'Finished Node Classification Backup '\
81
+ "STATUS=OK BYTESIZE=#{str.bytesize} GROUPCOUNT=#{groups.size} "\
82
+ "OUTPUT=#{file}"
83
+ rescue Exception => e
84
+ fatal "ERROR Obtaining backup: #{format_error e}"
85
+ raise e
78
86
  end
87
+ # rubocop:enable Metrics/AbcSize
79
88
 
80
89
  ##
81
90
  # Restore all groups in a manner suitable for the node classification
82
91
  # hierarchy import. See: [NC Import
83
92
  # Hierarchy](https://docs.puppet.com/pe/2016.1/nc_import-hierarchy.html)
84
93
  def restore_groups
94
+ raise "Some random error"
95
+ warn 'Starting Node Classification Restore using '\
96
+ "POST #{uri}/import-hierarchy"
85
97
  api = self.api
86
98
  debug "Open #{file} for streaming ..."
87
99
  input_stream(map_file_option(file)) do |stream|
88
100
  debug "POST #{uri}/import-hierarchy"
89
101
  api.import_hierarchy(stream)
90
102
  end
91
- info 'Successfully restored node classification groups!'
103
+ warn 'Finished Node Classification Restore '\
104
+ "STATUS=OK INPUT=#{file}"
105
+ rescue Exception => e
106
+ fatal "ERROR Restoring backup: #{format_error e}"
107
+ raise e
92
108
  end
93
109
 
94
110
  ##
@@ -1,3 +1,7 @@
1
+ require 'logger'
2
+ require 'syslog/logger'
3
+ require 'json'
4
+
1
5
  module Ncio
2
6
  ##
3
7
  # Support module to mix into other classes, particularly the application and
@@ -8,10 +12,26 @@ module Ncio
8
12
  attr_reader :opts
9
13
 
10
14
  def self.reset_logging!(opts)
15
+ logger = opts[:syslog] ? syslog_logger : stream_logger(opts)
16
+ @log = logger
17
+ end
18
+
19
+ ##
20
+ # Return a new Syslog::Logger instance configured for syslog output
21
+ def self.syslog_logger
22
+ # Use the daemon facility, matching Puppet behavior.
23
+ Syslog::Logger.new('ncio', Syslog::LOG_DAEMON)
24
+ end
25
+
26
+ ##
27
+ # Return a new Logger instance configured for file output
28
+ def self.stream_logger(opts)
11
29
  out = map_file_option(opts[:logto])
12
30
  logger = Logger.new(out)
13
- logger.level = opts[:debug] ? Logger::DEBUG : Logger::INFO
14
- @log = logger
31
+ logger.level = Logger::WARN
32
+ logger.level = Logger::INFO if opts[:verbose]
33
+ logger.level = Logger::DEBUG if opts[:debug]
34
+ logger
15
35
  end
16
36
 
17
37
  ##
@@ -62,13 +82,37 @@ module Ncio
62
82
  end
63
83
 
64
84
  ##
65
- # Log an info message
85
+ # Logs a message at the fatal (syslog err) log level
86
+ def fatal(msg)
87
+ log.fatal msg
88
+ end
89
+
90
+ ##
91
+ # Logs a message at the error (syslog warning) log level.
92
+ # i.e. May indicate that an error will occur if action is not taken.
93
+ # e.g. A non-root file system has only 2GB remaining.
94
+ def error(msg)
95
+ log.error msg
96
+ end
97
+
98
+ ##
99
+ # Logs a message at the warn (syslog notice) log level.
100
+ # e.g. Events that are unusual, but not error conditions.
101
+ def warn(msg)
102
+ log.warn msg
103
+ end
104
+
105
+ ##
106
+ # Logs a message at the info (syslog info) log level
107
+ # i.e. Normal operational messages that require no action.
108
+ # e.g. An application has started, paused or ended successfully.
66
109
  def info(msg)
67
110
  log.info msg
68
111
  end
69
112
 
70
113
  ##
71
- # Log a debug message
114
+ # Logs a message at the debug (syslog debug) log level
115
+ # i.e. Information useful to developers for debugging the application.
72
116
  def debug(msg)
73
117
  log.debug msg
74
118
  end
@@ -105,6 +149,16 @@ module Ncio
105
149
  end
106
150
  end
107
151
 
152
+ ##
153
+ # Format an exception for logging. JSON is used to aid centralized log
154
+ # systems such as Logstash and Splunk
155
+ #
156
+ # @param [Exception] e the exception to format
157
+ def format_error(e)
158
+ data = { error: "#{e.class}", message: e.message, backtrace: e.backtrace }
159
+ JSON.pretty_generate(data)
160
+ end
161
+
108
162
  ##
109
163
  # Return the application version as a Semantic Version encoded string
110
164
  #
@@ -1,4 +1,6 @@
1
1
  require 'ncio/version'
2
+ require 'socket'
3
+
2
4
  # rubocop:disable Metrics/ModuleLength
3
5
  module Ncio
4
6
  module Support
@@ -70,7 +72,9 @@ module Ncio
70
72
  log_msg = 'Log file to write to or keywords '\
71
73
  'STDOUT, STDERR {NCIO_LOGTO}'
72
74
  opt :logto, log_msg, default: env['NCIO_LOGTO'] || 'STDERR'
73
- opt :debug
75
+ opt :syslog, 'Log to syslog', default: true, conflicts: :logto
76
+ opt :verbose, 'Set log level to INFO'
77
+ opt :debug, 'Set log level to DEBUG'
74
78
  end
75
79
  end
76
80
  # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
@@ -1,3 +1,3 @@
1
1
  module Ncio
2
- VERSION = '0.2.2'.freeze
2
+ VERSION = '1.0.0'.freeze
3
3
  end
@@ -9,8 +9,9 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Jeff McCune"]
10
10
  spec.email = ["jeff@openinfrastructure.co"]
11
11
 
12
- spec.summary = %q{Puppet Node Classifier Import / Export}
13
- spec.description = %q{Puppet Node Classifier Import / Export}
12
+ spec.summary = 'Puppet Node Classifier backup / restore / transform'
13
+ spec.description = 'ncio is a small command line utility to backup, '\
14
+ 'restore, and transform Puppet Enterprise Node Classification groups.'
14
15
  spec.homepage = "https://www.openinfrastructure.co"
15
16
  spec.license = "MIT"
16
17
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ncio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff McCune
@@ -122,7 +122,8 @@ dependencies:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
124
  version: 0.11.2
125
- description: Puppet Node Classifier Import / Export
125
+ description: ncio is a small command line utility to backup, restore, and transform
126
+ Puppet Enterprise Node Classification groups.
126
127
  email:
127
128
  - jeff@openinfrastructure.co
128
129
  executables:
@@ -176,6 +177,6 @@ rubyforge_project:
176
177
  rubygems_version: 2.4.5.1
177
178
  signing_key:
178
179
  specification_version: 4
179
- summary: Puppet Node Classifier Import / Export
180
+ summary: Puppet Node Classifier backup / restore / transform
180
181
  test_files: []
181
182
  has_rdoc: yard