mobingi 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mocloud.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 David Siaw (via Travis CI)
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,23 @@
1
+ # Mobingi Command Line Interface
2
+
3
+ This gem provides a command line interface to Mobingi.
4
+
5
+ ## Installation
6
+
7
+ Simply install the gem by running:
8
+
9
+ $ gem install mobingi
10
+
11
+ ## Usage
12
+
13
+ You can find help on the tool by typing
14
+
15
+ $ mobingi --help
16
+
17
+ ## Contributing
18
+
19
+ 1. Fork it ( https://github.com/mobingilabs/mobingi-cli/fork )
20
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
21
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
22
+ 4. Push to the branch (`git push origin my-new-feature`)
23
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env ruby
2
+ require 'trollop'
3
+
4
+ require "mocloud/utils"
5
+ require "mocloud/commands"
6
+
7
+ command_constants = Mocloud::Commands.constants.
8
+ select { |c| Mocloud::Commands.const_get(c).is_a? Class }
9
+
10
+ SUB_COMMANDS = command_constants.map { |c| c.to_s.downcase }
11
+
12
+ commandInfos = command_constants.
13
+ map do |c|
14
+ cmdclass = Mocloud::Commands.const_get(c)
15
+ instance = cmdclass.new
16
+ {
17
+ :class => cmdclass,
18
+ :name => c.to_s.downcase,
19
+ :desc => instance.description
20
+ }
21
+ end
22
+
23
+ commands = {}
24
+
25
+ commandString = ""
26
+ commandInfos.each do |cmd|
27
+ commandString += "\n #{cmd[:name]}\t#{cmd[:desc]}"
28
+ commands[cmd[:name]] = cmd
29
+ end
30
+
31
+ mocloudbanner = <<-ENDOFBANNER
32
+ Usage:
33
+ mobingi <command> [options]
34
+
35
+ Available commands are:
36
+ #{commandString}
37
+
38
+ Type mobingi <command> --help to learn more about the command.
39
+ ENDOFBANNER
40
+
41
+ global_opts = Trollop::options do
42
+ version "mobingi v1.0 (c) 2015 Mobingi"
43
+ banner "#{mocloudbanner}\nOptions:"
44
+ stop_on SUB_COMMANDS
45
+ end
46
+
47
+ cmd = ARGV.shift # get the subcommand
48
+
49
+ if commands.has_key?(cmd)
50
+ command_instance = commands[cmd][:class].new
51
+ command_instance.run
52
+ else
53
+ Trollop::die "Unknown subcommand #{cmd.inspect}"
54
+ end
@@ -0,0 +1,5 @@
1
+ require "mocloud/version"
2
+
3
+ module Mocloud
4
+
5
+ end
@@ -0,0 +1,24 @@
1
+ require 'mocloud/utils'
2
+
3
+ module Mocloud
4
+ class API
5
+
6
+ def get_unauthed(endpoint, resource, query={})
7
+ Mocloud::Utils.http_get("https://#{endpoint}#{resource}", query)
8
+ end
9
+
10
+ def get(resource, query={})
11
+
12
+ creds = Mocloud::Utils::Credentials.new
13
+ time = Time.now.to_i
14
+ user = creds.read_credentials()
15
+ token = creds.make_token(time)
16
+
17
+ query[:token] = token
18
+ query[:time] = time
19
+
20
+ Mocloud::Utils.http_get("https://#{user['endpoint']}#{resource}", query)
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ require 'require_all'
2
+
3
+ Gem.find_files("mocloud/commands/*.rb").each { |path| require path }
@@ -0,0 +1,76 @@
1
+ module Mocloud
2
+ module Commands
3
+
4
+ class Instances
5
+ def description
6
+ "Show instances in a stack"
7
+ end
8
+
9
+ def run
10
+
11
+
12
+ opts = Trollop::options do
13
+ banner <<-ENDOFHELP
14
+ Usage: mobingi instances <stack name> [options]"
15
+
16
+ Displays the instances in a specified stack.
17
+ ENDOFHELP
18
+ end
19
+
20
+ stackname = ARGV.shift
21
+
22
+ if !stackname or stackname.length == 0
23
+ puts "Please specify a stack. Type 'mobingi stacks' to see your stacks"
24
+ exit!
25
+ end
26
+
27
+ creds = Mocloud::Utils::Credentials.new
28
+ user = creds.read_credentials()
29
+ token = creds.make_token(0)
30
+
31
+ api = Mocloud::API.new
32
+ res = api.get ("/v1/describe/#{user['uid']}/stack")
33
+
34
+ Mocloud::Utils.handle_error(res, "Show stacks failed")
35
+
36
+ data = JSON.parse(res[:body])
37
+
38
+ stackMap = {}
39
+
40
+ output = data.each do |x|
41
+ stackMap[x["stack_id"]["S"]] = x
42
+ stackMap[x["nickname"]["S"]] = x
43
+ end
44
+
45
+ if stackMap.has_key?(stackname)
46
+ instances = stackMap[stackname]["instances"]["L"]
47
+
48
+
49
+ rows = []
50
+ instances.each do |x|
51
+
52
+ rows << [
53
+ x["M"]["InstanceId"]["S"],
54
+ x["M"]["PublicIpAddress"]["S"],
55
+ x["M"]["PrivateIpAddress"]["S"],
56
+ x["M"]["State"]["M"]["Name"]["S"] ]
57
+
58
+ end
59
+
60
+ table = Terminal::Table.new :headings => ['Instance ID', 'Public IP', 'Private IP', 'State'], :rows => rows
61
+
62
+ puts "Instances in #{stackname}"
63
+ puts table
64
+
65
+ else
66
+ puts "No stack named #{stackname}. Type 'mobingi stacks' to see your stacks"
67
+ exit!
68
+ end
69
+
70
+
71
+
72
+ end
73
+ end
74
+
75
+ end
76
+ end
@@ -0,0 +1,53 @@
1
+ require 'uri'
2
+ require 'cgi'
3
+ require 'net/http'
4
+ require 'active_support'
5
+ require 'active_support/core_ext/object'
6
+
7
+ require 'mocloud/utils'
8
+ require 'mocloud/api'
9
+
10
+ module Mocloud
11
+ module Commands
12
+
13
+ class Login
14
+ def description
15
+ "Log in to Mobingi"
16
+ end
17
+
18
+ def run
19
+ opts = Trollop::options do
20
+ banner <<-ENDOFHELP
21
+ Usage: mobingi login [options]"
22
+
23
+ Login to your Mobingi account. You need to login to be able to create, delete, inspect and modify your stacks.
24
+ ENDOFHELP
25
+
26
+ opt :endpoint, "Set API Endpoint", :default => "api.mobingi.com"
27
+ end
28
+
29
+ print "Username (email): "
30
+ username = STDIN.gets.chomp
31
+ password = Mocloud::Utils.get_password
32
+ puts ""
33
+
34
+ api = Mocloud::API.new
35
+ res = api.get_unauthed(opts[:endpoint], "/v1/login", :username => username, :password => password)
36
+
37
+ Mocloud::Utils.handle_error(res, "Login failed")
38
+
39
+ puts "Successfully logged in!"
40
+
41
+ creds = Mocloud::Utils::Credentials.new
42
+
43
+ userinfo = JSON.parse(res[:body])
44
+ userinfo["username"] = username
45
+ userinfo["endpoint"] = opts[:endpoint]
46
+
47
+ creds.write_credentials(userinfo)
48
+
49
+ end
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,14 @@
1
+ module Mocloud
2
+ module Commands
3
+
4
+ class Logout
5
+ def description
6
+ "Log out from Mobingi (does not do anything right now)"
7
+ end
8
+
9
+ def run
10
+ end
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,65 @@
1
+ require 'uri'
2
+ require 'cgi'
3
+ require 'net/http'
4
+ require 'active_support'
5
+ require 'active_support/core_ext/object'
6
+ require 'terminal-table'
7
+
8
+ require 'mocloud/utils'
9
+ require 'mocloud/api'
10
+
11
+ module Mocloud
12
+ module Commands
13
+
14
+ class Stacks
15
+ def description
16
+ "List your stacks"
17
+ end
18
+
19
+ def run
20
+ Trollop::options do
21
+ banner <<-ENDOFHELP
22
+ Usage: mobingi stacks [options]"
23
+
24
+ Shows the stacks on your Mobingi account
25
+
26
+ ENDOFHELP
27
+ end
28
+
29
+ creds = Mocloud::Utils::Credentials.new
30
+ user = creds.read_credentials()
31
+ token = creds.make_token(0)
32
+
33
+ api = Mocloud::API.new
34
+ res = api.get ("/v1/describe/#{user['uid']}/stack")
35
+
36
+ Mocloud::Utils.handle_error(res, "Show stacks failed")
37
+
38
+ data = JSON.parse(res[:body])
39
+
40
+ output = data.map do |x|
41
+ {
42
+ :id => x["stack_id"]["S"],
43
+ :nick => x["nickname"]["S"],
44
+ :instance_count => x["instances"]["L"].length
45
+ }
46
+ end
47
+
48
+ puts "List of stacks:"
49
+
50
+ rows = []
51
+ output.each do |x|
52
+
53
+ rows << [x[:nick], x[:id], x[:instance_count]]
54
+
55
+ end
56
+
57
+ table = Terminal::Table.new :headings => ['Name', 'ID', 'Instances'], :rows => rows
58
+
59
+ puts table
60
+
61
+ end
62
+ end
63
+
64
+ end
65
+ end
@@ -0,0 +1,44 @@
1
+ Gem.find_files("mocloud/utils/*.rb").each { |path| require path }
2
+
3
+ module Mocloud
4
+ module Utils
5
+
6
+ if STDIN.respond_to?(:noecho)
7
+ def Utils.get_password(prompt="Password: ")
8
+ print prompt
9
+ STDIN.noecho(&:gets).chomp
10
+ end
11
+ else
12
+ def Utils.get_password(prompt="Password: ")
13
+ `read -s -p "#{prompt}" password; echo $password`.chomp
14
+ end
15
+ end
16
+
17
+ def Utils.http_get(url, params)
18
+
19
+ req = RedirectFollower.new ("#{url}?#{params.to_query}")
20
+ res = req.resolve
21
+
22
+ {
23
+ :status => res.status,
24
+ :body => res.body
25
+ }
26
+ end
27
+
28
+
29
+ def Utils.handle_error(res, prepend)
30
+ if (res[:status] != "200")
31
+ begin
32
+ result = JSON.parse(res[:body]);
33
+ puts "#{prepend}: #{result['reason']}"
34
+ rescue
35
+ puts "#{prepend}: #{res[:body]}"
36
+ end
37
+
38
+ exit!
39
+ end
40
+ end
41
+
42
+
43
+ end
44
+ end
File without changes
@@ -0,0 +1,42 @@
1
+ require 'openssl'
2
+ require 'json'
3
+
4
+ module Mocloud
5
+ module Utils
6
+
7
+ class Credentials
8
+
9
+ def initialize
10
+ @credentials_directory = "#{File.expand_path('~')}/.mobingi"
11
+ end
12
+
13
+ def write_credentials(creds)
14
+ unless File.directory?(@credentials_directory)
15
+ FileUtils.mkdir_p(@credentials_directory)
16
+ end
17
+ File.write("#{@credentials_directory}/credentials", creds.to_json)
18
+
19
+ end
20
+
21
+ def read_credentials()
22
+ unless File.directory?(@credentials_directory)
23
+ puts "You have not logged in yet. Please type 'mocloud login' to login"
24
+ exit!
25
+ end
26
+ text = File.read("#{@credentials_directory}/credentials")
27
+
28
+ JSON.parse(text)
29
+ end
30
+
31
+ def make_token(time)
32
+ creds = read_credentials()
33
+
34
+ key = "#{creds['uid']}#{time}"
35
+ signature = "#{creds['token']}"
36
+
37
+ OpenSSL::HMAC.hexdigest('sha256', signature, key)
38
+ end
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,49 @@
1
+
2
+ module Mocloud
3
+ module Utils
4
+
5
+ class RedirectFollower
6
+ class TooManyRedirects < StandardError; end
7
+
8
+ attr_accessor :url, :body, :status, :redirect_limit, :response
9
+
10
+ def initialize(url, limit=5)
11
+ @url, @redirect_limit = url, limit
12
+ end
13
+
14
+ def resolve
15
+ raise TooManyRedirects if redirect_limit < 0
16
+
17
+ uri = URI.parse(self.url)
18
+
19
+ http = Net::HTTP.new(uri.host, uri.port)
20
+ http.use_ssl = true if uri.scheme == "https" # enable SSL/TLS
21
+ http.start do
22
+ http.request_get("#{uri.path}?#{uri.query}") do |res|
23
+ self.response = res
24
+ end
25
+ end
26
+
27
+ if response.kind_of?(Net::HTTPRedirection)
28
+ self.url = redirect_url
29
+ self.redirect_limit -= 1
30
+
31
+ resolve
32
+ end
33
+
34
+ self.body = response.body
35
+ self.status = response.code
36
+ self
37
+ end
38
+
39
+ def redirect_url
40
+ if response['location'].nil?
41
+ response.body.match(/<a href=\"([^>]+)\">/i)[1]
42
+ else
43
+ response['location']
44
+ end
45
+ end
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module Mocloud
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mocloud/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mobingi"
8
+ spec.version = Mocloud::VERSION
9
+ spec.authors = ["David Siaw"]
10
+ spec.email = ["david.siaw@mobingi.com"]
11
+ spec.summary = %q{Mobingi Command Line Tool}
12
+ spec.description = %q{This command line tool helps you manage stacks on Mobingi}
13
+ spec.homepage = "https://mobingi.com"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport"
22
+ spec.add_dependency "aws-sdk-core"
23
+ spec.add_dependency "json"
24
+ spec.add_dependency "trollop"
25
+ spec.add_dependency "terminal-table"
26
+ spec.add_dependency "require_all"
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.6"
29
+ spec.add_development_dependency "rake"
30
+ end
metadata ADDED
@@ -0,0 +1,194 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mobingi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Siaw
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-06-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !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: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: aws-sdk-core
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: json
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: trollop
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: terminal-table
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: require_all
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: bundler
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: '1.6'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: '1.6'
126
+ - !ruby/object:Gem::Dependency
127
+ name: rake
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ description: This command line tool helps you manage stacks on Mobingi
143
+ email:
144
+ - david.siaw@mobingi.com
145
+ executables:
146
+ - mobingi
147
+ extensions: []
148
+ extra_rdoc_files: []
149
+ files:
150
+ - .gitignore
151
+ - Gemfile
152
+ - LICENSE.txt
153
+ - README.md
154
+ - Rakefile
155
+ - bin/mobingi
156
+ - lib/mocloud.rb
157
+ - lib/mocloud/api.rb
158
+ - lib/mocloud/commands.rb
159
+ - lib/mocloud/commands/instances.rb
160
+ - lib/mocloud/commands/login.rb
161
+ - lib/mocloud/commands/logout.rb
162
+ - lib/mocloud/commands/stacks.rb
163
+ - lib/mocloud/utils.rb
164
+ - lib/mocloud/utils/api.rb
165
+ - lib/mocloud/utils/credentials.rb
166
+ - lib/mocloud/utils/redirect_follower.rb
167
+ - lib/mocloud/version.rb
168
+ - mobingi.gemspec
169
+ homepage: https://mobingi.com
170
+ licenses:
171
+ - MIT
172
+ post_install_message:
173
+ rdoc_options: []
174
+ require_paths:
175
+ - lib
176
+ required_ruby_version: !ruby/object:Gem::Requirement
177
+ none: false
178
+ requirements:
179
+ - - ! '>='
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ required_rubygems_version: !ruby/object:Gem::Requirement
183
+ none: false
184
+ requirements:
185
+ - - ! '>='
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ requirements: []
189
+ rubyforge_project:
190
+ rubygems_version: 1.8.29
191
+ signing_key:
192
+ specification_version: 3
193
+ summary: Mobingi Command Line Tool
194
+ test_files: []