aws_client 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eb9f2d7d1282f3fa8fff4efee7c147769e4ca528
4
+ data.tar.gz: f918751ee5ee64bc06ebc258c2c9cce6e1e99072
5
+ SHA512:
6
+ metadata.gz: b2eb460108a486dc98ee0434e767204bac1948b25d867b09f5270de038d66b1a5dc1ff22defbe3601f9abf3b09217286e7cb95251d62051559b396776d99f557
7
+ data.tar.gz: dfa5c5959e58e8807fad8f865253bcfed5a739c6b0c3fa9262e277e97c48d464817b7b5daf86272c49fe9f21efb0d9f6d8b20643c9114ad2963c98600bc89e9f
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in aws_client.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ # AwsClient
2
+
3
+ Very light-weight wrapper for AWS interactions
4
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'aws_client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "aws_client"
8
+ spec.version = AwsClient::VERSION
9
+ spec.authors = ["Webzakimbo"]
10
+ spec.email = ["guillaume@webzakimbo.com"]
11
+
12
+ spec.summary = %q{Light-weight AWS wrapper.}
13
+ spec.description = %q{A very light-weight wrapper for my most commonly used AWS functions}
14
+ spec.homepage = "http://webzakimbo.com"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency 'aws-sdk', '~> 2'
23
+ spec.add_development_dependency "bundler", "~> 1.8"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "aws_client"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module AwsClient
2
+ VERSION = "0.0.1"
3
+ end
data/lib/aws_client.rb ADDED
@@ -0,0 +1,60 @@
1
+ require "aws-sdk"
2
+ require "aws_client/version"
3
+ require "wrapper"
4
+ require "s3_wrapper"
5
+ require "cf_wrapper"
6
+ require "rds_wrapper"
7
+ require "ec2_wrapper"
8
+
9
+ module AwsClient
10
+
11
+ CREDENTIALS_FILE = ".fog"
12
+ PATH_TO_CREDENTIALS = File.join(Dir.home, CREDENTIALS_FILE)
13
+
14
+ class Container
15
+
16
+ def initialize(credentials_key, aws_region, provisioning_region)
17
+ @aws_account_credentials_key = credentials_key
18
+ @aws_region = aws_region
19
+ @provisioning_region = provisioning_region
20
+ validate
21
+ end
22
+
23
+ def rds
24
+ @rds ||= ::AwsClient::RdsWrapper.new(client: client_connect(::Aws::RDS::Client))
25
+ end
26
+
27
+ def cloudformation
28
+ @cloudformation ||= ::AwsClient::CfWrapper.new(client: client_connect(::Aws::CloudFormation::Client))
29
+ end
30
+
31
+ def s3
32
+ @s3 ||= ::AwsClient::S3Wrapper.new(client: client_connect(::Aws::S3::Client), credentials_key: @aws_account_credentials_key, region: @aws_region)
33
+ end
34
+
35
+ def ec2
36
+ @ec2 ||= ::AwsClient::Ec2Wrapper.new(client: client_connect(::Aws::EC2::Client))
37
+ end
38
+
39
+ protected
40
+
41
+ def credentials
42
+ all_credentials = YAML.load_file(::AwsClient::PATH_TO_CREDENTIALS)
43
+ raise "Cannot find credentials for '#{@aws_account_credentials_key}'" unless all_credentials.has_key?(@aws_account_credentials_key)
44
+ creds_for_key = all_credentials[@aws_account_credentials_key]
45
+ return Aws::Credentials.new(creds_for_key["aws_access_key_id"], creds_for_key["aws_secret_access_key"])
46
+ end
47
+
48
+ def validate
49
+ raise "Missing Aws Account Credentials Key" unless @aws_account_credentials_key
50
+ raise "Missing credentials file: cannot find #{::AwsClient::CREDENTIALS_FILE} in your home directory" unless File.exists?(::AwsClient::PATH_TO_CREDENTIALS)
51
+ end
52
+
53
+ private
54
+
55
+ def client_connect(client_klass)
56
+ return client_klass.new(credentials: credentials, region: @provisioning_region)
57
+ end
58
+
59
+ end
60
+ end
data/lib/cf_wrapper.rb ADDED
@@ -0,0 +1,66 @@
1
+ module AwsClient
2
+ class CfWrapper < Wrapper
3
+
4
+ STACK_COMPLETE_STATUS = "CREATE_COMPLETE"
5
+ STACK_ROLLLBACK_STATUS = "ROLLBACK_IN_PROGRESS"
6
+ STACK_WAIT_SLEEP_TIME = 3
7
+
8
+ def create_stack(stack_params)
9
+ puts "Creating stack with the following parameters: #{stack_params.inspect}"
10
+ response = client.create_stack(stack_params)
11
+ wait_for_stack(stack_params[:stack_name])
12
+ end
13
+
14
+ def stack_status_for(stack_name)
15
+ stack_info = stack_info_for(stack_name)
16
+ return stack_info_for(stack_name).stack_status
17
+ end
18
+
19
+ def stack_info_for(stack_name)
20
+ page = client.describe_stacks
21
+ stack_info = page.data.stacks.select{|stack| stack["stack_name"] == stack_name }
22
+ raise "Cannot find CF stack named '#{stack_name}'" unless stack_info.any?
23
+ return stack_info.last
24
+ end
25
+
26
+ def stack_resources_by_stack_name_and_resource_type(stack_name, resource_type)
27
+ return stack_resources_for_stack(stack_name).select{|r| r.resource_type == resource_type }
28
+ end
29
+
30
+ def ec2_server_stack_resource_for_node_name(stack_name, node_name)
31
+ server_resources = ec2_server_stack_resources(stack_name)
32
+ server_resource = server_resources.select{|s| s.logical_resource_id == node_name}
33
+ return server_resource
34
+ end
35
+
36
+ def ec2_server_stack_resources(stack_name)
37
+ stack_resources_by_stack_name_and_resource_type(stack_name, "AWS::EC2::Instance")
38
+ end
39
+
40
+ def stack_resources_for_stack(stack_name)
41
+ @resources ||= client.describe_stack_resources("stack_name" => stack_name).data[:stack_resources]
42
+ end
43
+
44
+ def resource_data_for_resource_name(stack_name, resource_name)
45
+ stack_resources_for_stack(stack_name).select{|r| r.logical_resource_id == resource_name }.first
46
+ end
47
+
48
+ def physical_resource_id_for(stack_name, resource_name)
49
+ resource_data_for_resource_name(stack_name, resource_name).physical_resource_id
50
+ end
51
+
52
+ def wait_for_stack(stack_name)
53
+ puts "Waiting for stack to spin up"
54
+ status = ""
55
+ while status != STACK_COMPLETE_STATUS
56
+ status = stack_status_for(stack_name)
57
+ puts "'#{stack_name}' stack status: #{status}"
58
+
59
+ raise "Rollback in progress - CF build failed" if status == STACK_ROLLLBACK_STATUS
60
+ sleep(STACK_WAIT_SLEEP_TIME)
61
+ end
62
+ puts "Stack is up. Ready for bootstrapping"
63
+ end
64
+
65
+ end
66
+ end
@@ -0,0 +1,19 @@
1
+ module AwsClient
2
+ class Ec2Wrapper < AwsClient::Wrapper
3
+
4
+ def ec2_instance_public_address(node_name)
5
+ return ec2_instance_via_cloudformation_lookup(node_name).public_ip_address
6
+ end
7
+
8
+ def ec2_instance_via_cloudformation_lookup(node_name)
9
+ instance = cloudformation_resource_for_server(node_name).first
10
+ instances_result = client.describe_instances(filters: [{name:'instance-id', values:[instance.physical_resource_id]}])
11
+ return instances_result.reservations[0].instances[0]
12
+ end
13
+
14
+ def cloudformation_resource_for_server(node_name)
15
+ return CLIENTS.cloudformation.ec2_server_stack_resource_for_node_name(BOOT_CONFIG.cf_stack_name, node_name)
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,38 @@
1
+ module AwsClient
2
+ class RdsWrapper < AwsClient::Wrapper
3
+
4
+ def instance_by_database_name(database_name)
5
+ return all_instances.select{|db_instance| db_instance.db_name == database_name }.last
6
+ end
7
+
8
+ def instance_data_by_vpc_id(vpc_id)
9
+ return all_instances.select{|instance| instance.db_subnet_group.vpc_id = vpc_id }
10
+ end
11
+
12
+ def instance_endpoint(instance)
13
+ return "UNDEFINED" if instance.nil?
14
+ return instance.endpoint.address
15
+ end
16
+
17
+ def instance_endpoint_for_named_database(database_name)
18
+ instance = instance_by_database_name(database_name)
19
+ return instance_endpoint(instance)
20
+ end
21
+
22
+ def all_instances
23
+ @all_instances ||= get_all_instances.collect{|raw_instance| raw_instance.db_instances.flatten }.flatten
24
+ end
25
+
26
+ def get_all_instances
27
+ all_instances = []
28
+ pages = client.describe_db_instances
29
+ all_instances << pages.data
30
+ while pages.next_page?
31
+ pages = pages.next_page
32
+ @all_instances << pages.data
33
+ end
34
+ return all_instances
35
+ end
36
+
37
+ end
38
+ end
data/lib/s3_wrapper.rb ADDED
@@ -0,0 +1,68 @@
1
+ module AwsClient
2
+ class S3Wrapper < AwsClient::Wrapper
3
+
4
+ BUCKET_DEFINITION_FILE = ".aws_buckets"
5
+ PATH_TO_BUCKETS_DEFINITION = File.join(Dir.home, BUCKET_DEFINITION_FILE)
6
+
7
+ def initialize(*params)
8
+ super
9
+ @credentials_key = params[0][:credentials_key]
10
+ @aws_region = params[0][:region]
11
+ end
12
+
13
+ def resource
14
+ @resource ||= Aws::S3::Resource.new(client: client)
15
+ end
16
+
17
+ def bucket_object_for_bucket_prefix(bucket_prefix_code)
18
+ bucket = find_or_create_bucket_for(bucket_prefix_code)
19
+ return bucket.objects.collect{|object| object.object } # 'object.object' The S3 object belonging to the S3 resource
20
+ end
21
+
22
+ def find_bucket_by(bucket_name)
23
+ buckets.select{|bucket| bucket.name == bucket_name }.first
24
+ end
25
+
26
+ def write_file_to_bucket(bucket_prefix_code, file_path)
27
+ bucket = find_or_create_bucket_for(bucket_prefix_code)
28
+ s3_object = bucket.put_object(
29
+ :key => File.basename(file_path),
30
+ :body => IO.read(file_path)
31
+ )
32
+ puts "\nRemote file"
33
+ puts "#{s3_object.public_url}"
34
+ return s3_object
35
+ end
36
+
37
+ def buckets
38
+ return resource.buckets
39
+ end
40
+
41
+ def find_or_create_bucket_for(bucket_prefix_code)
42
+ bucket_prefix = bucket_prefixes[bucket_prefix_code]
43
+ full_bucket_name = bucket_name_for_function(bucket_prefix)
44
+ return find_or_create_bucket(full_bucket_name)
45
+ end
46
+
47
+ def bucket_name_for_function(bucket_function)
48
+ return "#{bucket_function}.#{@credentials_key}.#{@aws_region}"
49
+ end
50
+
51
+ def bucket_prefixes
52
+ YAML.load_file(PATH_TO_BUCKETS_DEFINITION)
53
+ end
54
+
55
+ private
56
+
57
+ def find_or_create_bucket(bucket_name)
58
+ begin
59
+ bucket = resource.create_bucket(:bucket => bucket_name)
60
+ rescue Aws::S3::Errors::BucketAlreadyOwnedByYou
61
+ bucket = find_bucket_by(bucket_name)
62
+ end
63
+
64
+ return bucket
65
+ end
66
+
67
+ end
68
+ end
data/lib/wrapper.rb ADDED
@@ -0,0 +1,11 @@
1
+ module AwsClient
2
+
3
+ class Wrapper
4
+ attr_reader :client
5
+
6
+ def initialize(*params)
7
+ @client = params[0][:client]
8
+ end
9
+ end
10
+
11
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aws_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Webzakimbo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: A very light-weight wrapper for my most commonly used AWS functions
56
+ email:
57
+ - guillaume@webzakimbo.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - aws_client.gemspec
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/aws_client.rb
71
+ - lib/aws_client/version.rb
72
+ - lib/cf_wrapper.rb
73
+ - lib/ec2_wrapper.rb
74
+ - lib/rds_wrapper.rb
75
+ - lib/s3_wrapper.rb
76
+ - lib/wrapper.rb
77
+ homepage: http://webzakimbo.com
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.2.2
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Light-weight AWS wrapper.
101
+ test_files: []