diff options
author | ytti <saku@ytti.fi> | 2016-02-22 11:39:45 +0200 |
---|---|---|
committer | ytti <saku@ytti.fi> | 2016-02-22 11:39:45 +0200 |
commit | 4e4d2d9d146305be9180fc0ed560abf0e317a65d (patch) | |
tree | ed07723610dfec4ca2c7a55b25a13010846d522e /lib/oxidized/output | |
parent | 01348ad8db7b916f6ce89a6ab2ce32f2046af753 (diff) | |
parent | 1209fb0343348f3d71457161625b2b827c6d8717 (diff) |
Merge pull request #313 from jgroom33/master
add http output type
Diffstat (limited to 'lib/oxidized/output')
-rw-r--r-- | lib/oxidized/output/http.rb | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/lib/oxidized/output/http.rb b/lib/oxidized/output/http.rb new file mode 100644 index 0000000..13ba300 --- /dev/null +++ b/lib/oxidized/output/http.rb @@ -0,0 +1,58 @@ +module Oxidized + class Http < Output + attr_reader :commitref + def initialize + @cfg = Oxidized.config.output.http + end + + def setup + if @cfg.empty? + CFGS.user.output.http.user = 'Oxidized' + CFGS.user.output.http.pasword = 'secret' + CFGS.user.output.http.url = 'http://localhost/web-api/oxidized' + CFGS.save :user + raise NoConfig, 'no output http config, edit ~/.config/oxidized/config' + end + end + require "net/http" + require "uri" + require "json" + def store node, outputs, opt={} + @commitref = nil + json = JSON.pretty_generate( + { + 'msg' => opt[:msg], + 'user' => opt[:user], + 'email' => opt[:email], + 'group' => opt[:group], + 'node' => node, + 'config' => outputs.to_cfg, + # actually we need to also iterate outputs, for other types like in gitlab. But most people don't use 'type' functionality. + } + ) + uri = URI.parse @cfg.url + http = Net::HTTP.new uri.host, uri.port + #http.use_ssl = true if uri.scheme = 'https' + req = Net::HTTP::Post.new(uri.request_uri, initheader = { 'Content-Type' => 'application/json'}) + req.basic_auth @cfg.user, @cfg.password + req.body = json + response = http.request req + + case response.code.to_i + when 200 || 201 + Oxidized.logger.info "Configuration http backup complete for #{node}" + p [:success] + when (400..499) + Oxidized.logger.info "Configuration http backup for #{node} failed status: #{response.body}" + p [:bad_request] + when (500..599) + p [:server_problems] + Oxidized.logger.info "Configuration http backup for #{node} failed status: #{response.body}" + end + + end + + end +end + + |