Class: Fuggle::Dsl

Inherits:
Object
  • Object
show all
Defined in:
lib/fuggle/dsl.rb

Instance Method Summary collapse

Constructor Details

#initialize(tasks, environments) ⇒ Dsl

Returns a new instance of Dsl.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/fuggle/dsl.rb', line 5

def initialize(tasks, environments)
  @hosts = []
  @ssh_opts = ''

  @tasks = {}
  @environments = {}
  @templates = {}

  [environments, tasks].each { |thing| instance_eval(thing) }

  load_environment(ARGV[0])
  execute_task(ARGV[1])
end

Instance Method Details

#compile_template(name) ⇒ Object



44
45
46
# File 'lib/fuggle/dsl.rb', line 44

def compile_template(name)
  @templates[name].call
end

#environment(name, &block) ⇒ Object



23
24
25
# File 'lib/fuggle/dsl.rb', line 23

def environment(name, &block)
  @environments[name] = block
end

#local(cmd) ⇒ Object



48
49
50
# File 'lib/fuggle/dsl.rb', line 48

def local(cmd)
  run(cmd, 'Local command failed')
end

#log(message) ⇒ Object



65
66
67
# File 'lib/fuggle/dsl.rb', line 65

def log(message)
  puts "\e[33m#{message}\e[0m"
end

#remote(cmd) ⇒ Object



52
53
54
55
56
# File 'lib/fuggle/dsl.rb', line 52

def remote(cmd)
  @hosts.each do |host|
    run("ssh #{@ssh_opts} #{host} \"#{cmd}\"", 'Remote command failed')
  end
end

#remote_template(name, remote_path, sudo = false) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fuggle/dsl.rb', line 31

def remote_template(name, remote_path, sudo = false)
  tmp_file = Tempfile.new('remote_template')
  File.write(tmp_file.path, compile_template(name))
  if sudo
    remote_tmp_file_path = "/tmp/#{File.basename(tmp_file.path)}"
    sync(tmp_file.path, remote_tmp_file_path)
    remote("sudo mv #{remote_tmp_file_path} #{remote_path}")
  else
    sync(tmp_file.path, remote_path)
  end
  tmp_file.unlink
end

#sync(local_path, remote_path, opts = '') ⇒ Object



58
59
60
61
62
63
# File 'lib/fuggle/dsl.rb', line 58

def sync(local_path, remote_path, opts = '')
  @hosts.each do |host|
    cmd = "rsync #{opts} -e \"ssh #{@ssh_opts}\" #{local_path} #{host}:#{remote_path}"
    run(cmd, 'Sync failed')
  end
end

#task(name, &block) ⇒ Object



19
20
21
# File 'lib/fuggle/dsl.rb', line 19

def task(name, &block)
  @tasks[name] = block
end

#template(name, &block) ⇒ Object



27
28
29
# File 'lib/fuggle/dsl.rb', line 27

def template(name, &block)
  @templates[name] = block
end