Class: PleaseRun::Platform::Base

Inherits:
Object
  • Object
show all
Includes:
Configurable, MustacheMethods
Defined in:
lib/pleaserun/platform/base.rb

Overview

Base class for all platforms.

This class provides all the general attributes common among all process runners.

For example, pretty much every runner (upstart, runit, sysv, etc) has concepts for the ‘name’ of a thing, what program it runs, what user to run as, etc.

Direct Known Subclasses

Launchd, Runit, SYSV, Systemd, SystemdUser, Upstart

Defined Under Namespace

Classes: InvalidTemplate

Instance Method Summary collapse

Methods included from MustacheMethods

#escaped, #escaped_args, #quoted, #shell_args, #shell_continuation, #shell_quote

Methods included from Configurable

#configurable_setup, included, #validate

Constructor Details

#initialize(target_version) ⇒ Base

Returns a new instance of Base.



166
167
168
169
# File 'lib/pleaserun/platform/base.rb', line 166

def initialize(target_version)
  configurable_setup
  self.target_version = target_version
end

Instance Method Details

#all_environment_variablesObject



242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/pleaserun/platform/base.rb', line 242

def all_environment_variables
  parsed_env_vars = {}
  parsed_env_vars = parsed_environment_variables unless parsed_environment_variables.nil?
  flag_env_vars = {}
  flag_env_vars = environment_variables unless environment_variables.nil?

  variables = parsed_env_vars.merge(flag_env_vars)
  return nil if variables.empty?
  result = []
  variables.each {|k, v| result << {'key' => k, 'value' => v} }
  result
end

#default_fileObject



255
256
257
# File 'lib/pleaserun/platform/base.rb', line 255

def default_file
  "/etc/default/#{name}"
end

#install_actionsObject

The default install_actions is none.

Subclasses which need installation actions should implement this method. This method will return an Array of String commands to execute in order to install this given runner.

For examples, see launchd and systemd platforms.



216
217
218
# File 'lib/pleaserun/platform/base.rb', line 216

def install_actions
  return []
end

#log_pathObject

def install_actions



220
221
222
# File 'lib/pleaserun/platform/base.rb', line 220

def log_path
  File.join(log_directory.chomp("/"), name)
end

#log_path_stderrObject



224
225
226
227
228
# File 'lib/pleaserun/platform/base.rb', line 224

def log_path_stderr
  filename = "#{name}-stderr.log"
  filename = log_file_stderr unless log_file_stderr.nil?
  File.join(log_directory.chomp("/"), filename)
end

#log_path_stdoutObject



230
231
232
233
234
# File 'lib/pleaserun/platform/base.rb', line 230

def log_path_stdout
  filename = "#{name}-stdout.log"
  filename = log_file_stdout unless log_file_stdout.nil?
  File.join(log_directory.chomp("/"), filename)
end

#parsed_environment_variablesObject



236
237
238
239
240
# File 'lib/pleaserun/platform/base.rb', line 236

def parsed_environment_variables
  return {} if environment_file.nil?
  return {} unless File.exist?(environment_file)
  Dotenv::Parser.call(File.open(environment_file, "rb:bom|utf-8", &:read))
end

#platformObject

Get the platform name for this class. The platform name is simply the lowercased class name, but this can be overridden by subclasses (but don’t, because that makes things confusing!)



173
174
175
# File 'lib/pleaserun/platform/base.rb', line 173

def platform
  self.class.name.split("::").last.gsub(/(?<=[^A-Z])[A-Z]+/, "-\\0").downcase
end

#render(text) ⇒ Object

Render a text input through Mustache based on this object.



198
199
200
# File 'lib/pleaserun/platform/base.rb', line 198

def render(text)
  return Mustache.render(text, self)
end

#render_template(name) ⇒ Object

def template_path

Raises:



182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/pleaserun/platform/base.rb', line 182

def render_template(name)
  possibilities = [
    File.join(template_path, target_version, name),
    File.join(template_path, "default", name),
    File.join(template_path, name)
  ]

  possibilities.each do |path|
    next unless File.readable?(path) && File.file?(path)
    return render(File.read(path))
  end

  raise InvalidTemplate, "Could not find template file for '#{name}'. Tried all of these: #{possibilities.inspect}"
end

#safe_filename(str) ⇒ Object

Get a safe-ish filename.

This renders ‘str` through Mustache and replaces spaces with underscores.



205
206
207
# File 'lib/pleaserun/platform/base.rb', line 205

def safe_filename(str)
  return render(str).gsub(" ", "_")
end

#sysconfig_fileObject



259
260
261
# File 'lib/pleaserun/platform/base.rb', line 259

def sysconfig_file
  "/etc/sysconfig/#{name}"
end

#template_pathObject

Get the template path for this platform.



178
179
180
# File 'lib/pleaserun/platform/base.rb', line 178

def template_path
  return File.join(File.dirname(__FILE__), "../../../templates", platform)
end