From 70bbbb2ca879af28afb8d7b739e4379907fd426a Mon Sep 17 00:00:00 2001 From: Danilo Sousa Date: Fri, 19 Feb 2016 18:11:25 -0200 Subject: create `githubrepo` hook documentation in README --- README.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'README.md') diff --git a/README.md b/README.md index a130961..21a0956 100644 --- a/README.md +++ b/README.md @@ -525,6 +525,39 @@ hooks: timeout: 120 ``` +### githubrepo + +This hook configures the repository `remote` and _push_ the code when the specified event is triggerd. If the `username` and `password` are not provided, the `Rugged::Credentials::SshKeyFromAgent` will be used. + +`githubrepo` hook recognizes following configuration keys: + + * `remote_repo`: the remote repository to be pushed to. + * `username`: username for repository auth. + * `password`: password for repository auth. + +When using groups repositories, the remotes should be passed in the `groups` config for each group. + +``` yaml +vars: {} +groups: + routers: git@git.intranet:oxidized/routers.git + switches: git@git.intranet:oxidized/switches.git + firewalls: git@git.intranet:oxidized/firewalls.git +``` + + +## Hook configuration example + +``` yaml +hooks: + push_to_gitlab: + type: githubrepo + events: [node_success, post_store] + remote_repo: git@git.intranet:oxidized/test.git + username: user + password: pass +``` + # Ruby API The following objects exist in Oxidized. -- cgit v1.2.3 From a486e086fc3b52f26ade94e58a16fa2150fc9cae Mon Sep 17 00:00:00 2001 From: Danilo Sousa Date: Fri, 19 Feb 2016 18:58:47 -0200 Subject: move the groups remote to the hook config thanks to @ElvinEfendi for the idea! :+1: --- README.md | 15 ++++++++------- lib/oxidized/hook/githubrepo.rb | 2 +- spec/githubrepo_spec.rb | 14 ++++++++++---- 3 files changed, 19 insertions(+), 12 deletions(-) (limited to 'README.md') diff --git a/README.md b/README.md index 21a0956..3c1a41e 100644 --- a/README.md +++ b/README.md @@ -535,14 +535,15 @@ This hook configures the repository `remote` and _push_ the code when the specif * `username`: username for repository auth. * `password`: password for repository auth. -When using groups repositories, the remotes should be passed in the `groups` config for each group. +When using groups repositories, each group must have its own `remote` in the `remote_repo` config. ``` yaml -vars: {} -groups: - routers: git@git.intranet:oxidized/routers.git - switches: git@git.intranet:oxidized/switches.git - firewalls: git@git.intranet:oxidized/firewalls.git +hooks: + push_to_remote: + remote_repo: + routers: git@git.intranet:oxidized/routers.git + switches: git@git.intranet:oxidized/switches.git + firewalls: git@git.intranet:oxidized/firewalls.git ``` @@ -550,7 +551,7 @@ groups: ``` yaml hooks: - push_to_gitlab: + push_to_remote: type: githubrepo events: [node_success, post_store] remote_repo: git@git.intranet:oxidized/test.git diff --git a/lib/oxidized/hook/githubrepo.rb b/lib/oxidized/hook/githubrepo.rb index dd9520b..0c75c68 100644 --- a/lib/oxidized/hook/githubrepo.rb +++ b/lib/oxidized/hook/githubrepo.rb @@ -58,7 +58,7 @@ class GithubRepo < Oxidized::Hook if node.group.nil? || single_repo? cfg.remote_repo else - Oxidized.config.groups[node.group].remote_repo + cfg.remote_repo[node.group] end end diff --git a/spec/githubrepo_spec.rb b/spec/githubrepo_spec.rb index a5bbeca..8d85761 100644 --- a/spec/githubrepo_spec.rb +++ b/spec/githubrepo_spec.rb @@ -126,8 +126,6 @@ describe GithubRepo do before do Rugged::Credentials::SshKeyFromAgent.expects(:new).with(username: 'git').returns(credentials) Rugged::Repository.expects(:new).with(repository).returns(repo) - Oxidized.config.groups.ggrroouupp.remote_repo = 'ggrroouupp#remote_repo' - Oxidized.config.hooks.github_repo_hook.remote_repo = 'github_repo_hook#remote_repo' Oxidized.config.output.git.single_repo = single_repo repo.expects(:remotes).twice.returns(remotes) @@ -137,22 +135,30 @@ describe GithubRepo do remote.expects(:push).with(['refs/heads/master'], credentials: credentials).returns(true) end - describe 'when there are several repositories' do + describe 'and there are several repositories' do let(:create_remote) { 'ggrroouupp#remote_repo' } let(:repository) { './ggrroouupp.git' } let(:single_repo) { nil } + before do + Oxidized.config.hooks.github_repo_hook.remote_repo.ggrroouupp = 'ggrroouupp#remote_repo' + end + it 'will push to the node group repository' do gr.cfg = Oxidized.config.hooks.github_repo_hook gr.run_hook(ctx).must_equal true end end - describe 'when is a single repository' do + describe 'and has a single repository' do let(:create_remote) { 'github_repo_hook#remote_repo' } let(:repository) { 'foo.git' } let(:single_repo) { true } + before do + Oxidized.config.hooks.github_repo_hook.remote_repo = 'github_repo_hook#remote_repo' + end + it 'will push to the correct repository' do gr.cfg = Oxidized.config.hooks.github_repo_hook gr.run_hook(ctx).must_equal true -- cgit v1.2.3 From 69210c7ddf7d77cc9eb7b78f0bc00ad2f1475955 Mon Sep 17 00:00:00 2001 From: Danilo Sousa Date: Mon, 22 Feb 2016 14:16:29 -0300 Subject: follow the hook remotes config also for groups repositories ref.: https://github.com/ytti/oxidized/pull/326/files#r53557892 --- README.md | 27 +++++++++++++++++++++++++++ lib/oxidized/node.rb | 10 ++++------ spec/githubrepo_spec.rb | 4 +++- spec/node_spec.rb | 47 ++++++++++++++++++++++++++++++++++------------- 4 files changed, 68 insertions(+), 20 deletions(-) (limited to 'README.md') diff --git a/README.md b/README.md index 3c1a41e..1dcc426 100644 --- a/README.md +++ b/README.md @@ -354,13 +354,40 @@ output: This uses the rugged/libgit2 interface. So you should remember that normal Git hooks will not be executed. + +For a single repositories for all devices: + +``` yaml +output: + default: git + git: + user: Oxidized + email: o@example.com + repo: "/var/lib/oxidized/devices.git" ``` + +And for groups repositories: + +``` yaml output: default: git git: user: Oxidized email: o@example.com + repo: + first: "/var/lib/oxidized/first.git" + second: "/var/lib/oxidized/second.git" +``` + +If you would like to use groups and a single repository, you can force this with the `single_repo` config. + +``` yaml +output: + default: git + git: + single_repo: true repo: "/var/lib/oxidized/devices.git" + ``` ### Output types diff --git a/lib/oxidized/node.rb b/lib/oxidized/node.rb index a1504b9..35bcad9 100644 --- a/lib/oxidized/node.rb +++ b/lib/oxidized/node.rb @@ -171,14 +171,12 @@ module Oxidized end def resolve_repo - git = Oxidized.config.output.git - cfg_repo = git.repo + remote_repo = Oxidized.config.output.git.repo - if group && !git.single_repo? - basedir = File.dirname(cfg_repo) - File.join(basedir, "#{group}.git") + if Oxidized.config.output.git.single_repo? || @group.nil? || remote_repo.is_a?(String) + remote_repo else - cfg_repo + remote_repo[@group] end end diff --git a/spec/githubrepo_spec.rb b/spec/githubrepo_spec.rb index b6a8d9e..ab5e251 100644 --- a/spec/githubrepo_spec.rb +++ b/spec/githubrepo_spec.rb @@ -12,7 +12,6 @@ describe GithubRepo do before(:each) do Oxidized.asetus = Asetus.new - Oxidized.config.output.git.repo = 'foo.git' Oxidized.config.log = '/dev/null' Oxidized.setup_logger end @@ -97,6 +96,7 @@ describe GithubRepo do describe 'when there is only one repository and no groups' do before do + Oxidized.config.output.git.repo = 'foo.git' remote.expects(:url).returns('https://github.com/username/foo.git') remote.expects(:push).with(['refs/heads/master'], credentials: credentials).returns(true) repo.expects(:remotes).returns({'origin' => remote}) @@ -139,6 +139,7 @@ describe GithubRepo do let(:repository) { './ggrroouupp.git' } before do + Oxidized.config.output.git.repo.ggrroouupp = repository Oxidized.config.hooks.github_repo_hook.remote_repo.ggrroouupp = 'ggrroouupp#remote_repo' end @@ -153,6 +154,7 @@ describe GithubRepo do let(:repository) { 'foo.git' } before do + Oxidized.config.output.git.repo = repository Oxidized.config.hooks.github_repo_hook.remote_repo = 'github_repo_hook#remote_repo' Oxidized.config.output.git.single_repo = true end diff --git a/spec/node_spec.rb b/spec/node_spec.rb index f9c912e..21c6e34 100644 --- a/spec/node_spec.rb +++ b/spec/node_spec.rb @@ -3,7 +3,6 @@ require 'spec_helper' describe Oxidized::Node do before(:each) do Oxidized.stubs(:asetus).returns(Asetus.new) - Oxidized.config.output.git.repo = '/tmp/repository.git' Oxidized::Node.any_instance.stubs(:resolve_output) @node = Oxidized::Node.new(name: 'example.com', @@ -44,25 +43,47 @@ describe Oxidized::Node do end describe '#repo' do - it 'when there is no groups' do - @node.repo.must_equal '/tmp/repository.git' + let(:group) { nil } + let(:node) do + Oxidized::Node.new({ + ip: '127.0.0.1', group: group, model: 'junos' + }) + end + + it 'when there are no groups' do + Oxidized.config.output.git.repo = '/tmp/repository.git' + node.repo.must_equal '/tmp/repository.git' end describe 'when there are groups' do - let(:node) do - Oxidized::Node.new({ - ip: '127.0.0.1', group: 'ggrroouupp', model: 'junos' - }) + let(:group) { 'ggrroouupp' } + + before do + Oxidized.config.output.git.single_repo = single_repo end - it 'with only one repository' do - Oxidized.config.output.git.single_repo = true - node.repo.must_equal '/tmp/repository.git' + describe 'with only one repository' do + let(:single_repo) { true } + + before do + Oxidized.config.output.git.repo = '/tmp/repository.git' + end + + it 'should use the correct remote' do + node.repo.must_equal '/tmp/repository.git' + end end - it 'with more than one repository' do - Oxidized.config.output.git.single_repo = false - node.repo.must_equal '/tmp/ggrroouupp.git' + describe 'with more than one repository' do + let(:single_repo) { nil } + + before do + Oxidized.config.output.git.repo.ggrroouupp = '/tmp/ggrroouupp.git' + end + + it 'should use the correct remote' do + node.repo.must_equal '/tmp/ggrroouupp.git' + end end end end -- cgit v1.2.3