Class: Cuboid::OptionGroups::Paths

Inherits:
Cuboid::OptionGroup show all
Defined in:
lib/cuboid/option_groups/paths.rb

Overview

Holds paths to the directories of various system components.

Author:

  • Tasos “Zapotek” Laskos <tasos.laskos@gmail.com>

Constant Summary collapse

TMPDIR_SUFFIX =
'Cuboid_'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Cuboid::OptionGroup

#==, attr_accessor, attributes, #attributes, defaults, #defaults, #hash, inherited, #merge, set_defaults, #to_h, #to_hash, #to_rpc_data, #update, #validate

Constructor Details

#initializePaths

Returns a new instance of Paths.



75
76
77
78
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
# File 'lib/cuboid/option_groups/paths.rb', line 75

def initialize
    @root = self.root_path
    FileUtils.mkdir_p home_path

    @snapshots = self.config['snapshots'] || home_path + 'snapshots/'
    FileUtils.mkdir_p @snapshots

    @reports = self.config['reports'] || home_path + 'reports/'
    FileUtils.mkdir_p @reports

    if ENV['CUBOID_LOGDIR'].to_s != ''
        @logs = "#{ENV['CUBOID_LOGDIR']}/"
    elsif self.config['logs']
        @logs = self.config['logs']
    else
        @logs = "#{home_path}logs/"
        FileUtils.mkdir_p @logs
    end

    @lib         = @root    + 'lib/cuboid/'
    @executables = @lib     + 'processes/executables/'
    @support     = @lib     + 'support/'
    @mixins      = @support + 'mixins/'

    instance_variables.each do |iv|
        defaults[iv.to_s.sub( '@', '' ).to_sym] = instance_variable_get( iv )
    end

    tmpdir
end

Instance Attribute Details

#applicationObject

@return [String]



43
44
45
# File 'lib/cuboid/option_groups/paths.rb', line 43

def application
  @application
end

#componentsString

Returns:

  • (String)


# File 'lib/cuboid/option_groups/paths.rb', line 57

#executablesString

Returns System processes (instance, agent, browser, etc.).

Returns:

  • (String)

    System processes (instance, agent, browser, etc.).



75
76
77
78
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
# File 'lib/cuboid/option_groups/paths.rb', line 75

def initialize
    @root = self.root_path
    FileUtils.mkdir_p home_path

    @snapshots = self.config['snapshots'] || home_path + 'snapshots/'
    FileUtils.mkdir_p @snapshots

    @reports = self.config['reports'] || home_path + 'reports/'
    FileUtils.mkdir_p @reports

    if ENV['CUBOID_LOGDIR'].to_s != ''
        @logs = "#{ENV['CUBOID_LOGDIR']}/"
    elsif self.config['logs']
        @logs = self.config['logs']
    else
        @logs = "#{home_path}logs/"
        FileUtils.mkdir_p @logs
    end

    @lib         = @root    + 'lib/cuboid/'
    @executables = @lib     + 'processes/executables/'
    @support     = @lib     + 'support/'
    @mixins      = @support + 'mixins/'

    instance_variables.each do |iv|
        defaults[iv.to_s.sub( '@', '' ).to_sym] = instance_variable_get( iv )
    end

    tmpdir
end

#libString

Returns:

  • (String)


# File 'lib/cuboid/option_groups/paths.rb', line 48

#mixinsString

Returns:

  • (String)


# File 'lib/cuboid/option_groups/paths.rb', line 54

#reportsString

Returns Report storage.

Returns:

  • (String)

    Report storage.



# File 'lib/cuboid/option_groups/paths.rb', line 63

#rootString

Returns:

  • (String)


# File 'lib/cuboid/option_groups/paths.rb', line 45

#servicesString

Returns:

  • (String)


# File 'lib/cuboid/option_groups/paths.rb', line 60

#snapshotsString

Returns Snapshot storage.

Returns:

  • (String)

    Snapshot storage.



# File 'lib/cuboid/option_groups/paths.rb', line 67

#supportString

Returns:

  • (String)


# File 'lib/cuboid/option_groups/paths.rb', line 51

Class Method Details

.clear_config_cacheObject



197
198
199
# File 'lib/cuboid/option_groups/paths.rb', line 197

def self.clear_config_cache
    @config = nil
end

.configObject



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/cuboid/option_groups/paths.rb', line 201

def self.config
    return @config if @config

    if !File.exist?( paths_config_file )
        @config = {}
    else
        @config = YAML.load( IO.read( paths_config_file ) )
    end

    @config.dup.each do |category, dir|
        if dir.to_s.empty?
            @config.delete( category )
            next
        end

        dir = Cuboid.get_long_win32_filename( dir )

        if !Cuboid.windows?
            dir.gsub!( '~', ENV['HOME'] )
        end

        dir << '/' if !dir.end_with?( '/' )

        @config[category] = dir

        FileUtils.mkdir_p dir
    end

    @config
end

.paths_config_fileObject



193
194
195
# File 'lib/cuboid/option_groups/paths.rb', line 193

def self.paths_config_file
    Cuboid.get_long_win32_filename "#{root_path}config/paths.yml"
end

.root_pathString

Returns Root path of the engine.

Returns:

  • (String)

    Root path of the engine.



116
117
118
# File 'lib/cuboid/option_groups/paths.rb', line 116

def self.root_path
    File.expand_path( File.dirname( __FILE__ ) + '/../../..' ) + '/'
end

Instance Method Details

#configObject



189
190
191
# File 'lib/cuboid/option_groups/paths.rb', line 189

def config
    self.class.config
end

#home_pathObject



106
107
108
# File 'lib/cuboid/option_groups/paths.rb', line 106

def home_path
    @home_path ||= "#{ENV['HOME']}/.cuboid/"
end

#os_tmpdirObject



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/cuboid/option_groups/paths.rb', line 120

def os_tmpdir
    return @os_tmpdir if @os_tmpdir

    if config['tmpdir'].to_s.empty?
        # On MS Windows Dir.tmpdir can return the path with a shortname,
        # better avoid that as it can be insonsistent with other paths.
        @os_tmpdir = Cuboid.get_long_win32_filename( Dir.tmpdir )
    else
        @os_tmpdir = Cuboid.get_long_win32_filename( config['tmpdir'] )
    end
end

#root_pathObject



110
111
112
# File 'lib/cuboid/option_groups/paths.rb', line 110

def root_path
    self.class.root_path
end

#sweep_orphaned_tmpdirsObject

Sweep ‘<os_tmpdir>/Cuboid_<pid>` dirs whose pid is no longer alive. Best-effort: a dir we can’t probe / remove (permission, racing peer boot, etc.) is silently skipped — never block boot on cleanup. Ignores ‘Cuboid_Snapshot_<token>` and any other non-pid-suffixed siblings.



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/cuboid/option_groups/paths.rb', line 161

def sweep_orphaned_tmpdirs
    Dir.glob( "#{os_tmpdir}/#{TMPDIR_SUFFIX}*" ).each do |path|
        next if !File.directory?( path )

        suffix = File.basename( path ).sub( TMPDIR_SUFFIX, '' )
        next if suffix !~ /\A\d+\z/

        pid = suffix.to_i
        begin
            Process.kill( 0, pid )
            # Still alive — leave it.
            next
        rescue Errno::ESRCH
            # No such process — orphan; fall through to cleanup.
        rescue Errno::EPERM
            # Alive but owned by another user — leave it.
            next
        rescue
            next
        end

        FileUtils.rm_rf( path )
    rescue
        # Anything unexpected (filesystem race, transient I/O
        # error) — skip this entry, keep going.
    end
end

#tmp_dir_for(pid) ⇒ Object



152
153
154
# File 'lib/cuboid/option_groups/paths.rb', line 152

def tmp_dir_for( pid )
    "#{os_tmpdir}/#{TMPDIR_SUFFIX}#{pid}"
end

#tmpdirObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/cuboid/option_groups/paths.rb', line 132

def tmpdir
    return @tmpdir if @tmpdir

    # Reap dirs left over from prior Cuboid runs that were
    # SIGKILL'd / segfaulted / had their host crash — anything
    # that bypassed the `at_exit` block below. Done at boot
    # rather than on a timer so the user doesn't have to think
    # about it.
    sweep_orphaned_tmpdirs

    dir = tmp_dir_for( Process.pid )

    FileUtils.mkdir_p dir
    at_exit do
        FileUtils.rm_rf dir
    end

    @tmpdir = dir
end