morpheus-cli 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,170 @@
1
+ # require 'yaml'
2
+ require 'io/console'
3
+ require 'rest_client'
4
+ require 'term/ansicolor'
5
+ require 'optparse'
6
+
7
+
8
+ class Morpheus::Cli::Groups
9
+ include Term::ANSIColor
10
+ def initialize()
11
+ @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
12
+ @access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials()
13
+ @groups_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).groups
14
+ @active_groups = ::Morpheus::Cli::Groups.load_group_file
15
+
16
+ end
17
+
18
+ def handle(args)
19
+ if @access_token.empty?
20
+ print red,bold, "\nInvalid Credentials. Unable to acquire access token. Please verify your credentials and try again.\n\n",reset
21
+ return 1
22
+ end
23
+ if args.empty?
24
+ puts "\nUsage: morpheus groups [list,add,remove] [name]\n\n"
25
+ end
26
+
27
+ case args[0]
28
+ when 'list'
29
+ list(args[1..-1])
30
+ when 'add'
31
+ add(args[1..-1])
32
+ when 'use'
33
+ use(args[1..-1])
34
+ when 'remove'
35
+ remove(args[1..-1])
36
+ else
37
+ puts "\nUsage: morpheus groups [list,add,remove] [name]\n\n"
38
+ end
39
+ end
40
+
41
+ def add(args)
42
+ if args.count < 1
43
+ puts "\nUsage: morpheus groups add [name] [--location]\n\n"
44
+ return
45
+ end
46
+ params = {}
47
+ optparse = OptionParser.new do|opts|
48
+ opts.on( '-l', '--location LOCATION', "Location" ) do |desc|
49
+ params[:location] = desc
50
+ end
51
+ end
52
+ optparse.parse(args)
53
+
54
+ group = {name: args[0], location: params[:location]}
55
+ begin
56
+ @groups_interface.create(group)
57
+ rescue => e
58
+ if e.response.code == 400
59
+ error = JSON.parse(e.response.to_s)
60
+ ::Morpheus::Cli::ErrorHandler.new.print_errors(error)
61
+ else
62
+ puts "Error Communicating with the Appliance. Please try again later. #{e}"
63
+ end
64
+ return nil
65
+ end
66
+ list([])
67
+ end
68
+
69
+ def remove(args)
70
+ if args.count < 1
71
+ puts "\nUsage: morpheus groups remove [name]\n\n"
72
+ return
73
+ end
74
+
75
+ begin
76
+ group_results = @groups_interface.get(args[0])
77
+ if group_results['groups'].empty?
78
+ puts "Group not found by name #{args[0]}"
79
+ return
80
+ end
81
+ @groups_interface.destroy(group_results['groups'][0]['id'])
82
+ list([])
83
+ rescue RestClient::Exception => e
84
+ if e.response.code == 400
85
+ error = JSON.parse(e.response.to_s)
86
+ ::Morpheus::Cli::ErrorHandler.new.print_errors(error)
87
+ else
88
+ puts "Error Communicating with the Appliance. Please try again later. #{e}"
89
+ end
90
+ return nil
91
+ end
92
+ end
93
+
94
+ def list(args)
95
+ begin
96
+ json_response = @groups_interface.get()
97
+ groups = json_response['groups']
98
+ print "\n" ,cyan, bold, "Morpheus Groups\n","==================", reset, "\n\n"
99
+ if groups.empty?
100
+ puts yellow,"No groups currently configured.",reset
101
+ else
102
+ groups.each do |group|
103
+ if @active_groups[@appliance_name.to_sym] == group['id']
104
+ print cyan, bold, "=> #{group['name']} - #{group['location']}",reset,"\n"
105
+ else
106
+ print cyan, "= #{group['name']} - #{group['location']}\n",reset
107
+ end
108
+ end
109
+ end
110
+ print reset,"\n\n"
111
+
112
+ rescue => e
113
+ puts "Error Communicating with the Appliance. Please try again later. #{e}"
114
+ return nil
115
+ end
116
+ end
117
+
118
+ def use(args)
119
+ if args.length < 1
120
+ puts "Usage: morpheus groups use [name]"
121
+ end
122
+ begin
123
+ json_response = @groups_interface.get(args[0])
124
+ groups = json_response['groups']
125
+ if groups.length > 0
126
+ @active_groups[@appliance_name.to_sym] = groups[0]['id']
127
+ ::Morpheus::Cli::Groups.save_groups(@active_groups)
128
+ list([])
129
+ else
130
+ puts "Group not found"
131
+ end
132
+ rescue => e
133
+ puts "Error Communicating with the Appliance. Please try again later. #{e}"
134
+ return nil
135
+ end
136
+ end
137
+
138
+ # Provides the current active group information
139
+ def self.active_group
140
+ appliance_name, appliance_url = Morpheus::Cli::Remote.active_appliance
141
+ if !defined?(@@groups)
142
+ @@groups = load_group_file
143
+ end
144
+ return @@groups[appliance_name.to_sym]
145
+ end
146
+
147
+
148
+
149
+ def self.load_group_file
150
+ remote_file = groups_file_path
151
+ if File.exist? remote_file
152
+ return YAML.load_file(remote_file)
153
+ else
154
+ {}
155
+ end
156
+ end
157
+
158
+ def self.groups_file_path
159
+ home_dir = Dir.home
160
+ morpheus_dir = File.join(home_dir,".morpheus")
161
+ if !Dir.exist?(morpheus_dir)
162
+ Dir.mkdir(morpheus_dir)
163
+ end
164
+ return File.join(morpheus_dir,"groups")
165
+ end
166
+
167
+ def self.save_groups(group_map)
168
+ File.open(groups_file_path, 'w') {|f| f.write group_map.to_yaml } #Store
169
+ end
170
+ end
@@ -0,0 +1,100 @@
1
+ # require 'yaml'
2
+ require 'io/console'
3
+ require 'rest_client'
4
+ require 'term/ansicolor'
5
+ require 'optparse'
6
+ require 'filesize'
7
+
8
+ class Morpheus::Cli::InstanceTypes
9
+ include Term::ANSIColor
10
+ def initialize()
11
+ @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
12
+ @access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials()
13
+ @instance_types_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).instance_types
14
+ @groups_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).groups
15
+ @active_groups = ::Morpheus::Cli::Groups.load_group_file
16
+ end
17
+
18
+
19
+ def handle(args)
20
+ if @access_token.empty?
21
+ print red,bold, "\nInvalid Credentials. Unable to acquire access token. Please verify your credentials and try again.\n\n",reset
22
+ return 1
23
+ end
24
+ if args.empty?
25
+ puts "\nUsage: morpheus instance-types [list,details] [name]\n\n"
26
+ end
27
+
28
+ case args[0]
29
+ when 'list'
30
+ list(args[1..-1])
31
+ when 'details'
32
+ details(args[1..-1])
33
+ else
34
+ puts "\nUsage: morpheus instance-types [list,details] [name]\n\n"
35
+ end
36
+ end
37
+
38
+
39
+ def details(args)
40
+ if args.count < 1
41
+ puts "\nUsage: morpheus instances stats [name]\n\n"
42
+ return
43
+ end
44
+ begin
45
+ instance_results = @instances_interface.get({name: args[0]})
46
+ if instance_results['instances'].empty?
47
+ puts "Instance not found by name #{args[0]}"
48
+ return
49
+ end
50
+ instance = instance_results['instances'][0]
51
+ instance_id = instance['id']
52
+ stats = instance_results['stats'][instance_id.to_s]
53
+ print "\n" ,cyan, bold, "#{instance['name']} (#{instance['instanceType']['name']})\n","==================", reset, "\n\n"
54
+ print cyan, "Memory: \t#{Filesize.from("#{stats['usedMemory']} B").pretty} / #{Filesize.from("#{stats['maxMemory']} B").pretty}\n"
55
+ print cyan, "Storage: \t#{Filesize.from("#{stats['usedStorage']} B").pretty} / #{Filesize.from("#{stats['maxStorage']} B").pretty}\n\n",reset
56
+ puts instance
57
+ rescue RestClient::Exception => e
58
+ if e.response.code == 400
59
+ error = JSON.parse(e.response.to_s)
60
+ ::Morpheus::Cli::ErrorHandler.new.print_errors(error)
61
+ else
62
+ puts "Error Communicating with the Appliance. Please try again later. #{e}"
63
+ end
64
+ return nil
65
+ end
66
+ end
67
+
68
+ def list(args)
69
+ options = {}
70
+ optparse = OptionParser.new do|opts|
71
+ opts.on( '-g', '--group GROUP', "Group Name" ) do |group|
72
+ options[:group] = group
73
+ end
74
+ end
75
+ optparse.parse(args)
76
+ begin
77
+ params = {}
78
+
79
+ json_response = @instance_types_interface.get(params)
80
+ instance_types = json_response['instanceTypes']
81
+ print "\n" ,cyan, bold, "Morpheus Instance Types\n","==================", reset, "\n\n"
82
+ if instance_types.empty?
83
+ puts yellow,"No instance types currently configured.",reset
84
+ else
85
+ instance_types.each do |instance_type|
86
+ versions = instance_type['versions'].join(', ')
87
+ print cyan, "= #{instance_type['name']} (#{instance_type['code']}) - #{versions}\n"
88
+ instance_type['instanceTypeLayouts'].each do |layout|
89
+ print green, " - #{layout['name']}\n",reset
90
+ end
91
+ end
92
+ end
93
+ print reset,"\n\n"
94
+
95
+ rescue => e
96
+ puts "Error Communicating with the Appliance. Please try again later. #{e}"
97
+ return nil
98
+ end
99
+ end
100
+ end