lmcadm 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,396 @@
1
+ require 'gli'
2
+ module LMCAdm
3
+ include GLI::App
4
+ extend self
5
+
6
+ subcommand_option_handling :normal
7
+
8
+ desc 'Work on accounts (invite users)'
9
+ command :account do |c|
10
+ c.desc 'Account type (DISTRIBUTION, ...)'
11
+ c.flag :t, :account_type
12
+
13
+ c.desc 'List accounts'
14
+ c.command :list do |account_list|
15
+ account_list.desc 'Show detailed info (long)'
16
+ account_list.switch :l, :long
17
+ account_list.action do |global_options, options, args|
18
+ t = ProgressVisualizer.new "Cloud login"
19
+ lmcen = LMC::Cloud.new(global_options[:cloud_host], global_options[:user], global_options[:password])
20
+ t.done
21
+ t = ProgressVisualizer.new "Getting accounts"
22
+ accounts = lmcen.get_accounts_objects
23
+ t.done
24
+ if options[:account_type]
25
+ accounts = accounts.select do |a|
26
+ a["type"] == options[:account_type]
27
+ end
28
+ end
29
+ accounts.sort {|a, b| a["name"] <=> b["name"]}.each do |account|
30
+ puts account.inspect if global_options[:v]
31
+ if options[:l]
32
+ puts account["name"] + " (" + account["type"] + ") ID: " + account["id"]
33
+ else
34
+ puts account["name"] + " (" + account["type"] + ")"
35
+ end
36
+ end
37
+ puts accounts.length.to_s + " Accounts found"
38
+ end
39
+ end
40
+
41
+ c.desc 'Show account'
42
+ c.arg_name 'UUID|Name'
43
+ c.command :show do |account_show|
44
+
45
+ account_show.action do |global_options, options, args|
46
+ a = LMC::Account.get_by_uuid_or_name(args[0])
47
+ puts a.inspect
48
+ a.sites.each do |site|
49
+ puts "#{site} - #{site.account}"
50
+ end
51
+ end
52
+ end
53
+
54
+ c.desc 'Show device config state summary for account'
55
+ c.arg_name 'UUID|Name'
56
+ c.command :configstates do |account_configstates|
57
+ account_configstates.action do |global_options, options, args|
58
+ account = LMC::Account.get_by_uuid_or_name args[0]
59
+ states = account.config_updatestates
60
+ puts "Current: #{states.actual}, outdated: #{states.outdated}"
61
+ end
62
+ end
63
+
64
+ c.desc 'Show account sites'
65
+ c.arg_name 'UUID|Name', [:required]
66
+ c.command :sites do |c|
67
+ c.action do |global_options, options, args|
68
+ account = LMC::Account.get_by_uuid_or_name args[0]
69
+ sites = account.sites
70
+ sites.each do |site|
71
+ puts "#{site} - Config current: #{site.configstates.actual}, outdated: #{site.configstates.outdated}"
72
+ end
73
+ end
74
+ end
75
+
76
+ c.arg_name "name"
77
+ c.desc 'Create new accounts'
78
+ c.command :create do |account_create|
79
+
80
+ account_create.desc 'parent uuid|name'
81
+ account_create.flag :p
82
+ account_create.action do |global_options, options, args|
83
+ parent = LMC::Account.get_by_uuid_or_name options[:p]
84
+ t = ProgressVisualizer.new "Creating object"
85
+ a = LMC::Account.new({"name" => args.first,
86
+ "type" => options[GLI::Command::PARENT][:account_type],
87
+ "parent" => parent.id})
88
+ t.done
89
+ t = ProgressVisualizer.new "Saving #{a.name}"
90
+ result = a.save
91
+ t.done
92
+ puts a.id
93
+ puts result.inspect if global_options[:debug]
94
+
95
+
96
+ end
97
+ end
98
+
99
+
100
+ c.arg_name 'UUID|Name'
101
+ c.desc 'Delete account'
102
+ c.command :delete do |account_del|
103
+ account_del.desc 'Treat argument as pattern against account name'
104
+ account_del.switch :e
105
+ account_del.action do |global_options, options, args|
106
+ t = ProgressVisualizer.new "Getting accounts"
107
+ if options[:e]
108
+ accounts = LMC::Cloud.instance.get_accounts_objects
109
+ matched_accounts = accounts.select {|account| /#{args.first}/.match(account.name)}
110
+ else
111
+ matched_accounts = [LMC::Account.get_by_uuid_or_name(args.first)]
112
+ end
113
+ t.done
114
+ puts 'Accounts to delete:'
115
+ puts matched_accounts.map {|a| "#{a.id} - #{a.name}"}.join("\n")
116
+ print('Type yes to confirm: ')
117
+ exit unless STDIN.gets.chomp == 'yes'
118
+ t = ProgressVisualizer.new "Deleting accounts"
119
+ matched_accounts.each do |a|
120
+ a.delete
121
+ t.dot
122
+ end
123
+ t.done
124
+ end
125
+ end
126
+
127
+ c.arg_name 'UUID|Name'
128
+ c.desc 'Show account logs'
129
+ c.command :logs do |account_show|
130
+ account_show.action do |global_options, options, args|
131
+ t = ProgressVisualizer.new "Getting account"
132
+ a = LMC::Account.get_by_uuid_or_name(args.first)
133
+ t.done
134
+ t = ProgressVisualizer.new "Getting account logs"
135
+ logs = a.logs
136
+ t.done
137
+ logs.each do |line|
138
+ puts "#{line['created']} #{line['message']}"
139
+ end
140
+ end
141
+ end
142
+
143
+ c.arg_name "new name"
144
+ c.desc 'Rename account'
145
+ c.command :rename do |account_rename|
146
+ account_rename.desc 'Account uuid|name'
147
+ account_rename.flag :A, :account
148
+ account_rename.action do |global_options, options, args|
149
+ account = LMC::Account.get_by_uuid_or_name options[:account]
150
+ account.name = args.first
151
+ account.save
152
+ end
153
+ end
154
+
155
+
156
+ c.arg_name 'UUID|Name'
157
+ c.desc 'List account members'
158
+ c.command :memberlist do |memberlist|
159
+ memberlist.action do |global_options, options, args|
160
+ account = LMC::Account.get_by_uuid_or_name args.first
161
+ members = account.members
162
+ tp members, [{:id => {:width => 36}}, :name, :type, :state, :invitationState, :principalState, :authorities]
163
+ end
164
+ end
165
+
166
+ # Commented out due to bad quality at the moment.
167
+ # This code is untested after the gem split.
168
+ =begin
169
+ c.desc 'Modify account members'
170
+ c.command :memberupdate do |memberupdate|
171
+ memberupdate.desc 'Account UUID'
172
+ memberupdate.flag :a, "account-uuid", :required => true
173
+
174
+ memberupdate.desc 'Principal UUID'
175
+ memberupdate.flag :u, :uuid, :required => true
176
+
177
+ memberupdate.desc 'Authority'
178
+ memberupdate.flag :authority
179
+
180
+ memberupdate.action do |global_options, options, args|
181
+ account = LMCAccount.get options[:a]
182
+
183
+ account_authorities = account.authorities
184
+ puts account_authorities.inspect if global_options[:debug]
185
+ authority = account_authorities.find {|auth| auth["name"] == options[:authority]}
186
+ puts authority if global_options[:debug]
187
+ puts "authority id: #{authority["id"]}" if global_options[:debug]
188
+
189
+ resp = account.update_member options[:u], {:authorities => [authority["id"]]}
190
+ puts "response: #{resp.inspect}"
191
+ if resp.code == 200
192
+ puts "Updated success"
193
+
194
+ else
195
+ loggerr.error "ERROR: #{resp.code} #{resp.body.message}"
196
+ end
197
+
198
+ end
199
+ end
200
+
201
+ c.arg_name "email address", [:optional, :multiple]
202
+ c.desc 'Invite members, requires an account type'
203
+ c.command :invite do |account_invite|
204
+
205
+ account_invite.desc "File with email addresses; one address per line"
206
+ account_invite.flag :file
207
+ #account_invite.desc "Email address to invite"
208
+ #account_invite.flag :
209
+ account_invite.desc 'Account name'
210
+ account_invite.flag :account_name, :A, :required => true
211
+
212
+ account_invite.desc 'Member type'
213
+ account_invite.default_value "MEMBER"
214
+ account_invite.flag :member_type
215
+
216
+ account_invite.desc 'Authority'
217
+ account_invite.default_value "PROJECT_VIEWER"
218
+ account_invite.flag :authority
219
+
220
+ account_invite.desc 'Do not make changes'
221
+ account_invite.switch :n, :dry
222
+
223
+ account_invite.desc "Send emails (deprecated)"
224
+ account_invite.default_value false
225
+ account_invite.switch :e, "send-mail", "send-email"
226
+
227
+ account_invite.default_value false
228
+ account_invite.switch "show-csv"
229
+
230
+ account_invite.action do |global_options, options, args|
231
+
232
+ puts "ARGS:" + args.inspect if global_options[:debug]
233
+ lmcen = Cloud.new(global_options[:cloud_host], global_options[:user], global_options[:password])
234
+ t = ProgressVisualizer.new "Getting account"
235
+ account = lmcen.get_account options[:account_name], options[GLI::Command::PARENT][:account_type]
236
+ t.done
237
+
238
+ am = AccountManager.new(options, global_options)
239
+ if options[:file]
240
+ t = ProgressVisualizer.new "Inviting from file"
241
+ File.foreach(options[:file]) do |line|
242
+ if nil != am.invite(lmcen, account, line, options[:member_type], options[:authority])
243
+ t.dot
244
+ else
245
+ t.X
246
+ end
247
+ end
248
+ t.done
249
+ end
250
+ if not args.empty?
251
+ t = ProgressVisualizer.new "Inviting from arguments"
252
+ args.each do |line|
253
+ if nil != am.invite(lmcen, account, line, options[:member_type], options[:authority])
254
+ t.dot
255
+ else
256
+ t.X
257
+ end
258
+ end
259
+ t.done
260
+ end
261
+ loggerr.info am.errors unless am.errors.empty?
262
+
263
+ end
264
+ end
265
+ =end
266
+ c.arg_name "Account name|UUID", [:required]
267
+ c.desc 'List authorities'
268
+ c.command :authorities do |auth|
269
+
270
+ auth.action do |g, o, args|
271
+ account = LMC::Account.get_by_uuid_or_name args.first
272
+ authorities = account.authorities
273
+ puts authorities.first.inspect
274
+ max = Helpers::longest_in_collection(authorities.map {|a| a.name})
275
+ puts max
276
+ tp authorities, [{:id => {:width => 36}}, {:name => {:width => max}}, :visibility, :type]
277
+ end
278
+ end
279
+
280
+ c.arg_name "email address", [:multiple]
281
+ c.desc 'Invite members, requires an account type'
282
+ c.command :invite do |account_invite|
283
+ account_invite.flag :A, :account, :required => true
284
+ account_invite.flag :r, :role, :required => true
285
+ account_invite.flag :t, :type, :required => true
286
+ account_invite.action do |global_options, options, args|
287
+ account = LMC::Account.get_by_uuid_or_name options[:account]
288
+ cloud = LMC::Cloud.instance
289
+ chosen_authorities = account.authorities.select {|auth| auth.name == options[:role]}
290
+ args.each do |email|
291
+ cloud.invite_user_to_account email, account.id, options[:type], chosen_authorities
292
+ end
293
+ end
294
+
295
+ end
296
+
297
+ c.desc 'Leave account'
298
+ c.arg_name "Account id"
299
+ c.command :leave do |leave|
300
+ leave.action do |global_options, options, args|
301
+ account = LMC::Account.get_by_uuid(args[0])
302
+ puts "Leave account \"#{account.name}\""
303
+ puts account.remove_membership_self
304
+ end
305
+ end
306
+
307
+ c.arg_name 'member name', [:required]
308
+ c.desc 'Add Member'
309
+ c.command :memberadd do |ma|
310
+ ma.flag :A, :account, :required => true
311
+ ma.action do |global_options, options, args|
312
+ account = LMC::Account.get_by_uuid_or_name(options[:account])
313
+ membership = LMC::Membership.new
314
+ membership.name = args.first
315
+ membership.type = "MEMBER"
316
+ membership.state = "ACTIVE"
317
+ membership.authorities = [LMC::Authority::CLOUD_SERVICE_DEVICE_PRIVATE_CLOUD]
318
+ puts membership.to_json
319
+ c = LMC::Cloud.instance
320
+ target_account = LMC::Account.get_by_uuid_or_name options[:account]
321
+ c.auth_for_account LMC::Account.get LMC::Account::ROOT_ACCOUNT_UUID
322
+ c.post ['cloud-service-auth', 'accounts', target_account.id, 'members'], membership
323
+ end
324
+ end
325
+
326
+
327
+ c.arg_name 'member name', [:required]
328
+ c.desc 'Remove member from account'
329
+ c.command :memberremove do |memberremove|
330
+ memberremove.flag :A, :account, :required => true
331
+ memberremove.action do |global_options, options, args|
332
+ account = LMC::Account.get_by_uuid_or_name(options[:account])
333
+ membership = account.find_member_by_name args.first
334
+ puts "Leave account \"#{account.name}\""
335
+ puts account.remove_membership membership.id
336
+ end
337
+ end
338
+
339
+
340
+ # IF the special flag is set, do weird license magic for ninden
341
+ # * Für alle unterliegenden Organisationen
342
+ # -> POST /accounts/{accountId}/conditions/device-operation
343
+ # -> "templateAllowTestAllocation": true,
344
+ # -> "templateTestDuration": 30
345
+ # * Für alle unterliegenden Projekte
346
+ # -> POST /accounts/{accountId}/conditions/device-operation
347
+ # -> "allowTestAllocation": true,
348
+ # -> "testDuration": 30
349
+ c.desc 'List account children'
350
+ c.arg_name 'UUID|Name'
351
+ c.command :children do |children|
352
+ children.flag :special
353
+ children.action do |global_options, options, args|
354
+ account = LMC::Account.get_by_uuid_or_name args.first
355
+ cloud = LMC::Cloud.instance
356
+
357
+ # def recurse_print_children(root_acc, account, options, cloud)
358
+ # children = root_acc.children_for_account_id account.id
359
+ # children.each do |child|
360
+ # puts "\n#{child["name"]} #{child["id"]} #{child["type"]}"
361
+ # if options[:special] == "read"
362
+ # puts cloud.get ["cloud-service-licenses", "accounts", child["id"], "conditions"]
363
+ # end
364
+ # if options[:special] == "write"
365
+ # if child["type"] == "ORGANIZATION"
366
+ # #puts cloud.get ["cloud-service-licenses", "accounts", child["id"], "conditions", "device-operation"]
367
+ # puts cloud.post ["cloud-service-licenses", "accounts", child["id"], "conditions", "device-operation"], {"templateAllowTestAllocation" => true, "templateTestDuration": 30}
368
+ # end
369
+ # if child["type"] == "PROJECT"
370
+ # #puts cloud.get ["cloud-service-licenses", "accounts", child["id"], "conditions", "device-operation"]
371
+ # puts cloud.post ["cloud-service-licenses", "accounts", child["id"], "conditions", "device-operation"], {"allowTestAllocation" => true, "testDuration": 30}
372
+ # end
373
+ #
374
+ # end
375
+ # end
376
+ # children.each do |child|
377
+ # childacc = LMCAccount.new(child)
378
+ # recurse_print_children(childacc, childacc, options, cloud)
379
+ # end
380
+ # end
381
+ #recurse_print_children(account, account, options, cloud)
382
+ def recurse_childen account, indent_level
383
+ children = account.children
384
+ children.each do |child|
385
+ puts ' ' * indent_level + child.to_s
386
+ begin
387
+ recurse_childen child, indent_level + 1
388
+ rescue RestClient::Forbidden => e
389
+ end
390
+ end
391
+ end
392
+ recurse_childen account, 0
393
+ end
394
+ end
395
+ end
396
+ end
@@ -0,0 +1,77 @@
1
+ require 'gli'
2
+ module LMCAdm
3
+ include GLI::App
4
+ extend self
5
+ subcommand_option_handling :normal
6
+
7
+ desc 'Generic lmc operations'
8
+ command :cloud do |c|
9
+ c.desc 'Check cloud connectivity'
10
+ c.action do |global_options|
11
+ lmcen = LMC::Cloud.new(global_options[:cloud_host], global_options[:user], global_options[:password])
12
+ puts "Base URL: #{lmcen.build_url}"
13
+ puts "Cloud connection OK" if lmcen.auth_ok
14
+ if global_options[:v]
15
+ puts "authentication token: " + lmcen.session_token
16
+ end
17
+ end
18
+ c.command :about do |cloud_about|
19
+ cloud_about.action do |global_options|
20
+ cloud = LMC::Cloud.instance
21
+ #account = cloud.get_account(nil, 'ROOT')
22
+ #puts account.inspect
23
+ #cloud.auth_for_accounts([account.id])
24
+ backstage_infos = cloud.get_backstage_serviceinfos.body.map {|info| {'serviceId' => info.serviceId,
25
+ 'instanceCount' => info.instanceCount,
26
+ 'versions' => info.versionInfoList.map {|vil| vil['version']}.uniq.join(",")
27
+ }}
28
+ tp backstage_infos
29
+
30
+ end
31
+ end
32
+
33
+ c.desc 'View and accept terms of use information'
34
+ c.command :tos do |tos|
35
+ tos.default_desc 'View terms of service'
36
+ tos.action do ||
37
+ begin
38
+ c = LMC::Cloud.instance
39
+ puts "No outstanding terms of service"
40
+ rescue LMC::OutdatedTermsOfUseException => e
41
+ puts e.response
42
+ end
43
+ end
44
+ tos.arg_name 'TOS name', :multiple => true
45
+ tos.desc 'Accept terms of use by name'
46
+ tos.command :accept do |accept|
47
+ accept.action do |global_options, options, args|
48
+ begin
49
+ cloud = LMC::Cloud.instance
50
+ rescue LMC::OutdatedTermsOfUseException => e
51
+ matched_tos = e.missing.select do |missingtos|
52
+ args.include? missingtos['name']
53
+ end
54
+ cloud = LMC::Cloud.instance authorize: false
55
+ puts "Accepting TOS #{matched_tos.to_s}"
56
+ cloud.accept_tos matched_tos
57
+ end
58
+ end
59
+ end
60
+ end
61
+ c.desc 'Change user information'
62
+ c.command :changeuser do |cloud_register|
63
+ cloud_register.switch :password
64
+ cloud_register.action do |global_options, options|
65
+ newdata = {}
66
+ if options[:password]
67
+ newpass = Helpers::read_pw "Enter new password for " + global_options[:user] + ":"
68
+ newpass_confirm = Helpers::read_pw "Confirm password " + global_options[:user] + ":"
69
+ raise 'Mismatch' unless newpass == newpass_confirm
70
+ newdata['password'] = newpass
71
+ end
72
+ user = LMC::User.new(newdata)
73
+ user.update(global_options[:password])
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,27 @@
1
+ module LMCAdm
2
+ command :maintenance do |maintenance|
3
+ maintenance.arg_name 'Account UUID'
4
+ maintenance.desc 'Resync license status in devices service for an account'
5
+ maintenance.command :licenseresync do |resync|
6
+ resync.action do |global_options, options, args|
7
+ root_account = LMC::Account.get LMC::Account::ROOT_ACCOUNT_UUID
8
+ cloud = LMC::Cloud.instance
9
+ cloud.auth_for_account root_account
10
+ result = cloud.post ['cloud-service-devices', 'maintenance', 'licenses', 'resync'], {'accountId' => args[0]}
11
+ end
12
+ end
13
+
14
+ maintenance.arg_name 'Account UUID'
15
+ maintenance.desc 'Enable scripting for an account'
16
+ maintenance.command :scripting do |scr|
17
+ scr.switch :enable
18
+ scr.action do |global_options, options, args|
19
+ root_account = LMC::Account.get LMC::Account::ROOT_ACCOUNT_UUID
20
+ cloud = LMC::Cloud.instance
21
+ cloud.auth_for_account root_account
22
+ result = cloud.put ['cloud-service-config', 'configroot', 'accounts', args.first, 'scriptauthority'], options[:enable]
23
+ raise "error - unexpected result" unless result.body == options[:enable]
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,47 @@
1
+ module LMCAdm
2
+ command :principal do |principal|
3
+ principal.desc "Create principal"
4
+ principal.arg_name 'Principal name'
5
+ principal.command :create do |pc|
6
+ pc.flag :t, :type, :required => true
7
+ pc.action do |global_options, options, args|
8
+ root_account = LMC::Account.get LMC::Account::ROOT_ACCOUNT_UUID
9
+ cloud = LMC::Cloud.instance
10
+ cloud.auth_for_account root_account
11
+ pw = Helpers::read_pw "Enter password for #{args.first}"
12
+ principal = LMC::Principal.new({ 'name' => args.first, 'password' => pw, 'type' => options[:type] })
13
+ puts principal.save.inspect
14
+ begin
15
+ rescue Exception => e
16
+ puts e.inspect
17
+ puts e.message.inspect
18
+ puts e.response
19
+ puts e.response.message
20
+ end
21
+
22
+
23
+ end
24
+ end
25
+
26
+ principal.desc "List principals"
27
+ principal.command :list do |l|
28
+ l.action do |global_options|
29
+ c = LMC::Cloud.instance
30
+ c.auth_for_account LMC::Account.get LMC::Account::ROOT_ACCOUNT_UUID
31
+ principals = c.get ['cloud-service-auth', 'principals']
32
+ tp principals.body, [{:id => {width: 36}}, :name, :type]
33
+ end
34
+ end
35
+
36
+ principal.arg_name "Principal ID"
37
+ principal.desc "Delete principal"
38
+ principal.command :delete do |d|
39
+ d.action do |global_options, options, args|
40
+ c = LMC::Cloud.instance
41
+ c.auth_for_account LMC::Account.get LMC::Account::ROOT_ACCOUNT_UUID
42
+ c.delete ['cloud-service-auth', 'principals', args.first]
43
+
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,27 @@
1
+ module LMCAdm
2
+ command :privatecloud do |pc|
3
+ pc.arg_name 'Redirect URL'
4
+ pc.desc 'Set redirect URL for account'
5
+ pc.command :url do |url|
6
+ url.flag :A, :account
7
+ url.action do |global_options, options, args|
8
+ account = LMC::Account.get_by_uuid_or_name options[:account]
9
+ puts account
10
+ LMC::Cloud.instance.auth_for_account account
11
+ LMC::Cloud.instance.put ["cloud-service-devices", "accounts", account.id, "redirect" ], { "url" => args.first }
12
+ end
13
+ end
14
+
15
+ pc.arg_name 'Account name|UUID'
16
+ pc.desc 'Show privatecloud account infos'
17
+ pc.command :show do |show|
18
+ show.action do |global_options, options, args|
19
+ account = LMC::Account.get_by_uuid_or_name args.first
20
+ LMC::Cloud.instance.auth_for_account account
21
+ redirect = LMC::Cloud.instance.get ["cloud-service-devices", "accounts", account.id, "redirect" ]
22
+ puts "Account: #{account}"
23
+ puts "URL: " + redirect.body.url
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ require 'gli'
2
+ module LMCAdm
3
+ include GLI::App
4
+ extend self
5
+ end