Module: Minicron

Defined in:
lib/minicron.rb,
lib/minicron/cli.rb,
lib/minicron/cron.rb,
lib/minicron/alert.rb,
lib/minicron/monitor.rb,
lib/minicron/transport.rb,
lib/minicron/alert/sms.rb,
lib/minicron/constants.rb,
lib/minicron/alert/email.rb,
lib/minicron/cli/commands.rb,
lib/minicron/transport/ssh.rb,
lib/minicron/hub/models/job.rb,
lib/minicron/alert/pagerduty.rb,
lib/minicron/hub/models/host.rb,
lib/minicron/transport/server.rb,
lib/minicron/hub/models/alert.rb,
lib/minicron/transport/client.rb,
lib/minicron/hub/models/schedule.rb,
lib/minicron/hub/serializers/job.rb,
lib/minicron/hub/models/execution.rb,
lib/minicron/hub/serializers/host.rb,
lib/minicron/transport/faye/server.rb,
lib/minicron/transport/faye/client.rb,
lib/minicron/hub/serializers/schedule.rb,
lib/minicron/hub/serializers/execution.rb,
lib/minicron/hub/models/job_execution_output.rb,
lib/minicron/hub/serializers/job_execution_output.rb,
lib/minicron/transport/faye/extensions/job_handler.rb

Overview

The minicron module

Defined Under Namespace

Modules: CLI, Hub, Transport Classes: Alert, Cron, Email, Monitor, PagerDuty, SMS

Constant Summary collapse

VERSION =
'0.3'
DEFAULT_CONFIG_FILE =
'/etc/minicron.toml'
BASE_PATH =
File.expand_path('../../../', __FILE__)
LIB_PATH =
File.expand_path('../../', __FILE__)
HUB_PATH =
File.expand_path('../../minicron/hub', __FILE__)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject

Returns the value of attribute config


60
61
62
# File 'lib/minicron.rb', line 60

def config
  @config
end

Class Method Details

.capture_output(options = {}, &block) ⇒ StringIO, Hash

Helper function to capture STDOUT and/or STDERR adapted from stackoverflow.com/a/11349621/483271

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • type (Symbol) — default: :both

    what to capture: :stdout, :stderr or :both

Returns:

  • (StringIO)

    if the type was set to :stdout or :stderr

  • (Hash)

    containg both the StringIO instances if the type was set to :both


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/minicron.rb', line 105

def self.capture_output(options = {}, &block)
  # Default options
  options[:type] ||= :both

  # Make copies of the origin STDOUT/STDERR
  original_stdout = $stdout
  original_stderr = $stderr

  # Which are we handling?
  case options[:type]
  when :stdout
    $stdout = stdout = StringIO.new
  when :stderr
    $stderr = stderr = StringIO.new
  when :both
    $stderr = $stdout = stdout = stderr = StringIO.new
  else
    fail ArgumentError, 'The type must be one of [stdout, stderr, both]'
  end

  # Yield to the code block to do whatever it has to do
  begin
    yield
  # Whatever happens make sure we reset STDOUT/STDERR
  ensure
    $stdout = original_stdout
    $stderr = original_stderr
  end

  # What are we going to return?
  case options[:type]
  when :stdout
    stdout
  when :stderr
    stderr
  else
    { :stdout => stdout, :stderr => stderr }
  end
end

.generate_ssh_key(type, id, name) ⇒ Object

Used to generate SSH keys for hosts but is completely generic

so this could be used for something other than hosts if needed

Parameters:

  • type (String)

    the thing that is using the key, this is just here

  • id (Integer)
  • name (String)

151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/minicron.rb', line 151

def self.generate_ssh_key(type, id, name)
  key = SSHKey.generate(:comment => "minicron public key for #{name}")

  # Set the locations to save the public key private key pair
  private_key_path = File.expand_path("~/.ssh/minicron_#{type}_#{id}_rsa")
  public_key_path = File.expand_path("~/.ssh/minicron_#{type}_#{id}_rsa.pub")

  # Save the public key private key pair
  File.write(private_key_path, key.private_key)
  File.write(public_key_path, key.ssh_public_key)

  # Set the correct permissions on the files
  File.chmod(0600, private_key_path)
  File.chmod(0644, public_key_path)

  key
end

.get_fqdnString

Get the system fully qualified domain name

Returns:

  • (String)

172
173
174
# File 'lib/minicron.rb', line 172

def self.get_fqdn
  `hostname -f`.strip
end

.get_hostnameString

Get the system short hostname

Returns:

  • (String)

179
180
181
# File 'lib/minicron.rb', line 179

def self.get_hostname
  `hostname -s`.strip
end

.get_userString

Get the user minicron is being run as

Returns:

  • (String)

186
187
188
# File 'lib/minicron.rb', line 186

def self.get_user
  `whoami`.strip
end

.parse_config_hash(options = {}) ⇒ Object

Parses the config options from the given hash that matches the expected config format in Minicron.config


85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/minicron.rb', line 85

def self.parse_config_hash(options = {})
  options.each do |key, value|
    if options[key].respond_to?(:each)
      options[key].each do |k, v|
        if !v.nil?
          @config[key][k] = v
        end
      end
    else
      @config[key] = value
    end
  end
end

.parse_file_config(file_path) ⇒ Object

Parse the given config file and update the config hash

Parameters:

  • file_path (String)

66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/minicron.rb', line 66

def self.parse_file_config(file_path)
  file_path ||= Minicron::DEFAULT_CONFIG_FILE

  begin
    @config = TOML.load_file(file_path)
  rescue Errno::ENOENT
    # Fail if the file doesn't exist unless it's the default config file
    if file_path != DEFAULT_CONFIG_FILE
      raise Exception, "Unable to the load the file '#{file_path}', are you sure it exists?"
    end
  rescue Errno::EACCES
    fail Exception, "Unable to the read the file '#{file_path}', check it has the right permissions."
  rescue TOML::ParseError
    fail Exception, "An error occured parsing the config file '#{file_path}', please check it uses valid TOML syntax."
  end
end