File: s3_health_check.rb

package info (click to toggle)
ruby-health-check 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 256 kB
  • sloc: sh: 1,021; ruby: 596; makefile: 3
file content (65 lines) | stat: -rw-r--r-- 1,837 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module HealthCheck
  class S3HealthCheck
    extend BaseHealthCheck

    class << self
      def check
        unless defined?(::Aws)
          raise "Wrong configuration. Missing 'aws-sdk' gem"
        end
        return create_error 's3', 'Could not connect to aws' if aws_s3_client.nil?
        HealthCheck.buckets.each do |bucket_name, permissions|
          if permissions.nil? # backward compatible
            permissions = [:R, :W, :D]
          end
          permissions.each do |permision|
            begin
            send(permision, bucket_name)
          rescue Exception => e
            raise "bucket:#{bucket_name}, permission:#{permision} - #{e.message}"
          end
          end
        end
        ''
      rescue Exception => e
        create_error 's3', e.message
      end

      private

      def configure_client
        return unless defined?(Rails)

        aws_configuration = {
          region: Rails.application.secrets.aws_default_region,
          credentials: ::Aws::Credentials.new(
            Rails.application.secrets.aws_access_key_id,
            Rails.application.secrets.aws_secret_access_key
          ),
          force_path_style: true
        }

        ::Aws::S3::Client.new aws_configuration
      end

      def aws_s3_client
        @aws_s3_client ||= configure_client
      end

      def R(bucket)
        aws_s3_client.list_objects(bucket: bucket)
      end

      def W(bucket)
        aws_s3_client.put_object(bucket: bucket,
                                 key: "healthcheck_#{Rails.application.class.parent_name}",
                                 body: Time.new.to_s)
      end

      def D(bucket)
        aws_s3_client.delete_object(bucket: bucket,
                                    key: "healthcheck_#{Rails.application.class.parent_name}")
      end
    end
  end
end