Class: Machines::Commandline
- Inherits:
-
Object
- Object
- Machines::Commandline
- Defined in:
- lib/machines/commandline.rb
Class Method Summary collapse
-
.execute(options) ⇒ Object
Execute a command (build dryrun generate htpasswd packages override tasks).
Instance Method Summary collapse
-
#build(options) ⇒ Object
Loads Machinesfile, opens an SCP connection and runs all commands and file uploads.
- #dryrun(options) ⇒ Object
- #generate(options) ⇒ Object
- #htpasswd(options) ⇒ Object
- #init ⇒ Object
-
#initialize ⇒ Commandline
constructor
A new instance of Commandline.
- #list(notused) ⇒ Object
- #override(package) ⇒ Object
- #packages(notused) ⇒ Object
- #tasks(options) ⇒ Object
Constructor Details
#initialize ⇒ Commandline
Returns a new instance of Commandline.
4 5 6 |
# File 'lib/machines/commandline.rb', line 4 def initialize @core = Core.new end |
Class Method Details
.execute(options) ⇒ Object
Execute a command (build dryrun generate htpasswd packages override tasks)
9 10 11 12 13 14 15 16 17 |
# File 'lib/machines/commandline.rb', line 9 def self.execute help = Help.new action = .shift if help.actions.include?(action) new.send action.gsub('new', 'generate'), else say help.syntax end end |
Instance Method Details
#build(options) ⇒ Object
Loads Machinesfile, opens an SCP connection and runs all commands and file uploads
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/machines/commandline.rb', line 20 def build $conf.machine_name = .shift return say(Help.new.syntax) unless $conf.machine_name init @core.package 'Machinesfile' @core.task if .any? = {: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
57 58 59 60 |
# File 'lib/machines/commandline.rb', line 57 def dryrun $conf.log_only = true build end |
#generate(options) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/machines/commandline.rb', line 62 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
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/machines/commandline.rb', line 73 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
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/machines/commandline.rb', line 85 def init $exit_requested = false $conf.passwords = [] $conf.commands = [] $conf.tasks = {} $conf.load('config.yml') Command.file ||= Logger.new File.open('log/output.log', 'w') Command.debug ||= Logger.new File.open('log/debug.log', 'w') Command.console ||= Logger.new STDOUT, :truncate => true end |
#list(notused) ⇒ Object
97 98 99 |
# File 'lib/machines/commandline.rb', line 97 def list notused say Help.new.machine_list end |
#override(package) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/machines/commandline.rb', line 114 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
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/machines/commandline.rb', line 101 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(options) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/machines/commandline.rb', line 127 def tasks $conf.machine_name = .shift return say(Help.new.syntax) unless $conf.machine_name $conf.log_only = true init @core.package 'Machinesfile' say 'Tasks' $conf.tasks.each do |task_name, settings| say " %-20s #{settings[:description]}" % task_name end end |