Class: Machines::Core

Constant Summary

Constants included from Machines::Commands::Installation

Machines::Commands::Installation::APTGET_QUIET

Instance Method Summary collapse

Methods included from Machines::Commands::Services

#restart, #start, #stop

Methods included from Machines::Commands::Questions

#enter_password

Methods included from Machines::Commands::Installation

#add_ppa, #deb, #debconf, #extract, #gem, #gem_update, #git_clone, #install, #uninstall, #update, #upgrade

Methods included from Machines::Commands::FileOperations

#append, #chmod, #chown, #copy, #create_from, #link, #mkdir, #remove, #remove_version_info, #rename, #replace, #write

Methods included from Machines::Commands::Database

#write_database_yml

Methods included from Machines::Commands::Configuration

#add, #add_user, #configure, #del_user

Methods included from Machines::Commands::Checks

#check_command, #check_daemon, #check_dir, #check_file, #check_gem, #check_init_d, #check_link, #check_owner, #check_package, #check_perms, #check_string, #echo_result

Instance Method Details

#except(options, &block) ⇒ Object

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



47
48
49
# File 'lib/machines/core.rb', line 47

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

#generate_passwordObject



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

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

#load_app_settings(apps) ⇒ Object



51
52
53
# File 'lib/machines/core.rb', line 51

def load_app_settings(apps)
  AppSettings.new.load_app_settings(apps)
end

#matched(options) ⇒ Object

TODO: Move matched into separate class to test



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/machines/core.rb', line 115

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



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

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

#package(name) ⇒ Object

Loads the Machinesfile or a package



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/machines/core.rb', line 56

def package name
  if name == 'Machinesfile'
    custom_name = builtin_name = 'Machinesfile'
    error = "Cannot find 'Machinesfile'. Use `machines new` to create a template."
  else
    custom_name = File.join('packages', "#{name}.rb")
    builtin_name = File.join($conf.application_dir, 'packages', "#{name}.rb")
    error = "Cannot find custom or built-in package '#{name}'."
  end
  package = load_and_eval(custom_name) || load_and_eval(builtin_name)
  package || raise(LoadError, error, caller)
end

#required_options(options, required) ⇒ Object

Validate some methods that require certain options



108
109
110
111
112
# File 'lib/machines/core.rb', line 108

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.



72
73
74
75
# File 'lib/machines/core.rb', line 72

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

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



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

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



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

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 tasks so they can be run standalone

Parameters:

  • name (Symbol, String, Array)

    Name of the task or array of task names

  • 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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/machines/core.rb', line 17

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
    tasks = [name].flatten
    $conf.commands = []
    tasks.each do |name|
      $conf.tasks[name.to_sym][:block].call
    end
  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



103
104
105
# File 'lib/machines/core.rb', line 103

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