Class: Datapimp::Clients::Amazon

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/datapimp/clients/amazon.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.client(options = {}) ⇒ Object



110
111
112
113
114
# File 'lib/datapimp/clients/amazon.rb', line 110

def self.client(options={})
  @client ||= begin
                instance.with_options(options)
              end
end

.method_missing(meth, *args, &block) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/datapimp/clients/amazon.rb', line 117

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_idObject



8
9
10
# File 'lib/datapimp/clients/amazon.rb', line 8

def aws_access_key_id
  options[:aws_access_key_id] || options[:access_key_id] || Datapimp.config.aws_access_key_id
end

#aws_regionObject



16
17
18
# File 'lib/datapimp/clients/amazon.rb', line 16

def aws_region
  options[:aws_region] || options[:region] || Datapimp.config.aws_region || "us-east-1"
end

#aws_secret_access_keyObject



12
13
14
# File 'lib/datapimp/clients/amazon.rb', line 12

def aws_secret_access_key
  options[:aws_secret_access_key] || options[:secret_access_key] || Datapimp.config.aws_secret_access_key
end

#cdnObject



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



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/datapimp/clients/amazon.rb', line 96

def cdn_options(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

#computeObject



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



149
150
151
152
153
154
# File 'lib/datapimp/clients/amazon.rb', line 149

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



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/datapimp/clients/amazon.rb', line 84

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(cdn_options(website_url: website_url, comment: comment, aliases: aliases))
end

#create_redirect_bucket(bucket_name, redirect_to_bucket_name) ⇒ Object



156
157
158
159
# File 'lib/datapimp/clients/amazon.rb', line 156

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



145
146
147
# File 'lib/datapimp/clients/amazon.rb', line 145

def find_bucket_by_name(bucket_name)
  storage.directories.get(bucket_name) rescue nil
end

#find_or_create_bucket(bucket_name) ⇒ Object



141
142
143
# File 'lib/datapimp/clients/amazon.rb', line 141

def find_or_create_bucket(bucket_name)
  find_bucket_by_name(bucket_name) || create_bucket(bucket_name)
end

#has_application_keys?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/datapimp/clients/amazon.rb', line 170

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



174
175
176
177
178
179
180
181
182
183
# File 'lib/datapimp/clients/amazon.rb', line 174

def interactive_setup(options={})
  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

#optionsObject



166
167
168
# File 'lib/datapimp/clients/amazon.rb', line 166

def options
  @options ||= {}
end

#s3_bucketObject



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/datapimp/clients/amazon.rb', line 72

def s3_bucket
  if bucket_name = options[: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_urlObject



57
58
59
60
61
# File 'lib/datapimp/clients/amazon.rb', line 57

def s3_bucket_website_url
  if s3_bucket.is_a?(Fog::Storage::AWS::Directory)
    website_url_for(s3_bucket)
  end
end

#site_descriptionObject



63
64
65
# File 'lib/datapimp/clients/amazon.rb', line 63

def site_description
  options[:description] || options[:site_name] || s3_bucket.key
end

#site_domain_aliasesObject

the domain, and the domain with www



68
69
70
# File 'lib/datapimp/clients/amazon.rb', line 68

def site_domain_aliases
  options[:aliases]
end

#storageObject



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



125
126
127
# File 'lib/datapimp/clients/amazon.rb', line 125

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



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/datapimp/clients/amazon.rb', line 129

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



161
162
163
164
# File 'lib/datapimp/clients/amazon.rb', line 161

def with_options(opts={})
  options.merge!(opts)
  self
end