class Fuggle::Dsl

Public Class Methods

new(tasks, environments) click to toggle source
# 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

Public Instance Methods

compile_template(name) click to toggle source
# File lib/fuggle/dsl.rb, line 44
def compile_template(name)
  @templates[name].call
end
environment(name, &block) click to toggle source
# File lib/fuggle/dsl.rb, line 23
def environment(name, &block)
  @environments[name] = block
end
local(cmd) click to toggle source
# File lib/fuggle/dsl.rb, line 48
def local(cmd)
  run(cmd, 'Local command failed')
end
log(message) click to toggle source
# File lib/fuggle/dsl.rb, line 65
def log(message)
  puts "\e[33m#{message}\e[0m"
end
remote(cmd) click to toggle source
# 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) click to toggle source
# 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 = '') click to toggle source
# 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) click to toggle source
# File lib/fuggle/dsl.rb, line 19
def task(name, &block)
  @tasks[name] = block
end
template(name, &block) click to toggle source
# File lib/fuggle/dsl.rb, line 27
def template(name, &block)
  @templates[name] = block
end

Private Instance Methods

execute_task(name) click to toggle source
# File lib/fuggle/dsl.rb, line 80
def execute_task(name)
  name = name.to_sym
  if @tasks[name].nil?
    Fuggle::System.abort "Unknown task '#{name}'"
  else
    @tasks[name].call
  end
end
load_environment(name) click to toggle source
# File lib/fuggle/dsl.rb, line 71
def load_environment(name)
  name = name.to_sym
  if @environments[name].nil?
    Fuggle::System.abort "Unknown environment '#{name}'"
  else
    @environments[name].call
  end
end
run(cmd, failure_message = 'Command failed') click to toggle source
# File lib/fuggle/dsl.rb, line 89
def run(cmd, failure_message = 'Command failed')
  system cmd
  if $? != 0 # Check command exit status
    Fuggle::System.abort "#{failure_message} '#{cmd}'"
  end
end