Module: RightscaleAutocomplete

Defined in:
lib/rightscale-autocomplete.rb

Constant Summary collapse

BASE_DIR =
File.join(File.expand_path('~'), '.rightscale')
File.join(BASE_DIR, "cookie")
SERVER_CACHE =
File.join(BASE_DIR, "servers")
CREDENTIALS_FILE =
File.join(BASE_DIR, "credentials")
SSH_CONFIG =
File.join(BASE_DIR, "ssh")

Class Method Summary collapse

Class Method Details

.complete(partial_server_name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rightscale-autocomplete.rb', line 21

def self.complete(partial_server_name)
  after_match = partial_server_name.split[1..-1].join(' ')
  server_match = (after_match.empty? || after_match =~ /\s$/) ? nil : after_match.strip
  servers = server_list.split("\n").map { |line| line.split('::').first }
  servers = servers.select { |t| /^#{Regexp.escape server_match}/i =~ t } if server_match

  if server_match =~ /^([-\w:]+:)/
    upto_last_colon = $1
    after_match = $'
    servers = servers.map { |t| (t =~ /^#{Regexp.escape upto_last_colon}([-\w:]+)$/i) ? "#{$1}" : t }
  end

  puts servers
end

.resetObject



36
37
38
39
40
41
42
# File 'lib/rightscale-autocomplete.rb', line 36

def self.reset
  File.delete(COOKIE_CACHE) rescue nil
  File.delete(SERVER_CACHE) rescue nil
  
  authenticate
  get_server_list
end

.setupObject



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/rightscale-autocomplete.rb', line 44

def self.setup
  Dir.mkdir(BASE_DIR) unless File.exists?(BASE_DIR)
  
  credentials = {}
  print "Enter your Rightscale account_id: "
  credentials[:account_id] = STDIN.gets.chomp
  print "Enter your Rightscale username: "
  credentials[:user] = STDIN.gets.chomp
  print "Enter your Rightscale password: "
  credentials[:password] = STDIN.gets.chomp
  
  File.open(CREDENTIALS_FILE, 'w') { |f| f << Marshal.dump(credentials) }
  
  ssh_config = {}
  print "Enter the username to login as (eg root): "
  ssh_config[:user] = STDIN.gets.chomp
  
  print "Enter the location of your ssh key (eg ~/.ssh/ec2_prod_key): "
  ssh_config[:key] = STDIN.gets.chomp
  
  File.open(SSH_CONFIG, 'w') { |f| f << Marshal.dump(ssh_config) }
  
  puts "\nAdd this line to your profile"
  puts "complete -C rs-autocomplete -o default sshrs"
end

.ssh(server) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rightscale-autocomplete.rb', line 8

def self.ssh(server)
  authenticate
  
  ssh_config = Marshal.load(File.read(SSH_CONFIG))
  servers = File.readlines(SERVER_CACHE)
  server_id = servers.grep(/#{server.strip}/).first.split("::").last.strip
  dns = Rightscaler::Manage::Server.find(server_id).get(:settings)['dns_name']
  
  cmd = "ssh #{ssh_config[:user]}@#{dns} -i #{ssh_config[:key]}"
  puts cmd
  exec cmd
end