From 21e3d6490496573f25ef77fe8172766ac7d1a736 Mon Sep 17 00:00:00 2001 From: Wild Kat Date: Sat, 21 Apr 2018 13:27:05 +0200 Subject: the great makeover - standardize layout, alignment, indentation --- lib/oxidized/output/file.rb | 83 ++++++++------- lib/oxidized/output/git.rb | 228 ++++++++++++++++++++-------------------- lib/oxidized/output/gitcrypt.rb | 27 +++-- lib/oxidized/output/http.rb | 50 ++++----- lib/oxidized/output/output.rb | 3 +- 5 files changed, 191 insertions(+), 200 deletions(-) (limited to 'lib/oxidized/output') diff --git a/lib/oxidized/output/file.rb b/lib/oxidized/output/file.rb index 45f72e1..bad1b6a 100644 --- a/lib/oxidized/output/file.rb +++ b/lib/oxidized/output/file.rb @@ -1,59 +1,58 @@ module Oxidized -class OxidizedFile < Output - require 'fileutils' + class OxidizedFile < Output + require 'fileutils' - attr_reader :commitref + attr_reader :commitref - def initialize - @cfg = Oxidized.config.output.file - end + def initialize + @cfg = Oxidized.config.output.file + end - def setup - if @cfg.empty? - Oxidized.asetus.user.output.file.directory = File.join(Config::Root, 'configs') - Oxidized.asetus.save :user - raise NoConfig, 'no output file config, edit ~/.config/oxidized/config' + def setup + if @cfg.empty? + Oxidized.asetus.user.output.file.directory = File.join(Config::Root, 'configs') + Oxidized.asetus.save :user + raise NoConfig, 'no output file config, edit ~/.config/oxidized/config' + end end - end - def store node, outputs, opt={} - file = File.expand_path @cfg.directory - if opt[:group] - file = File.join File.dirname(file), opt[:group] + def store node, outputs, opt = {} + file = File.expand_path @cfg.directory + if opt[:group] + file = File.join File.dirname(file), opt[:group] + end + FileUtils.mkdir_p file + file = File.join file, node + open(file, 'w') { |fh| fh.write outputs.to_cfg } + @commitref = file end - FileUtils.mkdir_p file - file = File.join file, node - open(file, 'w') { |fh| fh.write outputs.to_cfg } - @commitref = file - end - def fetch node, group - cfg_dir = File.expand_path @cfg.directory - node_name = node.name + def fetch node, group + cfg_dir = File.expand_path @cfg.directory + node_name = node.name - if group # group is explicitly defined by user - cfg_dir = File.join File.dirname(cfg_dir), group - File.read File.join(cfg_dir, node_name) - else - if File.exists? File.join(cfg_dir, node_name) # node configuration file is stored on base directory + if group # group is explicitly defined by user + cfg_dir = File.join File.dirname(cfg_dir), group File.read File.join(cfg_dir, node_name) else - path = Dir.glob(File.join(File.dirname(cfg_dir), '**', node_name)).first # fetch node in all groups - File.read path + if File.exists? File.join(cfg_dir, node_name) # node configuration file is stored on base directory + File.read File.join(cfg_dir, node_name) + else + path = Dir.glob(File.join(File.dirname(cfg_dir), '**', node_name)).first # fetch node in all groups + File.read path + end end + rescue Errno::ENOENT + return nil end - rescue Errno::ENOENT - return nil - end - def version node, group - # not supported - [] - end + def version node, group + # not supported + [] + end - def get_version node, group, oid - 'not supported' + def get_version node, group, oid + 'not supported' + end end - -end end diff --git a/lib/oxidized/output/git.rb b/lib/oxidized/output/git.rb index fee0ab6..8acfd46 100644 --- a/lib/oxidized/output/git.rb +++ b/lib/oxidized/output/git.rb @@ -1,74 +1,73 @@ module Oxidized -class Git < Output - class GitError < OxidizedError; end - begin - require 'rugged' - rescue LoadError - raise OxidizedError, 'rugged not found: sudo gem install rugged' - end - - attr_reader :commitref + class Git < Output + class GitError < OxidizedError; end + begin + require 'rugged' + rescue LoadError + raise OxidizedError, 'rugged not found: sudo gem install rugged' + end - def initialize - @cfg = Oxidized.config.output.git - end + attr_reader :commitref - def setup - if @cfg.empty? - Oxidized.asetus.user.output.git.user = 'Oxidized' - Oxidized.asetus.user.output.git.email = 'o@example.com' - Oxidized.asetus.user.output.git.repo = File.join(Config::Root, 'oxidized.git') - Oxidized.asetus.save :user - raise NoConfig, 'no output git config, edit ~/.config/oxidized/config' + def initialize + @cfg = Oxidized.config.output.git end - if @cfg.repo.respond_to?(:each) - @cfg.repo.each do |group, repo| - @cfg.repo["#{group}="] = File.expand_path repo + def setup + if @cfg.empty? + Oxidized.asetus.user.output.git.user = 'Oxidized' + Oxidized.asetus.user.output.git.email = 'o@example.com' + Oxidized.asetus.user.output.git.repo = File.join(Config::Root, 'oxidized.git') + Oxidized.asetus.save :user + raise NoConfig, 'no output git config, edit ~/.config/oxidized/config' end - else - @cfg.repo = File.expand_path @cfg.repo - end - end - def store file, outputs, opt={} - @msg = opt[:msg] - @user = (opt[:user] or @cfg.user) - @email = (opt[:email] or @cfg.email) - @opt = opt - @commitref = nil - repo = @cfg.repo - - outputs.types.each do |type| - type_cfg = '' - type_repo = File.join(File.dirname(repo), type + '.git') - outputs.type(type).each do |output| - (type_cfg << output; next) if not output.name - type_file = file + '--' + output.name - if @cfg.type_as_directory? - type_file = type + '/' + type_file - type_repo = repo + if @cfg.repo.respond_to?(:each) + @cfg.repo.each do |group, repo| + @cfg.repo["#{group}="] = File.expand_path repo end - update type_repo, type_file, output + else + @cfg.repo = File.expand_path @cfg.repo end - update type_repo, file, type_cfg end - update repo, file, outputs.to_cfg - end + def store file, outputs, opt = {} + @msg = opt[:msg] + @user = (opt[:user] or @cfg.user) + @email = (opt[:email] or @cfg.email) + @opt = opt + @commitref = nil + repo = @cfg.repo + + outputs.types.each do |type| + type_cfg = '' + type_repo = File.join(File.dirname(repo), type + '.git') + outputs.type(type).each do |output| + (type_cfg << output; next) if not output.name + type_file = file + '--' + output.name + if @cfg.type_as_directory? + type_file = type + '/' + type_file + type_repo = repo + end + update type_repo, type_file, output + end + update type_repo, file, type_cfg + end + update repo, file, outputs.to_cfg + end - def fetch node, group - begin - repo, path = yield_repo_and_path(node, group) - repo = Rugged::Repository.new repo - index = repo.index - index.read_tree repo.head.target.tree unless repo.empty? - repo.read(index.get(path)[:oid]).data - rescue - 'node not found' + def fetch node, group + begin + repo, path = yield_repo_and_path(node, group) + repo = Rugged::Repository.new repo + index = repo.index + index.read_tree repo.head.target.tree unless repo.empty? + repo.read(index.get(path)[:oid]).data + rescue + 'node not found' + end end - end # give a hash of all oid revision for the given node, and the date of the commit def version node, group @@ -80,7 +79,7 @@ class Git < Output walker.sorting(Rugged::SORT_DATE) walker.push(repo.head.target) i = -1 - tab = [] + tab = [] walker.each do |commit| if commit.diff(paths: [path]).size > 0 hash = {} @@ -98,18 +97,18 @@ class Git < Output end end - #give the blob of a specific revision + # give the blob of a specific revision def get_version node, group, oid begin repo, path = yield_repo_and_path(node, group) repo = Rugged::Repository.new repo - repo.blob_at(oid,path).content + repo.blob_at(oid, path).content rescue 'version not found' end end - #give a hash with the patch of a diff between 2 revision and the stats (added and deleted lines) + # give a hash with the patch of a diff between 2 revision and the stats (added and deleted lines) def get_diff node, group, oid1, oid2 begin diff_commits = nil @@ -122,15 +121,15 @@ class Git < Output diff = repo.diff(commit_old, commit) diff.each do |patch| if /#{node.name}\s+/.match(patch.to_s.lines.first) - diff_commits = {:patch => patch.to_s, :stat => patch.stat} + diff_commits = { :patch => patch.to_s, :stat => patch.stat } break end end else stat = commit.parents[0].diff(commit).stat - stat = [stat[1],stat[2]] + stat = [stat[1], stat[2]] patch = commit.parents[0].diff(commit).patch - diff_commits = {:patch => patch, :stat => stat} + diff_commits = { :patch => patch, :stat => stat } end diff_commits @@ -139,68 +138,67 @@ class Git < Output end end - private + private - def yield_repo_and_path(node, group) - repo, path = node.repo, node.name + def yield_repo_and_path(node, group) + repo, path = node.repo, node.name - if group and @cfg.single_repo? - path = "#{group}/#{node.name}" - end + if group and @cfg.single_repo? + path = "#{group}/#{node.name}" + end - [repo, path] - end + [repo, path] + end - def update repo, file, data - return if data.empty? + def update repo, file, data + return if data.empty? - if @opt[:group] - if @cfg.single_repo? - file = File.join @opt[:group], file - else - repo = if repo.is_a?(::String) - File.join File.dirname(repo), @opt[:group] + '.git' - else - repo[@opt[:group]] - end + if @opt[:group] + if @cfg.single_repo? + file = File.join @opt[:group], file + else + repo = if repo.is_a?(::String) + File.join File.dirname(repo), @opt[:group] + '.git' + else + repo[@opt[:group]] + end + end end - end - begin - repo = Rugged::Repository.new repo - update_repo repo, file, data, @msg, @user, @email - rescue Rugged::OSError, Rugged::RepositoryError => open_error begin - Rugged::Repository.init_at repo, :bare - rescue => create_error - raise GitError, "first '#{open_error.message}' was raised while opening git repo, then '#{create_error.message}' was while trying to create git repo" + repo = Rugged::Repository.new repo + update_repo repo, file, data, @msg, @user, @email + rescue Rugged::OSError, Rugged::RepositoryError => open_error + begin + Rugged::Repository.init_at repo, :bare + rescue => create_error + raise GitError, "first '#{open_error.message}' was raised while opening git repo, then '#{create_error.message}' was while trying to create git repo" + end + retry end - retry end - end - def update_repo repo, file, data, msg, user, email - oid = repo.write data, :blob - index = repo.index - index.read_tree repo.head.target.tree unless repo.empty? - - tree_old = index.write_tree repo - index.add :path=>file, :oid=>oid, :mode=>0100644 - tree_new = index.write_tree repo - - if tree_old != tree_new - repo.config['user.name'] = user - repo.config['user.email'] = email - @commitref = Rugged::Commit.create(repo, - :tree => index.write_tree(repo), - :message => msg, - :parents => repo.empty? ? [] : [repo.head.target].compact, - :update_ref => 'HEAD', - ) - - index.write - true + def update_repo repo, file, data, msg, user, email + oid = repo.write data, :blob + index = repo.index + index.read_tree repo.head.target.tree unless repo.empty? + + tree_old = index.write_tree repo + index.add :path => file, :oid => oid, :mode => 0100644 + tree_new = index.write_tree repo + + if tree_old != tree_new + repo.config['user.name'] = user + repo.config['user.email'] = email + @commitref = Rugged::Commit.create(repo, + :tree => index.write_tree(repo), + :message => msg, + :parents => repo.empty? ? [] : [repo.head.target].compact, + :update_ref => 'HEAD',) + + index.write + true + end end end end -end diff --git a/lib/oxidized/output/gitcrypt.rb b/lib/oxidized/output/gitcrypt.rb index 0567458..2da0179 100644 --- a/lib/oxidized/output/gitcrypt.rb +++ b/lib/oxidized/output/gitcrypt.rb @@ -22,7 +22,7 @@ module Oxidized if @cfg.empty? Oxidized.asetus.user.output.gitcrypt.user = 'Oxidized' Oxidized.asetus.user.output.gitcrypt.email = 'o@example.com' - Oxidized.asetus.user.output.gitcrypt.repo = File.join(Config::Root, 'oxidized.git') + Oxidized.asetus.user.output.gitcrypt.repo = File.join(Config::Root, 'oxidized.git') Oxidized.asetus.save :user raise NoConfig, 'no output git config, edit ~/.config/oxidized/config' end @@ -60,13 +60,13 @@ module Oxidized end end - def store file, outputs, opt={} + def store file, outputs, opt = {} @msg = opt[:msg] @user = (opt[:user] or @cfg.user) @email = (opt[:email] or @cfg.email) @opt = opt @commitref = nil - repo = @cfg.repo + repo = @cfg.repo outputs.types.each do |type| type_cfg = '' @@ -86,7 +86,6 @@ module Oxidized update repo, file, outputs.to_cfg end - def fetch node, group begin repo, path = yield_repo_and_path(node, group) @@ -115,7 +114,7 @@ module Oxidized unlock repo walker = repo.log.path(path) i = -1 - tab = [] + tab = [] walker.each do |commit| hash = {} hash[:date] = commit.date.to_s @@ -131,7 +130,7 @@ module Oxidized end end - #give the blob of a specific revision + # give the blob of a specific revision def get_version node, group, oid begin repo, path = yield_repo_and_path(node, group) @@ -145,7 +144,7 @@ module Oxidized end end - #give a hash with the patch of a diff between 2 revision and the stats (added and deleted lines) + # give a hash with the patch of a diff between 2 revision and the stats (added and deleted lines) def get_diff node, group, oid1, oid2 begin diff_commits = nil @@ -160,15 +159,15 @@ module Oxidized stats = [diff.stats[:files][node.name][:insertions], diff.stats[:files][node.name][:deletions]] diff.each do |patch| if /#{node.name}\s+/.match(patch.patch.to_s.lines.first) - diff_commits = {:patch => patch.patch.to_s, :stat => stats} + diff_commits = { :patch => patch.patch.to_s, :stat => stats } break end end else stat = commit.parents[0].diff(commit).stats - stat = [stat[:files][node.name][:insertions],stat[:files][node.name][:deletions]] + stat = [stat[:files][node.name][:insertions], stat[:files][node.name][:deletions]] patch = commit.parents[0].diff(commit).patch - diff_commits = {:patch => patch, :stat => stat} + diff_commits = { :patch => patch, :stat => stat } end lock repo diff_commits @@ -199,10 +198,10 @@ module Oxidized file = File.join @opt[:group], file else repo = if repo.is_a?(::String) - File.join File.dirname(repo), @opt[:group] + '.git' - else - repo[@opt[:group]] - end + File.join File.dirname(repo), @opt[:group] + '.git' + else + repo[@opt[:group]] + end end end diff --git a/lib/oxidized/output/http.rb b/lib/oxidized/output/http.rb index 13ba300..0467261 100644 --- a/lib/oxidized/output/http.rb +++ b/lib/oxidized/output/http.rb @@ -7,9 +7,9 @@ module Oxidized def setup if @cfg.empty? - CFGS.user.output.http.user = 'Oxidized' + CFGS.user.output.http.user = 'Oxidized' CFGS.user.output.http.pasword = 'secret' - CFGS.user.output.http.url = 'http://localhost/web-api/oxidized' + CFGS.user.output.http.url = 'http://localhost/web-api/oxidized' CFGS.save :user raise NoConfig, 'no output http config, edit ~/.config/oxidized/config' end @@ -17,42 +17,38 @@ module Oxidized require "net/http" require "uri" require "json" - def store node, outputs, opt={} + 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. - } + { + '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'}) + # 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}" + 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 - - diff --git a/lib/oxidized/output/output.rb b/lib/oxidized/output/output.rb index 1355d03..9fae7b0 100644 --- a/lib/oxidized/output/output.rb +++ b/lib/oxidized/output/output.rb @@ -3,8 +3,7 @@ module Oxidized class NoConfig < OxidizedError; end def cfg_to_str cfg - cfg.select{ |h| h[:type]=='cfg' }.map{ |h| h[:data] }.join + cfg.select { |h| h[:type] == 'cfg' }.map { |h| h[:data] }.join end - end end -- cgit v1.2.1