lampwick 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format=documentation
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.8.7
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in lampwick.gemspec
4
+ gemspec
5
+
6
+ gem 'rake', :group => :development
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Aziz Shamim
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # Lampwick
2
+
3
+ This gem makes it easy to run an autostage script, in the event that the puppet environment needs 'PULL' only and cannot use git-hooks
4
+
5
+ [![Build Status](https://travis-ci.org/azizshamim/lampwick.png?branch=master)](https://travis-ci.org/azizshamim/lampwick)
6
+
7
+ * **TODO: auth for private repos**
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```
14
+ gem 'lampwick'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ ```
20
+ $ bundle
21
+ ```
22
+
23
+ Or install it yourself as:
24
+
25
+ ```
26
+ $ gem install lampwick
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ Create a configuration file with the following elements
32
+ ```
33
+ ---
34
+ git: 'git repository' (e.g. https://github.com/fup/example.git or git@github.com:fup/example
35
+ target: 'path to the directory where the environments will be set up'
36
+ repo: 'path to the directory where the repository will be initially cloned' [Optional]
37
+ ```
38
+ If using basic auth, include it in the URI (e.g. https://**user**:**password**@github.com/fup/somerepo.git)
39
+
40
+ If using ssh, make sure the key is loaded using keyagent, key location is currently unsupported.
41
+
42
+ For GitHub private repos, username and password are needed.
43
+
44
+ ```shell
45
+ lampwick --config <config_file> [--named] [--purge]
46
+ ```
47
+ ## Contributing
48
+
49
+ 1. Fork it
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "bundler"
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec) do |t|
7
+ t.rspec_opts = ' -r ./spec/spec_helper'
8
+ t.rspec_opts += ' --tag focus'
9
+ end
10
+
11
+ RSpec::Core::RakeTask.new(:spec_all)
12
+
13
+ task :default => :spec_all
14
+
15
+ task :guard do
16
+ Bundler.setup
17
+ exec("guard")
18
+ end
data/bin/lampwick ADDED
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'ostruct'
5
+ require 'rubygems'
6
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','lib','lampwick.rb'))
7
+
8
+ $stderr = File.new("/dev/null", "w")
9
+
10
+ optparse = OptionParser.new do |opts|
11
+ opts.banner = "Usage: lampwick -c <config>"
12
+
13
+ opts.on('--named', 'Create named environments from github pull request information' do |named|
14
+ Lampwick::Runner.named_environments = true
15
+ end
16
+
17
+ opts.on('--purge', 'Purge target before creating new directories' do |purge|
18
+ Lampwick::Runner.purge_before = true
19
+ end
20
+
21
+ opts.on('-c', '--config CONFIGFILE', 'Load a CONFIGFILE (see sample in config/autostage.conf)') do |file|
22
+ Lampwick::Runner.config(file)
23
+ end
24
+ end.parse!
25
+ Lampwick::Runner.run!
26
+
@@ -0,0 +1,4 @@
1
+ ---
2
+ git: 'https://github.com/jfryman/puppet-nginx.git'
3
+ target: '/var/tmp/environments'
4
+ repo: '/var/tmp/repo'
data/lampwick.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/lampwick/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Paul Morgan", "Aziz Shamim"]
6
+ gem.email = ["jumanjiman@gmail.com", "azizshamim@gmail.com"]
7
+ gem.description = %q{"Pull repositories and create puppet environment branches"}
8
+ gem.summary = %q{"Pull repositories and create puppet environment branches"}
9
+ gem.homepage = ""
10
+
11
+ gem.requirements << 'git, >1.7'
12
+
13
+ gem.add_dependency('github_api')
14
+ gem.add_development_dependency('rb-fsevent', '~> 0.9')
15
+ gem.add_development_dependency('guard-rspec')
16
+ gem.add_development_dependency('pry')
17
+
18
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ gem.files = `git ls-files`.split("\n")
20
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ gem.name = "lampwick"
22
+ gem.require_paths = ["lib"]
23
+ gem.version = Lampwick::VERSION
24
+ end
data/lib/lampwick.rb ADDED
@@ -0,0 +1,8 @@
1
+
2
+ module Lampwick; end
3
+
4
+ this = File.expand_path(File.dirname(__FILE__))
5
+
6
+ require File.join(this, 'lampwick', 'version.rb')
7
+ require File.join(this, 'lampwick', 'runner.rb')
8
+ require File.join(this, 'lampwick', 'git.rb')
@@ -0,0 +1,89 @@
1
+ require 'ostruct'
2
+ require 'tmpdir'
3
+ require 'uri'
4
+ require 'github_api'
5
+
6
+ module Lampwick
7
+ class Git
8
+ attr_reader :config
9
+ REQUIRED_INITIAL = [ :git ]
10
+ # pass a git repository, access credentials to the git repo (username/password or path to an ssh key)
11
+ # { :git => '', :user => 'foo', :password => 'bar', :key => 'path/to/key' }
12
+ # returns a new lampwick object
13
+ def initialize(config)
14
+ @config = ::OpenStruct.new(config)
15
+ REQUIRED_INITIAL.each do |arg|
16
+ raise ArgumentError, "Needs the #{arg} argument" unless @config.respond_to?(arg)
17
+ raise ArgumentError, "#{arg} argument cannot be empty/blank" if @config.__send__(arg.to_s).nil? or @config.__send__(arg.to_s).empty?
18
+ end
19
+ @config.git = URI(@config.git)
20
+ end
21
+
22
+ def update_or_clone(repo = @config.repo)
23
+ if @config.git.scheme == 'https'
24
+ %x|git clone --quiet #{@config.git} #{repo}| unless repo_exists?(repo)
25
+ end
26
+ end
27
+
28
+ def pull_requests(repo = @config.repo)
29
+ raise StandardError, "Repo has not been cloned at: #{repo}" unless repo_exists?(repo)
30
+ refs = Hash.new
31
+ Dir.chdir @config.repo do
32
+ %x|git fetch --quiet origin '+refs/pull/*/head:refs/remotes/origin/pr/*'|
33
+ refs = %x|git show-ref --dereference|
34
+ flipped_refs = refs.split("\n").map{|ab| ab.split(/\s+/).reverse }.flatten
35
+ refs = Hash[*flipped_refs].delete_if { |ref,hash| ref !~ /origin\/pr\/\d+$/ }
36
+ end
37
+ refs
38
+ end
39
+
40
+ def populate_environments(target = @config.target)
41
+ raise StandardError, "Repo has not been cloned at: #{repo}" unless repo_exists?(@config.repo)
42
+ Dir.chdir target do
43
+ pull_requests.each do |ref, hash|
44
+ %x|git clone #{@config.repo} #{target}/#{hash}|
45
+ Dir.chdir "#{target}/#{hash}" do
46
+ %x|git checkout --quiet #{hash}|
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ def named_directories(target = @config.target)
53
+ Dir.chdir target do
54
+ github_requests.each do |hash,name|
55
+ File.symlink(hash, name)
56
+ end
57
+ end
58
+ end
59
+
60
+ private
61
+ def repo_exists?(repo = @config.repo)
62
+ File.exist? "#{repo}/.git/config"
63
+ end
64
+
65
+ def purge!
66
+ raise StandardError, "Unsafe purge halted" if @config.target.empty?
67
+ Dir.chdir @config.target do
68
+ FileUtils.rm_rf(".", :secure => true)
69
+ end
70
+ end
71
+
72
+ def repo
73
+ @config.git.path.split('/').last.chomp('.git')
74
+ end
75
+
76
+ def github_requests
77
+ github = Github.new
78
+ reqs = Hash.new
79
+ github.pull_requests.list(:repo => repo, :user => user) do |req|
80
+ reqs[req['head']['sha']] = "#{req['user']['login']}_#{req['head']['ref']}"
81
+ end
82
+ reqs
83
+ end
84
+
85
+ def user
86
+ @config.git.path.split('/')[-2]
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,23 @@
1
+ module Lampwick
2
+ class Runner
3
+ class << self; attr_accessor :config, :purge, :named; end
4
+ @config = ''
5
+ @named, @purge = false, false
6
+
7
+ def self.run!
8
+ raise ArgumentError, "Need to have a config" if @config.empty?
9
+ f = File.open(@config)
10
+ config = YAML::load(f)
11
+ git = Lampwick::Git.new(config)
12
+ if @purge and !git.config.target.nil?
13
+ puts "Purging #{git.config.target}"
14
+ Dir.chdir git.config.target do
15
+ FileUtils.rm_rf(".", :secure => true)
16
+ end
17
+ end
18
+ git.update_or_clone
19
+ git.populate_environments
20
+ git.named_directories if @named and git.config.target
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module Lampwick
2
+ VERSION = "0.0.1"
3
+ end
data/spec/git_spec.rb ADDED
@@ -0,0 +1,61 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper.rb')
2
+
3
+ describe Lampwick::Git, :constraint => 'slow' do
4
+ let(:git) { 'https://github.com/jfryman/puppet-nginx.git' }
5
+ let(:cleanup!) do
6
+ FileUtils.rm_rf("#{@target}/.", :secure => true)
7
+ FileUtils.rm_rf("#{@repo}/.", :secure => true)
8
+ end
9
+
10
+ before(:all) do
11
+ @tmpdir = Dir.mktmpdir
12
+ @target = File.join(@tmpdir,'target')
13
+ @repo = File.join(@tmpdir,'repo')
14
+ %x|mkdir -p #{@target}|
15
+ %x|mkdir -p #{@repo}|
16
+ end
17
+
18
+ after(:each) do
19
+ cleanup!
20
+ end
21
+
22
+ after(:all) do
23
+ FileUtils.rm_rf("#{@tmpdir}/.", :secure => true)
24
+ end
25
+
26
+ it 'should require git argument' do
27
+ lambda { Lampwick::Git.new }.should raise_exception
28
+ lambda { Lampwick::Git.new(:git => '') }.should raise_exception
29
+ lambda { Lampwick::Git.new(:git => git) }.should_not raise_exception
30
+ end
31
+
32
+ it 'should clone a respoitory in the target directory' do
33
+ as = Lampwick::Git.new(:git => git, :repo => @repo)
34
+ as.update_or_clone
35
+ Dir["#{@repo}/*"].should_not be_empty
36
+ end
37
+
38
+ it 'should create a hash_environment for every branch in the target directory' do
39
+ as = Lampwick::Git.new(:git => git, :repo => @repo)
40
+ as.update_or_clone
41
+ as.populate_environments(@target)
42
+ hash_environments = Dir["#{@target}/*"]
43
+ hash_environments.should_not be_empty
44
+ end
45
+
46
+ it 'should return a list of pull requests' do
47
+ as = Lampwick::Git.new(:git => git, :repo => @repo)
48
+ as.update_or_clone
49
+ as.pull_requests.should be_a(Hash)
50
+ end
51
+
52
+ it 'should created named directories' do
53
+ as = Lampwick::Git.new(:git => git, :repo => @repo)
54
+ as.update_or_clone
55
+ as.populate_environments(@target)
56
+ pre_naming = Dir["#{@target}/*"]
57
+ as.named_directories(@target)
58
+ post_naming = Dir["#{@target}/*"]
59
+ (post_naming - pre_naming).should_not be_empty
60
+ end
61
+ end
@@ -0,0 +1,67 @@
1
+ describe Lampwick::Runner, :focus => true do
2
+ let(:git) { 'https://github.com/jfryman/puppet-nginx.git' }
3
+ let(:cleanup!) do
4
+ FileUtils.rm_rf("#{@target}/.", :secure => true)
5
+ FileUtils.rm_rf("#{@repo}/.", :secure => true)
6
+ end
7
+
8
+ before(:all) do
9
+ @tmpdir = Dir.mktmpdir
10
+ @target = File.join(@tmpdir,'target')
11
+ @repo = File.join(@tmpdir,'repo')
12
+ %x|mkdir -p #{@target}|
13
+ %x|mkdir -p #{@repo}|
14
+
15
+ content =<<EOF
16
+ ---
17
+ git: 'https://github.com/jfryman/puppet-nginx.git'
18
+ target: '#{@target}'
19
+ repo: '#{@repo}'
20
+ EOF
21
+
22
+ @autostage_conf = File.join(@tmpdir,'autostage.conf')
23
+ File.open(@autostage_conf, 'w') {|f| f.write(content) }
24
+ end
25
+
26
+ after(:each) do
27
+ cleanup!
28
+ end
29
+
30
+ after(:all) do
31
+ FileUtils.rm_rf("#{@tmpdir}/.", :secure => true)
32
+ end
33
+
34
+
35
+ subject { Lampwick::Runner }
36
+ it { should respond_to(:run!) }
37
+
38
+ it 'should store config' do
39
+ subject.config.should be_empty
40
+ subject.config = @autostage_conf
41
+ subject.config.should_not be_empty
42
+ end
43
+
44
+ it 'should store purge' do
45
+ subject.purge = true
46
+ subject.purge.should be_true
47
+ subject.purge = false
48
+ end
49
+
50
+ it 'should store named' do
51
+ subject.named = true
52
+ subject.named.should be_true
53
+ subject.named = false
54
+ end
55
+
56
+ it 'should load up a config file' do
57
+ subject.config = @autostage_conf
58
+ subject.config.should_not be_empty
59
+ end
60
+
61
+ it 'should run!' do
62
+ subject.config = @autostage_conf
63
+ #lambda { subject.run!.should_not raise_execption }
64
+ subject.run!
65
+ end
66
+
67
+ end
@@ -0,0 +1,13 @@
1
+ require File.join(File.dirname(__FILE__),'..','lib','lampwick.rb')
2
+
3
+ RSpec.configure do |config|
4
+ # RSpec automatically cleans stuff out of backtraces;
5
+ # sometimes this is annoying when trying to debug something e.g. a gem
6
+ config.backtrace_clean_patterns = [
7
+ /\/lib\d*\/ruby\//,
8
+ /bin\//,
9
+ /gems/,
10
+ /spec\/spec_helper\.rb/,
11
+ /lib\/rspec\/(core|expectations|matchers|mocks)/
12
+ ]
13
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lampwick
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Paul Morgan
9
+ - Aziz Shamim
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-04-26 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: github_api
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: rb-fsevent
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: '0.9'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: '0.9'
47
+ - !ruby/object:Gem::Dependency
48
+ name: guard-rspec
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ - !ruby/object:Gem::Dependency
64
+ name: pry
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ description: ! '"Pull repositories and create puppet environment branches"'
80
+ email:
81
+ - jumanjiman@gmail.com
82
+ - azizshamim@gmail.com
83
+ executables:
84
+ - lampwick
85
+ extensions: []
86
+ extra_rdoc_files: []
87
+ files:
88
+ - .gitignore
89
+ - .rspec
90
+ - .travis.yml
91
+ - Gemfile
92
+ - LICENSE
93
+ - README.md
94
+ - Rakefile
95
+ - bin/lampwick
96
+ - examples/lampwick.conf
97
+ - lampwick.gemspec
98
+ - lib/lampwick.rb
99
+ - lib/lampwick/git.rb
100
+ - lib/lampwick/runner.rb
101
+ - lib/lampwick/version.rb
102
+ - spec/git_spec.rb
103
+ - spec/runner_spec.rb
104
+ - spec/spec_helper.rb
105
+ homepage: ''
106
+ licenses: []
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements:
124
+ - git, >1.7
125
+ rubyforge_project:
126
+ rubygems_version: 1.8.24
127
+ signing_key:
128
+ specification_version: 3
129
+ summary: ! '"Pull repositories and create puppet environment branches"'
130
+ test_files:
131
+ - spec/git_spec.rb
132
+ - spec/runner_spec.rb
133
+ - spec/spec_helper.rb