Class: Really::Script

Inherits:
Object show all
Defined in:
lib/really/script.rb

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Script

Returns a new instance of Script.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/really/script.rb', line 9

def initialize(path, options = {})
  path = File.expand_path path
  raise "Script at '#{path}' is not readable. :( (Does it exist?)" unless File.readable?(path)

  @options = options
  @tasks = {}
  @roles = {}
  @drivers = []

  instance_eval File.read(path)
end

Instance Method Details

#configure(&block) ⇒ Object



41
42
43
# File 'lib/really/script.rb', line 41

def configure(&block)
  instance_eval &block
end

#execute(role_names = []) ⇒ Object

Internal public API



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/really/script.rb', line 57

def execute(role_names = [])
  @drivers.each do |driver|
    driver.open

    role_graph = DependencyGraph.new @roles

    # All drivers implicitly get the magic :default role.
    role_graph.add @roles[:default] if @roles.has_key?(:default)

    driver.role_names.each do |role_name|
      raise "Role '#{role_name}' does not exist." unless @roles.has_key?(role_name)
      role_graph.add @roles[role_name]
    end

    all_tasks = Hash[@tasks.collect { |key, value| [key, value.dup] }]
    task_graph = DependencyGraph.new all_tasks

    roles = role_graph.ordered_nodes

    roles.each do |role|
      role.task_descriptors.each do |descriptor|
        task_name = descriptor[:task_name]
        raise "Task '#{task_name}' does not exist." unless all_tasks.has_key?(task_name)

        task = all_tasks[task_name]
        task.options.merge! descriptor[:options]
        task_graph.add task
      end
    end

    logger.status "[#{roles.length > 1 ? 'roles' : 'role'}: #{roles.collect(&:name).join ', '}]"

    task_graph.ordered_nodes.each do |task|
      logger.status "task:#{task.name}", header: '***', header_color: :green
      driver.execute_task task, @options
    end

    driver.close
  end
end

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



32
33
34
35
36
37
38
39
# File 'lib/really/script.rb', line 32

def role(name, options = {}, &block)
  name = name.to_sym
  raise "Duplicate definition of role name '#{name}'." if @roles.has_key?(name)

  role = Role.new(name, options, &block)
  @roles[name] = role
  role
end

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

Public API for use in really.rb scripts



23
24
25
26
27
28
29
30
# File 'lib/really/script.rb', line 23

def task(name, options = {}, &block)
  name = name.to_sym
  raise "Duplicate definition of group name '#{name}'." if @tasks.has_key?(name)

  task = Task.new(name, options, &block)
  @tasks[name] = task
  task
end

#via(name, &block) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/really/script.rb', line 45

def via(name, &block)
  name = name.to_s.capitalize
  name = "SSH" if name == "Ssh"
  klass = eval "Really::Drivers::#{name}"

  driver = klass.new &block
  @drivers << driver
  driver
end