Class: Teleport::Main

Inherits:
Object
  • Object
show all
Includes:
Constants, Util
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

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 @options[:cmd]
  when :teleport
    $stderr = $stdout
    teleport
  when :install
    $stderr = $stdout
    install
  when :infer
    infer
  end
end

Instance Method Details

#assemble_tgzObject

Assemble the the tgz before we teleport to the host



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
112
113
114
115
116
117
118
119
# File 'lib/teleport/main.rb', line 84

def assemble_tgz
  banner "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_UPGRADE='#{@options[:upgrade] || false}'")
    f.puts("CONFIG_RECIPE='#{@options[:recipe]}'") if @options[: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
62
63
64
65
66
67
68
# File 'lib/teleport/main.rb', line 27

def cli(cmd)
  @options = { }
  @options[:cmd] = cmd

  opt = OptionParser.new do |o|
    o.banner = "Usage: teleport <hostname>"
    o.on("-i", "--infer", "infer a new Telfile from YOUR machine") do |f|
      @options[:cmd] = :infer
    end
    o.on("-r", "--recipe [RECIPE]", "run a single recipe instead of a full install") do |f|
      @options[:recipe] = f
    end
    o.on("-u", "--upgrade", "upgrade the existing ruby to the version configured") do |f|
      @options[:upgrade] = true
    end
    o.on("-v", "--version", "print the teleport version number") do |f|
      puts "teleport #{VERSION}"
      exit(0)
    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 @options[: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
    @options[:host] = ARGV.shift
  end
end

#inferObject

try to infer a new Telfile based on the current machine



168
169
170
# File 'lib/teleport/main.rb', line 168

def infer
  Infer.new
end

#installObject

We’re running on the host - install!



160
161
162
163
164
165
# File 'lib/teleport/main.rb', line 160

def install
  Dir.chdir(DATA) do
    read_config
  end
  Install.new(@config)
end

#read_configObject

Read Telfile



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/teleport/main.rb', line 71

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 @options[:recipe] && !File.exists?("recipes/#{@options[:recipe]}")
    fatal "I couldn't find recipe #{@options[:recipe].inspect}. Oops."
  end
end

#ssh_tgzObject

Copy the tgz to the host, then run there.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/teleport/main.rb', line 122

def ssh_tgz
  begin
    banner "scp #{TAR} to #{@options[:host]}:#{TAR}..."

    args = []
    args += @config.ssh_options if @config.ssh_options
    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"
          ]
    banner "ssh to #{@options[:host]} and run..."

    args = []
    args += @config.ssh_options if @config.ssh_options
    args << @options[:host]
    args << cmd.join(" && ")
    run("ssh", args)
  rescue RunError
    fatal("Failed!")
  end
  banner "Success!"
end

#teleportObject

Teleport to the host.



153
154
155
156
157
# File 'lib/teleport/main.rb', line 153

def teleport
  read_config
  assemble_tgz
  ssh_tgz
end