Class: Machines::Core
- Inherits:
-
Object
- Object
- Machines::Core
- Includes:
- Machines::Commands::Checks, Machines::Commands::Configuration, Machines::Commands::Database, Machines::Commands::FileOperations, Machines::Commands::Installation, Machines::Commands::Questions, Machines::Commands::Services
- Defined in:
- lib/machines/core.rb
Constant Summary
Constants included from Machines::Commands::Installation
Machines::Commands::Installation::APTGET_QUIET
Instance Method Summary collapse
-
#except(options, &block) ⇒ Object
Does not execute the code if $conf parameters match what is given in args.
- #generate_password ⇒ Object
- #load_app_settings(apps) ⇒ Object
-
#matched(options) ⇒ Object
TODO: Move matched into separate class to test.
-
#only(options, &block) ⇒ Object
Only executes the code if $conf parameters match what is given in args.
-
#package(name) ⇒ Object
Loads the Machinesfile or a package.
-
#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 tasks 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.
Methods included from Machines::Commands::Services
Methods included from Machines::Commands::Questions
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
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 , &block yield unless matched() end |
#generate_password ⇒ Object
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 .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 , &block yield if matched() 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 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
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
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
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, = {}, &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 tasks = [name].flatten $conf.commands = [] tasks.each do |name| $conf.tasks[name.to_sym][:block].call end end end |