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



96
97
98
99
100
# File 'lib/datapimp/clients/amazon.rb', line 96

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

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



103
104
105
106
107
108
109
# File 'lib/datapimp/clients/amazon.rb', line 103

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_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



35
36
37
38
39
40
41
# File 'lib/datapimp/clients/amazon.rb', line 35

def cdn
  @cdn ||= Fog::CDN.new({
    provider: 'AWS',
    aws_access_key_id: aws_access_key_id,
    aws_secret_access_key: aws_secret_access_key
  })
end

#cdn_options(o = {}) ⇒ Object



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

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

#create_bucket(bucket_name) ⇒ Object



135
136
137
138
139
140
# File 'lib/datapimp/clients/amazon.rb', line 135

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



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

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



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

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



131
132
133
# File 'lib/datapimp/clients/amazon.rb', line 131

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

#find_or_create_bucket(bucket_name) ⇒ Object



127
128
129
# File 'lib/datapimp/clients/amazon.rb', line 127

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

#has_application_keys?Boolean

Returns:

  • (Boolean)


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

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



160
161
162
163
164
165
166
167
168
169
# File 'lib/datapimp/clients/amazon.rb', line 160

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



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

def options
  @options ||= {}
end

#s3_bucketObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/datapimp/clients/amazon.rb', line 58

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



43
44
45
46
47
# File 'lib/datapimp/clients/amazon.rb', line 43

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

#site_descriptionObject



49
50
51
# File 'lib/datapimp/clients/amazon.rb', line 49

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

#site_domain_aliasesObject

the domain, and the domain with www



54
55
56
# File 'lib/datapimp/clients/amazon.rb', line 54

def site_domain_aliases
  options[:aliases]
end

#storageObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/datapimp/clients/amazon.rb', line 16

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,
    path_style: true
  })


  @storage
end

#website_host_for(bucket_or_bucket_name) ⇒ Object



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

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



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

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



147
148
149
150
# File 'lib/datapimp/clients/amazon.rb', line 147

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