thor-tropo 0.0.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/thor-tropo.rb CHANGED
@@ -1,19 +1,23 @@
1
+ module ThorTropo
2
+
3
+
4
+ $:.unshift File.expand_path('../thor-tropo', __FILE__)
5
+
1
6
  require 'thor'
2
7
  require 'thor/actions'
3
8
  require 'thor/scmversion'
4
9
  require 'tmpdir'
5
10
  require 'archive/tar/minitar'
6
11
  require 'zlib'
7
- #require 'thor/foodcritic'
8
12
  require 'berkshelf/thor'
9
13
  require 'berkshelf/chef'
14
+ require 'uploader'
15
+ require 'configuration'
10
16
 
11
-
12
- module ThorTropo
13
17
  class Tasks < Thor
14
18
  include Thor::Actions
15
19
 
16
-
20
+ @packaged_cookbook = nil
17
21
  class_option :called_path,
18
22
  :type => :string,
19
23
  :default => Dir.pwd
@@ -23,23 +27,51 @@ module ThorTropo
23
27
  :aliases => "-v",
24
28
  :default => false
25
29
 
26
-
27
30
  namespace "tropo"
28
31
 
29
- desc "bump", "Bump version"
30
- def bump
31
- puts "This should bump the version in the metatdata.rb file... eventually"
32
- end
33
-
32
+ method_option :force,
33
+ :type => :boolean,
34
+ :aliases => "-f",
35
+ :default => false,
36
+ :desc => "Force override of any files on s3 without confirmation",
37
+ :banner => "Force overrides of existing files"
38
+
39
+ method_option :noop,
40
+ :type => :boolean,
41
+ :aliases => "-n",
42
+ :default => false,
43
+ :desc => "NO-OP mode, dont actually upload anything"
44
+
45
+ method_option :ignore,
46
+ :type => :boolean,
47
+ :aliases => "-i",
48
+ :default => false,
49
+ :desc => "Ignore any dirty files in directory and package anyways",
50
+ :banner => "Ignore dirty repository"
51
+
52
+ method_option :clean_mode,
53
+ :type => :boolean,
54
+ :aliases => "-c",
55
+ :default => false,
56
+ :desc => "Delete lockfile before running `Berks install`",
57
+ :banner => "Delete lockfile before running `Berks install`"
58
+
59
+ desc "package", "Package cookbooks and upload to S3"
34
60
 
35
- desc "package", "Package cookbook"
36
61
  def package
37
- unless clean?
38
- say "There are files that need to be committed first.", :red
39
- exit 1
62
+
63
+ $config = ThorTropo::Configuration.new(options[:called_path])
64
+
65
+ unless options[:ignore]
66
+ unless clean?
67
+ say "There are files that need to be committed first.", :red
68
+ exit 1
69
+ end
40
70
  end
41
71
 
42
72
  bundle_cookbook
73
+
74
+ upload_cookbook @packaged_cookbook, $config.project_name, {:force => options[:force],:noop => options[:noop]}
43
75
  # tag_version {
44
76
  # publish_cookbook(options)
45
77
  # }
@@ -59,25 +91,51 @@ module ThorTropo
59
91
 
60
92
  no_tasks do
61
93
 
94
+ def upload_cookbook(local_file,path,opts={})
95
+ uploader = ThorTropo::Uploader.new({
96
+ :access_key => $config.aws_key,
97
+ :secret_key => $config.aws_secret,
98
+ :bucket => $config.bucket_name
99
+ })
100
+
101
+ uploader.upload :local_file => local_file, :path => path, :force => options[:force], :noop=>options[:noop]
102
+ end
103
+
62
104
  def clean?
63
105
  sh_with_excode("git diff --exit-codeBerkshelf")[1] == 0
64
106
  end
65
107
 
108
+ def clean_lockfile
109
+ berksfile = "#{options[:called_path]}/Berksfile.lock"
110
+
111
+ if File.exists? berksfile
112
+ say "[ TROPO ] - Removing Berksfile.lock before running Berkshelf", :green
113
+ remove_file berksfile
114
+ else
115
+ say "[ TROPO ] - Unable to find berksfile to delete - [ #{berksfile} ]", :yellow
116
+ end
117
+ end
118
+
66
119
  def bundle_cookbook
67
- tmp = Dir.mktmpdir
120
+ clean_lockfile if options[:clean_mode]
121
+
122
+ say "[ TROPO ] - Packaging cookbooks from Berksfile", :blue
123
+ @tmp_dir = Dir.mktmpdir
68
124
  opts = {
69
125
  berksfile: File.join(options[:called_path],"/Berksfile"),
70
- path: "#{tmp}/cookbooks"
126
+ path: "#{@tmp_dir}/cookbooks"
71
127
  }
72
128
 
73
129
  invoke("berkshelf:install", [], opts)
74
- output = File.expand_path(File.join(Dir.home, "#{options[:called_path].split("/")[-1]}-#{current_version}.tar.gz"))
130
+ output = File.expand_path(File.join(@tmp_dir, "#{options[:called_path].split("/")[-1]}-#{current_version}.tar.gz"))
75
131
 
76
132
 
77
- Dir.chdir(tmp) do |dir|
133
+ Dir.chdir(@tmp_dir) do |dir|
78
134
  tgz = Zlib::GzipWriter.new(File.open(output, 'wb'))
79
135
  Archive::Tar::Minitar.pack('./cookbooks', tgz)
80
136
  end
137
+ @packaged_cookbook = output
138
+
81
139
  end
82
140
 
83
141
  def current_version
@@ -0,0 +1,49 @@
1
+ module ThorTropo
2
+ class Configuration < Thor
3
+
4
+ no_commands {
5
+
6
+ require 'pathname'
7
+ require 'yaml'
8
+
9
+ attr_reader :bucket_name, :aws_secret, :aws_key, :project_name
10
+
11
+ def initialize(path)
12
+ file_name=".deployer"
13
+
14
+ Pathname.new(path).ascend do |dir|
15
+ config_file = dir + file_name
16
+ if dir.children.include?(config_file)
17
+ merge_config(YAML::load_file(config_file))
18
+ end
19
+ end
20
+
21
+ end
22
+
23
+ private
24
+
25
+ def merge_config(config)
26
+
27
+ if config['bucket_name']
28
+ @bucket_name = config['bucket_name'] unless @bucket_name
29
+ end
30
+
31
+ if config['aws_secret']
32
+ @aws_secret = config['aws_secret'] unless @aws_secret
33
+ end
34
+
35
+ if config['aws_key']
36
+ @aws_key = config['aws_key'] unless @aws_key
37
+ end
38
+
39
+ if config['project_name']
40
+ @project_name = config['project_name'] unless @project_name
41
+ end
42
+
43
+ end
44
+ }
45
+
46
+ end
47
+ end
48
+
49
+
@@ -0,0 +1,115 @@
1
+ module ThorTropo
2
+ class Uploader < Thor
3
+
4
+ include Thor::Actions
5
+
6
+ require 'digest/md5'
7
+ require 'fog'
8
+ require 'mime/types'
9
+
10
+ require 'pry'
11
+
12
+ no_commands {
13
+
14
+ def initialize(opts={})
15
+ @debug_mode = opts[:debug_mode]
16
+ @connection = setup_connection(opts[:access_key],opts[:secret_key])
17
+ @bucket_name = get_bucket( opts[:bucket] || 'artifacts.voxeolabs.net' )
18
+ end
19
+
20
+ def upload(opts={})
21
+
22
+ local_file = opts[:local_file]
23
+ remote_file = opts[:path] + "/" + opts[:local_file].split("/")[-1]
24
+ force = opts[:force].nil? ? false : opts[:force]
25
+ noop = opts[:noop].nil? ? false : opts[:noop]
26
+
27
+ if file_exists? remote_file
28
+
29
+ if force
30
+
31
+ send_file :remote_file => remote_file, :local_file => local_file, :noop => noop
32
+
33
+ else
34
+
35
+ response = ask "[ TROPO ] - file already exists on S3, do you want me to overwrite it ? (yes|no) ? ", :yellow
36
+
37
+ if response.downcase.match /yes|ok|y/
38
+ begin
39
+
40
+ send_file :remote_file => remote_file, :local_file => local_file, :noop => noop
41
+
42
+ rescue Exception => e
43
+ say "[ TROPO ] Error: #{e}", :red
44
+ say "[ TROPO ] Backtrace:\n\t#{e.backtrace.join("\n\t")}" if @debug_mode
45
+ end
46
+
47
+ else
48
+ say "[ TROPO ] - Quiting..", :red
49
+ end
50
+ end
51
+ else
52
+
53
+ send_file :remote_file => remote_file, :local_file => local_file, :noop => noop
54
+
55
+ end
56
+ end
57
+
58
+ private
59
+
60
+ def setup_connection(u,p)
61
+ Fog::Storage.new({
62
+ :provider => 'AWS',
63
+ :aws_access_key_id => u,
64
+ :aws_secret_access_key => p
65
+ })
66
+ end
67
+
68
+ def get_bucket(bucket_name)
69
+ directory = @connection.directories.create(
70
+ :key => bucket_name,
71
+ :public => true
72
+ )
73
+ end
74
+
75
+ def file_exists?(key)
76
+ !@bucket_name.files.head(key).nil?
77
+ end
78
+
79
+ def send_file(opts={})
80
+ local_file = opts[:local_file]
81
+ remote_file = opts[:remote_file]
82
+
83
+ say "[ TROPO ] - Uploading file.", :blue
84
+ unless opts[:noop]
85
+ file = @bucket_name.files.create(
86
+ :key => remote_file,
87
+ :body => File.open(local_file),
88
+ :public => true,
89
+ :content_type => get_mime_type(local_file),
90
+ :metadata => {
91
+ "x-amz-meta-sha256-hash" =>get_sha256_sum(local_file)
92
+ }
93
+ )
94
+ public_url = file.public_url
95
+
96
+ else
97
+ public_url = " *** NOOP ** /#{remote_file}"
98
+
99
+ end
100
+ say "[ TROPO ] - Public URL: #{public_url}", :blue
101
+
102
+ end
103
+
104
+ def get_sha256_sum(file)
105
+ Digest::SHA256.file(file).hexdigest
106
+ end
107
+
108
+ def get_mime_type(file)
109
+ MIME::Types.type_for(file)[0].to_s
110
+ end
111
+
112
+ }
113
+ end
114
+
115
+ end
data/thor-tropo.gemspec CHANGED
@@ -15,12 +15,14 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib","bin"]
16
16
  gem.version = ThorTropo::VERSION
17
17
 
18
- gem.add_dependency 'json', ">= 1.7.0"
18
+ #gem.add_dependency 'json', ">= 1.7.0"
19
19
  gem.add_dependency 'thor'
20
20
  gem.add_dependency 'chef', "~> 11.0"
21
21
  gem.add_dependency 'berkshelf'
22
22
  gem.add_dependency 'thor-scmversion'
23
23
  gem.add_dependency 'minitar', '~> 0.5.4'
24
+ gem.add_dependency 'aws-sdk'
25
+ gem.add_dependency 'fog'
24
26
 
25
27
  gem.add_development_dependency 'foodcritic'
26
28
  gem.add_development_dependency 'webmock'
@@ -31,4 +33,5 @@ Gem::Specification.new do |gem|
31
33
  gem.add_development_dependency 'rspec'
32
34
  gem.add_development_dependency "bundler", "~> 1.3"
33
35
  gem.add_development_dependency "rake"
36
+ gem.add_development_dependency "pry"
34
37
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thor-tropo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,24 +9,8 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-13 00:00:00.000000000 Z
12
+ date: 2013-07-15 00:00:00.000000000 Z
13
13
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: json
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: 1.7.0
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: 1.7.0
30
14
  - !ruby/object:Gem::Dependency
31
15
  name: thor
32
16
  requirement: !ruby/object:Gem::Requirement
@@ -107,6 +91,38 @@ dependencies:
107
91
  - - ~>
108
92
  - !ruby/object:Gem::Version
109
93
  version: 0.5.4
94
+ - !ruby/object:Gem::Dependency
95
+ name: aws-sdk
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: fog
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
110
126
  - !ruby/object:Gem::Dependency
111
127
  name: foodcritic
112
128
  requirement: !ruby/object:Gem::Requirement
@@ -251,6 +267,22 @@ dependencies:
251
267
  - - ! '>='
252
268
  - !ruby/object:Gem::Version
253
269
  version: '0'
270
+ - !ruby/object:Gem::Dependency
271
+ name: pry
272
+ requirement: !ruby/object:Gem::Requirement
273
+ none: false
274
+ requirements:
275
+ - - ! '>='
276
+ - !ruby/object:Gem::Version
277
+ version: '0'
278
+ type: :development
279
+ prerelease: false
280
+ version_requirements: !ruby/object:Gem::Requirement
281
+ none: false
282
+ requirements:
283
+ - - ! '>='
284
+ - !ruby/object:Gem::Version
285
+ version: '0'
254
286
  description: Thor tasks to package a project
255
287
  email:
256
288
  - johntdyer@gmail.com
@@ -268,6 +300,8 @@ files:
268
300
  - VERSION
269
301
  - bin/scmversion
270
302
  - lib/thor-tropo.rb
303
+ - lib/thor-tropo/configuration.rb
304
+ - lib/thor-tropo/uploader.rb
271
305
  - lib/thor-tropo/version.rb
272
306
  - lib/thor/tropo.rb
273
307
  - thor-tropo.gemspec