rightscale-autocomplete 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Joshua Bates
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = rightscale-autocomplete
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Joshua Bates. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rightscale-autocomplete"
8
+ gem.summary = %Q{SSH completion for rightscale servers}
9
+ gem.description = %Q{SSH completion for rightscale servers}
10
+ gem.email = "joshuabates@gmail.com"
11
+ gem.homepage = "http://github.com/joshuabates/rightscale-autocomplete"
12
+ gem.authors = ["Joshua Bates"]
13
+ gem.add_development_dependency "rightscaler"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rightscale-autocomplete'
4
+ if ENV['COMP_LINE'].nil?
5
+ RightscaleAutocomplete.send(ARGV.first)
6
+ else
7
+ RightscaleAutocomplete.complete(ENV['COMP_LINE'])
8
+ exit 0
9
+ end
data/bin/sshrs ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rightscale-autocomplete'
4
+ RightscaleAutocomplete.ssh(ARGV.first)
@@ -0,0 +1,121 @@
1
+ module RightscaleAutocomplete
2
+ BASE_DIR = File.join(File.expand_path('~'), '.rightscale')
3
+ COOKIE_CACHE = File.join(BASE_DIR, "cookie")
4
+ SERVER_CACHE = File.join(BASE_DIR, "servers")
5
+ CREDENTIALS_FILE = File.join(BASE_DIR, "credentials")
6
+ SSH_CONFIG = File.join(BASE_DIR, "ssh")
7
+
8
+ def self.ssh(server)
9
+ authenticate
10
+
11
+ ssh_config = Marshal.load(File.read(SSH_CONFIG))
12
+ servers = File.readlines(SERVER_CACHE)
13
+ server_id = servers.grep(/#{server.strip}/).first.split("::").last.strip
14
+ dns = Rightscaler::Manage::Server.find(server_id).get(:settings)['dns_name']
15
+
16
+ cmd = "ssh #{ssh_config[:user]}@#{dns} -i #{ssh_config[:key]}"
17
+ puts cmd
18
+ exec cmd
19
+ end
20
+
21
+ def self.complete(partial_server_name)
22
+ after_match = partial_server_name.split[1..-1].join(' ')
23
+ server_match = (after_match.empty? || after_match =~ /\s$/) ? nil : after_match.strip
24
+ servers = server_list.split("\n").map { |line| line.split('::').first }
25
+ servers = servers.select { |t| /^#{Regexp.escape server_match}/i =~ t } if server_match
26
+
27
+ if server_match =~ /^([-\w:]+:)/
28
+ upto_last_colon = $1
29
+ after_match = $'
30
+ servers = servers.map { |t| (t =~ /^#{Regexp.escape upto_last_colon}([-\w:]+)$/i) ? "#{$1}" : t }
31
+ end
32
+
33
+ puts servers
34
+ end
35
+
36
+ def self.reset
37
+ File.delete(COOKIE_CACHE) rescue nil
38
+ File.delete(SERVER_CACHE) rescue nil
39
+
40
+ authenticate
41
+ get_server_list
42
+ end
43
+
44
+ def self.setup
45
+ Dir.mkdir(BASE_DIR) unless File.exists?(BASE_DIR)
46
+
47
+ credentials = {}
48
+ print "Enter your Rightscale account_id: "
49
+ credentials[:account_id] = STDIN.gets.chomp
50
+ print "Enter your Rightscale username: "
51
+ credentials[:user] = STDIN.gets.chomp
52
+ print "Enter your Rightscale password: "
53
+ credentials[:password] = STDIN.gets.chomp
54
+
55
+ File.open(CREDENTIALS_FILE, 'w') { |f| f << Marshal.dump(credentials) }
56
+
57
+ ssh_config = {}
58
+ print "Enter the username to login as (eg root): "
59
+ ssh_config[:user] = gets.chomp
60
+
61
+ print "Enter the location of your ssh key (eg ~/.ssh/ec2_prod_key): "
62
+ ssh_config[:key] = gets.chomp
63
+
64
+ File.open(SSH_CONFIG, 'w') { |f| f << Marshal.dump(ssh_config) }
65
+
66
+ puts "\nAdd this line to your profile"
67
+ puts "complete -C rs-autocomplete -o default sshrs"
68
+ end
69
+
70
+ private
71
+
72
+ def self.authenticate
73
+ require "rightscaler"
74
+
75
+ credentials = load_credentials
76
+
77
+ Rightscaler::Resource.account_id = credentials[:account_id]
78
+ Rightscaler::Resource.user = credentials[:user]
79
+ Rightscaler::Resource.password = credentials[:password]
80
+
81
+ if cookie = valid_cookie
82
+ Rightscaler::Account.set_auth_cookie(cookie)
83
+ else
84
+ Rightscaler::Account.authenticate
85
+ File.open(COOKIE_CACHE, 'w') { |f| f.puts Rightscaler::Resource.headers['cookie'] }
86
+ end
87
+ end
88
+
89
+ def self.load_credentials
90
+ setup unless File.exists? CREDENTIALS_FILE
91
+ Marshal.load(File.read(CREDENTIALS_FILE))
92
+ end
93
+
94
+ def self.valid_cookie
95
+ return unless File.exists?(COOKIE_CACHE) && File.mtime(COOKIE_CACHE) > (Time.now - 360)
96
+ cookie = File.read(COOKIE_CACHE)
97
+ !cookie.blank? && cookie
98
+ end
99
+
100
+ def self.server_list
101
+ if File.exists?(SERVER_CACHE)
102
+ File.read(SERVER_CACHE)
103
+ else
104
+ get_server_list
105
+ end
106
+ end
107
+
108
+ def self.get_server_list
109
+ authenticate
110
+ deployments = Rightscaler::Manage::Deployment.find(:all)
111
+ servers = deployments.inject([]) do |mem, deployment|
112
+ mem << deployment.servers.map { |s| "#{underscore(deployment.nickname)}:#{underscore(s.nickname)}::#{s.id}"}
113
+ end.join("\n")
114
+ File.open(SERVER_CACHE, 'w') { |f| f.puts servers }
115
+ servers
116
+ end
117
+
118
+ def self.underscore(string)
119
+ string.strip.gsub(/\W+/, '_')
120
+ end
121
+ end
@@ -0,0 +1,57 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rightscale-autocomplete}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Joshua Bates"]
12
+ s.date = %q{2010-03-24}
13
+ s.description = %q{SSH completion for rightscale servers}
14
+ s.email = %q{joshuabates@gmail.com}
15
+ s.executables = ["rs-autocomplete", "sshrs"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ ".gitignore",
23
+ "LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "bin/rs-autocomplete",
28
+ "bin/sshrs",
29
+ "lib/rightscale-autocomplete.rb",
30
+ "rightscale-autocomplete.gemspec",
31
+ "test/helper.rb",
32
+ "test/test_rightscale-autocomplete.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/joshuabates/rightscale-autocomplete}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.6}
38
+ s.summary = %q{SSH completion for rightscale servers}
39
+ s.test_files = [
40
+ "test/helper.rb",
41
+ "test/test_rightscale-autocomplete.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
49
+ s.add_development_dependency(%q<rightscaler>, [">= 0"])
50
+ else
51
+ s.add_dependency(%q<rightscaler>, [">= 0"])
52
+ end
53
+ else
54
+ s.add_dependency(%q<rightscaler>, [">= 0"])
55
+ end
56
+ end
57
+
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'rightscale-autocomplete'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestRightscaleAutocomplete < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rightscale-autocomplete
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Joshua Bates
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-24 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rightscaler
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :development
31
+ version_requirements: *id001
32
+ description: SSH completion for rightscale servers
33
+ email: joshuabates@gmail.com
34
+ executables:
35
+ - rs-autocomplete
36
+ - sshrs
37
+ extensions: []
38
+
39
+ extra_rdoc_files:
40
+ - LICENSE
41
+ - README.rdoc
42
+ files:
43
+ - .document
44
+ - .gitignore
45
+ - LICENSE
46
+ - README.rdoc
47
+ - Rakefile
48
+ - VERSION
49
+ - bin/rs-autocomplete
50
+ - bin/sshrs
51
+ - lib/rightscale-autocomplete.rb
52
+ - rightscale-autocomplete.gemspec
53
+ - test/helper.rb
54
+ - test/test_rightscale-autocomplete.rb
55
+ has_rdoc: true
56
+ homepage: http://github.com/joshuabates/rightscale-autocomplete
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options:
61
+ - --charset=UTF-8
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project:
81
+ rubygems_version: 1.3.6
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: SSH completion for rightscale servers
85
+ test_files:
86
+ - test/helper.rb
87
+ - test/test_rightscale-autocomplete.rb