Class: Pairhost::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/pairhost.rb

Instance Method Summary collapse

Instance Method Details

#attach(instance_id) ⇒ Object



229
230
231
232
233
# File 'lib/pairhost.rb', line 229

def attach(instance_id)
  invoke :verify, []
  Pairhost.write_instance_id(instance_id)
  invoke :status, []
end

#browse(port = 80) ⇒ Object



243
244
245
246
247
# File 'lib/pairhost.rb', line 243

def browse(port=80)
  require 'launchy'
  puts 'Launching browser...'
  Launchy.open("http://#{Pairhost.fetch.dns_name}:#{port}")
end

#create(name = nil) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/pairhost.rb', line 116

def create(name=nil)
  invoke :verify, []

  if name == nil
    initials = `git config user.initials`.chomp.split("/").map(&:upcase).join(" ")
    name = "Pairhost (#{initials})"
  end

  puts "Provisioning \"#{name}\"..."
  server = Pairhost.create(name)
  puts "provisioned!"
  invoke :status, []
end

#destroyObject



198
199
200
201
202
203
204
205
206
207
208
# File 'lib/pairhost.rb', line 198

def destroy
  invoke :verify
  server = Pairhost.fetch!
  confirm = ask("Type 'yes' to confirm deleting '#{server.tags['Name']}'.\n>")
  return unless confirm == "yes"

  puts "Destroying..."
  server.destroy
  server.wait_for { state == "terminated" }
  puts "destroyed!"
end

#detachObject



236
237
238
239
240
# File 'lib/pairhost.rb', line 236

def detach
  invoke :verify
  # TODO implement
  puts "Coming soon..."
end

#initObject



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/pairhost.rb', line 101

def init
  if File.exists?(Pairhost.config_file)
    STDERR.puts "pairhost: Already initialized."
  else
    puts "Creating ~/.pairhost directory"
    FileUtils.mkdir_p File.dirname(Pairhost.config_file)

    puts "Copying example.yml file to ~/.pairhost directory"
    FileUtils.cp(File.dirname(__FILE__) + '/../config.example.yml', Pairhost.config_file)

    puts "Edit ~/.pairhost/config.yml to use your real EC2 & AMI settings"
  end
end

#listObject



211
212
213
214
215
216
217
218
219
# File 'lib/pairhost.rb', line 211

def list
  require 'hirb'
  Hirb.enable

  puts Hirb::Helpers::AutoTable.render Pairhost.connection.servers,
    :headers => {:tags => 'name', :flavor_id => 'type' },
    :fields => [:tags, :id, :state, :flavor_id, :created_at, :image_id, :dns_name],
      :filters => {:tags => lambda {|e| e['Name'] || 'No Name' } }
end

#provisionObject



222
223
224
225
226
# File 'lib/pairhost.rb', line 222

def provision
  invoke :verify
  # TODO implement
  puts "Coming soon..."
end

#resumeObject



133
134
135
136
137
138
139
140
141
# File 'lib/pairhost.rb', line 133

def resume
  invoke :verify
  server = Pairhost.fetch!
  puts "Starting..."
  Pairhost.start(server.reload)
  puts "started!"

  invoke :status
end

#sshObject



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/pairhost.rb', line 168

def ssh
  invoke :verify
  server = Pairhost.fetch!

  abort "Server is currently #{server.state}" if ["stopping"].include? server.state

  if ["pending", "stopped"].include? server.state
    invoke :up
    server.wait_for { sshable? }
  end

  exec "ssh -A -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=QUIET pair@#{server.dns_name}"
end

#statusObject



156
157
158
159
160
161
162
163
164
165
# File 'lib/pairhost.rb', line 156

def status
  invoke :verify
  server = Pairhost.fetch!
  puts "#{server.id}: #{server.tags['Name']}"
  puts "State: #{server.state}"
  if server.dns_name
    puts server.dns_name
    puts Socket.getaddrinfo(server.dns_name, "http").first[3]
  end
end

#stopObject



187
188
189
190
191
192
193
# File 'lib/pairhost.rb', line 187

def stop
  invoke :verify
  server = Pairhost.fetch!
  puts "Shutting down..."
  Pairhost.stop(server)
  puts "shutdown!"
end

#upObject



144
145
146
147
148
149
150
151
152
153
# File 'lib/pairhost.rb', line 144

def up
  invoke :verify
  server = Pairhost.fetch

  if server
    invoke :resume
  else
    invoke :create
  end
end

#verifyObject



96
97
98
# File 'lib/pairhost.rb', line 96

def verify
  Pairhost.config
end