Class: Teleport::Main
- Inherits:
-
Object
- Object
- Teleport::Main
- Defined in:
- lib/teleport/main.rb
Overview
The main class for the teleport command line.
Constant Summary collapse
- TAR =
"#{DIR}.tgz"
Constants included from Util
Util::BLUE, Util::CYAN, Util::GREEN, Util::MAGENTA, Util::RED, Util::RESET, Util::YELLOW
Constants included from Constants
Constants::DATA, Constants::DIR, Constants::FILES, Constants::GEM, Constants::PUBKEY, Constants::RUBYGEMS
Instance Method Summary collapse
-
#assemble_tgz ⇒ Object
Assemble the the tgz before we teleport to the host.
-
#cli(cmd) ⇒ Object
Parse ARGV.
-
#infer ⇒ Object
try to infer a new Telfile based on the current machine.
-
#initialize(cmd = :teleport) ⇒ Main
constructor
A new instance of Main.
-
#install ⇒ Object
We’re running on the host - install!.
-
#read_config ⇒ Object
Read Telfile.
-
#ssh_tgz ⇒ Object
Copy the tgz to the host, then run there.
-
#teleport ⇒ Object
Teleport to the host.
Methods included from Util
#banner, #chmod, #chown, #copy_metadata, #copy_perms, #cp, #cp_if_necessary, #cp_with_mkdir, #different?, #fails?, #fatal, #gem_if_necessary, #gem_version, #ln, #ln_if_necessary, #md5sum, #mkdir, #mkdir_if_necessary, #mv, #mv_with_mkdir, #package_if_necessary, #package_is_installed?, #process_by_pid?, #rm, #rm_and_mkdir, #rm_if_necessary, #run, #run_capture, #run_capture_lines, #run_quietly, #run_verbose!, #shell, #shell_escape, #succeeds?, #warning, #whoami
Constructor Details
#initialize(cmd = :teleport) ⇒ Main
Returns a new instance of Main.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/teleport/main.rb', line 11 def initialize(cmd = :teleport) cli(cmd) case [:cmd] when :teleport $stderr = $stdout teleport when :install $stderr = $stdout install when :infer infer end end |
Instance Method Details
#assemble_tgz ⇒ Object
Assemble the the tgz before we teleport to the host
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/teleport/main.rb', line 77 def assemble_tgz "Assembling #{TAR}..." rm_and_mkdir(DIR) # gem run("cp", ["-r", "#{File.dirname(__FILE__)}/../../lib", GEM]) # data mkdir(DATA) copy = [] copy << "Telfile" copy += Dir["files*"] copy << "recipes" if File.exists?("recipes") copy.sort.each { |i| run("cp", ["-r", i, DATA]) } # config.sh File.open("#{DIR}/config", "w") do |f| f.puts("CONFIG_HOST='#{@options[:host]}'") f.puts("CONFIG_RUBY='#{@config.ruby}'") f.puts("CONFIG_RUBYGEMS='#{RUBYGEMS}'") f.puts("CONFIG_RECIPE='#{@options[:recipe]}'") if [:recipe] end # keys if ssh_key = @config.ssh_key if !File.exists?(ssh_key) fatal("I can't find the specified ssh key: #{ssh_key}") end end ssh_key ||= "#{ENV["HOME"]}/.ssh/#{PUBKEY}" if File.exists?(ssh_key) run("cp", [ssh_key, "#{DIR}/#{PUBKEY}"]) end Dir.chdir(File.dirname(DIR)) do run("tar", ["cfpz", TAR, File.basename(DIR)]) end end |
#cli(cmd) ⇒ Object
Parse ARGV.
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 56 57 58 59 60 61 |
# File 'lib/teleport/main.rb', line 27 def cli(cmd) = { } [:cmd] = cmd opt = OptionParser.new do |o| o. = "Usage: teleport <hostname>" o.on("-i", "--infer", "infer a new Telfile from YOUR machine") do |f| [:cmd] = :infer end o.on("-r", "--recipe [RECIPE]", "run a single recipe instead of a full install") do |f| [:recipe] = f end o.on_tail("-h", "--help", "print this help text") do puts opt exit(0) end end begin opt.parse! rescue OptionParser::InvalidOption, OptionParser::MissingArgument puts $! puts opt exit(1) end if [:cmd] == :teleport # print this error message early, to give the user a hint # instead of complaining about command line arguments if ARGV.length != 1 puts opt exit(1) end [:host] = ARGV.shift end end |
#infer ⇒ Object
try to infer a new Telfile based on the current machine
160 161 162 |
# File 'lib/teleport/main.rb', line 160 def infer Infer.new end |
#install ⇒ Object
We’re running on the host - install!
152 153 154 155 156 157 |
# File 'lib/teleport/main.rb', line 152 def install Dir.chdir(DATA) do read_config end Install.new(@config) end |
#read_config ⇒ Object
Read Telfile
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/teleport/main.rb', line 64 def read_config if !File.exists?("Telfile") fatal("Sadly, I can't find Telfile here. Please create one.") end @config = Config.new("Telfile") # check if recipe exists if [:recipe] && !File.exists?("recipes/#{@options[:recipe]}") fatal "I couldn't find recipe #{@options[:recipe].inspect}. Oops." end end |
#ssh_tgz ⇒ Object
Copy the tgz to the host, then run there.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/teleport/main.rb', line 114 def ssh_tgz begin "scp #{TAR} to #{@options[:host]}:#{TAR}..." args = [] args += @config. if @config. args << TAR args << "#{@options[:host]}:#{TAR}" run("scp", args) cmd = [ "cd /tmp", "(sudo -n echo gub > /dev/null 2> /dev/null || (echo `whoami` could not sudo. && exit 1))", "sudo rm -rf #{DIR}", "sudo tar xmfpz #{TAR}", "sudo #{DIR}/gem/teleport/run.sh" ] "ssh to #{@options[:host]} and run..." args = [] args += @config. if @config. args << [:host] args << cmd.join(" && ") run("ssh", args) rescue RunError fatal("Failed!") end "Success!" end |
#teleport ⇒ Object
Teleport to the host.
145 146 147 148 149 |
# File 'lib/teleport/main.rb', line 145 def teleport read_config assemble_tgz ssh_tgz end |