Module: Machines::Core

Defined in:
lib/machines/core.rb

Instance Method Summary collapse

Instance Method Details

#command_from_string(commands) ⇒ Object



112
113
114
# File 'lib/machines/core.rb', line 112

def command_from_string commands
  commands.first.is_a?(String) ? [Command.new(commands[0], commands[1])] : commands
end

#except(options, &block) ⇒ Object

Does not execute the code if $conf parameters match what is given in args



42
43
44
# File 'lib/machines/core.rb', line 42

def except options, &block
  yield unless matched(options)
end

#generate_passwordObject



32
33
34
# File 'lib/machines/core.rb', line 32

def generate_password
  WEBrick::Utils.random_string(20)
end

#list_tasksObject



26
27
28
29
30
# File 'lib/machines/core.rb', line 26

def list_tasks
  $conf.tasks.each do |name, task|
    say "  #{"%-20s" % name}#{task[:description]}"
  end
end

#matched(options) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/machines/core.rb', line 46

def matched options
  options.each do |key, value|
    value = value.is_a?(Array) ? value.map{|o| o.to_s } : value.to_s
    if $conf[key].is_a?(Array)
      values = $conf[key].map{|o| o.to_s }
      if value.is_a?(Array)
        return unless values.reject{ |symbol| !value.include?(symbol.to_s) }.any?
      else
        return unless values.include?(value)
      end
    else
      if value.is_a?(Array)
        return unless value.include?($conf[key].to_s)
      else
        return unless value == $conf[key].to_s
      end
    end
  end
  true
end

#only(options, &block) ⇒ Object

Only executes the code if $conf parameters match what is given in args



37
38
39
# File 'lib/machines/core.rb', line 37

def only options, &block
  yield if matched(options)
end

#required_options(options, required) ⇒ Object

Validate some methods that require certain options



106
107
108
109
110
# File 'lib/machines/core.rb', line 106

def required_options options, required
  required.each do |option|
    raise ArgumentError, "Missing option '#{option}'. Check trace for location of the problem." unless options[option]
  end
end

#run(*commands) ⇒ Object

Queue up command(s) to run remotely If first command is a string it creates a Command object using the first two strings as command and check

Parameters:

  • commands (Array)

    Command(s) to run.



70
71
72
73
# File 'lib/machines/core.rb', line 70

def run *commands
  commands = command_from_string(commands)
  $conf.commands += commands.flatten
end

#store_task(name, description, &block) ⇒ Object



22
23
24
# File 'lib/machines/core.rb', line 22

def store_task name, description, &block
  $conf.tasks[name] = {:description => description, :block => block}
end

#sudo(*commands) ⇒ Object

Queue up command(s) using SUDO to run remotely

Parameters:

  • commands (Array)

    Command(s) to run



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/machines/core.rb', line 77

def sudo *commands
  commands = command_from_string commands
  commands.flatten.each do |command|
    if command.is_a?(Upload)
      temp_path = "/tmp/#{File.basename(command.remote)}"
      dir_suffix = command.local.is_a?(String) && File.directory?(command.local) ? '/.' : ''
      remote_dest = command.remote
      command.remote = temp_path
      command.check = check_file(temp_path)
      run command
      sudo copy(temp_path + dir_suffix, remote_dest)
      run remove temp_path
    else
      command.use_sudo
      run command
    end
  end
end

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

If a block is given, store the task, log it and run it If no block is given, sets commands to only those of the specified task so they can be run standalone

Parameters:

  • name (Symbol)

    Name of the task

  • description (String) (defaults to: nil)

    Describe the task

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :if (Symbol, Array)

    Dependent tasks that must already have been added for this task to be added



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

def task name, description = nil, options = {}, &block
  if block
    dependencies = [options[:if]].flatten
    return if options[:if] && (dependencies - $conf.tasks.keys).any?
    store_task name, description, &block
    $conf.commands << LogCommand.new(name, description)
    yield
  else
    $conf.commands = []
    $conf.tasks[name][:block].call
  end
end

#upload(local_source, remote_dest) ⇒ Object

Upload a file or folder using SCP Can be used with sudo or run

Parameters:

  • local_source (String)

    File or folder on the local machine

  • remote_dest (String)

    Folder on the remote machine to copy to upload 'source_dir', '~' #=> creates source_dir/subdir as ~/subdir



101
102
103
# File 'lib/machines/core.rb', line 101

def upload local_source, remote_dest
  Upload.new(local_source, remote_dest, check_file(remote_dest))
end