Class: Inari::Configuration

Inherits:
Object show all
Includes:
Singleton
Defined in:
lib/inari/configuration.rb

Overview

Loads the tasks file and defines task manager to carry out the tasks

Defined Under Namespace

Classes: Server

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

:nodoc:



20
21
22
23
24
25
26
27
# File 'lib/inari/configuration.rb', line 20

def initialize() #:nodoc:
  @servers = Hash.new { |h, k| h[k] = [] }
  @options = Hash.new { |h, k| h[k] = [] }
  @default_load_path = File.join(Config::CONFIG['prefix'], 'etc')
  @load_paths = ['.', @default_load_path]
  @now = Time.now.utc
  @taskmanager = TaskManager.instance
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

:nodoc:



120
121
122
123
124
125
126
# File 'lib/inari/configuration.rb', line 120

def method_missing(sym, *args, &block) #:nodoc:
  if args.length == 0 && block.nil? && @variables.has_key?(sym)
    self[sym]
  else
    super
  end
end

Instance Attribute Details

#default_load_pathObject (readonly)

The load paths used for locating recipe files.



12
13
14
# File 'lib/inari/configuration.rb', line 12

def default_load_path
  @default_load_path
end

#load_pathsObject (readonly)

The load paths used for locating recipe files.



12
13
14
# File 'lib/inari/configuration.rb', line 12

def load_paths
  @load_paths
end

#optionsObject (readonly)

The hash of global options.



18
19
20
# File 'lib/inari/configuration.rb', line 18

def options
  @options
end

#serversObject (readonly)

The hash of servers.



15
16
17
# File 'lib/inari/configuration.rb', line 15

def servers
  @servers
end

#taskmanagerObject (readonly)

The TaskManager created for this configuration instance.



9
10
11
# File 'lib/inari/configuration.rb', line 9

def taskmanager
  @taskmanager
end

Instance Method Details

#config(field, *args) ⇒ Object



88
89
90
# File 'lib/inari/configuration.rb', line 88

def config(field, *args)
  @options[field] = args
end

#desc(text) ⇒ Object

Describe the next task to be defined.



93
94
95
# File 'lib/inari/configuration.rb', line 93

def desc(text)
  @next_description = text
end

#load(*args, &block) ⇒ Object

Load a configuration file or string into this configuration.

Usage:

load(:file => "settings.conf"):
  Load a configuration file.

load(:string => "set :scm, :subversion"):
  Load the given string as a configuration specification.

load { ... }
  Load the block in the context of the configuration.


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/inari/configuration.rb', line 41

def load(*args, &block)
  options = args.last.is_a?(Hash) ? args.pop : {}
  args.each { |arg| load options.merge(:file => arg) }
  return unless args.empty?

  if block
    raise 'Loading a block requires 2 parameters' unless args.empty?
    load(options.merge(:proc => block))

  elsif options[:file]
    file = options[:file]
    unless file[0] == ?/
      load_paths.each do |path|
        if File.file?(File.join(path, file))
          file = File.join(path, file)
          break
        end
      end
      
      raise ArgumentError, "Configuration file #{file} not found." if file.nil?
    end

    load :string => File.read(file), :name => options[:name] || file

  elsif options[:string]
    instance_eval(options[:string], options[:name] || "<eval>")

  elsif options[:proc]
    instance_eval(&options[:proc])

  else
    raise ArgumentError, "Don't know how to load #{options.inspect}"
  end
end

#loggerObject

Returns the logging object



98
99
100
# File 'lib/inari/configuration.rb', line 98

def logger
  Inari::logger
end

#run_tasksObject



116
117
118
# File 'lib/inari/configuration.rb', line 116

def run_tasks
  taskmanager.run_tasks
end

#server(which, *args) ⇒ Object

Define a new server and its associated servers. You must specify at least one host for each server.

Usage:

server :db, 'db1.example.com', 'db2.example.com'
server :mail, 'mail.example.com'

Raises:

  • (ArgumentError)


83
84
85
86
# File 'lib/inari/configuration.rb', line 83

def server(which, *args)
  raise ArgumentError, 'must give at least one host' if args.empty?
  args.each { |host| servers[which] << Server.new(host) }
end

#task(name, options = {}, &block) ⇒ Object

Define a new task. If a description is active (see #desc), it is added to the options under the :desc key. This method ultimately delegates to TaskManager#define_task.

Raises:

  • (ArgumentError)


105
106
107
108
109
110
111
112
113
114
# File 'lib/inari/configuration.rb', line 105

def task(name, options={}, &block)
  raise ArgumentError, 'expected a block' unless block
  
  if @next_description
    options = options.merge(:desc => @next_description)
    @next_description = nil
  end

  taskmanager.define_task(name, options, &block)
end