Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
247 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
aws_docker_utils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module AwsDockerUtils | ||
module Controllers | ||
|
||
class Base | ||
|
||
def initialize(opts={}) | ||
|
||
end | ||
|
||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.