Class: Smash::CloudPowers::Storage::Local

Inherits:
Resource
  • Object
show all
Includes:
Smash::CloudPowers::Storage, Zenv
Defined in:
lib/cloud_powers/storage/local.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 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 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, #zlib_path

Methods included from LogicHelp

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

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 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 AwsResources

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

Methods included from Auth

creds, region

Methods included from Creatable

included

Constructor Details

#initialize(name:, origin: zlib_home, **config) ⇒ Local

Returns a new instance of Local.



12
13
14
15
16
17
# File 'lib/cloud_powers/storage/local.rb', line 12

def initialize(name:, origin: zlib_home, **config)
  super
  tmp_origin = origin.kind_of?(Pathname) ? origin : to_pathname(origin)
  @origin = paths_gcd(tmp_origin, project_root)
  @tie_in_path = to_pathname(@origin, to_camel(name))
end

Instance Attribute Details

#originObject

Returns the value of attribute origin.



10
11
12
# File 'lib/cloud_powers/storage/local.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/local.rb', line 10

def tie_in_path
  @tie_in_path
end

Class Method Details

.tie_in_configObject



19
20
21
22
# File 'lib/cloud_powers/storage/local.rb', line 19

def self.tie_in_config
  # TODO make this able to use #to_snake so it can be dynamic
  { type: 'local', origin: 'zlib_path' }
end

Instance Method Details

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

Creates an actual directory or file using the standard format for this storage name (camel case)

Returns <tt>Storage::Local</tt?

Yields:

  • (_self)

Yield Parameters:



37
38
39
40
41
42
# File 'lib/cloud_powers/storage/local.rb', line 37

def create_resource
  @response = to_realpath(tie_in_path)

  yield self if block_given?
  self
end

#exists?(scope: origin.parent) ⇒ Boolean

Find out if this local directory exists. Because directories are one of the ways to scope in CloudPowers::Storage::Local, we can find out if this local object is already on disk by searching the system, up to and including the root directory. The search attempts to first search through a smaller scope by checking in the project_root, origin and ./zlib directories before giving up and searching the whole machine from / or C: etc

Parameters

  • scope Pathname - default is project_root|origin|zlib|+Root Dir+

Returns

  • Boolean

Notes:

  • See #initialize for origin

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/cloud_powers/storage/local.rb', line 60

def exists?(scope: origin.parent)
  search_results = path_search(name, scope)
  !(search_results.empty?)
end

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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cloud_powers/storage/local.rb', line 107

def find(*args, location: origin, **opts)
  # TODO:
  # it's good to use select but this should use a stronger search or
  # something, to provide something more than just `.first`-ing an array
  path = select(*args, location: location, **opts).first
  file_rights = opts[:file_rights] || 'a+'
  if block_given?
    begin
      File.open(path, file_rights) do |file|
        yield file.read
      end
    rescue TypeError => e
      logger.info("file doesn't exist #{path}")
      nil
    end
  else
    path
  end
end

#job_file_exists?(object_name = '') ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/cloud_powers/storage/local.rb', line 75

def job_file_exists?(object_name = '')
  !!(find(object_name, location: job_home))
end

Tie the object on disk with this object, in memory. If the object on disk doesn’t exist, it will be created now.

Returns

  • Smash::CloudPowers::Storage::Local - self



70
71
72
73
# File 'lib/cloud_powers/storage/local.rb', line 70

def link
  @linked = exists? ? true : save!
  self
end

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/cloud_powers/storage/local.rb', line 79

def select(*patterns, **opts)
  file_rights = opts[:file_rights] || 'a+'
  scope = paths_lcd(
    tie_in_path, to_pathname(opts[:location]).expand_path
  )

  regexs = patterns.map do |pattern|
    if pattern.kind_of?(Pathname)
      additional_scope, basename = pattern.split
      scope = paths_lcd(additional_scope, scope)
      %r"#{basename}$"
    else
      pattern.kind_of?(Regexp) ? pattern : %r"#{pattern.to_s}$"
    end
  end

  paths = regexs.map do |regex|
    Dir["#{scope}/**/*"].
      grep(regex).
      map { |path| to_realpath(path) }
    end.flatten
  if block_given?
    paths.map { |path| yield File.open(path, file_rights) }
  else
    paths
  end.flatten
end

#touch(path) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/cloud_powers/storage/local.rb', line 127

def touch(path)
  if @tie_in_path.nil?
    logger.debug("Local#touch(#{path}) called")
    super(path)
  else
    scope, last_piece = to_pathname(path).split
    tied_in_path = paths_gcd(@tie_in_path, scope.expand_path) +
      (filename?(last_piece) ? last_piece : '')
    logger.debug("Local#touch(#{tied_in_path}) called")
    super(tied_in_path)
  end
end