heirloom 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use ruby-1.9.3-p125@heirloom --create
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in heirloom.gemspec
4
+ gemspec
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'heirloom'
4
+ require 'heirloom/cli'
5
+
6
+ Heirloom::CLI.start
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "heirloom/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "heirloom"
7
+ s.version = Heirloom::VERSION
8
+ s.authors = ["Brett Weaver"]
9
+ s.email = ["brett@weav.net"]
10
+ s.homepage = ""
11
+ s.summary = %q{I help with artifacts}
12
+ s.description = %q{I help build and manage artifacts}
13
+
14
+ s.rubyforge_project = "heirloom"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "rspec"
23
+
24
+ s.add_runtime_dependency 'fog'
25
+ s.add_runtime_dependency 'grit'
26
+ s.add_runtime_dependency 'logger'
27
+ s.add_runtime_dependency 'minitar'
28
+ s.add_runtime_dependency 'trollop'
29
+ end
@@ -0,0 +1,189 @@
1
+ require "heirloom/aws"
2
+ require "heirloom/version"
3
+
4
+ require 'socket'
5
+ require 'fog'
6
+ require 'grit'
7
+ require 'logger'
8
+ require 'time'
9
+ require 'zlib'
10
+ require 'archive/tar/minitar'
11
+ include Archive::Tar
12
+ include Grit
13
+
14
+ module Heirloom
15
+
16
+ class Heirloom
17
+ attr_accessor :heirloom_type, :commit, :source_dir, :prefix
18
+
19
+ def self.list
20
+ sdb = self.connect_to_sdb
21
+
22
+ r = {}
23
+
24
+ sdb.domains.each do |domain|
25
+ r[domain] = sdb.select("select * from #{domain}")
26
+ end
27
+
28
+ r
29
+ end
30
+
31
+ def self.info(args)
32
+ sdb = self.connect_to_sdb
33
+
34
+ sdb.select("select * from #{args[:class]} where itemName() = '#{args[:sha]}'")
35
+ end
36
+
37
+ def self.delete(args)
38
+ sdb = self.connect_to_sdb
39
+
40
+ sdb.delete args[:class], args[:sha]
41
+ end
42
+
43
+ def self.connect_to_sdb
44
+ @access_key = ENV['AWS_ACCESS_KEY_ID']
45
+ @secret_key = ENV['AWS_SECRET_ACCESS_KEY']
46
+
47
+ AWS::SimpleDb.new(@access_key, @secret_key)
48
+ end
49
+
50
+ def initialize(args)
51
+ @sdb = self.class.connect_to_sdb
52
+
53
+ self.heirloom_type = args[:heirloom_type]
54
+ self.source_dir = args[:source_dir]
55
+ self.prefix = args[:prefix]
56
+ end
57
+
58
+ def build_and_upload_to_s3(args)
59
+ get_commit args[:sha]
60
+ raise 'Artifact already uploaded to S3' if already_in_sdb?
61
+ build_local_artifact
62
+ create_sdb_record
63
+ upload_to_s3
64
+ delete_local_artifact
65
+ end
66
+
67
+ protected
68
+
69
+ def get_commit(sha = nil)
70
+ r = Repo.new source_dir
71
+ if sha
72
+ self.commit = r.commits(sha).first
73
+ else
74
+ self.commit = r.commits.first
75
+ end
76
+ end
77
+
78
+ def build_local_artifact
79
+ tgz = Zlib::GzipWriter.new File.open(artifact_path, 'wb')
80
+ Minitar.pack(source_dir, tgz)
81
+ end
82
+
83
+ def delete_local_artifact
84
+ File.delete(artifact_path)
85
+ end
86
+
87
+ def create_sdb_domain
88
+ @sdb.create_domain(domain)
89
+ end
90
+
91
+ def create_sdb_record
92
+ create_sdb_domain
93
+ @sdb.put_attributes domain, sha, { 'built_by' => "#{user}@#{hostname}",
94
+ 'built_at' => Time.now.utc.iso8601,
95
+ 'tests' => 'pending',
96
+ 'sha' => sha,
97
+ 'abbreviated_sha' => abbreviated_sha,
98
+ 'message' => message,
99
+ 'author' => author }
100
+ end
101
+
102
+ def already_in_sdb?
103
+ @sdb.select("select * from #{domain} where itemName() = '#{sha}'").count > 0
104
+ end
105
+
106
+ def upload_to_s3
107
+ target_buckets = buckets
108
+
109
+ # Upload the artifact to each bucket
110
+ target_buckets.each_pair do |bucket, options|
111
+
112
+ # Connect to s3 in region of bucket
113
+ region = options['region']
114
+ connection = Fog::Storage.new :provider => 'AWS',
115
+ :aws_access_key_id => ENV['AWS_ACCESS_KEY_ID'],
116
+ :aws_secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
117
+ :region => region
118
+
119
+ # Get bucket
120
+ b = connection.directories.get bucket
121
+
122
+ # Upload the artifact
123
+ b.files.create :key => "#{folder}/#{artifact}",
124
+ :body => File.open(artifact_path),
125
+ :public => false
126
+
127
+ # Add the artifact location
128
+ @sdb.put_attributes domain, sha, { "#{region}-s3-url" => "s3://#{bucket}/#{folder}/#{artifact}" }
129
+
130
+ # Add the http url
131
+ @sdb.put_attributes domain, sha, { "#{region}-http-url" => "http://#{bucket}/#{folder}/#{artifact}" }
132
+
133
+ # Add the https url
134
+ @sdb.put_attributes domain, sha, { "#{region}-https-url" => "https://#{bucket}/#{folder}/#{artifact}" }
135
+ end
136
+ end
137
+
138
+ private
139
+
140
+ def buckets
141
+ {
142
+ "#{prefix}-us-west-1" => {
143
+ 'region' => 'us-west-1'
144
+ }
145
+ }
146
+ end
147
+
148
+ def artifact
149
+ "#{sha}.tar.gz"
150
+ end
151
+
152
+ def artifact_path
153
+ "/tmp/#{artifact}"
154
+ end
155
+
156
+ def domain
157
+ heirloom_type
158
+ end
159
+
160
+ def folder
161
+ heirloom_type
162
+ end
163
+
164
+ def author
165
+ commit.author.name
166
+ end
167
+
168
+ def message
169
+ commit.message
170
+ end
171
+
172
+ def sha
173
+ commit.id
174
+ end
175
+
176
+ def abbreviated_sha
177
+ commit.id_abbrev
178
+ end
179
+
180
+ def user
181
+ ENV['USER']
182
+ end
183
+
184
+ def hostname
185
+ Socket.gethostname
186
+ end
187
+
188
+ end
189
+ end
@@ -0,0 +1,35 @@
1
+ module Heirloom
2
+
3
+ class AWS
4
+ class SimpleDb
5
+
6
+ def initialize(access_key, secret_key)
7
+ @sdb = Fog::AWS::SimpleDB.new :aws_access_key_id => access_key,
8
+ :aws_secret_access_key => secret_key,
9
+ :region => 'us-west-1'
10
+ end
11
+
12
+ def domains
13
+ @sdb.list_domains.body['Domains']
14
+ end
15
+
16
+ def create_domain(domain)
17
+ @sdb.create_domain(domain) unless domains.include? domain
18
+ end
19
+
20
+ def put_attributes(domain, key, args)
21
+ @sdb.put_attributes domain, key, args
22
+ end
23
+
24
+ def select(query)
25
+ @sdb.select(query).body['Items']
26
+ end
27
+
28
+ def delete(domain, key)
29
+ @sdb.delete_attributes domain, key
30
+ end
31
+
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,48 @@
1
+ require 'trollop'
2
+
3
+ module Heirloom
4
+ module CLI
5
+ def self.start
6
+ @opts = Trollop::options do
7
+ banner <<-EOS
8
+ build and manage artifacts
9
+
10
+ Usage:
11
+
12
+ heirloom [options] build
13
+ heirloom list
14
+ EOS
15
+ opt :help, "Display Help"
16
+ opt :class, "Class of artifact. This should match the SCM repo", :type => :string
17
+ opt :dir, "Directory which contains git repo", :type => :string
18
+ opt :prefix, "Bucket prefix", :type => :string
19
+ opt :sha, "Git Sha", :type => :string
20
+ end
21
+
22
+ cmd = ARGV.shift
23
+
24
+ case cmd
25
+ when 'build'
26
+ raise 'Missing required args' unless @opts[:sha] && @opts[:class]
27
+ h = Heirloom.new :heirloom_type => @opts[:class],
28
+ :source_dir => @opts[:dir] ||= ".",
29
+ :prefix => @opts[:prefix]
30
+
31
+ h.build_and_upload_to_s3(:sha => @opts[:sha])
32
+ when 'list'
33
+ Heirloom.list.each do |a|
34
+ puts a.to_yaml
35
+ end
36
+ when 'info'
37
+ raise 'Missing required args' unless @opts[:sha] && @opts[:class]
38
+ puts Heirloom.info(:class => @opts[:class], :sha => @opts[:sha]).to_yaml
39
+ when 'delete'
40
+ raise 'Missing required args' unless @opts[:sha] && @opts[:class]
41
+ puts Heirloom.delete(:class => @opts[:class], :sha => @opts[:sha]).to_yaml
42
+ puts "!!Make sure to manualy delete any artifacts from S3!!!"
43
+ else
44
+ puts "Unkown command: '#{cmd}'."
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module Heirloom
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: heirloom
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brett Weaver
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70129331233340 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70129331233340
25
+ - !ruby/object:Gem::Dependency
26
+ name: fog
27
+ requirement: &70129331232880 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70129331232880
36
+ - !ruby/object:Gem::Dependency
37
+ name: grit
38
+ requirement: &70129331228060 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70129331228060
47
+ - !ruby/object:Gem::Dependency
48
+ name: logger
49
+ requirement: &70129331227600 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *70129331227600
58
+ - !ruby/object:Gem::Dependency
59
+ name: minitar
60
+ requirement: &70129331227180 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: *70129331227180
69
+ - !ruby/object:Gem::Dependency
70
+ name: trollop
71
+ requirement: &70129331226740 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: *70129331226740
80
+ description: I help build and manage artifacts
81
+ email:
82
+ - brett@weav.net
83
+ executables:
84
+ - heirloom
85
+ extensions: []
86
+ extra_rdoc_files: []
87
+ files:
88
+ - .gitignore
89
+ - .rvmrc
90
+ - Gemfile
91
+ - Rakefile
92
+ - bin/heirloom
93
+ - heirloom.gemspec
94
+ - lib/heirloom.rb
95
+ - lib/heirloom/aws.rb
96
+ - lib/heirloom/cli.rb
97
+ - lib/heirloom/version.rb
98
+ homepage: ''
99
+ licenses: []
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ segments:
111
+ - 0
112
+ hash: 2848188182829183969
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ segments:
120
+ - 0
121
+ hash: 2848188182829183969
122
+ requirements: []
123
+ rubyforge_project: heirloom
124
+ rubygems_version: 1.8.16
125
+ signing_key:
126
+ specification_version: 3
127
+ summary: I help with artifacts
128
+ test_files: []