Module: Slugforge::Helper::Fog

Defined in:
lib/slugforge/helper/fog.rb

Instance Method Summary collapse

Instance Method Details

#autoscalingObject



13
14
15
# File 'lib/slugforge/helper/fog.rb', line 13

def autoscaling
  @autoscaling ||= ::Fog::AWS::AutoScaling.new(aws_credentials)
end

#aws_bucketObject



31
32
33
# File 'lib/slugforge/helper/fog.rb', line 31

def aws_bucket
  config.slug_bucket || raise(error_class, "You must specify a slug bucket in your configuration")
end

#aws_credentialsObject



23
24
25
26
27
28
29
# File 'lib/slugforge/helper/fog.rb', line 23

def aws_credentials
  {
    :aws_access_key_id     => verify_aws_config(config.aws_access_key, 'access key'),
    :aws_secret_access_key => verify_aws_config(config.aws_secret_key, 'secret key'),
    :aws_session_token     => config.aws_session_token
  }.reject{ |_,v| v.nil? }
end

#aws_session(duration = 30) ⇒ Hash

Create a temporary AWS session

Returns:

  • (Hash)

    hash containing :access_key_id, :secret_access_key, :session_token



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/slugforge/helper/fog.rb', line 42

def aws_session(duration = 30)
  @aws_session ||= begin
    sts = ::Fog::AWS::STS.new(aws_credentials)

    # Request a token for the user that has permissions masked to a single S3 bucket and only lasts a short time
    token = sts.get_federation_token( username, bucket_policy, duration * 60 ) # session duration in minutes

    {
      aws_access_key_id:     token.body['AccessKeyId'],
      aws_secret_access_key: token.body['SecretAccessKey'],
      aws_session_token:     token.body['SessionToken'],
      aws_region:            config.aws_region
    }
  end
end

#computeObject



6
7
8
9
10
11
# File 'lib/slugforge/helper/fog.rb', line 6

def compute
  @compute ||= ::Fog::Compute.new(aws_credentials.merge({
    :region   => config.aws_region,
    :provider => 'AWS'
  }))
end

#expiring_url(file, expiration = nil) ⇒ Object



35
36
37
38
# File 'lib/slugforge/helper/fog.rb', line 35

def expiring_url(file, expiration=nil)
  expiration ||= Time.now + 60*60
  file.url(expiration)
end

#s3Object



17
18
19
20
21
# File 'lib/slugforge/helper/fog.rb', line 17

def s3
  @s3 ||= ::Fog::Storage.new(aws_credentials.merge({
    :provider => 'AWS'
  }))
end