summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanilo Sousa <dsgoncalves@uoldiveo.com>2016-07-20 07:00:31 -0300
committerDanilo Sousa <dsgoncalves@uoldiveo.com>2016-07-20 10:32:59 -0300
commitde2a9d18a5847439b40adfc0cc4c75e6cbf6262d (patch)
tree5cda65dc6210c89a4d85518c1c55e42034f8807d
parent3310af0e9017eb7ea16ae19a98929ec5541890bf (diff)
fixed the #fetch and #verions web problems
apart from the refactorings, now the node path its correct even when groups are used
-rw-r--r--lib/oxidized/nodes.rb54
-rw-r--r--lib/oxidized/output/git.rb50
-rw-r--r--spec/input/ssh_spec.rb1
-rw-r--r--spec/node_spec.rb5
-rw-r--r--spec/nodes_spec.rb1
5 files changed, 53 insertions, 58 deletions
diff --git a/lib/oxidized/nodes.rb b/lib/oxidized/nodes.rb
index cd67007..f5a1ad0 100644
--- a/lib/oxidized/nodes.rb
+++ b/lib/oxidized/nodes.rb
@@ -56,11 +56,8 @@ module Oxidized
end
end
- def fetch node, group
- with_lock do
- i = find_node_index node
- output = self[i].output.new
- raise Oxidized::NotSupported unless output.respond_to? :fetch
+ def fetch node_name, group
+ yield_node_output(node_name) do |node, output|
output.fetch node, group
end
end
@@ -94,6 +91,24 @@ module Oxidized
find_index node or raise Oxidized::NodeNotFound, "unable to find '#{node}'"
end
+ def version node_name, group
+ yield_node_output(node_name) do |node, output|
+ output.version node, group
+ end
+ end
+
+ def get_version node_name, group, oid
+ yield_node_output(node_name) do |node, output|
+ output.get_version node, group, oid
+ end
+ end
+
+ def get_diff node_name, group, oid1, oid2
+ yield_node_output(node_name) do |node, output|
+ output.get_diff node, group, oid1, oid2
+ end
+ end
+
private
def initialize opts={}
@@ -151,34 +166,13 @@ module Oxidized
sort_by! { |x| x.last.nil? ? Time.new(0) : x.last.end }
end
- public
-
- def version node, group
+ def yield_node_output(node_name)
with_lock do
- i = find_node_index node
- output = self[i].output.new
+ node = find { |n| n.name == node_name }
+ output = node.output.new
raise Oxidized::NotSupported unless output.respond_to? :fetch
- output.version node, group
- end
- end
-
- def get_version node, group, oid
- with_lock do
- i = find_node_index node
- output = self[i].output.new
- raise Oxidized::NotSupported unless output.respond_to? :fetch
- output.get_version node, group, oid
+ yield node, output
end
end
-
- def get_diff node, group, oid1, oid2
- with_lock do
- i = find_node_index node
- output = self[i].output.new
- raise Oxidized::NotSupported unless output.respond_to? :fetch
- output.get_diff node, group, oid1, oid2
- end
- end
-
end
end
diff --git a/lib/oxidized/output/git.rb b/lib/oxidized/output/git.rb
index 8d9dae1..23bd663 100644
--- a/lib/oxidized/output/git.rb
+++ b/lib/oxidized/output/git.rb
@@ -41,7 +41,7 @@ class Git < Output
outputs.types.each do |type|
type_cfg = ''
- type_repo = File.join File.dirname(repo), type + '.git'
+ puts 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
@@ -60,29 +60,21 @@ class Git < Output
def fetch node, group
begin
- repo = @cfg.repo
- repo = File.join File.dirname(repo), group + '.git' if group and not @cfg.single_repo?
+ 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?
- file = node
- file = File.join(group, node) if group and @cfg.single_repo?
- repo.read(index.get(file)[:oid]).data
+ repo.read(index.get(path)[:oid]).data
rescue
'node not found'
end
end
- #give a hash of all oid revision for the given node, and the date of the commit
+ # give a hash of all oid revision for the given node, and the date of the commit
def version node, group
begin
- repo = @cfg.repo
- path = node
- if group and @cfg.single_repo?
- path = "#{group}/#{node}"
- elsif group
- repo = File.join File.dirname(repo), group + '.git'
- end
+ repo, path = yield_repo_and_path(node, group)
+
repo = Rugged::Repository.new repo
walker = Rugged::Walker.new(repo)
walker.sorting(Rugged::SORT_DATE)
@@ -109,14 +101,9 @@ class Git < Output
#give the blob of a specific revision
def get_version node, group, oid
begin
- repo = @cfg.repo
- if group && group != '' && !@cfg.single_repo?
- repo = File.join File.dirname(repo), group + '.git'
- elsif group && group != ''
- node = File.join group, node
- end
+ repo, path = yield_repo_and_path(node, group)
repo = Rugged::Repository.new repo
- repo.blob_at(oid,node).content
+ repo.blob_at(oid,path).content
rescue
'version not found'
end
@@ -125,30 +112,27 @@ class Git < Output
#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
- repo = @cfg.repo
diff_commits = nil
- if group && group != '' && !@cfg.single_repo?
- repo = File.join File.dirname(repo), group + '.git'
- end
+ repo, _ = yield_repo_and_path(node, group)
repo = Rugged::Repository.new repo
commit = repo.lookup(oid1)
- #if the second revision is precised
+
if oid2
commit_old = repo.lookup(oid2)
diff = repo.diff(commit_old, commit)
diff.each do |patch|
- if /#{node}\s+/.match(patch.to_s.lines.first)
+ if /#{node.name}\s+/.match(patch.to_s.lines.first)
diff_commits = {:patch => patch.to_s, :stat => patch.stat}
break
end
end
- #else gives the diffs between the first oid and his first parrent
else
stat = commit.parents[0].diff(commit).stat
stat = [stat[1],stat[2]]
patch = commit.parents[0].diff(commit).patch
diff_commits = {:patch => patch, :stat => stat}
end
+
diff_commits
rescue
'no diffs'
@@ -157,6 +141,16 @@ class Git < Output
private
+ def yield_repo_and_path(node, group)
+ repo, path = node.repo, node.name
+
+ if group and @cfg.single_repo?
+ path = "#{group}/#{node.name}"
+ end
+
+ [repo, path]
+ end
+
def update repo, file, data
return if data.empty?
diff --git a/spec/input/ssh_spec.rb b/spec/input/ssh_spec.rb
index 2550d5c..d86ffa0 100644
--- a/spec/input/ssh_spec.rb
+++ b/spec/input/ssh_spec.rb
@@ -5,6 +5,7 @@ describe Oxidized::SSH do
before(:each) do
Oxidized.asetus = Asetus.new
Oxidized.setup_logger
+ Oxidized::Node.any_instance.stubs(:resolve_repo)
Oxidized::Node.any_instance.stubs(:resolve_input)
Oxidized::Node.any_instance.stubs(:resolve_output)
@node = Oxidized::Node.new(name: 'example.com',
diff --git a/spec/node_spec.rb b/spec/node_spec.rb
index 1c38558..5cb5216 100644
--- a/spec/node_spec.rb
+++ b/spec/node_spec.rb
@@ -5,6 +5,7 @@ describe Oxidized::Node do
Oxidized.asetus = Asetus.new
Oxidized.setup_logger
+ Oxidized::Node.any_instance.stubs(:resolve_repo)
Oxidized::Node.any_instance.stubs(:resolve_output)
@node = Oxidized::Node.new(name: 'example.com',
input: 'ssh',
@@ -44,6 +45,10 @@ describe Oxidized::Node do
end
describe '#repo' do
+ before do
+ Oxidized::Node.any_instance.unstub(:resolve_repo)
+ end
+
let(:group) { nil }
let(:node) do
Oxidized::Node.new({
diff --git a/spec/nodes_spec.rb b/spec/nodes_spec.rb
index 4560bdb..6fa4b41 100644
--- a/spec/nodes_spec.rb
+++ b/spec/nodes_spec.rb
@@ -15,6 +15,7 @@ describe Oxidized::Nodes do
prompt: 'test_prompt'
}
+ Oxidized::Node.any_instance.stubs(:resolve_repo)
Oxidized::Node.any_instance.stubs(:resolve_output)
@nodes_org = %w(ltt-pe1.hel kes2-rr1.tku tor-peer1.oul
hal-p2.tre sav-gr1-sw1.kuo psl-sec-pe1.hel).map { |e| Oxidized::Node.new(opts.merge(name: e)) }