mms-api 0.0.9 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +52 -59
- data/bin/mms-api +1 -146
- data/lib/mms.rb +13 -0
- data/lib/mms/agent.rb +40 -30
- data/lib/mms/cli.rb +258 -0
- data/lib/mms/client.rb +19 -37
- data/lib/mms/config.rb +57 -0
- data/lib/mms/errors.rb +41 -0
- data/lib/mms/resource.rb +28 -0
- data/lib/mms/resource/alert.rb +83 -0
- data/lib/mms/resource/cluster.rb +87 -10
- data/lib/mms/resource/group.rb +56 -9
- data/lib/mms/resource/host.rb +32 -3
- data/lib/mms/resource/restore_job.rb +45 -4
- data/lib/mms/resource/snapshot.rb +51 -10
- data/lib/mms/version.rb +1 -1
- metadata +21 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7f238c7242a72f7afcaf801e4471a6a772377b30
|
4
|
+
data.tar.gz: b7db404c176c296832c46552d104a7adb8abb899
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b8dca3e5b69c6dc916920e52abffb1d1647f6a59571cbdf0307714564f7a8e43256dbb78ce46bea5d937eca2def867dc92e43d04e2ebd4e3182d2984df680ad8
|
7
|
+
data.tar.gz: 98f2a85f55e5f1cf942dda16d2745955745dee3ff52afbcb60d3c3261aa332e1e978d804eafeb16e99c59fd85098468f1c71b1e0e1dcbe6fd5b6824cf78e4b97
|
data/README.md
CHANGED
@@ -8,40 +8,6 @@ Installation
|
|
8
8
|
gem install mms-api
|
9
9
|
```
|
10
10
|
|
11
|
-
Cli
|
12
|
-
---
|
13
|
-
```bash
|
14
|
-
$ mms-api --help
|
15
|
-
mms-api is a tool for accessing MMS API
|
16
|
-
|
17
|
-
Usage:
|
18
|
-
|
19
|
-
mms-api command [options]
|
20
|
-
|
21
|
-
Commands:
|
22
|
-
|
23
|
-
groups | hosts | clusters | snapshots | restorejobs | restorejobs-create
|
24
|
-
|
25
|
-
Options:
|
26
|
-
|
27
|
-
-u, --username <string> MMS user
|
28
|
-
-k, --apikey <string> MMS api-key
|
29
|
-
-a, --apiurl <string> MMS api url. Full url including version: https://mms.mydomain.tld/api/public/v1.0
|
30
|
-
-n, --name <string> Filter by resource name using regexp
|
31
|
-
-l, --limit <int> Limit for result items
|
32
|
-
-v, --version Version
|
33
|
-
-h, --help Show this help
|
34
|
-
```
|
35
|
-
|
36
|
-
`mms-api` reads default configuration from your home directory `~/.mms-api`. Example configuration:
|
37
|
-
|
38
|
-
```
|
39
|
-
username=sysadmin@example.tld
|
40
|
-
apikey=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
|
41
|
-
# apiurl=https://mms.mydomain.tld/api/public/v1.0
|
42
|
-
# action=hosts
|
43
|
-
```
|
44
|
-
|
45
11
|
API coverage
|
46
12
|
------------
|
47
13
|
The MMS Public API follows the principles of the REST architectural style to expose a number of internal resources which enable programmatic access to [MMS’s features](http://mms.mongodb.com/help/reference/api/). Current implementation support only a few of API features.
|
@@ -52,39 +18,66 @@ The MMS Public API follows the principles of the REST architectural style to exp
|
|
52
18
|
|Hosts| + | + | | | | |
|
53
19
|
|Clusters| + | + | | | | |
|
54
20
|
|Snapshots| + | + | | | | |
|
21
|
+
|Alerts| + | + | | | | |
|
55
22
|
|Restore Jobs| + | + | + | | | |
|
56
23
|
|
24
|
+
Config
|
25
|
+
------
|
26
|
+
```ruby
|
27
|
+
config = MMS::Config.new
|
28
|
+
```
|
29
|
+
|
30
|
+
Client
|
31
|
+
------
|
32
|
+
```ruby
|
33
|
+
client = MMS::Client.new(config.username, config.apikey)
|
34
|
+
```
|
57
35
|
|
58
|
-
|
59
|
-
|
36
|
+
Agent
|
37
|
+
-----
|
60
38
|
```ruby
|
61
|
-
|
39
|
+
agent = MMS::Agent.new(client)
|
40
|
+
```
|
62
41
|
|
63
|
-
|
42
|
+
Cli
|
43
|
+
---
|
44
|
+
```bash
|
45
|
+
$ mms-api --help
|
46
|
+
Usage:
|
47
|
+
[OPTIONS] SUBCOMMAND [ARG] ...
|
64
48
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
cluster_list = agent.clusters
|
69
|
-
snapshot_list = agent.snapshots
|
70
|
-
job_list = agent.restorejobs
|
49
|
+
Parameters:
|
50
|
+
SUBCOMMAND subcommand
|
51
|
+
[ARG] ... subcommand arguments
|
71
52
|
|
72
|
-
|
73
|
-
|
53
|
+
Subcommands:
|
54
|
+
groups Groups
|
55
|
+
hosts Hosts
|
56
|
+
clusters Clusters
|
57
|
+
alerts Alerts
|
58
|
+
snapshots Snapshots
|
59
|
+
restorejobs Restorejobs
|
74
60
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
61
|
+
Options:
|
62
|
+
-h, --help print help
|
63
|
+
-u, --username <string> MMS user
|
64
|
+
-k, --apikey <string> MMS api-key
|
65
|
+
-a, --apiurl <string> MMS api url. Full url including version: https://mms.mydomain.tld/api/public/v1.0
|
66
|
+
-v, --version Version
|
67
|
+
-g, --default-group-id <string> Default MMS group id
|
68
|
+
-c, --default-cluster-id <string> Default MMS cluster id
|
69
|
+
--cfg <string> Config file path
|
70
|
+
-i, --ignore Ignore flag of --group-id and -cluster-id (default: false)
|
71
|
+
-j, --json Print JSON output (default: false)
|
72
|
+
-l, --limit <integer> Limit for result items
|
73
|
+
```
|
79
74
|
|
80
|
-
|
81
|
-
snapshot = snapshot_list.first
|
75
|
+
`mms-api` reads default configuration from your home directory `~/.mms-api`. Example configuration:
|
82
76
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
job_list = snapshot.create_restorejob
|
77
|
+
```
|
78
|
+
username=sysadmin@example.tld
|
79
|
+
apikey=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
|
80
|
+
# apiurl=https://mms.mydomain.tld/api/public/v1.0
|
81
|
+
default_group_id=your-group-id
|
82
|
+
default_cluster_id=your-cluster-id
|
90
83
|
```
|
data/bin/mms-api
CHANGED
@@ -1,151 +1,6 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
|
4
|
-
|
5
4
|
require 'mms'
|
6
|
-
require 'optparse'
|
7
|
-
require 'terminal-table'
|
8
|
-
require 'parseconfig'
|
9
|
-
require 'pathname'
|
10
|
-
|
11
|
-
actions_available = ["groups", "hosts", "clusters", "snapshots", "restorejobs", "restorejobs-create"]
|
12
|
-
|
13
|
-
app_name = 'mms-api'
|
14
|
-
app_dscr = "#{app_name} is a tool for accessing MMS API"
|
15
|
-
app_usage = "#{app_name} command [options]"
|
16
|
-
app_version = MMS::VERSION
|
17
|
-
app_commands = "#{actions_available.join(' | ')}"
|
18
|
-
|
19
|
-
config = {}
|
20
|
-
options = {}
|
21
|
-
|
22
|
-
config_file = Pathname.new(Dir.home) + app_name.prepend('.')
|
23
|
-
if config_file.exist?
|
24
|
-
config = ParseConfig.new(config_file)
|
25
|
-
end
|
26
|
-
|
27
|
-
optparse = OptionParser.new do |opts|
|
28
|
-
opts.banner = "#{app_dscr}\n\nUsage:\n\n\t#{app_usage}\n\nCommands:\n\n\t#{app_commands}\n\nOptions:\n\n"
|
29
|
-
|
30
|
-
options[:username] = config['username']
|
31
|
-
opts.on("-u", "--username <string>", "MMS user") do |u|
|
32
|
-
options[:username] = u
|
33
|
-
end
|
34
|
-
|
35
|
-
options[:apikey] = config['apikey']
|
36
|
-
opts.on("-k", "--apikey <string>", "MMS api-key") do |k|
|
37
|
-
options[:apikey] = k
|
38
|
-
end
|
39
|
-
|
40
|
-
options[:apiurl] = config['apiurl']
|
41
|
-
opts.on("-a", "--apiurl <string>", "MMS api url. Full url including version: https://mms.mydomain.tld/api/public/v1.0") do |a|
|
42
|
-
options[:apiurl] = a
|
43
|
-
end
|
44
|
-
|
45
|
-
options[:name] = '.*'
|
46
|
-
opts.on("-n", "--name <string>", "Filter for resource name using regexp") do |n|
|
47
|
-
options[:name] = n
|
48
|
-
end
|
49
|
-
|
50
|
-
options[:limit] = config['limit'] || 5
|
51
|
-
opts.on("-l", "--limit <int>", "Limit for result items") do |l|
|
52
|
-
options[:limit] = l.to_i
|
53
|
-
end
|
54
|
-
|
55
|
-
opts.on("-v", "--version", "Version") do |v|
|
56
|
-
puts "#{app_name} v#{app_version}"
|
57
|
-
exit
|
58
|
-
end
|
59
|
-
|
60
|
-
opts.on("-h", "--help", "Show this help") do |h|
|
61
|
-
puts opts
|
62
|
-
exit
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
begin
|
67
|
-
optparse.parse!
|
68
|
-
if not options[:username]
|
69
|
-
puts "Missing options: MMS username [-u <string>]"
|
70
|
-
exit
|
71
|
-
end
|
72
|
-
if not options[:apikey]
|
73
|
-
puts "Missing options: MMS api-key [-k <string>]"
|
74
|
-
exit
|
75
|
-
end
|
76
|
-
rescue OptionParser::InvalidOption, OptionParser::MissingArgument
|
77
|
-
puts $!.to_s
|
78
|
-
puts optparse
|
79
|
-
exit
|
80
|
-
end
|
81
|
-
|
82
|
-
begin
|
83
|
-
action = (ARGV.first || config['action'] || '').downcase
|
84
|
-
raise("Unknown action #{action.upcase}") unless actions_available.include? (action)
|
85
|
-
rescue => e
|
86
|
-
puts "Error: #{e.message}"
|
87
|
-
puts "Available actions: #{(actions_available.join ', ').upcase}"
|
88
|
-
puts optparse
|
89
|
-
exit 1
|
90
|
-
end
|
91
|
-
|
92
|
-
begin
|
93
|
-
ARGV.shift
|
94
|
-
agent = MMS::Agent.new(options[:username], options[:apikey])
|
95
|
-
if options[:apiurl] then
|
96
|
-
agent.set_apiurl(options[:apiurl])
|
97
|
-
end
|
98
|
-
|
99
|
-
results = agent.send action.sub('-', '_'), *ARGV
|
100
|
-
results.select! { |resource| !resource.name.match(Regexp.new(options[:name])).nil? }
|
101
|
-
|
102
|
-
rows = []
|
103
|
-
case action
|
104
|
-
when 'groups'
|
105
|
-
heading = ['Name', 'Active Agents', 'Replicas count', 'Shards count', 'Last Active Agent', 'GroupId']
|
106
|
-
results.each do |group|
|
107
|
-
rows << [group.name, group.active_agent_count, group.replicaset_count, group.shard_count, group.last_active_agent, group.id]
|
108
|
-
end
|
109
|
-
when 'hosts'
|
110
|
-
heading = ['Group', 'Type', 'Hostname', 'IP', 'Port', 'Last ping', 'Alerts enabled', 'HostId', 'Shard', 'Replica']
|
111
|
-
results.each do |host|
|
112
|
-
rows << [host.group.name, host.type_name, host.name, host.ip_address, host.port, host.last_ping, host.alerts_enabled, host.id, host.shard_name, host.replicaset_name]
|
113
|
-
end
|
114
|
-
when 'clusters'
|
115
|
-
heading = ['Group', 'Cluster', 'Shard name', 'Replica name', 'Type', 'Last heartbeat', 'Cluster Id']
|
116
|
-
results.each do |cluster|
|
117
|
-
rows << [cluster.group.name, cluster.name, cluster.shard_name, cluster.replicaset_name, cluster.type_name, cluster.last_heartbeat, cluster.id]
|
118
|
-
end
|
119
|
-
when 'snapshots'
|
120
|
-
heading = ['Group', 'Cluster', 'SnapshotId', 'Complete', 'Created increment', 'Name (created date)', 'Expires']
|
121
|
-
results_sorted = results.sort_by { |snapshot| snapshot.created_date }.reverse
|
122
|
-
results_sorted.first(options[:limit]).each do |snapshot|
|
123
|
-
rows << [snapshot.cluster.group.name, snapshot.cluster.name, snapshot.id, snapshot.complete, snapshot.created_increment, snapshot.name, snapshot.expires]
|
124
|
-
rows << :separator
|
125
|
-
part_count = 0
|
126
|
-
snapshot.parts.each do |part|
|
127
|
-
file_size_mb = part['fileSizeBytes'].to_i / (1024*1024)
|
128
|
-
rows << [{:value => "part #{part_count}", :colspan => 4, :alignment => :right}, part['typeName'], part['replicaSetName'], "#{file_size_mb} MB"]
|
129
|
-
part_count += 1
|
130
|
-
end
|
131
|
-
rows << :separator
|
132
|
-
end
|
133
|
-
when 'restorejobs', 'restorejobs-create'
|
134
|
-
heading = ['RestoreId', 'SnapshotId / Cluster / Group', 'Name (created)', 'Status', 'Point in time', 'Delivery', 'Restore status']
|
135
|
-
results_sorted = results.sort_by { |job| job.created }.reverse
|
136
|
-
results_sorted.first(options[:limit]).each do |job|
|
137
|
-
rows << [job.id, job.snapshot_id, job.name, job.status_name, job.point_in_time, job.delivery_method_name, job.delivery_status_name]
|
138
|
-
rows << ['', "#{job.cluster.name} (#{job.cluster.id})", {:value => '', :colspan => 5}]
|
139
|
-
rows << ['', job.cluster.group.name, {:value => '', :colspan => 5}]
|
140
|
-
rows << [{:value => 'download url:', :colspan => 7}]
|
141
|
-
rows << [{:value => job.delivery_url, :colspan => 7}]
|
142
|
-
rows << :separator
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
puts Terminal::Table.new :title => action.upcase, :headings => (heading.nil? ? [] : heading), :rows => rows
|
147
5
|
|
148
|
-
|
149
|
-
puts "Error: `#{e.message}`"
|
150
|
-
exit 1
|
151
|
-
end
|
6
|
+
MMS::CLI::CommandManager.run
|
data/lib/mms.rb
CHANGED
@@ -1,15 +1,28 @@
|
|
1
1
|
module MMS
|
2
2
|
require 'rubygems' # For ruby < 1.9
|
3
3
|
|
4
|
+
require "uri"
|
5
|
+
require "json"
|
6
|
+
require "cgi"
|
7
|
+
require "net/http"
|
8
|
+
require 'net/http/digest_auth'
|
9
|
+
require 'terminal-table'
|
10
|
+
require 'pathname'
|
11
|
+
|
12
|
+
require 'mms/config'
|
4
13
|
require 'mms/agent'
|
5
14
|
require 'mms/client'
|
6
15
|
require 'mms/cache'
|
7
16
|
require 'mms/version'
|
8
17
|
require 'mms/resource'
|
18
|
+
require 'mms/errors'
|
9
19
|
|
10
20
|
require 'mms/resource/group'
|
11
21
|
require 'mms/resource/host'
|
12
22
|
require 'mms/resource/cluster'
|
13
23
|
require 'mms/resource/snapshot'
|
14
24
|
require 'mms/resource/restore_job'
|
25
|
+
require 'mms/resource/alert'
|
26
|
+
|
27
|
+
require 'mms/cli'
|
15
28
|
end
|
data/lib/mms/agent.rb
CHANGED
@@ -2,37 +2,18 @@ module MMS
|
|
2
2
|
|
3
3
|
class Agent
|
4
4
|
|
5
|
-
|
6
|
-
|
5
|
+
attr_accessor :client
|
6
|
+
|
7
|
+
def initialize(client)
|
8
|
+
@client = client
|
7
9
|
end
|
8
10
|
|
9
11
|
def set_apiurl(apiurl)
|
10
|
-
|
11
|
-
url_info = URI(apiurl)
|
12
|
-
rescue URI::InvalidURIError
|
13
|
-
puts "Unable to parse given apiurl: #{apiurl}"
|
14
|
-
exit
|
15
|
-
end
|
16
|
-
|
17
|
-
# Split out version from URL path
|
18
|
-
path_parts = url_info.path.split '/'
|
19
|
-
api_version = path_parts.pop
|
20
|
-
url_info.path = path_parts.join '/'
|
21
|
-
|
22
|
-
# Update client singleton
|
23
|
-
MMS::Client.instance.api_protocol = url_info.scheme
|
24
|
-
MMS::Client.instance.api_host = url_info.host
|
25
|
-
MMS::Client.instance.api_port = url_info.port
|
26
|
-
MMS::Client.instance.api_path = url_info.path
|
27
|
-
MMS::Client.instance.api_version = api_version
|
12
|
+
@client.url = apiurl
|
28
13
|
end
|
29
14
|
|
30
15
|
def groups
|
31
|
-
|
32
|
-
MMS::Client.instance.get('/groups').each do |group|
|
33
|
-
group_list.push MMS::Resource::Group.new group['id'], group
|
34
|
-
end
|
35
|
-
group_list
|
16
|
+
MMS::Resource::Group.findGroups(@client)
|
36
17
|
end
|
37
18
|
|
38
19
|
def hosts
|
@@ -56,7 +37,15 @@ module MMS
|
|
56
37
|
clusters.each do |cluster|
|
57
38
|
snapshot_list.concat cluster.snapshots
|
58
39
|
end
|
59
|
-
snapshot_list
|
40
|
+
snapshot_list.sort_by { |snapshot| snapshot.created_date }.reverse
|
41
|
+
end
|
42
|
+
|
43
|
+
def alerts
|
44
|
+
alert_list = []
|
45
|
+
groups.each do |group|
|
46
|
+
alert_list.concat group.alerts
|
47
|
+
end
|
48
|
+
alert_list.sort_by { |alert| alert.created }.reverse
|
60
49
|
end
|
61
50
|
|
62
51
|
def restorejobs
|
@@ -64,15 +53,36 @@ module MMS
|
|
64
53
|
clusters.each do |cluster|
|
65
54
|
restorejob_list.concat cluster.restorejobs
|
66
55
|
end
|
67
|
-
restorejob_list
|
56
|
+
restorejob_list.sort_by { |job| job.created }.reverse
|
68
57
|
end
|
69
58
|
|
70
|
-
def
|
71
|
-
|
59
|
+
def restorejob_create(type_value, group_id, cluster_id)
|
60
|
+
if type_value.length == 24
|
61
|
+
findGroup(group_id).cluster(cluster_id).snapshot(type_value).create_restorejob
|
62
|
+
elsif datetime = (type_value == 'now' ? DateTime.now : DateTime.parse(type_value))
|
63
|
+
raise('Invalid datetime. Correct `YYYY-MM-RRTH:m:s`') if datetime.nil?
|
64
|
+
datetime_string = [[datetime.year, datetime.day, datetime.month].join('-'), 'T', [datetime.hour, datetime.minute, datetime.second].join(':'), 'Z'].join
|
65
|
+
findGroup(group_id).cluster(cluster_id).create_restorejob(datetime_string)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def alert_ack(alert_id, timestamp, group_id)
|
70
|
+
timestamp = DateTime.now if timestamp == 'now'
|
71
|
+
timestamp = DateTime.new(4000, 1, 1, 1, 1, 1, 1, 1) if timestamp == 'forever'
|
72
|
+
|
73
|
+
group = findGroup(group_id)
|
74
|
+
|
75
|
+
if alert_id == 'all'
|
76
|
+
group.alerts.each do |alert|
|
77
|
+
alert.ack(timestamp, 'Triggered by CLI for all alerts.')
|
78
|
+
end
|
79
|
+
elsif group.alert(alert_id).ack(timestamp, 'Triggered by CLI.')
|
80
|
+
end
|
72
81
|
end
|
73
82
|
|
74
83
|
def findGroup(id)
|
75
|
-
MMS::Resource::Group.new id
|
84
|
+
MMS::Resource::Group.new(@client, {'id' => id})
|
76
85
|
end
|
86
|
+
|
77
87
|
end
|
78
88
|
end
|
data/lib/mms/cli.rb
ADDED
@@ -0,0 +1,258 @@
|
|
1
|
+
require 'clamp'
|
2
|
+
require 'parseconfig'
|
3
|
+
|
4
|
+
module MMS
|
5
|
+
|
6
|
+
class CLI
|
7
|
+
|
8
|
+
class MMS::CLI::Command < Clamp::Command
|
9
|
+
|
10
|
+
attr_accessor :app_name
|
11
|
+
attr_accessor :config
|
12
|
+
|
13
|
+
attr_accessor :client
|
14
|
+
attr_accessor :agent
|
15
|
+
|
16
|
+
option ['-u', '--username'], '<string>', 'MMS user' do |u|
|
17
|
+
@config.username = u
|
18
|
+
end
|
19
|
+
|
20
|
+
option ['-k', '--apikey'], '<string>', 'MMS api-key' do |a|
|
21
|
+
@config.apikey = a
|
22
|
+
end
|
23
|
+
|
24
|
+
option ['-a', '--apiurl'], '<string>', 'MMS api url. Full url including version: https://mms.mydomain.tld/api/public/v1.0' do |u|
|
25
|
+
@config.apiurl = u
|
26
|
+
end
|
27
|
+
|
28
|
+
option ['-v', '--version'], :flag, 'Version' do |v|
|
29
|
+
puts "mms-api v#{MMS::VERSION}"
|
30
|
+
exit
|
31
|
+
end
|
32
|
+
|
33
|
+
option ['-g', '--default-group-id'], '<string>', 'Default MMS group id' do |g|
|
34
|
+
@config.default_group_id = g
|
35
|
+
end
|
36
|
+
|
37
|
+
option ['-c', '--default-cluster-id'], '<string>', 'Default MMS cluster id' do |c|
|
38
|
+
@config.default_cluster_id = c
|
39
|
+
end
|
40
|
+
|
41
|
+
option ['--cfg'], '<string>', 'Config file path' do |p|
|
42
|
+
@config.config_path = p
|
43
|
+
parse_user_home_config
|
44
|
+
end
|
45
|
+
|
46
|
+
option ['-i', '--ignore'], :flag, 'Ignore flag of --group-id and -cluster-id', :default => false
|
47
|
+
|
48
|
+
option ['-j', '--json'], :flag, 'Print JSON output', :default => false
|
49
|
+
|
50
|
+
option ['-l', '--limit'], '<integer>', 'Limit for result items' do |l|
|
51
|
+
@config.limit = Integer(l)
|
52
|
+
end
|
53
|
+
|
54
|
+
def initialize(invocation_path, context = {}, parent_attribute_values = {})
|
55
|
+
@config ||= MMS::Config.new
|
56
|
+
end
|
57
|
+
|
58
|
+
def parse_user_home_config
|
59
|
+
raise(MMS::ConfigError.new('Config file path is not set!')) if @config.config_path.nil?
|
60
|
+
config_file = Pathname.new(@config.config_path)
|
61
|
+
raise(MMS::ConfigError.new("Config file `#{config_file}` does not exist")) unless config_file.exist?
|
62
|
+
|
63
|
+
config = ParseConfig.new(config_file)
|
64
|
+
config.params.map do |key, value|
|
65
|
+
begin
|
66
|
+
@config.send("#{key}=", value)
|
67
|
+
rescue Exception => e
|
68
|
+
raise MMS::ConfigError.new("Config option `#{key}` from file `#{config_file}` is not allowed!")
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
def agent
|
75
|
+
@client = MMS::Client.new(@config.username, @config.apikey)
|
76
|
+
@agent = MMS::Agent.new(client)
|
77
|
+
end
|
78
|
+
|
79
|
+
def print(heading, resource_list)
|
80
|
+
json? ? print_json(resource_list) : print_human(heading, resource_list)
|
81
|
+
end
|
82
|
+
|
83
|
+
def print_human(heading, resource_list)
|
84
|
+
rows = []
|
85
|
+
|
86
|
+
resource_list.first(@config.limit).each do |resource|
|
87
|
+
rows += resource.table_section
|
88
|
+
end
|
89
|
+
|
90
|
+
puts Terminal::Table.new :title => 'Hosts', :headings => (heading.nil? ? [] : heading), :rows => rows
|
91
|
+
|
92
|
+
print_tips unless ignore?
|
93
|
+
end
|
94
|
+
|
95
|
+
def print_json(resource_list)
|
96
|
+
rows = []
|
97
|
+
|
98
|
+
resource_list.first(@config.limit).each do |resource|
|
99
|
+
rows.push(resource.to_hash)
|
100
|
+
end
|
101
|
+
|
102
|
+
puts JSON.pretty_generate(rows)
|
103
|
+
end
|
104
|
+
|
105
|
+
def print_tips
|
106
|
+
puts 'Default group: ' + @config.default_group_id unless @config.default_group_id.nil?
|
107
|
+
puts 'Default cluster: ' + @config.default_cluster_id unless @config.default_cluster_id.nil?
|
108
|
+
|
109
|
+
if !@config.default_group_id.nil? or !@config.default_cluster_id.nil?
|
110
|
+
puts "Add flag --ignore or update --default-group-id, --default-cluster-id or update your `#{@config.config_path}` to see all resources"
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def run(arguments)
|
115
|
+
begin
|
116
|
+
parse_user_home_config
|
117
|
+
super
|
118
|
+
rescue Clamp::HelpWanted => e
|
119
|
+
raise(help)
|
120
|
+
rescue Clamp::UsageError => e
|
121
|
+
raise([e.message, help].join("\n"))
|
122
|
+
rescue MMS::AuthError => e
|
123
|
+
raise('Authorisation problem. Please check you credential!')
|
124
|
+
rescue MMS::ResourceError => e
|
125
|
+
raise(["Resource #{e.resource.class.name} problem:", e.message].join("\n"))
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
class MMS::CLI::Command::Hosts < MMS::CLI::Command
|
131
|
+
|
132
|
+
subcommand 'list', 'Host list' do
|
133
|
+
|
134
|
+
def execute
|
135
|
+
print(MMS::Resource::Host.table_header, agent.hosts)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
end
|
140
|
+
|
141
|
+
class MMS::CLI::Command::Groups < MMS::CLI::Command
|
142
|
+
|
143
|
+
subcommand 'list', 'Group list' do
|
144
|
+
|
145
|
+
def execute
|
146
|
+
|
147
|
+
group_list = agent.groups
|
148
|
+
group_list.reject! { |group| group.id != @config.default_group_id } unless @config.default_group_id.nil? or ignore?
|
149
|
+
|
150
|
+
print(MMS::Resource::Group.table_header, group_list)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
end
|
155
|
+
|
156
|
+
class MMS::CLI::Command::Clusters < MMS::CLI::Command
|
157
|
+
|
158
|
+
subcommand 'list', 'Cluster list' do
|
159
|
+
|
160
|
+
def execute
|
161
|
+
cluster_list = agent.clusters
|
162
|
+
cluster_list.reject! { |cluster| cluster.id != @config.default_cluster_id } unless @config.default_cluster_id.nil? or ignore?
|
163
|
+
|
164
|
+
print(MMS::Resource::Cluster.table_header, cluster_list)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
end
|
169
|
+
|
170
|
+
|
171
|
+
class MMS::CLI::Command::Alerts < MMS::CLI::Command
|
172
|
+
|
173
|
+
subcommand 'list', 'Alerts list' do
|
174
|
+
|
175
|
+
def execute
|
176
|
+
print(MMS::Resource::Alert.table_header, agent.alerts)
|
177
|
+
end
|
178
|
+
|
179
|
+
end
|
180
|
+
|
181
|
+
subcommand 'ack', 'Acknowledge alert' do
|
182
|
+
|
183
|
+
parameter '[alert-id]', 'Alert ID', :default => 'all'
|
184
|
+
parameter '[group-id]', 'Group ID', :default => '--default-group-id'
|
185
|
+
parameter '[timestamp]', 'Postpone to timestamp', :default => 'forever'
|
186
|
+
|
187
|
+
def execute
|
188
|
+
g_id = group_id == '--default-group-id' ? @config.default_group_id : group_id
|
189
|
+
agent.alert_ack(alert_id, timestamp, g_id)
|
190
|
+
puts 'Done.'
|
191
|
+
end
|
192
|
+
|
193
|
+
end
|
194
|
+
|
195
|
+
end
|
196
|
+
|
197
|
+
class MMS::CLI::Command::Snapshots < MMS::CLI::Command
|
198
|
+
|
199
|
+
subcommand 'list', 'Snapshot list' do
|
200
|
+
|
201
|
+
def execute
|
202
|
+
print(MMS::Resource::Snapshot.table_header, agent.snapshots)
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
end
|
207
|
+
|
208
|
+
class MMS::CLI::Command::RestoreJobs < MMS::CLI::Command
|
209
|
+
|
210
|
+
subcommand 'list', 'Restorejob list' do
|
211
|
+
|
212
|
+
def execute
|
213
|
+
print(MMS::Resource::RestoreJob.table_header, agent.restorejobs)
|
214
|
+
end
|
215
|
+
|
216
|
+
end
|
217
|
+
|
218
|
+
subcommand 'create', 'Restorejob create' do
|
219
|
+
|
220
|
+
parameter '[snapshot-source]', 'Restore from source. Options: now | timestamp | snapshot-id', :default => 'now'
|
221
|
+
parameter '[group-id]', 'Group ID', :default => '--default-group-id'
|
222
|
+
parameter '[cluster-id]', 'Cluster ID', :default => '--default-cluster-id'
|
223
|
+
|
224
|
+
def execute
|
225
|
+
g_id = group_id == '--default-group-id' ? @config.default_group_id : group_id
|
226
|
+
c_id = cluster_id == '--default-cluster-id' ? @config.default_cluster_id : cluster_id
|
227
|
+
|
228
|
+
agent.restorejob_create(snapshot_source, g_id, c_id)
|
229
|
+
|
230
|
+
puts 'Done.'
|
231
|
+
end
|
232
|
+
|
233
|
+
end
|
234
|
+
|
235
|
+
end
|
236
|
+
|
237
|
+
class MMS::CLI::CommandManager < MMS::CLI::Command
|
238
|
+
|
239
|
+
def run(arguments)
|
240
|
+
begin
|
241
|
+
super
|
242
|
+
rescue Exception => e
|
243
|
+
abort(e.message.empty? ? 'Unknown error/Interrupt' : e.message)
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
subcommand 'groups', 'Groups ', MMS::CLI::Command::Groups
|
248
|
+
subcommand 'hosts', 'Hosts', MMS::CLI::Command::Hosts
|
249
|
+
subcommand 'clusters', 'Clusters', MMS::CLI::Command::Clusters
|
250
|
+
subcommand 'alerts', 'Alerts', MMS::CLI::Command::Alerts
|
251
|
+
subcommand 'snapshots', 'Snapshots', MMS::CLI::Command::Snapshots
|
252
|
+
subcommand 'restorejobs', 'Restorejobs', MMS::CLI::Command::RestoreJobs
|
253
|
+
|
254
|
+
end
|
255
|
+
|
256
|
+
end
|
257
|
+
|
258
|
+
end
|