knife-s3 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,7 +12,7 @@
12
12
  # limitations under the License.
13
13
  #
14
14
 
15
- # s3_base.rb based on cfn_base.rb by Neil Turner, which can be found at https://github.com/neillturner/knife-cfn
15
+ # s3_base.rb based on cfn_base.rb by Neill Turner, which can be found at https://github.com/neillturner/knife-cfn
16
16
 
17
17
  require 'chef/knife'
18
18
 
@@ -43,8 +43,9 @@ class Chef
43
43
  option :region,
44
44
  :long => "--region REGION",
45
45
  :description => "Your AWS region",
46
- :default => "us-west-1",
46
+ :default => "us-east-1",
47
47
  :proc => Proc.new { |key| Chef::Config[:knife][:region] = key }
48
+
48
49
  end
49
50
  end
50
51
  def connection
@@ -0,0 +1,75 @@
1
+ require 'chef/knife'
2
+ require 'chef/knife/s3_base'
3
+
4
+ class Chef
5
+ class Knife
6
+ class S3Download < Chef::Knife::S3Base
7
+
8
+ deps do
9
+ require 'fog'
10
+ require 'readline'
11
+ require 'chef/json_compat'
12
+ require 'chef/knife/bootstrap'
13
+ Chef::Knife::Bootstrap.load_deps
14
+ end
15
+
16
+ banner "knife s3 download -b <bucket_name> -r REMOTE_FILE [-f FILE]"
17
+
18
+ option :bucket,
19
+ :short => "-b BUCKET",
20
+ :long => "--bucket BUCKET",
21
+ :description => "Specify the S3 bucket"
22
+
23
+ option :pathname,
24
+ :short => "-r REMOTE_FILE",
25
+ :long => "--remote REMOTE_FILE",
26
+ :description => "Specify the remote path (and filename) to download."
27
+
28
+ option :localfile,
29
+ :short => "-f FILE",
30
+ :long => "--file FILE",
31
+ :description => "The local file to write to. Defaults to the remote filename."
32
+
33
+ option :overwrite,
34
+ :short => "-o",
35
+ :long => "--overwrite",
36
+ :description => "Forces the local file to be overwritten if it exists."
37
+
38
+ def run
39
+ $stdout.sync = true
40
+
41
+ validate!
42
+
43
+ if config[:bucket].nil?
44
+ puts "No bucket specified"
45
+ exit 1
46
+ end
47
+
48
+ if config[:pathname].nil?
49
+ puts "No path specified"
50
+ exit 1
51
+ end
52
+
53
+ if config[:localfile].nil?
54
+ config[:localfile] = config[:pathname] #TODO: should strip "directory" elements from the path?
55
+ end
56
+
57
+ if File.exists?(config[:localfile]) and config[:overwrite].nil?
58
+ print config[:localfile] + " exists. Overwrite (y/n)? "
59
+ ans=$stdin.gets
60
+ if !ans.match(/^y/i)
61
+ puts "Not overwriting, exiting"
62
+ exit 0
63
+ end
64
+ end
65
+
66
+ begin
67
+ remote_file = connection.directories.get(config[:bucket]).files.get(config[:pathname])
68
+ local_file = File.open(config[:localfile],'w')
69
+ local_file.write(remote_file.body)
70
+ end
71
+
72
+ end
73
+ end
74
+ end
75
+ end
@@ -13,12 +13,12 @@ class Chef
13
13
  Chef::Knife::Bootstrap.load_deps
14
14
  end
15
15
 
16
- banner "knife s3 list <bucket name>"
16
+ banner "knife s3 list -b <bucket name> [-p <prefix>]"
17
17
 
18
- option :bucket_name,
19
- :short => "-b BUCKET",
20
- :long => "--bucket BUCKET",
21
- :description => "Sets bucket name"
18
+ option :bucket,
19
+ :short => "-b BUCKET",
20
+ :long => "--bucket BUCKET",
21
+ :description => "Specify the S3 bucket"
22
22
 
23
23
  option :prefix,
24
24
  :short => "-p PREFIX",
@@ -30,24 +30,24 @@ class Chef
30
30
 
31
31
  validate!
32
32
 
33
- if config[:bucket_name]
34
- puts "Listing bucket " + config[:bucket_name]
33
+ if config[:bucket]
34
+ puts "Listing bucket " + config[:bucket]
35
35
  else
36
- puts "Please supply a bucket name"
36
+ puts "No bucket specified"
37
37
  exit 1
38
38
  end
39
39
 
40
40
  if config[:prefix]
41
- puts "Filtering by prefix " + config[:prefix]
41
+ puts "Listing with prefix " + config[:prefix]
42
42
  end
43
43
 
44
44
  begin
45
45
  if config[:prefix].nil?
46
- connection.directories.get(config[:bucket_name]).files.map do |file|
46
+ connection.directories.get(config[:bucket]).files.map do |file|
47
47
  puts file.key
48
48
  end
49
49
  else
50
- connection.directories.get(config[:bucket_name], prefix: config[:prefix]).files.map do |file|
50
+ connection.directories.get(config[:bucket], prefix: config[:prefix]).files.map do |file|
51
51
  puts file.key
52
52
  end
53
53
  end
@@ -0,0 +1,80 @@
1
+ require 'chef/knife'
2
+ require 'chef/knife/s3_base'
3
+
4
+ class Chef
5
+ class Knife
6
+ class S3Upload < Chef::Knife::S3Base
7
+
8
+ deps do
9
+ require 'fog'
10
+ require 'readline'
11
+ require 'chef/json_compat'
12
+ require 'chef/knife/bootstrap'
13
+ Chef::Knife::Bootstrap.load_deps
14
+ end
15
+
16
+ banner "knife s3 upload -b <bucket_name> -f <FILE> [-r REMOTE_FILE]"
17
+
18
+ option :bucket,
19
+ :short => "-b BUCKET",
20
+ :long => "--bucket BUCKET",
21
+ :description => "Specify the S3 bucket"
22
+
23
+ option :pathname,
24
+ :short => "-r REMOTE_FILE",
25
+ :long => "--remote REMOTE_FILE",
26
+ :description => "Remote path (and filename) to upload to."
27
+
28
+ option :localfile,
29
+ :short => "-f FILE",
30
+ :long => "--file FILE",
31
+ :description => "The local file to upload."
32
+
33
+ option :is_public,
34
+ :short => "-p",
35
+ :long => "--public",
36
+ :description => "If set, then the file will be set with public read access."
37
+
38
+ def run
39
+ $stdout.sync = true
40
+
41
+ validate!
42
+
43
+ public = false
44
+
45
+ if config[:bucket].nil?
46
+ puts "No bucket specified"
47
+ exit 1
48
+ end
49
+
50
+ if config[:localfile].nil?
51
+ puts "No file specified"
52
+ exit 1
53
+ end
54
+
55
+ if !File.exists?(config[:localfile])
56
+ puts "file '" + config[:localfile] + "' does not exist"
57
+ exit 1
58
+ end
59
+
60
+ if config[:pathname].nil?
61
+ puts "No remote path specified. Uploading as " + config[:localfile]
62
+ config[:pathname] = config[:localfile]
63
+ end
64
+
65
+ if !config[:is_public].nil?
66
+ public = true
67
+ end
68
+
69
+ begin
70
+ remote_file = connection.directories.get(config[:bucket]).files.create(
71
+ :key => config[:pathname],
72
+ :body => File.open(config[:localfile]),
73
+ :public => public
74
+ )
75
+ end
76
+
77
+ end
78
+ end
79
+ end
80
+ end
@@ -1,3 +1,3 @@
1
1
  module KnifeS3
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-s3
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -50,7 +50,9 @@ extensions: []
50
50
  extra_rdoc_files: []
51
51
  files:
52
52
  - lib/chef/knife/s3_list.rb
53
+ - lib/chef/knife/s3_download.rb
53
54
  - lib/chef/knife/s3_base.rb
55
+ - lib/chef/knife/s3_upload.rb
54
56
  - lib/knife-s3/version.rb
55
57
  homepage: https://github.com/brettcave/knife-s3
56
58
  licenses: []