morpheus-cli 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e66add7b4d8599a88bb70d080e22aa749f938160
4
+ data.tar.gz: 3d9a5ef82ef5328e8540c389d758b276c6d9096e
5
+ SHA512:
6
+ metadata.gz: e07a7987235f1e2ec370919a792dd2de3415b1d34089ad2f9db63f7217346b1d2514fe12b5e8095736515c243eb373d99a855eee1d00fda02eb787815401910b
7
+ data.tar.gz: a18fcee3f5ec31fb9e02f32360442aebc2745cdc810774a0459cc0074ec7f5925cd92b8116cdc89ed7026e41cc0120820b4775fbcc5639ece5d91e98cf6f0a99
data/.gitignore ADDED
@@ -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 morpheus-cli.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 David Estes
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.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Morpheus::Cli
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'morpheus-cli'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install morpheus-cli
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/morpheus-cli/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/morpheus ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+ require 'morpheus/cli'
3
+
4
+ remote_handler = Morpheus::Cli::Remote.new()
5
+
6
+ case ARGV[0]
7
+ when 'remote'
8
+ remote_handler.handle(ARGV[1..-1])
9
+ when 'login'
10
+ appliance_name, appliance_url = Morpheus::Cli::Remote.active_appliance
11
+ Morpheus::Cli::Credentials.new(appliance_name,appliance_url).login()
12
+ when 'groups'
13
+ Morpheus::Cli::Groups.new().handle(ARGV[1..-1])
14
+ when 'zones'
15
+ Morpheus::Cli::Zones.new().handle(ARGV[1..-1])
16
+ when 'servers'
17
+ Morpheus::Cli::Servers.new().handle(ARGV[1..-1])
18
+ when 'instances'
19
+ Morpheus::Cli::Instances.new().handle(ARGV[1..-1])
20
+ when 'instance-types'
21
+ Morpheus::Cli::InstanceTypes.new().handle(ARGV[1..-1])
22
+ else
23
+ puts "\nUsage: morpheus [command] [options]\n"
24
+ end
@@ -0,0 +1,33 @@
1
+ require 'json'
2
+ require 'rest-client'
3
+
4
+ class Morpheus::APIClient
5
+ def initialize(access_token, refresh_token=nil,expires_in = nil, base_url=nil)
6
+ @access_token = access_token
7
+ @refresh_token = refresh_token
8
+ @base_url = base_url
9
+ if expires_in != nil
10
+ @expires_at = DateTime.now + expires_in.seconds
11
+ end
12
+ end
13
+
14
+ def groups
15
+ Morpheus::GroupsInterface.new(@access_token, @refresh_token, @expires_at, @base_url)
16
+ end
17
+
18
+ def zones
19
+ Morpheus::ZonesInterface.new(@access_token, @refresh_token, @expires_at, @base_url)
20
+ end
21
+
22
+ def servers
23
+ Morpheus::ServersInterface.new(@access_token, @refresh_token, @expires_at, @base_url)
24
+ end
25
+
26
+ def instances
27
+ Morpheus::InstancesInterface.new(@access_token, @refresh_token, @expires_at, @base_url)
28
+ end
29
+
30
+ def instance_types
31
+ Morpheus::InstanceTypesInterface.new(@access_token, @refresh_token, @expires_at, @base_url)
32
+ end
33
+ end
@@ -0,0 +1,47 @@
1
+ require 'json'
2
+ require 'rest-client'
3
+
4
+ class Morpheus::GroupsInterface < Morpheus::APIClient
5
+ def initialize(access_token, refresh_token,expires_at = nil, base_url=nil)
6
+ @access_token = access_token
7
+ @refresh_token = refresh_token
8
+ @base_url = base_url
9
+ @expires_at = expires_at
10
+ end
11
+
12
+
13
+ def get(options=nil)
14
+ url = "#{@base_url}/api/groups"
15
+ headers = { params: {}, authorization: "Bearer #{@access_token}" }
16
+
17
+ if options.is_a?(Hash)
18
+ headers.params.merge!(options)
19
+ elsif options.is_a?(Numeric)
20
+ url = "#{@base_url}/api/groups/#{options}"
21
+ elsif options.is_a?(String)
22
+ headers[:params]['name'] = options
23
+ end
24
+ response = RestClient::Request.execute(method: :get, url: url,
25
+ timeout: 10, headers: headers)
26
+ JSON.parse(response.to_s)
27
+ end
28
+
29
+
30
+ def create(options)
31
+ url = "#{@base_url}/api/groups"
32
+ headers = { :authorization => "Bearer #{@access_token}", 'Content-Type' => 'application/json' }
33
+
34
+ payload = {group: options}
35
+ response = RestClient::Request.execute(method: :post, url: url,
36
+ timeout: 10, headers: headers, payload: payload.to_json)
37
+ JSON.parse(response.to_s)
38
+ end
39
+
40
+ def destroy(id)
41
+ url = "#{@base_url}/api/groups/#{id}"
42
+ headers = { :authorization => "Bearer #{@access_token}", 'Content-Type' => 'application/json' }
43
+ response = RestClient::Request.execute(method: :delete, url: url,
44
+ timeout: 10, headers: headers)
45
+ JSON.parse(response.to_s)
46
+ end
47
+ end
@@ -0,0 +1,42 @@
1
+ require 'json'
2
+ require 'rest-client'
3
+
4
+ class Morpheus::InstanceTypesInterface < Morpheus::APIClient
5
+ def initialize(access_token, refresh_token,expires_at = nil, base_url=nil)
6
+ @access_token = access_token
7
+ @refresh_token = refresh_token
8
+ @base_url = base_url
9
+ @expires_at = expires_at
10
+ end
11
+
12
+
13
+ def get(options=nil)
14
+ url = "#{@base_url}/api/instance-types"
15
+ headers = { params: {}, authorization: "Bearer #{@access_token}" }
16
+
17
+ if options.is_a?(Hash)
18
+ headers[:params].merge!(options)
19
+ elsif options.is_a?(Numeric)
20
+ url = "#{@base_url}/api/instance-types/#{options}"
21
+ elsif options.is_a?(String)
22
+ headers[:params]['name'] = options
23
+ end
24
+ response = RestClient::Request.execute(method: :get, url: url,
25
+ timeout: 10, headers: headers)
26
+ JSON.parse(response.to_s)
27
+ end
28
+
29
+ def service_plans(layout_id, name=nil)
30
+ url = "#{@base_url}/api/instance-types/service-plans/#{layout_id}"
31
+ headers = { params: {}, authorization: "Bearer #{@access_token}" }
32
+
33
+ if !name.nil?
34
+ headers[:params][:name] = name
35
+ end
36
+ response = RestClient::Request.execute(method: :get, url: url,
37
+ timeout: 10, headers: headers)
38
+ JSON.parse(response.to_s)
39
+ end
40
+
41
+
42
+ end
@@ -0,0 +1,98 @@
1
+ require 'json'
2
+ require 'rest-client'
3
+
4
+ class Morpheus::InstancesInterface < Morpheus::APIClient
5
+ def initialize(access_token, refresh_token,expires_at = nil, base_url=nil)
6
+ @access_token = access_token
7
+ @refresh_token = refresh_token
8
+ @base_url = base_url
9
+ @expires_at = expires_at
10
+ end
11
+
12
+
13
+ def get(options=nil)
14
+ url = "#{@base_url}/api/instances"
15
+ headers = { params: {}, authorization: "Bearer #{@access_token}" }
16
+
17
+ if options.is_a?(Hash)
18
+ headers[:params].merge!(options)
19
+ elsif options.is_a?(Numeric)
20
+ url = "#{@base_url}/api/instances/#{options}"
21
+ elsif options.is_a?(String)
22
+ headers[:params]['name'] = options
23
+ end
24
+ response = RestClient::Request.execute(method: :get, url: url,
25
+ timeout: 10, headers: headers)
26
+ JSON.parse(response.to_s)
27
+ end
28
+
29
+ def get_envs(id, options=nil)
30
+ url = "#{@base_url}/api/instances/#{id}/envs"
31
+ headers = { params: {}, authorization: "Bearer #{@access_token}" }
32
+ response = RestClient::Request.execute(method: :get, url: url,
33
+ timeout: 10, headers: headers)
34
+ JSON.parse(response.to_s)
35
+ end
36
+
37
+ def create_env(id, options)
38
+ url = "#{@base_url}/api/instances/#{id}/envs"
39
+ headers = { :authorization => "Bearer #{@access_token}", 'Content-Type' => 'application/json' }
40
+
41
+ payload = {envs: options}
42
+ response = RestClient::Request.execute(method: :post, url: url,
43
+ timeout: 10, headers: headers, payload: payload.to_json)
44
+ JSON.parse(response.to_s)
45
+ end
46
+
47
+ def del_env(id, name)
48
+ url = "#{@base_url}/api/instances/#{id}/envs/#{name}"
49
+ headers = { :authorization => "Bearer #{@access_token}", 'Content-Type' => 'application/json' }
50
+
51
+ response = RestClient::Request.execute(method: :delete, url: url,
52
+ timeout: 10, headers: headers)
53
+ JSON.parse(response.to_s)
54
+ end
55
+
56
+
57
+ def create(options)
58
+ url = "#{@base_url}/api/instances"
59
+ headers = { :authorization => "Bearer #{@access_token}", 'Content-Type' => 'application/json' }
60
+
61
+ payload = options
62
+ response = RestClient::Request.execute(method: :post, url: url,
63
+ timeout: 10, headers: headers, payload: payload.to_json)
64
+ JSON.parse(response.to_s)
65
+ end
66
+
67
+ def destroy(id)
68
+ url = "#{@base_url}/api/instances/#{id}"
69
+ headers = { :authorization => "Bearer #{@access_token}", 'Content-Type' => 'application/json' }
70
+ response = RestClient::Request.execute(method: :delete, url: url,
71
+ timeout: 10, headers: headers)
72
+ JSON.parse(response.to_s)
73
+ end
74
+
75
+ def stop(id)
76
+ url = "#{@base_url}/api/instances/#{id}/stop"
77
+ headers = { :authorization => "Bearer #{@access_token}", 'Content-Type' => 'application/json' }
78
+ response = RestClient::Request.execute(method: :put, url: url,
79
+ timeout: 10, headers: headers)
80
+ JSON.parse(response.to_s)
81
+ end
82
+
83
+ def start(id)
84
+ url = "#{@base_url}/api/instances/#{id}/start"
85
+ headers = { :authorization => "Bearer #{@access_token}", 'Content-Type' => 'application/json' }
86
+ response = RestClient::Request.execute(method: :put, url: url,
87
+ timeout: 10, headers: headers)
88
+ JSON.parse(response.to_s)
89
+ end
90
+
91
+ def restart(id)
92
+ url = "#{@base_url}/api/instances/#{id}/restart"
93
+ headers = { :authorization => "Bearer #{@access_token}", 'Content-Type' => 'application/json' }
94
+ response = RestClient::Request.execute(method: :put, url: url,
95
+ timeout: 10, headers: headers)
96
+ JSON.parse(response.to_s)
97
+ end
98
+ end
@@ -0,0 +1,46 @@
1
+ require 'json'
2
+ require 'rest-client'
3
+
4
+ class Morpheus::ServersInterface < Morpheus::APIClient
5
+ def initialize(access_token, refresh_token,expires_at = nil, base_url=nil)
6
+ @access_token = access_token
7
+ @refresh_token = refresh_token
8
+ @base_url = base_url
9
+ @expires_at = expires_at
10
+ end
11
+
12
+ def get(options=nil)
13
+ url = "#{@base_url}/api/servers"
14
+ headers = { params: {}, authorization: "Bearer #{@access_token}" }
15
+
16
+ if options.is_a?(Hash)
17
+ headers[:params].merge!(options)
18
+ elsif options.is_a?(Numeric)
19
+ url = "#{@base_url}/api/servers/#{options}"
20
+ elsif options.is_a?(String)
21
+ headers[:params]['name'] = options
22
+ end
23
+ response = RestClient::Request.execute(method: :get, url: url,
24
+ timeout: 10, headers: headers)
25
+ JSON.parse(response.to_s)
26
+ end
27
+
28
+
29
+ def create(options)
30
+ url = "#{@base_url}/api/servers"
31
+ headers = { :authorization => "Bearer #{@access_token}", 'Content-Type' => 'application/json' }
32
+
33
+ payload = options
34
+ response = RestClient::Request.execute(method: :post, url: url,
35
+ timeout: 10, headers: headers, payload: payload.to_json)
36
+ JSON.parse(response.to_s)
37
+ end
38
+
39
+ def destroy(id)
40
+ url = "#{@base_url}/api/servers/#{id}"
41
+ headers = { :authorization => "Bearer #{@access_token}", 'Content-Type' => 'application/json' }
42
+ response = RestClient::Request.execute(method: :delete, url: url,
43
+ timeout: 10, headers: headers)
44
+ JSON.parse(response.to_s)
45
+ end
46
+ end
@@ -0,0 +1,56 @@
1
+ require 'json'
2
+ require 'rest-client'
3
+
4
+ class Morpheus::ZonesInterface < Morpheus::APIClient
5
+ def initialize(access_token, refresh_token,expires_at = nil, base_url=nil)
6
+ @access_token = access_token
7
+ @refresh_token = refresh_token
8
+ @base_url = base_url
9
+ @expires_at = expires_at
10
+ end
11
+
12
+ def zone_types()
13
+ url = "#{@base_url}/api/zone-types"
14
+ headers = { params: {}, authorization: "Bearer #{@access_token}" }
15
+
16
+ response = RestClient::Request.execute(method: :get, url: url,
17
+ timeout: 10, headers: headers)
18
+ JSON.parse(response.to_s)
19
+ end
20
+
21
+
22
+ def get(options=nil)
23
+ url = "#{@base_url}/api/zones"
24
+ headers = { params: {}, authorization: "Bearer #{@access_token}" }
25
+
26
+ if options.is_a?(Hash)
27
+ headers[:params].merge!(options)
28
+ elsif options.is_a?(Numeric)
29
+ url = "#{@base_url}/api/zones/#{options}"
30
+ elsif options.is_a?(String)
31
+ headers[:params]['name'] = options
32
+ end
33
+ response = RestClient::Request.execute(method: :get, url: url,
34
+ timeout: 10, headers: headers)
35
+ JSON.parse(response.to_s)
36
+ end
37
+
38
+
39
+ def create(options)
40
+ url = "#{@base_url}/api/zones"
41
+ headers = { :authorization => "Bearer #{@access_token}", 'Content-Type' => 'application/json' }
42
+
43
+ payload = {zone: options}
44
+ response = RestClient::Request.execute(method: :post, url: url,
45
+ timeout: 10, headers: headers, payload: payload.to_json)
46
+ JSON.parse(response.to_s)
47
+ end
48
+
49
+ def destroy(id)
50
+ url = "#{@base_url}/api/zones/#{id}"
51
+ headers = { :authorization => "Bearer #{@access_token}", 'Content-Type' => 'application/json' }
52
+ response = RestClient::Request.execute(method: :delete, url: url,
53
+ timeout: 10, headers: headers)
54
+ JSON.parse(response.to_s)
55
+ end
56
+ end
@@ -0,0 +1,96 @@
1
+ require 'yaml'
2
+ require 'io/console'
3
+ require 'rest_client'
4
+ module Morpheus
5
+ module Cli
6
+ class Credentials
7
+ def initialize(appliance_name, appliance_url)
8
+ @appliance_url = appliance_url
9
+ @appliance_name = appliance_name
10
+ end
11
+
12
+ def request_credentials()
13
+ # We should return an access Key for Morpheus CLI Here
14
+ creds = load_saved_credentials
15
+ if !creds
16
+ puts "No Credentials on File for this Appliance: "
17
+ puts "Enter Username: "
18
+ username = $stdin.gets.chomp!
19
+ puts "Enter Password: "
20
+ password = STDIN.noecho(&:gets).chomp!
21
+
22
+ oauth_url = File.join(@appliance_url, "/oauth/token")
23
+ begin
24
+ authorize_response = RestClient.post oauth_url, {grant_type: 'password', scope:'write', client_id: 'morph-cli', username: username, password: password}
25
+
26
+ json_response = JSON.parse(authorize_response.to_s)
27
+ access_token = json_response['access_token']
28
+ if access_token
29
+ save_credentials(access_token)
30
+ return access_token
31
+ else
32
+ puts "Credentials not verified."
33
+ return nil
34
+ end
35
+ rescue => e
36
+ puts "Error Communicating with the Appliance. Please try again later. #{e}"
37
+ return nil
38
+ end
39
+ else
40
+ return creds
41
+ end
42
+ end
43
+
44
+ def login()
45
+ clear_saved_credentials
46
+ request_credentials
47
+ end
48
+
49
+
50
+ def clear_saved_credentials()
51
+ credential_map = load_credential_file
52
+ if credential_map.nil?
53
+ credential_map = {}
54
+ end
55
+ credential_map.delete(@appliance_name)
56
+ File.open(credentials_file_path, 'w') {|f| f.write credential_map.to_yaml } #Store
57
+ end
58
+
59
+ def load_saved_credentials()
60
+ credential_map = load_credential_file
61
+ if credential_map.nil?
62
+ return nil
63
+ else
64
+ return credential_map[@appliance_name]
65
+ end
66
+ end
67
+
68
+ def load_credential_file
69
+ creds_file = credentials_file_path
70
+ if File.exist? creds_file
71
+ return YAML.load_file(creds_file)
72
+ else
73
+ return nil
74
+ end
75
+ end
76
+
77
+ def credentials_file_path
78
+ home_dir = Dir.home
79
+ morpheus_dir = File.join(home_dir,".morpheus")
80
+ if !Dir.exist?(morpheus_dir)
81
+ Dir.mkdir(morpheus_dir)
82
+ end
83
+ return File.join(morpheus_dir,"credentials")
84
+ end
85
+
86
+ def save_credentials(token)
87
+ credential_map = load_credential_file
88
+ if credential_map.nil?
89
+ credential_map = {}
90
+ end
91
+ credential_map[@appliance_name] = token
92
+ File.open(credentials_file_path, 'w') {|f| f.write credential_map.to_yaml } #Store
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,24 @@
1
+ require 'yaml'
2
+ require 'rest_client'
3
+ require 'term/ansicolor'
4
+ require 'optparse'
5
+
6
+
7
+ class Morpheus::Cli::ErrorHandler
8
+ include Term::ANSIColor
9
+
10
+ def print_errors(response)
11
+ if !response['success']
12
+ print red,bold, "\n"
13
+ if response['msg']
14
+ puts response['msg']
15
+ end
16
+ if response['errors']
17
+ response['errors'].each do |key, value|
18
+ print "* #{key}: #{value}\n"
19
+ end
20
+ end
21
+ print reset, "\n"
22
+ end
23
+ end
24
+ end