scalarium 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in sshalarium.gemspec
4
+ gemspec
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+
4
+ task :run do
5
+ exec "bundle exec ruby -I lib bin/scalarium #{ARGV[1..-1]}"
6
+ end
7
+
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'scalarium'
4
+ require 'scalarium/cli'
5
+
6
+ Scalarium::CLI.start
@@ -0,0 +1,68 @@
1
+ require "scalarium/version"
2
+ require 'rest-client'
3
+ require 'json'
4
+ require 'dispatch_queue'
5
+ require 'ostruct'
6
+
7
+ class Scalarium
8
+ class Instance < OpenStruct ; end
9
+ class Rol < OpenStruct ; end
10
+ class Cloud < OpenStruct ; end
11
+
12
+ attr_reader :clouds
13
+ def initialize(token, cloud = nil)
14
+ @token = token
15
+ @clouds = []
16
+
17
+ @clouds = get('clouds').map{|c| Cloud.new(c) }
18
+
19
+ return if cloud == false
20
+
21
+ if cloud != nil
22
+ filtered_clouds = @clouds.select do |c|
23
+ (c.name =~ /#{cloud}/i) || c.name.downcase.include?(cloud.downcase)
24
+ end
25
+ @clouds = filtered_clouds || []
26
+ end
27
+ DQ[*(@clouds.map { |cloud| lambda { process_cloud(cloud) } })]
28
+ end
29
+
30
+ private
31
+
32
+ def get(resource)
33
+ $stderr.puts "Getting #{resource}" if $DEBUG
34
+ url = "https://manage.scalarium.com/api/#{resource}"
35
+ headers = {
36
+ 'X-Scalarium-Token' => @token,
37
+ 'Accept' => 'application/vnd.scalarium-v1+json'
38
+ }
39
+ response = RestClient.get(url, headers)
40
+ JSON.parse(response)
41
+ end
42
+
43
+ def get_roles_proc(cloud_id)
44
+ lambda do
45
+ get("clouds/#{cloud_id}/roles").map { |hash|
46
+ Rol.new(hash)
47
+ }
48
+ end
49
+ end
50
+
51
+ def get_instances_proc(cloud_id)
52
+ lambda do
53
+ get("clouds/#{cloud_id}/instances").map { |hash|
54
+ Instance.new(hash)
55
+ }
56
+ end
57
+ end
58
+
59
+ def process_cloud(cloud)
60
+ roles, instances = DQ[ get_roles_proc(cloud.id) , get_instances_proc(cloud.id) ]
61
+ cloud.roles = roles
62
+ cloud.instances = instances
63
+ roles.each do |rol|
64
+ rol.instances = instances.select{|instance| instance.role_ids.include?(rol.id)}
65
+ end
66
+ end
67
+ end
68
+
@@ -0,0 +1,113 @@
1
+ require 'thor'
2
+
3
+ trap("INT"){ exit -1 }
4
+
5
+ class Scalarium
6
+ class CLI < Thor
7
+ include Thor::Actions
8
+ desc 'update_sshconfig', "Create ssh hosts for your infraestructure in ~/.ssh/config"
9
+ method_option :cloud, :aliases => "-c", :desc => "only for clouds matchin this regexp"
10
+ method_option :certificate, :aliases => "-i", :desc => "specify alternate certificate for login"
11
+ def update_sshconfig
12
+ capture_exceptions do
13
+ scalarium = ::Scalarium.new( get_token, options[:cloud] )
14
+
15
+ config_file = File.expand_path("~/.ssh/config")
16
+ header = "# Added by scalarium"
17
+ tail = "# Do not modify"
18
+ if File.exists?(config_file)
19
+ config = File.read(config_file)
20
+ config.gsub!(/#{header}.*#{tail}/mi,'')
21
+ else
22
+ config = ""
23
+ end
24
+
25
+ config << header << "\n"
26
+ scalarium.clouds.each do |cloud|
27
+ say "Adding hosts from #{cloud.name}"
28
+ cloud.instances.each do |instance|
29
+ config << format_ssh_config_host(instance)
30
+ end
31
+ end
32
+ config << "\n" << tail << "\n"
33
+
34
+ File.open(config_file,'w'){|f|
35
+ f.rewind
36
+ f.write config
37
+ }
38
+ say("Configuration file updated")
39
+ end
40
+
41
+
42
+
43
+ end
44
+
45
+ desc 'inspect', "Show Clouds, Roles and Instance names"
46
+ method_option :cloud, :aliases => "-c", :desc => "only for clouds matchin this regexp"
47
+ def inspect
48
+ capture_exceptions do
49
+ scalarium = ::Scalarium.new( get_token, options[:cloud] )
50
+
51
+ scalarium.clouds.each do |cloud|
52
+ print_cloud(cloud)
53
+ end
54
+ end
55
+ end
56
+
57
+ desc 'clouds', "List the clouds"
58
+ def clouds
59
+ capture_exceptions do
60
+ scalarium = ::Scalarium.new( get_token, false )
61
+ scalarium.clouds.each do |cloud|
62
+ puts cloud.name
63
+ end
64
+ end
65
+ end
66
+
67
+
68
+ protected
69
+
70
+ def format_ssh_config_host(instance)
71
+ return "" if instance.ip.to_s.strip.empty?
72
+ host = "\nHost #{instance.nickname}\n" << " Hostname #{instance.ip}\n"
73
+ host << " IdentityFile #{options[:certificate]}" if options[:certificate]
74
+ host
75
+ end
76
+
77
+ def capture_exceptions
78
+ yield
79
+ rescue ::RestClient::Unauthorized
80
+ say("The token is not valid", Color::RED)
81
+ File.unlink token_path
82
+ retry
83
+ rescue ::OpenSSL::SSL::SSLError
84
+ say("There were problems with ssl. Pleas retry")
85
+ exit -2
86
+ end
87
+
88
+ def get_token
89
+ if File.exists?(token_path)
90
+ token = File.read(token_path).strip
91
+ else
92
+ token = ask("I need your token:").strip
93
+ File.open(token_path, 'w'){|f| f.write token }
94
+ end
95
+ token
96
+ end
97
+
98
+ def token_path
99
+ File.expand_path("~/.scalarium_token")
100
+ end
101
+
102
+ def print_cloud(cloud)
103
+ say "#{cloud.name} (#{cloud.id[0..6]})", Color::GREEN
104
+ cloud.roles.each do |rol|
105
+ say " #{rol.name}", Color::BLUE
106
+ rol.instances.each do |instance|
107
+ puts " #{instance.nickname} (#{instance.ip})"
108
+ end
109
+ end
110
+ end
111
+
112
+ end
113
+ end
@@ -0,0 +1,3 @@
1
+ class Scalarium
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "scalarium/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "scalarium"
7
+ s.version = Scalarium::VERSION
8
+ s.authors = ["Guillermo Álvarez"]
9
+ s.email = ["guillermo@cientifico.net"]
10
+ s.homepage = ""
11
+ s.summary = %q{Scalarium console access}
12
+ s.description = %q{Access your scalarium clouds from console}
13
+
14
+ s.rubyforge_project = "scalarium"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_runtime_dependency 'rest-client'
22
+ s.add_runtime_dependency 'dispatch_queue'
23
+ s.add_runtime_dependency 'thor'
24
+ s.add_development_dependency 'rake'
25
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scalarium
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Guillermo Álvarez
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-09 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rest-client
16
+ requirement: &2160501020 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2160501020
25
+ - !ruby/object:Gem::Dependency
26
+ name: dispatch_queue
27
+ requirement: &2160500600 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2160500600
36
+ - !ruby/object:Gem::Dependency
37
+ name: thor
38
+ requirement: &2160500180 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *2160500180
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: &2160499760 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *2160499760
58
+ description: Access your scalarium clouds from console
59
+ email:
60
+ - guillermo@cientifico.net
61
+ executables:
62
+ - scalarium
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - .gitignore
67
+ - Gemfile
68
+ - Rakefile
69
+ - bin/scalarium
70
+ - lib/scalarium.rb
71
+ - lib/scalarium/cli.rb
72
+ - lib/scalarium/version.rb
73
+ - scalarium.gemspec
74
+ homepage: ''
75
+ licenses: []
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project: scalarium
94
+ rubygems_version: 1.8.6
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: Scalarium console access
98
+ test_files: []