Module: Machines::Commandline
- Defined in:
- lib/machines/commandline.rb
Instance Method Summary collapse
-
#build(options) ⇒ Object
Loads Machinesfile, opens an SCP connection and runs all commands and file uploads.
- #dryrun(options) ⇒ Object
-
#execute(options) ⇒ Object
Execute a given command e.g.
- #generate(options) ⇒ Object
- #htpasswd(options) ⇒ Object
- #init ⇒ Object
- #load_machinesfile ⇒ Object
- #override(package) ⇒ Object
- #packages(notused) ⇒ Object
- #tasks ⇒ Object
Instance Method Details
#build(options) ⇒ Object
Loads Machinesfile, opens an SCP connection and runs all commands and file uploads
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/machines/commandline.rb', line 4 def build $conf.machine_name = .shift $conf.task = .shift init load_machinesfile task $conf.task.to_sym if $conf.task = {:paranoid => false} if $conf.machine.cloud username = $conf.machine.cloud.username [:keys] = [$conf.machine.cloud.private_key_path] else username = $conf.machine.user [:password] = $conf.password end if $conf.log_only $conf.commands.each do |command| command.run end else Kernel.trap("INT") { prepare_to_exit } begin Command.ssh = Net::SSH.start $conf.machine.address, username, Command.scp = Net::SCP.new(Command.ssh) $conf.commands.each do |command| command.run Command.file.flush exit if $exit_requested end ensure Command.ssh.close end end end |
#dryrun(options) ⇒ Object
41 42 43 44 |
# File 'lib/machines/commandline.rb', line 41 def dryrun $conf.log_only = true build end |
#execute(options) ⇒ Object
Execute a given command e.g. dryrun, build, generate, htpasswd, packages, override, tasks
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/machines/commandline.rb', line 47 def execute() help = Help.new action = .shift if help.actions.include?(action) action = 'generate' if action == 'new' send action, else say help.syntax end end |
#generate(options) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/machines/commandline.rb', line 58 def generate dir = .first || './' if File.exists? dir confirm = ask "Overwrite '#{dir}' (y/n)? " return unless confirm.downcase == 'y' end FileUtils.cp_r(File.join($conf.application_dir, 'template', '/.'), dir) FileUtils.mkdir_p(File.join(dir, 'packages')) say "Project created at #{dir}/" end |
#htpasswd(options) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/machines/commandline.rb', line 69 def htpasswd path = File.join($conf.webserver, 'htpasswd') say "Generate BasicAuth password and add to #{path}" username = ask('Username: ') password = enter_password 'users' crypted_pass = password.crypt(WEBrick::Utils.random_string(2)) FileUtils.mkdir_p File.dirname(path) File.open(path, 'a') {|file| file.puts "#{username}:#{crypted_pass}" } say "Password encrypted and added to #{path}" end |
#init ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/machines/commandline.rb', line 81 def init $exit_requested = false $conf.passwords = [] $conf.commands = [] $conf.tasks = {} $conf.load('config.yml') Command.file ||= Machines::Logger.new File.open('log/output.log', 'w') Command.debug ||= Machines::Logger.new File.open('log/debug.log', 'w') Command.console ||= Machines::Logger.new STDOUT, :truncate => true end |
#load_machinesfile ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/machines/commandline.rb', line 93 def load_machinesfile eval File.read('Machinesfile'), nil, "eval: Machinesfile" rescue LoadError => e if e. =~ /Machinesfile/ raise LoadError, "Machinesfile does not exist. Use `machines new <DIR>` to create a template." else raise end end |
#override(package) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/machines/commandline.rb', line 116 def override package package = package.first destination = File.join('packages', "#{package}.rb") answer = File.exists?(destination) ? ask('Project package already exists. Overwrite? (y/n)') : 'y' if answer == 'y' source = File.join($conf.application_dir, 'packages', "#{package}.rb") FileUtils.cp(source, destination) say "Package copied to #{destination}" else say 'Aborted.' end end |
#packages(notused) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/machines/commandline.rb', line 103 def packages notused say 'Default packages' Dir[File.join($conf.application_dir, 'packages', '**/*.rb')].each do |package| say " * #{File.basename(package, '.rb')}" end say '' say 'Project packages' Dir[File.join('packages', '**/*.rb')].each do |package| say " * #{File.basename(package, '.rb')}" end end |
#tasks ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'lib/machines/commandline.rb', line 129 def tasks $conf.log_only = true init load_machinesfile say 'Tasks' $conf.tasks.each do |task_name, settings| say " %-20s #{settings[:description]}" % task_name end end |