heirloom 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/CHANGELOG +6 -0
  2. data/README.md +0 -1
  3. data/lib/heirloom/acl/s3.rb +10 -8
  4. data/lib/heirloom/artifact.rb +44 -43
  5. data/lib/heirloom/artifact/artifact_authorizer.rb +20 -22
  6. data/lib/heirloom/artifact/artifact_builder.rb +41 -37
  7. data/lib/heirloom/artifact/artifact_destroyer.rb +16 -14
  8. data/lib/heirloom/artifact/artifact_downloader.rb +22 -40
  9. data/lib/heirloom/artifact/artifact_lister.rb +10 -7
  10. data/lib/heirloom/artifact/artifact_reader.rb +17 -20
  11. data/lib/heirloom/artifact/artifact_updater.rb +6 -5
  12. data/lib/heirloom/artifact/artifact_uploader.rb +8 -8
  13. data/lib/heirloom/aws/s3.rb +1 -1
  14. data/lib/heirloom/cli.rb +37 -30
  15. data/lib/heirloom/cli/build.rb +40 -0
  16. data/lib/heirloom/cli/destroy.rb +20 -0
  17. data/lib/heirloom/cli/download.rb +23 -0
  18. data/lib/heirloom/cli/list.rb +17 -0
  19. data/lib/heirloom/cli/show.rb +27 -0
  20. data/lib/heirloom/cli/update.rb +23 -0
  21. data/lib/heirloom/config.rb +11 -6
  22. data/lib/heirloom/destroyer/s3.rb +6 -4
  23. data/lib/heirloom/directory/directory.rb +15 -11
  24. data/lib/heirloom/directory/git_directory.rb +4 -3
  25. data/lib/heirloom/downloader/s3.rb +1 -1
  26. data/lib/heirloom/logger.rb +6 -4
  27. data/lib/heirloom/version.rb +1 -1
  28. data/spec/acl/s3_spec.rb +51 -0
  29. data/spec/artifact/artifact_authorizer_spec.rb +34 -0
  30. data/spec/artifact/artifact_builder_spec.rb +41 -0
  31. data/spec/artifact/artifact_destroyer_spec.rb +45 -0
  32. data/spec/artifact/artifact_downloader_spec.rb +93 -0
  33. data/spec/artifact/artifact_lister_spec.rb +21 -0
  34. data/spec/artifact/artifact_reader_spec.rb +55 -0
  35. data/spec/artifact/artifact_updater_spec.rb +16 -0
  36. data/spec/artifact/artifact_uploader_spec.rb +16 -0
  37. data/spec/artifact_spec.rb +69 -19
  38. data/spec/aws/s3_spec.rb +55 -0
  39. data/spec/aws/simpledb_spec.rb +50 -0
  40. data/spec/config_spec.rb +32 -3
  41. data/spec/destroyer/s3_spec.rb +21 -0
  42. data/spec/directory/directory_spec.rb +26 -0
  43. data/spec/directory/git_directory_spec.rb +28 -0
  44. data/spec/downloader/s3_spec.rb +23 -0
  45. data/spec/logger_spec.rb +20 -0
  46. metadata +55 -16
@@ -4,15 +4,12 @@ module Heirloom
4
4
 
5
5
  def initialize(args)
6
6
  @config = args[:config]
7
+ @name = args[:name]
7
8
  end
8
9
 
9
- def list(args)
10
- domain = args[:name]
11
- sdb.select("select * from #{domain}").keys.reverse
12
- end
13
-
14
- def names
15
- sdb.domains
10
+ def list(limit=10)
11
+ sdb.select("select * from #{@name} where built_at > '2000-01-01T00:00:00.000Z'\
12
+ order by built_at desc limit #{limit}").keys
16
13
  end
17
14
 
18
15
  private
@@ -21,5 +18,11 @@ module Heirloom
21
18
  @sdb ||= AWS::SimpleDB.new :config => @config
22
19
  end
23
20
 
21
+ def artifact_reader(id)
22
+ @artifact_reader ||= ArtifactReader.new :config => @config,
23
+ :name => @name,
24
+ :id => id
25
+ end
26
+
24
27
  end
25
28
  end
@@ -2,41 +2,38 @@ module Heirloom
2
2
 
3
3
  class ArtifactReader
4
4
 
5
+ attr_accessor :config, :id, :name
6
+
5
7
  def initialize(args)
6
- @config = args[:config]
8
+ self.config = args[:config]
9
+ self.name = args[:name]
10
+ self.id = args[:id]
7
11
  end
8
12
 
9
- def show(args)
10
- domain = args[:name]
11
- id = args[:id]
12
- items = sdb.select "select * from #{domain} where itemName() = '#{id}'"
13
- items[id]
13
+ def show
14
+ items = sdb.select "select * from #{name} where itemName() = '#{id}'"
15
+ items[@id]
14
16
  end
15
17
 
16
- def exists?(args)
17
- show(args) != nil
18
+ def exists?
19
+ show != nil
18
20
  end
19
21
 
20
22
  def get_bucket(args)
21
- artifact = show :name => args[:name],
22
- :id => args[:id]
23
-
24
- url = artifact["#{args[:region]}-s3-url"].first
25
-
26
- bucket = url.gsub('s3://', '').split('/').first
23
+ get_url(args).gsub('s3://', '').split('/').first
27
24
  end
28
25
 
29
26
  def get_key(args)
30
- artifact = show :name => args[:name],
31
- :id => args[:id]
32
-
33
- url = artifact["#{args[:region]}-s3-url"].first
34
-
35
- bucket = url.gsub('s3://', '').gsub(get_bucket(args), '')
27
+ bucket_path = get_bucket :region => args[:region]
28
+ bucket = get_url(args).gsub('s3://', '').gsub(bucket_path, '')
36
29
  bucket.slice!(0)
37
30
  bucket
38
31
  end
39
32
 
33
+ def get_url(args)
34
+ show["#{args[:region]}-s3-url"].first
35
+ end
36
+
40
37
  private
41
38
 
42
39
  def sdb
@@ -4,17 +4,18 @@ module Heirloom
4
4
 
5
5
  def initialize(args)
6
6
  @config = args[:config]
7
- @logger = args[:logger]
7
+ @name = args[:name]
8
+ @id = args[:id]
9
+
10
+ @logger = @config.logger
8
11
  end
9
12
 
10
13
  def update(args)
11
14
  attribute = args[:attribute]
12
- id = args[:id]
13
- name = args[:name]
14
15
  update = args[:update]
15
16
 
16
- sdb.put_attributes name, id, { attribute => update }, { :replace => attribute }
17
- @logger.info "Updated #{name} (#{id}): #{attribute} = #{update}"
17
+ sdb.put_attributes @name, @id, { attribute => update }, { :replace => attribute }
18
+ @logger.info "Updated #{@name} (#{@id}): #{attribute} = #{update}."
18
19
  end
19
20
 
20
21
  private
@@ -4,15 +4,14 @@ module Heirloom
4
4
 
5
5
  def initialize(args)
6
6
  @config = args[:config]
7
- @logger = args[:logger]
7
+ @name = args[:name]
8
+ @id = args[:id]
9
+ @logger = @config.logger
8
10
  end
9
11
 
10
12
  def upload(args)
11
- id = args[:id]
12
13
  file = args[:file]
13
- key_folder = args[:name]
14
- key_name = "#{id}.tar.gz"
15
- name = args[:name]
14
+ key_name = "#{@id}.tar.gz"
16
15
  bucket_prefix = args[:bucket_prefix]
17
16
  public_readable = args[:public_readable]
18
17
 
@@ -25,12 +24,13 @@ module Heirloom
25
24
 
26
25
  s3_uploader.upload_file :bucket => bucket,
27
26
  :file => file,
28
- :id => id,
29
- :key_folder => key_folder,
27
+ :id => @id,
28
+ :key_folder => @name,
30
29
  :key_name => key_name,
31
- :name => name,
30
+ :name => @name,
32
31
  :public_readable => public_readable
33
32
  end
33
+ @logger.info "Upload complete."
34
34
  end
35
35
 
36
36
  end
@@ -15,7 +15,7 @@ module Heirloom
15
15
  end
16
16
 
17
17
  def delete_object(bucket_name, object_name, options = {})
18
- @s3.delete_object(bucket_name, object_name, options = {})
18
+ @s3.delete_object(bucket_name, object_name, options)
19
19
  end
20
20
 
21
21
  def get_bucket(bucket)
@@ -1,16 +1,23 @@
1
1
  require 'trollop'
2
2
 
3
+ require 'heirloom/cli/build'
4
+ require 'heirloom/cli/list'
5
+ require 'heirloom/cli/show'
6
+ require 'heirloom/cli/update'
7
+ require 'heirloom/cli/download'
8
+ require 'heirloom/cli/destroy'
9
+
3
10
  module Heirloom
4
11
  module CLI
5
12
  def self.start
6
13
  @opts = Trollop::options do
14
+ version Heirloom::VERSION
7
15
  banner <<-EOS
8
16
 
9
17
  I build and manage artifacts
10
18
 
11
19
  Usage:
12
20
 
13
- heirloom names
14
21
  heirloom list -n NAME
15
22
  heirloom show -n NAME -i ID
16
23
  heirloom build -n NAME -i ID -b BUCKET_PREFIX [-d DIRECTORY] [-p] [-g]
@@ -28,6 +35,8 @@ EOS
28
35
  :default => '.git'
29
36
  opt :git, "Read git commit information from directory."
30
37
  opt :id, "Id of artifact.", :type => :string
38
+ opt :limit, "Limit the results returned by list.", :type => :integer,
39
+ :default => 10
31
40
  opt :name, "Name of artifact.", :type => :string
32
41
  opt :output, "Output download to file.", :type => :string
33
42
  opt :public, "Set this artifact as public readable?"
@@ -37,43 +46,41 @@ EOS
37
46
  end
38
47
 
39
48
  cmd = ARGV.shift
40
- a = Artifact.new :config => nil
41
49
 
42
50
  case cmd
43
- when 'names'
44
- puts a.names
45
51
  when 'list'
46
- unless @opts[:name]
47
- puts "Please specify an artifact name."
48
- puts a.names
49
- exit 1
50
- end
51
- puts a.list :name => @opts[:name]
52
+ cli_list = CLI::List.new :name => @opts[:name]
53
+ cli_list.list @opts[:limit]
52
54
  when 'show'
53
- puts a.show(:name => @opts[:name],
54
- :id => @opts[:id]).to_yaml
55
+ cli_show = CLI::Show.new :name => @opts[:name],
56
+ :id => @opts[:id]
57
+ cli_show.show
55
58
  when 'build'
56
- a.build :name => @opts[:name],
57
- :id => @opts[:id],
58
- :accounts => @opts[:accounts],
59
- :bucket_prefix => @opts[:bucket_prefix],
60
- :directory => @opts[:directory],
61
- :exclude => @opts[:exclude].split(','),
62
- :public => @opts[:public],
63
- :git => @opts[:git]
59
+ cli_build = CLI::Build.new :name => @opts[:name],
60
+ :id => @opts[:id],
61
+ :accounts => @opts[:accounts],
62
+ :bucket_prefix => @opts[:bucket_prefix],
63
+ :directory => @opts[:directory],
64
+ :exclude => @opts[:exclude],
65
+ :public => @opts[:public],
66
+ :git => @opts[:git]
67
+ cli_build.build
64
68
  when 'update'
65
- a.update :name => @opts[:name],
66
- :id => @opts[:id],
67
- :attribute => @opts[:attribute],
68
- :update => @opts[:update]
69
+ cli_update = CLI::Update.new :name => @opts[:name],
70
+ :id => @opts[:id],
71
+ :attribute => @opts[:attribute],
72
+ :update => @opts[:update]
73
+ cli_update.update
69
74
  when 'download'
70
- a.download :name => @opts[:name],
71
- :id => @opts[:id],
72
- :output => @opts[:output],
73
- :region => @opts[:region]
75
+ cli_download = CLI::Download.new :name => @opts[:name],
76
+ :id => @opts[:id],
77
+ :output => @opts[:output],
78
+ :region => @opts[:region]
79
+ cli_download.download
74
80
  when 'destroy', 'delete'
75
- a.destroy :name => @opts[:name],
76
- :id => @opts[:id]
81
+ cli_destroy = CLI::Destroy.new :name => @opts[:name],
82
+ :id => @opts[:id]
83
+ cli_destroy.destroy
77
84
  else
78
85
  puts "Unkown command: '#{cmd}'."
79
86
  end
@@ -0,0 +1,40 @@
1
+ module Heirloom
2
+ module CLI
3
+ class Build
4
+
5
+ def initialize(args)
6
+ @name = args[:name]
7
+ @id = args[:id]
8
+ @accounts = args[:accounts]
9
+ @bucket_prefix = args[:bucket_prefix]
10
+ @directory = args[:directory]
11
+ @exclude = args[:exclude].split(',')
12
+ @public = args[:public]
13
+ @git = args[:git]
14
+ @logger = args[:logger]
15
+ @artifact = Artifact.new :name => @name,
16
+ :id => @id,
17
+ :logger => @logger
18
+ end
19
+
20
+ def build
21
+ @artifact.destroy if @artifact.exists?
22
+
23
+ file = @artifact.build :accounts => @accounts,
24
+ :bucket_prefix => @bucket_prefix,
25
+ :directory => @directory,
26
+ :exclude => @exclude,
27
+ :public => @public,
28
+ :git => @git
29
+
30
+ @artifact.upload :bucket_prefix => @bucket_prefix,
31
+ :file => file
32
+
33
+ @artifact.authorize unless @public
34
+
35
+ @artifact.cleanup
36
+ end
37
+
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,20 @@
1
+ module Heirloom
2
+ module CLI
3
+ class Destroy
4
+
5
+ def initialize(args)
6
+ @name = args[:name]
7
+ @id = args[:id]
8
+ @logger = args[:logger]
9
+ @artifact = Artifact.new :name => @name,
10
+ :id => @id,
11
+ :logger => @logger
12
+ end
13
+
14
+ def destroy
15
+ @artifact.destroy
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,23 @@
1
+ module Heirloom
2
+ module CLI
3
+ class Download
4
+
5
+ def initialize(args)
6
+ @name = args[:name]
7
+ @id = args[:id]
8
+ @output = args[:output]
9
+ @region = args[:region]
10
+ @logger = args[:logger]
11
+ @artifact = Artifact.new :name => @name,
12
+ :id => @id,
13
+ :logger => @logger
14
+ end
15
+
16
+ def download
17
+ @artifact.download :output => @output,
18
+ :region => @region
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ module Heirloom
2
+ module CLI
3
+ class List
4
+
5
+ def initialize(args)
6
+ @artifact = Artifact.new :name => args[:name],
7
+ :logger => args[:logger]
8
+ end
9
+
10
+ def list(limit)
11
+ @logger = Logger
12
+ puts @artifact.list(limit)
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,27 @@
1
+ module Heirloom
2
+ module CLI
3
+ class Show
4
+
5
+ def initialize(args)
6
+ id = args[:id] ? args[:id] : latest_id(args)
7
+ @artifact = Artifact.new :name => args[:name],
8
+ :id => id,
9
+ :logger => args[:logger]
10
+ end
11
+
12
+ def show
13
+ @logger = Logger
14
+ puts @artifact.show.to_yaml
15
+ end
16
+
17
+ private
18
+
19
+ def latest_id(args)
20
+ @artifact = Artifact.new :name => args[:name],
21
+ :logger => args[:logger]
22
+ @artifact.list(1).first
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,23 @@
1
+ module Heirloom
2
+ module CLI
3
+ class Update
4
+
5
+ def initialize(args)
6
+ @name = args[:name]
7
+ @id = args[:id]
8
+ @logger = args[:logger]
9
+ @attribute = args[:attribute]
10
+ @update = args[:update]
11
+ @artifact = Artifact.new :name => @name,
12
+ :id => @id,
13
+ :logger => @logger
14
+ end
15
+
16
+ def update
17
+ @artifact.update :attribute => @attribute,
18
+ :update => @update
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -2,7 +2,8 @@ module Heirloom
2
2
  class Config
3
3
 
4
4
  attr_accessor :access_key, :secret_key, :regions,
5
- :primary_region, :bucket_prefix, :authorized_aws_accounts
5
+ :primary_region, :bucket_prefix, :authorized_aws_accounts,
6
+ :logger
6
7
 
7
8
  def initialize(args = {})
8
9
  @config = args[:config]
@@ -13,12 +14,16 @@ module Heirloom
13
14
  config_file = "#{ENV['HOME']}/.heirloom.yml"
14
15
  c = @config ? @config : YAML::load( File.open( config_file ) )
15
16
 
16
- self.access_key = c['aws']['access_key']
17
- self.secret_key = c['aws']['secret_key']
18
- self.regions = c['aws']['regions']
17
+ self.logger = c['logger'] ||= HeirloomLogger.new
18
+
19
+ aws = c['aws']
20
+
21
+ self.access_key = aws['access_key']
22
+ self.secret_key = aws['secret_key']
23
+ self.regions = aws['regions']
24
+ self.bucket_prefix = aws['bucket_prefix']
25
+ self.authorized_aws_accounts = aws['authorized_aws_accounts']
19
26
  self.primary_region = regions ? regions.first : 'us-west-1'
20
- self.bucket_prefix = c['aws']['bucket_prefix']
21
- self.authorized_aws_accounts = c['aws']['authorized_aws_accounts']
22
27
  end
23
28
 
24
29
  end