Module: Machines::Core
- Defined in:
- lib/machines/core.rb
Instance Method Summary collapse
- #command_from_string(commands) ⇒ Object
-
#except(options, &block) ⇒ Object
Does not execute the code if $conf parameters match what is given in args.
- #generate_password ⇒ Object
- #list_tasks ⇒ Object
- #matched(options) ⇒ Object
-
#only(options, &block) ⇒ Object
Only executes the code if $conf parameters match what is given in args.
-
#required_options(options, required) ⇒ Object
Validate some methods that require certain options.
-
#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.
- #store_task(name, description, &block) ⇒ Object
-
#sudo(*commands) ⇒ Object
Queue up command(s) using SUDO to run remotely.
-
#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.
-
#upload(local_source, remote_dest) ⇒ Object
Upload a file or folder using SCP Can be used with sudo or run.
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 , &block yield unless matched() end |
#generate_password ⇒ Object
32 33 34 |
# File 'lib/machines/core.rb', line 32 def generate_password WEBrick::Utils.random_string(20) end |
#list_tasks ⇒ Object
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 .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 , &block yield if matched() 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 required.each do |option| raise ArgumentError, "Missing option '#{option}'. Check trace for location of the problem." unless [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
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
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
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/machines/core.rb', line 9 def task name, description = nil, = {}, &block if block dependencies = [[:if]].flatten return if [: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 |