Class: Shaddox::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/shaddox/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doxfile) ⇒ Config

Returns a new instance of Config.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/shaddox/config.rb', line 4

def initialize(doxfile)
  doxfile = './Doxfile' unless doxfile
  if !File.exists?(doxfile)
    puts "Doxfile could not be found.".red
    exit(1)
  end

  @servers = Hash.new
  @targets = Hash.new {|hsh, key| @servers[key]}  # Fall back on @servers hash for missing targets
  @tasks = Hash.new
  @repos = Hash.new

  # :local and :localhost point to local by default
  @targets[:localhost] = Localhost.new
  @targets[:local] = :localhost

  instance_eval(File.read(doxfile), doxfile)
end

Instance Attribute Details

#reposObject

Returns the value of attribute repos.



3
4
5
# File 'lib/shaddox/config.rb', line 3

def repos
  @repos
end

#serversObject

Returns the value of attribute servers.



3
4
5
# File 'lib/shaddox/config.rb', line 3

def servers
  @servers
end

#targetsObject

Returns the value of attribute targets.



3
4
5
# File 'lib/shaddox/config.rb', line 3

def targets
  @targets
end

#tasksObject

Returns the value of attribute tasks.



3
4
5
# File 'lib/shaddox/config.rb', line 3

def tasks
  @tasks
end

Instance Method Details

#explode_target(target_key) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/shaddox/config.rb', line 23

def explode_target(target_key)
  exploded = []
  [@targets[target_key]].flatten.each do |target|
    if target.is_a? Symbol
      exploded += explode_target(target)
    else
      exploded.push(target)
    end
  end
  exploded
end

#invoke(task_key, target_key, opts = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/shaddox/config.rb', line 35

def invoke(task_key, target_key, opts = {})
  explode_target(target_key).each do |target|
    raise "The target :#{target_key} could not be found. Please check your Doxfile.".red unless target
    info "Deploying to #{target_key}..."
    begin
      script_opts = {}
      script_opts[:installer] = target.installer if target.respond_to? :installer
      script = ShadowScript.new(self, task_key, script_opts)
      target.deploy(script, opts)
      info "Provisioning on :#{target_key} complete.".green
    rescue TargetError => e
      err "Provisioning on :#{target_key} failed:".red
      puts e.message.red
    rescue => e
      err "Provisioning on :#{target_key} failed:".red
      puts e.message.red
      e.backtrace.each { |line| puts line }
    end
  end
end

#repo(key, info) ⇒ Object

### Add a repo info: A hash containing the repo’s info. Allowed keys: :repository (required) :branch



80
81
82
# File 'lib/shaddox/config.rb', line 80

def repo(key, info)
  @repos[key] = Repo.new(info)
end

#server(key, info) ⇒ Object

### Add a server info: A hash containing the server’s info. Allowed keys: :address (required) :user :port :identity_file :ssh_options



65
66
67
# File 'lib/shaddox/config.rb', line 65

def server(key, info)
  @servers[key] = Server.new(info)
end

#target(key, linked_target) ⇒ Object

### Add a target linked_target: A symbol or Array of symbols representing the other targets/servers that this target invokes



72
73
74
# File 'lib/shaddox/config.rb', line 72

def target(key, linked_target)
  @targets[key] = linked_target
end

#task(arg, &block) ⇒ Object

### Add a package blk: A block of code to be executed in the context of the target specified when running Shaddox key can be bound to a list to define dependencies, like with Rake task :example => :some_dep do … task :example => [:dep1, :dep2] do …



90
91
92
93
94
95
96
97
98
99
# File 'lib/shaddox/config.rb', line 90

def task(arg, &block)
  if arg.is_a? Hash
    fail "Task Argument Error" if arg.size != 1
    key, deps = arg.map { |k, v| [k, v] }.first
    @tasks[key] = Task.new(block, deps)
  else
    fail "Task Argument Error" if !arg.is_a? Symbol
    @tasks[arg] = Task.new(block, [])
  end
end