Skip to content

Commit

Permalink
Structure changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kulbida committed Apr 16, 2016
1 parent 0f64c31 commit 11d37ce
Show file tree
Hide file tree
Showing 15 changed files with 247 additions and 114 deletions.
1 change: 1 addition & 0 deletions .rbenv-gemsets
@@ -0,0 +1 @@
aws_docker_utils
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -18,3 +18,6 @@ backup_16-04-15_16-34.sql.tar.gz
```

If the AWS S3 bucket is not exists, it will be created.

#####License
2016 - Bogdan Kulbida. Under MIT license.
7 changes: 4 additions & 3 deletions bin/aws_docker_utils
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby

require "docopt"
require_relative "../lib/collectors/to_s3.rb"
require_relative "../lib/aws_docker_utils/router.rb"

module AwsDockerUtils

Expand All @@ -13,6 +13,8 @@ Author: Bogdan Kulbida.
Usage:
#{__FILE__} backup_file from <container_name> to <s3_bucket_name> as <as_task> using <cmd>
#{__FILE__} configure init
#{__FILE__} configure reset
#{__FILE__} -h | --help
#{__FILE__} --version
Expand All @@ -23,8 +25,7 @@ Options:
DOCOPT

begin
collector = Collectors::ToS3.new(Docopt::docopt(doc))
collector.push
Router.new(Docopt::docopt(doc)).route!.activate
rescue Docopt::Exit => e
puts e.message
end
Expand Down
42 changes: 42 additions & 0 deletions lib/aws_docker_utils/aws_config_storage.rb
@@ -0,0 +1,42 @@
require 'yaml'

module AwsDockerUtils
class AwsConfigStorage

attr_accessor :config

CONFIG_FILE_PATH = "./aws_docker_utils.yml"

def initialize
self.config = fetch_config
end

def persist!(key, value)
`echo '#{key}: "#{value}"' >> #{file_name}`
end

def clear!
`echo "# AWS credentials:" > #{file_name}`
end

def valid?
@config && @config.fetch('access_key') && @config.fetch('secret_key')
end

private

def exists?
!`ls #{file_name}`.empty?
end

def fetch_config
clear! unless exists?
YAML::load_file(file_name)
end

def file_name
CONFIG_FILE_PATH
end

end
end
14 changes: 14 additions & 0 deletions lib/aws_docker_utils/compressor.rb
@@ -0,0 +1,14 @@
module AwsDockerUtils
class Compressor

def initialize(file_path)
@file_path = file_path
end

def compress
# for now it does nothing
@file_path
end

end
end
13 changes: 13 additions & 0 deletions lib/aws_docker_utils/controllers/base.rb
@@ -0,0 +1,13 @@
module AwsDockerUtils
module Controllers

class Base

def initialize(opts={})

end

end

end
end
40 changes: 40 additions & 0 deletions lib/aws_docker_utils/controllers/configurator.rb
@@ -0,0 +1,40 @@
require 'io/console'
require_relative '../aws_config_storage'

module AwsDockerUtils
module Controllers

class Configurator

def initialize(opts={})
@opts = opts
@config = AwsConfigStorage.new
end

def activate
if @opts.fetch('init')
publish(:access_key, std_input("Please enter AWS user ACCESS KEY:"))
publish(:secret_key, std_input("Please enter AWS user SERCET KEY:"))
publish(:region, std_input("Please enter AWS preferred REGION:"))
else
@config.clear!
end
end

private

def std_input(request)
print request
STDIN.noecho do |b|
b.gets.chomp
end
end

def publish(type, value)
@config.persist!(type, value)
end

end

end
end
33 changes: 33 additions & 0 deletions lib/aws_docker_utils/controllers/s3.rb
@@ -0,0 +1,33 @@
require 'tempfile'
require_relative "../compressor"
require_relative "../docker/client"
require_relative "../providers/s3"

module AwsDockerUtils
module Controllers

class S3

def initialize(opts={})
@bucket_name = opts.fetch('<s3_bucket_name>')
@task_name = opts.fetch('<as_task>', "file")
@container_name = opts.fetch('<container_name>')
@cmd = opts.fetch('<cmd>')
@file_name = Tempfile.new("#{`date '+#{@task_name}_%y-%m-%d_%H-%M'`.chop}.sql")
end

def activate
Docker::Client.new(container_name: @container_name, cmd: "#{@cmd} > #{@file_name.path}").exec
s3 = Providers::S3.new(bucket_name: @bucket_name)
if s3.put(Compressor.new(@file_name.path).compress)
puts "Pushed to S3."
else
puts "Unable to do the job."
end
@file_name.close
end

end

end
end
18 changes: 18 additions & 0 deletions lib/aws_docker_utils/docker/client.rb
@@ -0,0 +1,18 @@
module AwsDockerUtils
module Docker

class Client

def initialize(opts={})
@container_name = opts[:container_name]
@cmd = opts[:cmd]
end

def exec
`docker exec $(docker ps -q --filter 'name=#{@container_name}') #{@cmd}`
end

end

end
end
59 changes: 59 additions & 0 deletions lib/aws_docker_utils/providers/s3.rb
@@ -0,0 +1,59 @@
require 'yaml'
require 'aws-sdk'

module AwsDockerUtils
module Providers

class S3

DEFAULT_REGION = "us-east-1"

def initialize(opts={})
@bucket_name = opts.fetch(:bucket_name)

storage = AwsConfigStorage.new
client = if storage.valid?
config = storage.config
Aws::S3::Client.new(
region: (config.fetch("region").to_s || DEFAULT_REGION),
access_key_id: config.fetch("access_key").to_s,
secret_access_key: config.fetch("secret_key").to_s
)
else
Aws::S3::Client.new(region: DEFAULT_REGION)
end

@s3 = Aws::S3::Resource.new(client: client)
end

def put(file_path)
raise "Please set bucket name with constructor." if @bucket_name.nil?

bucket = create_bucket(@bucket_name)
obj = bucket.object(File.basename(file_path.gsub(/\.sql.+/, '.sql')))

if obj.upload_file(file_path)
return true
else
puts "could not upload file #{@file_path} to S3."
end

false
end

private

def create_bucket(bucket_name)
bucket = @s3.bucket(bucket_name)
unless bucket.exists?
puts 'Bucket does not exist. Creating...'
bucket.create
puts 'Done.'
end
bucket
end

end

end
end
20 changes: 20 additions & 0 deletions lib/aws_docker_utils/router.rb
@@ -0,0 +1,20 @@
require_relative "controllers/s3.rb"
require_relative "controllers/configurator.rb"

class Router

def initialize(opts={})
@opts = opts
end

def route!
case
when @opts.fetch('configure') then AwsDockerUtils::Controllers::Configurator.new(@opts)
when @opts.fetch('backup_file') then AwsDockerUtils::Controllers::S3.new(@opts)
else raise "NOP"
end
end

private

end
33 changes: 0 additions & 33 deletions lib/collectors/to_s3.rb

This file was deleted.

14 changes: 0 additions & 14 deletions lib/docker_compose/client.rb

This file was deleted.

49 changes: 0 additions & 49 deletions lib/s3/client.rb

This file was deleted.

15 changes: 0 additions & 15 deletions lib/utils.rb

This file was deleted.

0 comments on commit 11d37ce

Please sign in to comment.