Class: Smash::CloudPowers::Storage::Bucket

Inherits:
Resource
  • Object
show all
Includes:
Smash::CloudPowers::Storage
Defined in:
lib/cloud_powers/storage/bucket.rb

Instance Attribute Summary collapse

Attributes inherited from Resource

#call_name, #client, #linked, #meta, #name, #remote_id, #tags, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Smash::CloudPowers::Storage

#all_storage, #build_storage, #create_storage, #existing_storage, #local_job_file_exists?, #search, #send_logs_to_s3, #source_job, #storage_select

Methods included from Helpers

#create_logger, #log_file, #logger

Methods included from PathHelp

#common_delimiter, #expand_path, #file_exists?, #file_search, #filename?, #job_exist?, #job_path, #job_require_path, #path_search, #paths_gcd, #paths_lcd, #to_path, #to_pathname, #to_realpath, #touch, #zlib_path

Methods included from LogicHelp

#attr_map, #called_from, #i_var_hash, #instance_attr_accessor, #smart_retry, #update_message_body

Methods included from LangHelp

#deep_modify_keys_with, #extract!, #find_and_remove, #format_error_message, #from_json, #modify_keys_with, #to_basic_hash, #to_camel, #to_hyph, #to_i_var, #to_pascal, #to_ruby_file_name, #to_snake, #valid_json?, #valid_url?

Methods included from AwsResources

#ec2, #image, #kinesis, #queue_poller, #region, #s3, #sns, #sqs

Methods included from Zenv

#env_vars, #i_vars, #lsof_cwd, #pid, #proc_cwd, #process_search, #project_root, #project_root=, #ps_cwd, #system_vars, #zfind, #zselect

Methods included from Auth

creds, region

Methods included from Creatable

included

Constructor Details

#initialize(name:, client: s3, **config) ⇒ Bucket

Returns a new instance of Bucket.



12
13
14
15
16
17
18
19
20
# File 'lib/cloud_powers/storage/bucket.rb', line 12

def initialize(name:, client: s3, **config)
  super
  @bucket = Aws::S3::Bucket.new(name: name, client: client)
  build_storage(name: 'local', named_type: 'storage').link
  @delimiter = config[:delimiter] || common_delimiter
  # should be the 'lowest level' of storage this object is aware of
  @origin = config[:origin] || project_root.split.last
  @tie_in_path = paths_gcd(local_storage.tie_in_path, project_root)
end

Instance Attribute Details

#delimiterObject

Returns the value of attribute delimiter.



10
11
12
# File 'lib/cloud_powers/storage/bucket.rb', line 10

def delimiter
  @delimiter
end

#originObject

Returns the value of attribute origin.



10
11
12
# File 'lib/cloud_powers/storage/bucket.rb', line 10

def origin
  @origin
end

#tie_in_pathObject

Returns the value of attribute tie_in_path.



10
11
12
# File 'lib/cloud_powers/storage/bucket.rb', line 10

def tie_in_path
  @tie_in_path
end

Class Method Details

.tie_in_configObject



22
23
24
25
# File 'lib/cloud_powers/storage/bucket.rb', line 22

def self.tie_in_config
  # TODO make this able to use #to_snake so it can be dynamic
  { name: 'bucket', client: 's3' }
end

Instance Method Details

#create_resource {|_self| ... } ⇒ Object

def bucket_select(pattern, **config)

client.list_buckets.buckets.select { |b| %r"#{name}" =~ b.name }

end

Yields:

  • (_self)

Yield Parameters:



35
36
37
38
39
# File 'lib/cloud_powers/storage/bucket.rb', line 35

def create_resource
  @response = s3.create_bucket(bucket: name)
  yield self if block_given?
  self
end

#exists?Boolean

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/cloud_powers/storage/bucket.rb', line 41

def exists?
  # TODO nil.exists? will asplode
  !!(@bucket.exists? rescue nil)
end

#find(*args, location: origin, **opts) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/cloud_powers/storage/bucket.rb', line 95

def find(*args, location: origin, **opts)
  path = select(*args, location: location, **opts).first
  file_name = path.kind_of?(Pathname) ? path.split.last.to_s : path
  file_permissions = opts[:file_permissions] || 'a+'
  path = local_storage.tie_in_path + path

  if block_given?
    begin
      File.open(path, file_permissions) do |file|
        data = get_file(file_name, location: location).get.body.read
        yield file, data
      end
    rescue Exception => e
      if e =~ /no implicit conversion of nil into String/
        logger.info "no file found from #{path} called #{file}"
        nil
      else
        super
      end
    end
  else
    path
  end
end

#job_file_exists?(file_name, location = job_path) ⇒ Boolean

Find out if a job exists in the bucket

Parameters

  • file_name Pathname|String - the name of the file you want to pass

  • location Pathname|String (optional) - this helps speed up the process by limiting any searching for the file

Returns IO -

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/cloud_powers/storage/bucket.rb', line 55

def job_file_exists?(file_name, location = job_path)
  normalized_location = paths_gcd(project_root, location)
  !get_file_names(file_name, location: normalized_location).empty?
end


60
61
62
63
64
65
66
67
68
# File 'lib/cloud_powers/storage/bucket.rb', line 60

def link
  if exists?
    # do stuff
  else
    save!
  end

  @linked = @response.nil?
end

#put(file_name, location = origin, **config) ⇒ Object



122
123
124
125
126
127
128
129
130
# File 'lib/cloud_powers/storage/bucket.rb', line 122

def put(file_name, location = origin, **config)
  body = get_local_file_body(file_name, location)
  return if body.nil?

  client.put_object(
    put_config(body: body, key: file_name, **config)
  )#.on_success { Thread.new { update_registry } }
  true
end

#select(*args, **opts) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/cloud_powers/storage/bucket.rb', line 70

def select(*args, **opts)
  pattern = args.pop
  location = args.shift || origin

  names = if filename?(pattern)
    get_file_names(pattern, location: location)
  else
    get_bucket_names(pattern, location: location)
  end


  names = get_file_names(pattern, location: location)
  if block_given?
    names.map do |file_name|
      new_file = to_realpath(local_storage.find(file_name))
      File.open(new_file, 'r+') do |file|
        data = get_file(file_name, location: location).get.body.read
        yield file, data
      end
    end
  else
    names
  end
end