buckler 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: db7db01a0faa13f5047d6248d3fe5ea5c64da8e2
4
+ data.tar.gz: 97a290a47b2fc14be5ae66fef9f1da37fcb6ae47
5
+ SHA512:
6
+ metadata.gz: b7e85bf75a278d549dfbe63aa2f948182ef1dde3cc2e092a8874bf00b02aa97d693d476dbf8dc0b553e2739e6087fe792ad90786a8c507281c184912f93559c7
7
+ data.tar.gz: 39ea505766afccb930f0b8aad05293cf8a82da2df088172fc6597910d589fb339e80b6c160f82eb3a3b18b2793aa3508d97d818d69e52441f7b2b30f4de8d8e7
data/.gitignore ADDED
@@ -0,0 +1,66 @@
1
+ # Special ignores for this project
2
+
3
+ Gemfile.lock
4
+
5
+ # Ignore local dev only files
6
+
7
+ *.dump
8
+ *.gem
9
+ *.log
10
+ *.rbc
11
+ *.cache
12
+ *.pid
13
+ *.sqlite3
14
+ *.sqlite3-journal
15
+ .bundle
16
+ .config
17
+ .env
18
+ .env.test
19
+ .yardoc
20
+ config/database.yml
21
+ coverage
22
+ export
23
+ InstalledFiles
24
+ lib/bundler/man
25
+ node_modules
26
+ pkg
27
+ public/system/**/*
28
+ rdoc
29
+ doc
30
+ rerun.txt
31
+ spec/reports
32
+ spec/tmp/**/*
33
+ tags
34
+ test/tmp
35
+ test/version_tmp
36
+ vendor/**/*
37
+ vim/.netrwhist
38
+ zeus.json
39
+ _yardoc
40
+
41
+ # Ignore compilation junk
42
+
43
+ **.orig
44
+ *.rbc
45
+ *.sassc
46
+ *.sw[nop]
47
+ .rspec
48
+ .sass-cache
49
+ capybara-*.html
50
+ pickle-email-*.html
51
+ rerun.txt
52
+
53
+ # Ignore filesystem trash
54
+
55
+ *.DS_Store
56
+ *Thumbs.db
57
+ .DS_Store?
58
+ ehthumbs.db
59
+ Thumbs.db
60
+
61
+ # Specifically DO NOT ignore these repo settings
62
+
63
+ !.gitignore
64
+ !.slugignore
65
+ !.ruby-version
66
+ !.yardops
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.1
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ - 2.2.4
5
+ - 2.2.5
6
+ - 2.3.0
7
+ - 2.3.1
8
+ script: "rake test"
data/.yardopts ADDED
@@ -0,0 +1,4 @@
1
+ --readme README.md
2
+ --charset utf-8
3
+ --markup markdown
4
+ --exclude /test
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,29 @@
1
+ This project welcomes contributions. While contributing, you must adhere to the project [code of conduct](#the-code-of-conduct).
2
+
3
+ Here’s how you can make Buckler better:
4
+
5
+ - [Report an issue](https://github.com/csuhta/buckler/issues/new) or ask questions
6
+ - [Suggest new features](https://github.com/threespot/csuhta/buckler/new)
7
+ - Check, vet, diagnose, or add details to existing issues.
8
+ - Improve documentation. There are probably still typos or unclear instructions. These are easy wins.
9
+ - Submit pull requests or bugfixes.
10
+
11
+ Thanks for your help!
12
+
13
+ ### The Code of Conduct
14
+
15
+ This project strongly values contributors from anywhere, regardless of gender, gender expression, sexual orientation, ability, physical appearance, body type, race, age, or religion. As a result, the maintainers have agreed to and enforces this code of conduct in order to provide a harassment-free experience for everyone who participates in the development of Buckler.
16
+
17
+ Harassment includes offensive comments related to gender, gender expression, sexual orientation, ability, physical appearance, body type, race, age, or religion. It also includes the posting of sexual images, deliberate intimidation, stalking, and unwelcome sexual attention.
18
+
19
+ Further, harassment includes ad-hominem attacks, grandstanding, derogatory comments, trolling, public or private community disruption, insults, or other unprofessional conduct.
20
+
21
+ Harassment is not tolerated in the issue tracker, code, events, chat channels, over emails, or any other way you can communicate with a contributor. Anyone who is asked to cease harassment is expected to comply immediately. Maintainers are not exempt from this policy.
22
+
23
+ If anyone engages in harassing behavior, maintainers may take appropriate action, up to and including warning the offender, deletion of comments, removal from the project’s codebase and communication systems, and escalation to GitHub support.
24
+
25
+ If you are being harassed, notice that someone else is being harassed, or have any other concerns, please open an issue or contact a maintainer.
26
+
27
+ ### License
28
+
29
+ This document is published from the United States. To the extent possible under law, Corey Csuhta and the Buckler contributors have waived all copyright and related or neighboring rights to this document worldwide. The text in this file may be re-used for any purpose and without credit. ✨
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,9 @@
1
+ ### The MIT License
2
+
3
+ Copyright (c) Corey Csuhta
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,96 @@
1
+ ### Buckler
2
+
3
+ [![Build Status](https://travis-ci.org/csuhta/buckler.svg?branch=master)](https://travis-ci.org/csuhta/buckler)
4
+
5
+ Buckler is a Ruby command line tool for performing common actions on Amazon S3 buckets. It’s more do-what-you-want and less overwhelmingly powerful than the AWS CLI. It’s also designed to work with Heroku applications.
6
+
7
+ ### Installing Buckler
8
+
9
+ Buckler requires at least Ruby 2.2.3. Get Buckler from Rubygems. The terminal command is called `bucket`.
10
+
11
+ ```shell
12
+ gem install buckler
13
+ bucket help
14
+ ```
15
+
16
+ ### Command Reference
17
+
18
+ ```shell
19
+ # Get a list of your buckets
20
+ bucket list
21
+
22
+ # Run any command with additional debuging info
23
+ bucket list --verbose
24
+
25
+ # Get a list of all S3 regions
26
+ bucket regions
27
+
28
+ # Create a new bucket on your account
29
+ bucket create new-bucket-name
30
+
31
+ # Create a new bucket on your account in a different region
32
+ bucket create new-bucket-name --region eu-west-1
33
+
34
+ # Remove all objects from one of your buckets
35
+ bucket empty bucket-name
36
+
37
+ # Destroy one of your buckets
38
+ bucket destroy bucket-name
39
+
40
+ # Copy the contents of one bucket into another
41
+ # This command also conveniently copies
42
+ # ACLs, headers, metadata, and a lot more.
43
+ bucket sync source-bucket-name target-bucket-name
44
+
45
+ # Get detailed help with Buckler commands
46
+ bucket help
47
+ bucket help sync
48
+ bucket help create
49
+ bucket help destroy
50
+ bucket help empty
51
+ bucket help list
52
+ bucket help regions
53
+ ```
54
+
55
+ ### Providing Credentials
56
+
57
+ You will need a AWS Access Key ID and AWS Secret Access Key pair with permission to mange your S3 buckets. **Do not use your root keys.** [Generate a new set of keys with S3 permissions only](http://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#lock-away-credentials).
58
+
59
+ When you run the `bucket` command, Buckler tries to automatically discover AWS credentials around your working directory.
60
+
61
+ **Dotenv**: If the current folder has a file named `.env`, Buckler will look for variables called `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` in the file. [See Heroku’s documentation on this environment file format](https://devcenter.heroku.com/articles/heroku-local#set-up-your-local-environment-variables).
62
+
63
+ **Heroku**: If the current folder has a Git repository with a Heroku remote, Buckler will ask your Heroku application for variables named `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` using `heroku config:get`
64
+
65
+ **Command Line Options**: You can set the pair directly by providing the command line options `--id` and `--secret`
66
+
67
+ ```shell
68
+ bucket list --id YOUR_AWS_ID --secret YOUR_AWS_SECRET
69
+ ```
70
+
71
+ **Environment Variables**: You can set the pair directly as environment variables.
72
+
73
+ ```shell
74
+ AWS_ACCESS_KEY_ID=your-id AWS_SECRET_ACCESS_KEY=your-secret bucket list
75
+ ```
76
+
77
+ ### Developing Buckler
78
+
79
+ **The Buckler test suite will create and destroy test buckets on your AWS account which will cost you more than 0 money.** Existing buckets won’t be affected.
80
+
81
+ ```shell
82
+ # Fork or clone Buckler
83
+ git clone -o github git@github.com:csuhta/buckler.git
84
+ cd buckler
85
+
86
+ # Set your credentials in a .env
87
+ echo "AWS_ACCESS_KEY_ID=your-id" >> .env
88
+ echo "AWS_SECRET_ACCESS_KEY_ID=your-secret" >> .env
89
+
90
+ # Work on stuff, then run the test suite
91
+ rake test
92
+ ```
93
+
94
+ ### License
95
+
96
+ Buckler is free software, and may be redistributed under the terms of the [MIT license](https://github.com/csuhta/bucklet/blob/master/LICENSE.md). Hope you like it! ❤️
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ BUCKLER_ROOT = File.dirname(__FILE__)
2
+ BUCKLER_EXECUTABLE = "#{BUCKLER_ROOT}/bin/bucket"
3
+
4
+ desc "Open an irb session preloaded with this library"
5
+ task :console do
6
+ exec "irb -rubygems -I lib -r buckler.rb"
7
+ end
8
+
9
+ desc "Run Buckler’s test suite"
10
+ task :test do
11
+ require "./test/run"
12
+ end
13
+
14
+ # Creates the build/install/release tasks
15
+ require "bundler/setup"
16
+ Bundler::GemHelper.install_tasks
data/bin/bucket ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "buckler"
4
+ include Buckler::Logging
5
+
6
+ begin
7
+ Buckler::Commands::Root.run(ARGV)
8
+ exit true
9
+ rescue Aws::S3::Errors::InvalidAccessKeyId
10
+ alert "Invalid AWS Access Key ID: #{Buckler.aws_access_key_id}"
11
+ exit false
12
+ rescue Aws::S3::Errors::SignatureDoesNotMatch
13
+ alert "Invalid AWS Secret Access Key provided for Access Key ID #{Buckler.aws_access_key_id}"
14
+ exit false
15
+ rescue Interrupt
16
+ alert "\nCommand aborted."
17
+ exit false
18
+ end
data/buckler.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ $LOAD_PATH.push File.expand_path("../lib", __FILE__)
2
+ require "buckler/version"
3
+
4
+ Gem::Specification.new do |s|
5
+
6
+ s.name = "buckler"
7
+ s.version = Buckler::VERSION::STRING
8
+ s.platform = Gem::Platform::RUBY
9
+ s.licenses = ["MIT"]
10
+ s.authors = ["Corey Csuhta"]
11
+ s.homepage = "https://github.com/csuhta/buckler"
12
+ s.summary = "Perform common actions on S3 buckets from the command line"
13
+ s.description = "Buckler is a Ruby command line tool for performing common actions on Amazon S3 buckets. It’s more do-what-you-want and less overwhelmingly powerful than the AWS CLI. It’s also designed to work with Heroku applications."
14
+
15
+ s.executables = ["bucket"]
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files --directory test`.split("\n")
18
+ s.require_paths = ["lib"]
19
+
20
+ s.required_ruby_version = ">= 2.2.3"
21
+
22
+ s.add_dependency "activesupport", "~> 5.0"
23
+ s.add_dependency "aws-sdk", "~> 2.0"
24
+ s.add_dependency "dotenv", "~> 2.1"
25
+ s.add_dependency "cri", "~> 2.7"
26
+
27
+ s.add_development_dependency "rake", "~> 11.2"
28
+ s.add_development_dependency "minitest", "~> 5.7"
29
+ s.add_development_dependency "yard", "~> 0.6"
30
+
31
+ end
data/lib/buckler.rb ADDED
@@ -0,0 +1,29 @@
1
+ require "mkmf"
2
+ require "cri"
3
+ require "dotenv"
4
+ require "etc"
5
+ require "active_support/core_ext/object"
6
+ require "active_support/core_ext/string"
7
+ require "active_support/core_ext/array"
8
+ require "active_support/core_ext/enumerable"
9
+ require "active_support/inflector"
10
+
11
+ require "aws-sdk"
12
+
13
+ require "buckler/version"
14
+ require "buckler/strings"
15
+ require "buckler/logging"
16
+ require "buckler/thread_dispatch"
17
+ require "buckler/monkey_patches"
18
+
19
+ require "buckler/regions"
20
+ require "buckler/aws"
21
+ require "buckler/heroku"
22
+ require "buckler/actions"
23
+ require "buckler/actions/create_bucket"
24
+ require "buckler/actions/destroy_bucket"
25
+ require "buckler/actions/empty_bucket"
26
+ require "buckler/actions/list_buckets"
27
+ require "buckler/actions/list_regions"
28
+ require "buckler/actions/sync_buckets"
29
+ require "buckler/commands"
@@ -0,0 +1,90 @@
1
+ module Buckler
2
+
3
+ # Generate an Aws::S3::Bucket for the given `name`
4
+ # Also checks that the bucket is real and we have access to it.
5
+ # The only way to truly test bucket access is to try to read an item from it.
6
+ # Exit with a message if there is no access.
7
+ # Prerequisite: `Buckler.discover_aws_credentials!`
8
+
9
+ def self.get_bucket!(name)
10
+
11
+ unless name.present?
12
+ alert "No bucket name provided"
13
+ exit false
14
+ end
15
+
16
+ @bucket = Aws::S3::Bucket.new(name, client:@s3)
17
+
18
+ unless @bucket.exists?
19
+ alert "No such bucket “#{name}”"
20
+ exit false
21
+ end
22
+
23
+ @bucket.objects(max_keys:1).first # Tests bucket access
24
+ return @bucket
25
+
26
+ rescue Aws::S3::Errors::NoSuchBucket
27
+
28
+ alert "No such bucket “#{name}”"
29
+ exit false
30
+
31
+ rescue Aws::S3::Errors::AccessDenied
32
+
33
+ alert "Access denied for bucket #{name}"
34
+ exit false
35
+
36
+ end
37
+
38
+ # Prints a warning message about irreversable changes to the screen.
39
+ # The user is required to confirm by typing the given `name_required`
40
+ # `additional_lines` are printed before the warning.
41
+ # If `confirmation` matches `name_required`, this method is a no-op.
42
+ # The program ends if the confirmation is not provided.
43
+
44
+ def self.require_confirmation!(name_required:, confirmation:nil, additional_lines:[])
45
+
46
+ return true if confirmation == name_required
47
+
48
+ alert "WARNING: Destructive Action"
49
+ additional_lines.each do |line|
50
+ log line
51
+ end
52
+ log "Depending on your S3 settings, this command may permanently"
53
+ log "delete objects from the bucket #{name_required.bucketize}."
54
+ log "To proceed, type “#{name_required}” or re-run this command with --confirm #{name_required}"
55
+ print "> ".dangerize
56
+
57
+ confirmation = STDIN.gets.chomp
58
+
59
+ if confirmation == name_required
60
+ return true
61
+ else
62
+ alert "Invalid confirmation “#{name_required}”, aborting"
63
+ exit false
64
+ end
65
+
66
+ end
67
+
68
+ # Prints a table neatly to the screen.
69
+ # The given `table_array` must be an Array of Arrays of Strings.
70
+ # Each inner array is a single row of the table, strings are cells of the row.
71
+
72
+ def self.puts_table!(table_array)
73
+
74
+ column_sizes = []
75
+
76
+ table_array.first.count.times do |column_index|
77
+ column_sizes << table_array.collect{ |row| row[column_index] }.collect(&:to_s).collect(&:length).max + 3
78
+ end
79
+
80
+ table_array.each do |line|
81
+ chart_row = ""
82
+ line.each_with_index do |column, index|
83
+ chart_row << column.to_s.ljust(column_sizes[index])
84
+ end
85
+ log chart_row
86
+ end
87
+
88
+ end
89
+
90
+ end
@@ -0,0 +1,53 @@
1
+ module Buckler
2
+
3
+ def self.create_bucket!(name:nil, region:nil)
4
+
5
+ unless name.present?
6
+ alert "No bucket name provided."
7
+ alert "Usage: bucket create <bucket-name> --region <region>"
8
+ exit false
9
+ end
10
+
11
+ region ||= "us-east-1"
12
+ unless valid_region?(region)
13
+ log "Invalid region “#{region}”"
14
+ log "Use `bucket regions` to see a list of all S3 regions"
15
+ exit false
16
+ end
17
+
18
+ connect_to_s3!(region:region)
19
+ @bucket = Aws::S3::Bucket.new(name, client:@s3)
20
+
21
+ if @bucket.exists?
22
+ alert "Bucket #{@bucket.name} already exists"
23
+ exit false
24
+ end
25
+
26
+ log "Creating bucket #{name.bucketize} on #{region}…"
27
+
28
+ options = {
29
+ acl: "private"
30
+ }
31
+
32
+ unless region.eql?("us-east-1")
33
+ options[:create_bucket_configuration] = {
34
+ location_constraint: region
35
+ }
36
+ end
37
+
38
+ @bucket.create(options)
39
+ @bucket.wait_until_exists
40
+
41
+ log "Bucket #{name.bucketize} is how available for use ✔"
42
+ exit true
43
+
44
+ rescue Aws::S3::Errors::BucketAlreadyExists
45
+
46
+ alert "The bucket name “#{name}” is already taken."
47
+ alert "Bucket names must be unique across the entire AWS ecosystem."
48
+ alert "Select a different bucket name and re-run your command."
49
+ exit false
50
+
51
+ end
52
+
53
+ end