Class: Datapimp::Clients::Amazon
- Inherits:
-
Object
- Object
- Datapimp::Clients::Amazon
- Includes:
- Singleton
- Defined in:
- lib/datapimp/clients/amazon.rb
Class Method Summary collapse
Instance Method Summary collapse
- #aws_access_key_id ⇒ Object
- #aws_region ⇒ Object
- #aws_secret_access_key ⇒ Object
- #cdn ⇒ Object
- #cdn_options(o = {}) ⇒ Object
- #cloud_formation ⇒ Object
- #compute ⇒ Object
- #create_bucket(bucket_name) ⇒ Object
- #create_cdn_for(website_url, comment, aliases) ⇒ Object
- #create_redirect_bucket(bucket_name, redirect_to_bucket_name) ⇒ Object
- #find_bucket_by_name(bucket_name) ⇒ Object
- #find_or_create_bucket(bucket_name) ⇒ Object
- #has_application_keys? ⇒ Boolean
- #interactive_setup(options = {}) ⇒ Object
- #options ⇒ Object
- #s3_bucket ⇒ Object
- #s3_bucket_website_url ⇒ Object
- #site_description ⇒ Object
-
#site_domain_aliases ⇒ Object
the domain, and the domain with www.
- #storage ⇒ Object
- #website_host_for(bucket_or_bucket_name) ⇒ Object
- #website_url_for(bucket_or_bucket_name) ⇒ Object
- #with_options(opts = {}) ⇒ Object
Class Method Details
.client(options = {}) ⇒ Object
116 117 118 119 120 |
# File 'lib/datapimp/clients/amazon.rb', line 116 def self.client(={}) @client ||= begin instance.() end end |
.method_missing(meth, *args, &block) ⇒ Object
123 124 125 126 127 128 129 |
# File 'lib/datapimp/clients/amazon.rb', line 123 def self.method_missing(meth, *args, &block) if client.respond_to?(meth) return client.send(meth, *args, &block) end super end |
Instance Method Details
#aws_access_key_id ⇒ Object
8 9 10 |
# File 'lib/datapimp/clients/amazon.rb', line 8 def aws_access_key_id [:aws_access_key_id] || [:access_key_id] || Datapimp.config.aws_access_key_id end |
#aws_region ⇒ Object
16 17 18 |
# File 'lib/datapimp/clients/amazon.rb', line 16 def aws_region [:aws_region] || [:region] || Datapimp.config.aws_region || "us-east-1" end |
#aws_secret_access_key ⇒ Object
12 13 14 |
# File 'lib/datapimp/clients/amazon.rb', line 12 def aws_secret_access_key [:aws_secret_access_key] || [:secret_access_key] || Datapimp.config.aws_secret_access_key end |
#cdn ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/datapimp/clients/amazon.rb', line 39 def cdn @cdn ||= Fog::CDN.new({ provider: 'AWS', aws_access_key_id: aws_access_key_id, aws_secret_access_key: aws_secret_access_key, region: aws_region }) end |
#cdn_options(o = {}) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/datapimp/clients/amazon.rb', line 102 def (o={}) { enabled: true, custom_origin: { 'DNSName'=> o.fetch(:website_url) { s3_bucket_website_url }, 'OriginProtocolPolicy'=>'http-only' }, comment: o.fetch(:comment) { site_description }, caller_reference: Time.now.to_i.to_s, cname: o.fetch(:aliases) { site_domain_aliases }, default_root_object: 'index.html' } end |
#cloud_formation ⇒ Object
58 59 60 61 |
# File 'lib/datapimp/clients/amazon.rb', line 58 def cloud_formation require 'aws-sdk' @cloud_formation ||= Aws::CloudFormation::Client.new(region: aws_region) end |
#compute ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/datapimp/clients/amazon.rb', line 48 def compute @compute ||= Fog::Compute.new({ provider: 'AWS', aws_access_key_id: aws_access_key_id, aws_secret_access_key: aws_secret_access_key, region: aws_region }) end |
#create_bucket(bucket_name) ⇒ Object
155 156 157 158 159 160 |
# File 'lib/datapimp/clients/amazon.rb', line 155 def create_bucket(bucket_name) storage.directories.create(key: bucket_name, public: true).tap do |bucket| storage.put_bucket_website(bucket_name, 'index.html', key: 'error.html') #storage.put_bucket_cors(bucket_name, {"AllowedOrigin"=>"*","AllowedMethod"=>"GET","AllowedHeader"=>"Authorization"}) end end |
#create_cdn_for(website_url, comment, aliases) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/datapimp/clients/amazon.rb', line 90 def create_cdn_for(website_url, comment, aliases) aliases = aliases.join(",") if aliases.is_a?(Array) existing = cdn.distributions.find do |distribution| distribution.comment == comment end return existing if existing cdn.distributions.create((website_url: website_url, comment: comment, aliases: aliases)) end |
#create_redirect_bucket(bucket_name, redirect_to_bucket_name) ⇒ Object
162 163 164 165 |
# File 'lib/datapimp/clients/amazon.rb', line 162 def create_redirect_bucket(bucket_name, redirect_to_bucket_name) create_bucket(redirect_to_bucket_name) unless find_bucket_by_name(redirect_to_bucket_name) create_bucket(bucket_name) end |
#find_bucket_by_name(bucket_name) ⇒ Object
151 152 153 |
# File 'lib/datapimp/clients/amazon.rb', line 151 def find_bucket_by_name(bucket_name) storage.directories.get(bucket_name) rescue nil end |
#find_or_create_bucket(bucket_name) ⇒ Object
147 148 149 |
# File 'lib/datapimp/clients/amazon.rb', line 147 def find_or_create_bucket(bucket_name) find_bucket_by_name(bucket_name) || create_bucket(bucket_name) end |
#has_application_keys? ⇒ Boolean
176 177 178 |
# File 'lib/datapimp/clients/amazon.rb', line 176 def has_application_keys? (Datapimp.config.aws_access_key_id.to_s.length > 0 && Datapimp.config.aws_secret_access_key.to_s.length > 0) end |
#interactive_setup(options = {}) ⇒ Object
180 181 182 183 184 185 186 187 188 189 |
# File 'lib/datapimp/clients/amazon.rb', line 180 def interactive_setup(={}) secret_key = Datapimp.config.aws_secret_access_key.to_s access_key_id = Datapimp.config.aws_access_key_id.to_s secret_key = ask("What is the AWS Secret Access Key?") unless secret_key.length > 8 access_key_id = ask("What is the AWS Access Key ID?") unless access_key_id.length > 8 Datapimp.config.set(:aws_access_key_id, access_key_id) if access_key_id.length > 8 Datapimp.config.set(:aws_secret_access_key, secret_key) if secret_key.length > 8 end |
#options ⇒ Object
172 173 174 |
# File 'lib/datapimp/clients/amazon.rb', line 172 def @options ||= {} end |
#s3_bucket ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/datapimp/clients/amazon.rb', line 78 def s3_bucket if bucket_name = [:bucket_name] || Datapimp.config.get("bucket_name") if bucket = find_bucket_by_name(bucket_name) return bucket else "There is no bucket named: #{ bucket_name }. You can create one by running 'datapimp setup amazon --create-bucket=BUCKET_NAME" end else raise 'Could not determine bucketname for Datapimp.amazon.s3_bucket' end end |
#s3_bucket_website_url ⇒ Object
63 64 65 66 67 |
# File 'lib/datapimp/clients/amazon.rb', line 63 def s3_bucket_website_url if s3_bucket.is_a?(Fog::Storage::AWS::Directory) website_url_for(s3_bucket) end end |
#site_description ⇒ Object
69 70 71 |
# File 'lib/datapimp/clients/amazon.rb', line 69 def site_description [:description] || [:site_name] || s3_bucket.key end |
#site_domain_aliases ⇒ Object
the domain, and the domain with www
74 75 76 |
# File 'lib/datapimp/clients/amazon.rb', line 74 def site_domain_aliases [:aliases] end |
#storage ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/datapimp/clients/amazon.rb', line 20 def storage return @storage if @storage # Silence fog warnings Fog::Logger.define_singleton_method(:warning) do |*args| nil end @storage = Fog::Storage.new({ provider: 'AWS', aws_access_key_id: aws_access_key_id, aws_secret_access_key: aws_secret_access_key, region: aws_region, path_style: true }) @storage end |
#website_host_for(bucket_or_bucket_name) ⇒ Object
131 132 133 |
# File 'lib/datapimp/clients/amazon.rb', line 131 def website_host_for(bucket_or_bucket_name) URI.parse(website_url_for(bucket_or_bucket_name)).host end |
#website_url_for(bucket_or_bucket_name) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/datapimp/clients/amazon.rb', line 135 def website_url_for(bucket_or_bucket_name) bucket = bucket_or_bucket_name if bucket_or_bucket_name.is_a?(String) bucket = storage.directories.get(bucket_or_bucket_name) end if bucket "http://#{bucket.key}.s3-website-#{ bucket.location }.amazonaws.com" end end |
#with_options(opts = {}) ⇒ Object
167 168 169 170 |
# File 'lib/datapimp/clients/amazon.rb', line 167 def (opts={}) .merge!(opts) self end |