module InitExporter::Helpers::Init

Constants

COMMENT_SYMBOL
DELIMITER
MAIN_SECTION
PREFIX_KEY
SECTION_SYMBOL

Public Instance Methods

all_procfiles() click to toggle source
# File lib/init_exporter/helpers/init.rb, line 29
def all_procfiles
  @all_procfiles ||= begin
    list_procfiles.map do |path|
      stage, role = extract_stage_and_role(path)
      {stage: stage, role: role, path: path}
    end
  end
end
application_init_name(application, role) click to toggle source
# File lib/init_exporter/helpers/init.rb, line 19
def application_init_name(application, role)
  suffix = role.to_s == 'all' ? '' : "_#{role}"

  application.tr('-', '_').gsub(/\W/, '') + suffix
end
detect_backend(ssh) click to toggle source
# File lib/init_exporter/helpers/init.rb, line 9
def detect_backend(ssh)
  if ssh.test 'which', 'systemctl'
    InitExporter::Helpers::Init::SystemdBackend.new(ssh)
  elsif ssh.test 'which', 'initctl'
    InitExporter::Helpers::Init::UpstartBackend.new(ssh)
  else
    raise 'No init system found (both systemctl and initctl are not found in path)'
  end
end
init_job_name(application, role) click to toggle source
# File lib/init_exporter/helpers/init.rb, line 25
def init_job_name(application, role)
  init_job_prefix + application_init_name(application, role)
end
init_roles() click to toggle source
# File lib/init_exporter/helpers/init.rb, line 52
def init_roles
  known_procfiles.map { |p| p[:role] }.uniq
end
known_procfiles() click to toggle source
# File lib/init_exporter/helpers/init.rb, line 38
def known_procfiles
  all_procfiles.reject do |p|
    p[:stage] == :unknown || p[:role] == :unknown
  end
end
procfile_exists?(stage, role) click to toggle source
# File lib/init_exporter/helpers/init.rb, line 44
def procfile_exists?(stage, role)
  all_procfiles.find { |p| p[:stage] == stage && p[:role] == role }
end
procfiles_for(stage) click to toggle source
# File lib/init_exporter/helpers/init.rb, line 48
def procfiles_for(stage)
  known_procfiles.select { |p| p[:stage] == stage }
end

Protected Instance Methods

extract_stage_and_role(procfile_path) click to toggle source
# File lib/init_exporter/helpers/init.rb, line 75
def extract_stage_and_role(procfile_path)
  case procfile_path
  when %r{\Aconfig/(?:init|upstart)/Procfile\.(\w+)\z}
    [Regexp.last_match(1), :all]
  when %r{\Aconfig/(?:init|upstart)/Procfile\.(\w+)\.(\w+)\z}
    [Regexp.last_match(2), Regexp.last_match(1)]
  when %r{\Aconfig/(?:init|upstart)/(\w+)/Procfile\.(\w+)\z}
    [Regexp.last_match(1), Regexp.last_match(2)]
  else
    [:unknown, :unknown]
  end.map(&:to_sym)
end
get_prefix(config_lines) click to toggle source
# File lib/init_exporter/helpers/init.rb, line 102
def get_prefix(config_lines)
  main_section = false
  prefix = nil
  config_lines.each do |line|
    next if line == '' || line[0] == COMMENT_SYMBOL
    if line[0] == SECTION_SYMBOL
      main_section = (line == MAIN_SECTION)
      next
    end
    next unless main_section
    key, value = line.split(DELIMITER)
    next unless key == PREFIX_KEY
    prefix = value.strip!
    break
  end
  prefix
end
init_job_prefix() click to toggle source
# File lib/init_exporter/helpers/init.rb, line 88
def init_job_prefix
  config = nil
  on release_roles :all do
    config = capture(:cat, '/etc/init-exporter.conf').split("\n").map(&:strip)
  end
  get_prefix(config)
end
list_procfiles() click to toggle source
# File lib/init_exporter/helpers/init.rb, line 58
def list_procfiles
  paths = []
  on release_roles :all do
    within release_path do
      paths = procfile_paths('config/init') + procfile_paths('config/upstart')
      break
    end
  end
  paths
end
procfile_paths(root) click to toggle source
# File lib/init_exporter/helpers/init.rb, line 69
def procfile_paths(root)
  capture(:find, root, '-type f', '-name "Procfile.*"', '2>/dev/null').lines.map(&:strip)
rescue SSHKit::Command::Failed
  []
end