Class: Pipely::Build::S3PathBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/pipely/build/s3_path_builder.rb

Overview

Builds paths to assets, logs, and steps that are on S3.

Constant Summary collapse

START_TIME =
"\#{format(@scheduledStartTime,'YYYY-MM-dd_HHmmss')}"
START_DATE =
"\#{format(@scheduledStartTime,'YYYY-MM-dd')}"

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ S3PathBuilder

options should contain a Hash of your desired S3 path patterns, formatted for Pathology. The remainder of the options Hash serves as interpolation values for the templates.

Several additional interpolation variables (:protocol, :timestamp, :datestamp) are provided by S3PathBuilder at interpolation time.

If options is not present, or if it is missing any of the legacy templates (assets, logs, steps, etc.), they will be automatically built, using bucket names found in the options Hash, preserving the original behavior.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pipely/build/s3_path_builder.rb', line 25

def initialize(options)
  @options = options.merge({
    timestamp: START_TIME,
    datestamp: START_DATE,
  })

  @path_templates = default_templates

  if templates = @options.delete(:templates)
    @path_templates.merge!(templates)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Implement path interpolation methods, e.g. s3_log_prefix, etc.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pipely/build/s3_path_builder.rb', line 56

def method_missing(method_name, *args, &block)
  case method_name
  when /^(s3n?)_(.*)_prefix$/
    if pattern = @path_templates[$2.to_sym]
      Pathology.template(pattern).interpolate(
        @options.merge({protocol: $1})
      )
    else
      super
    end
  else
    super
  end
end

Instance Method Details

#bucket_relative_s3_asset_prefixObject

Re-route legacy method name to the standard format implemented by method_missing above.



74
75
76
# File 'lib/pipely/build/s3_path_builder.rb', line 74

def bucket_relative_s3_asset_prefix
  s3_bucket_relative_asset_prefix
end

#default_templatesObject

Support legacy interface, wherein config simply contained bucket names, and users were forced to abide by Pipely’s somewhat arbitrary path structure.



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/pipely/build/s3_path_builder.rb', line 42

def default_templates
  assets, logs, steps = @options.values_at(:assets, :logs, :steps)

  {
    asset: ":protocol://#{assets}/:prefix/:timestamp",
    log: ":protocol://#{logs}/:prefix/:timestamp",
    step: ":protocol://#{steps}/:prefix",
    shared_asset: ":protocol://#{assets}/:prefix/shared/:datestamp",
    bucket_relative_asset: ':prefix/:timestamp',
  }
end

#to_hashObject



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/pipely/build/s3_path_builder.rb', line 78

def to_hash
  values = %w(s3 s3n).flat_map do |protocol|
    @path_templates.keys.map do |path_name|
      key = "#{protocol}_#{path_name}_prefix".to_sym
      [key, send(key)]
    end
  end

  # Support legacy method name.
  Hash[values].merge({
    bucket_relative_s3_asset_prefix: bucket_relative_s3_asset_prefix
  })
end