Class: Sneaql::JDBCDriverHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/sneaql_standard_lib/jdbc_drivers.rb

Overview

idempotent handling for jdbc driver

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ JDBCDriverHandler

pulls down the jdbc driver and loads it param [Hash] params parameter hash



14
15
16
# File 'lib/sneaql_standard_lib/jdbc_drivers.rb', line 14

def initialize(params)
  @params = params
end

Instance Attribute Details

#confirmed_pathObject

exposed for unit tests



9
10
11
# File 'lib/sneaql_standard_lib/jdbc_drivers.rb', line 9

def confirmed_path
  @confirmed_path
end

#target_pathObject

Returns the value of attribute target_path.



10
11
12
# File 'lib/sneaql_standard_lib/jdbc_drivers.rb', line 10

def target_path
  @target_path
end

Instance Method Details

#confirm_jdbc_driverObject

driver info must be provided jar file should be one of the following:

http store http://path/to/jarfile.jar
inside container file://path/to/jarfile.jar
s3 bucket s3://path/to/jarfile.jar requires aws credentials to be provided

this method confirms the existence of the jdbc driver jar file if the file exists, no action is taken. if file does not exist it is downloaded from the source location, either http or s3.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sneaql_standard_lib/jdbc_drivers.rb', line 26

def confirm_jdbc_driver
  @confirmed_path = nil
  if @params[:jdbc_driver_jar] =~ /^http.*/i
    @target_path = '/tmp/jdbc.jar'
    @confirmed_path = File.exist?(@target_path) ? @target_path : download_driver_http
  elsif @params[:jdbc_driver_jar] =~ /^file.*/i
    @target_path = @params[:jdbc_driver_jar].gsub(/^file\:\/\//i, '')
    @confirmed_path = @target_path if File.exist?(@target_path)
  elsif @params[:jdbc_driver_jar] =~ /^s3.*/i
    @target_path = '/tmp/jdbc.jar'
    @confirmed_path = File.exist?(@target_path) ? @target_path : download_driver_s3
  else raise 'no suitable driver provided'
  end

  # rubocop says to turn this into a guard statement
  # but this needs the driver to be present before running
  if @params[:jdbc_driver_jar_md5]
    raise 'driver jar md5 mismatch' unless md5_check(
      @confirmed_path,
      @params[:jdbc_driver_jar_md5]
    )
  end
end

#download_driver_httpObject

downloads driver from an http source assuming no credentials need to be provided



52
53
54
55
56
57
# File 'lib/sneaql_standard_lib/jdbc_drivers.rb', line 52

def download_driver_http
  File.write(
    @target_path,
    open(@params[:jdbc_driver_jar]).read
  )
end

#download_driver_s3Object

downloads jar file from s3 source uses standard AWS environment variables or instance profile for credentials



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/sneaql_standard_lib/jdbc_drivers.rb', line 62

def download_driver_s3
  bucket_name = @params[:jdbc_driver_jar].match(
    /^s3\:\/\/([a-zA-Z0-9]|\.|\-)+/i
  )[0].gsub(/s3\:\/\//i, '')

  object_key = @params[:jdbc_driver_jar].gsub(
    /^s3\:\/\/([a-zA-Z0-9]|\.|\-)+\//i,
    ''
  )

  aws_creds =
  if ENV['AWS_ACCESS_KEY_ID']
    Aws::Credentials.new(
      ENV['AWS_ACCESS_KEY_ID'],
      ENV['AWS_SECRET_ACCESS_KEY']
    )
  else
    Aws::InstanceProfileCredentials.new
  end

  s3 = Aws::S3.new(
    region: ENV['AWS_REGION'],
    credentials: aws_creds
  )

  s3.get_object(
    response_target: @target_path,
    bucket: bucket_name,
    key: object_key
  )
end

#md5_check(file_path, file_md5) ⇒ Boolean

confirms that file md5 matches value provided

Parameters:

  • file_path (String)

    path to file

  • file_md5 (String)

    known md5 of file

Returns:

  • (Boolean)


98
99
100
101
102
# File 'lib/sneaql_standard_lib/jdbc_drivers.rb', line 98

def md5_check(file_path, file_md5)
  m = Digest::MD5.file(file_path)
  return true if m.hexdigest == file_md5
  false
end

#require_jdbc_driverObject

requires the jar file and jdbc driver class into the current jruby context. after this runs all jdbc connections will use this driver class.



107
108
109
110
# File 'lib/sneaql_standard_lib/jdbc_drivers.rb', line 107

def require_jdbc_driver
  require @confirmed_path
  java_import @params[:jdbc_driver_class]
end