Class: Morpheus::Cli::WhitelabelSettingsCommand

Inherits:
Object
  • Object
show all
Includes:
CliCommand
Defined in:
lib/morpheus/cli/whitelabel_settings_command.rb

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from CliCommand

#build_common_options, #build_option_type_options, #command_name, #default_refresh_interval, #default_subcommand, #establish_remote_appliance_connection, #full_command_usage, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_id_list, #parse_list_options, #parse_list_subtitles, #parse_payload, #print, #print_error, #puts, #puts_error, #raise_command_error, #render_with_format, #run_command_for_each_arg, #subcommand_aliases, #subcommand_usage, #subcommands, #usage, #verify_access_token!

Constructor Details

#initializeWhitelabelSettingsCommand

Returns a new instance of WhitelabelSettingsCommand.



13
14
15
# File 'lib/morpheus/cli/whitelabel_settings_command.rb', line 13

def initialize()
  @image_types = {'header-logo' => 'headerLogo', 'footer-logo' => 'footerLogo', 'login-logo' => 'loginLogo', 'favicon' => 'favicon'}
end

Instance Method Details

#connect(opts) ⇒ Object



17
18
19
20
# File 'lib/morpheus/cli/whitelabel_settings_command.rb', line 17

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @whitelabel_settings_interface = @api_client.whitelabel_settings
end

#download_image(args) ⇒ Object



457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# File 'lib/morpheus/cli/whitelabel_settings_command.rb', line 457

def download_image(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = opts.banner = subcommand_usage("[image-type] [local-file]")
    opts.on( '-f', '--force', "Overwrite existing [local-file] if it exists." ) do
      options[:overwrite] = true
    end
    opts.on( '-p', '--mkdir', "Create missing directories for [local-file] if they do not exist." ) do
      options[:mkdir] = true
    end
    build_common_options(opts, options, [:dry_run, :quiet, :remote])
    opts.footer = "Download an image file.\n" +
        "[image-type] is required. This is the whitelabel image type (#{@image_types.collect {|k,v| k}.join('|')}) to be downloaded.\n" +
        "[local-file] is required. This is the full local filepath for the downloaded file."
  end

  optparse.parse!(args)
  connect(options)

  if args.count != 2
    raise_command_error "wrong number of arguments, expected 2 and got (#{args.count}) #{args}\n#{optparse}"
    return 1
  end
  if !@image_types[args[0]]
    raise_command_error "Invalid image type specified: #{args[0]}. Must be one of the following (#{@image_types.collect {|k,v| k}.join('|')})"
    return 1
  end

  begin
    image_type = @image_types[args[0]]
    outfile = File.expand_path(args[1])
    outdir = File.dirname(outfile)

    if Dir.exists?(outfile)
      print_red_alert "[local-file] is invalid. It is the name of an existing directory: #{outfile}"
      return 1
    end
    if !Dir.exists?(outdir)
      if options[:mkdir]
        print cyan,"Creating local directory #{outdir}",reset,"\n"
        FileUtils.mkdir_p(outdir)
      else
        print_red_alert "[local-file] is invalid. Directory not found: #{outdir}"
        return 1
      end
    end
    if File.exists?(outfile) && !options[:overwrite]
      print_red_alert "[local-file] is invalid. File already exists: #{outfile}\nUse -f to overwrite the existing file."
      return 1
    end

    @whitelabel_settings_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @whitelabel_settings_interface.dry.download_image(image_type, outfile)
      return
    end

    if !options[:quite]
      print cyan + "Downloading #{args[0]} to #{outfile} ... "
    end

    http_response = @whitelabel_settings_interface.download_image(image_type, outfile)

    success = http_response.code.to_i == 200
    if success
      if !options[:quiet]
        print green + "SUCCESS" + reset + "\n"
      end
      return 0
    else
      if !options[:quiet]
        print red + "ERROR" + reset + " HTTP #{http_response.code}" + "\n"
      end
      if File.exists?(outfile) && File.file?(outfile)
        Morpheus::Logging::DarkPrinter.puts "Deleting bad file download: #{outfile}" if Morpheus::Logging.debug?
        File.delete(outfile)
      end
      if options[:debug]
        puts_error http_response.inspect
      end
      return 1
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#get(args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/morpheus/cli/whitelabel_settings_command.rb', line 26

def get(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage()
    opts.on('--details', "Show full (not truncated) contents of Terms of Use, Privacy Policy, Override CSS" ) do
      options[:details] = true
    end
    build_common_options(opts, options, [:query, :json, :yaml, :csv, :fields, :dry_run, :remote])
    opts.footer = "Get whitelabel settings."
  end

  optparse.parse!(args)
  connect(options)

  if args.count != 0
    raise_command_error "wrong number of arguments, expected 0 and got (#{args.count}) #{args}\n#{optparse}"
    return 1
  end
  
  begin
    params = parse_list_options(options)
    @whitelabel_settings_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @whitelabel_settings_interface.dry.get()
      return
    end
    json_response = @whitelabel_settings_interface.get()
    if options[:json]
      puts as_json(json_response, options, "whitelabelSettings")
      return 0
    elsif options[:yaml]
      puts as_yaml(json_response, options, "whitelabelSettings")
      return 0
    elsif options[:csv]
      puts records_as_csv([json_response['whitelabelSettings']], options)
      return 0
    end

    whitelabel_settings = json_response['whitelabelSettings']

    print_h1 "Whitelabel Settings"
    print cyan
    description_cols = {
      "Enabled" => lambda {|it| format_boolean(it['enabled']) },
      "Appliance Name" => lambda {|it| it['applianceName'] },
      "Disable Support Menu" => lambda {|it| format_boolean(it['disableSupportMenu'])},
      "Header Logo" => lambda {|it| it['headerLogo'] ? it['headerLogo'].split('/').last : '' },
      "Footer Logo" => lambda {|it| it['footerLogo'] ? it['footerLogo'].split('/').last : '' },
      "Login Logo" => lambda {|it| it['loginLogo'] ? it['loginLogo'].split('/').last : '' },
      "Favicon" => lambda {|it| it['favicon'] ? it['favicon'].split('/').last : '' },
      "Header Background" => lambda {|it| it['headerBgColor']},
      "Header Foreground" => lambda {|it| it['headerFgColor']},
      "Nav Background" => lambda {|it| it['navBgColor']},
      "Nav Foreground" => lambda {|it| it['navFgColor']},
      "Nav Hover" => lambda {|it| it['navHoverColor']},
      "Primary Button Background" => lambda {|it| it['primaryButtonBgColor']},
      "Primary Button Foreground" => lambda {|it| it['primaryButtonFgColor']},
      "Primary Button Hover Background" => lambda {|it| it['primaryButtonHoverBgColor']},
      "Primary Button Hover Foreground" => lambda {|it| it['primaryButtonHoverFgColor']},
      "Footer Background" => lambda {|it| it['footerBgColor']},
      "Footer Foreground" => lambda {|it| it['footerFgColor']},
      "Login Background" => lambda {|it| it['loginBgColor']},
      "Copyright String" => lambda {|it| it['copyrightString']}
    }

    print_description_list(description_cols, whitelabel_settings)

    # Support Menu Links
    if !whitelabel_settings['supportMenuLinks'].empty?
      print_h2 "Support Menu Links"
      print cyan
      print as_pretty_table(whitelabel_settings['supportMenuLinks'], [:url, :label, :labelCode])
    end

    trunc_len = 80
    if !(content = whitelabel_settings['overrideCss']).nil? && content.length
      title = "Override CSS"
      title = title + ' (truncated, use --details for all content)' if content && content.length > trunc_len && !options[:details]
      print_h2 title
      print cyan
      print options[:details] ? content : truncate_string(content, trunc_len), "\n"
    end
    if !(content = whitelabel_settings['termsOfUse']).nil? && content.length
      title = "Terms of Use"
      title = title + ' (truncated, use --details for all content)' if content && content.length > trunc_len && !options[:details]
      print_h2 title
      print cyan
      print options[:details] ? content : truncate_string(content, trunc_len), "\n"
    end
    if !(content = whitelabel_settings['privacyPolicy']).nil? && content.length
      title = "Privacy Policy"
      title = title + ' (truncated, use --details for all content)' if content && content.length > trunc_len && !options[:details]
      print_h2 title
      print cyan
      print options[:details] ? content : truncate_string(content, trunc_len), "\n"
    end
    print reset "\n"
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#handle(args) ⇒ Object



22
23
24
# File 'lib/morpheus/cli/whitelabel_settings_command.rb', line 22

def handle(args)
  handle_subcommand(args)
end

#reset_image(args) ⇒ Object



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/morpheus/cli/whitelabel_settings_command.rb', line 363

def reset_image(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = opts.banner = subcommand_usage("[image-type]")
    build_common_options(opts, options, [:json, :dry_run, :quiet, :remote])
    opts.footer = "Reset your whitelabel image.\n" +
        "[image-type] is required. This is the whitelabel image type (#{@image_types.collect {|k,v| k}.join('|')})"
  end

  optparse.parse!(args)
  connect(options)

  if args.count != 1
    raise_command_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args}\n#{optparse}"
    return 1
  end
  if !@image_types[args[0]]
    raise_command_error "Invalid image type specified: #{args[0]}. Must be one of the following (#{@image_types.collect {|k,v| k}.join('|')})"
    return 1
  end

  begin
    image_type = @image_types[args[0]]
    @whitelabel_settings_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @whitelabel_settings_interface.dry.reset_image(image_type)
      return
    end

    json_response = @whitelabel_settings_interface.reset_image(image_type)

    if options[:json]
      puts as_json(json_response, options)
    elsif !options[:quiet]
      print_red_alert "Error resetting whitelabel image: #{json_response['msg'] || json_response['errors']}" if json_response['success'] == false
      print_green_success "Reset whitelabel image" if json_response['success'] == true
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#update(args) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/morpheus/cli/whitelabel_settings_command.rb', line 130

def update(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = opts.banner = subcommand_usage()
    opts.on('--active [on|off]', String, "Can be used to enable / disable whitelabel feature") do |val|
      params['enabled'] = val.to_s == 'on' || val.to_s == 'true' || val.to_s == '1' || val.to_s == ''
    end
    opts.on("--appliance-name NAME", String, "Appliance name. Only available to master account") do |val|
      params['applianceName'] = val == 'null' ? nil : val
    end
    opts.on("--disable-support-menu [on|off]", ['on','off'], "Can be used to disable support menu") do |val|
      params['disableSupportMenu'] = val.to_s == 'on' || val.to_s == 'true' || val.to_s == '1' || val.to_s == ''
    end
    opts.on("--reset-header-logo", String, "Resets header logo to default header logo") do |val|
      params['resetHeaderLogo'] = true
    end
    opts.on("--reset-footer-logo", String, "Resets footer logo to default footer logo") do |val|
      params['resetFooterLogo'] = true
    end
    opts.on("--reset-login-logo", String, "Resets login logo to default login logo") do |val|
      params['resetLoginLogo'] = true
    end
    opts.on("--reset-favicon", String, "Resets favicon default favicon") do |val|
      params['resetFavicon'] = true
    end
    opts.on("--header-bg-color VALUE", String, "Header background color") do |val|
      params['headerBgColor'] = val
    end
    opts.on("--header-fg-color VALUE", String, "Header foreground color") do |val|
      params['headerFgColor'] = val
    end
    opts.on("--nav-bg-color VALUE", String, "Nav background color") do |val|
      params['navBgColor'] = val
    end
    opts.on("--nav-fg-color VALUE", String, "Nav foreground color") do |val|
      params['navFgColor'] = val
    end
    opts.on("--nav-hover-color VALUE", String, "Nav hover color") do |val|
      params['navHoverColor'] = val
    end
    opts.on("--primary-button-bg-color VALUE", String, "Primary button background color") do |val|
      params['primaryButtonBgColor'] = val
    end
    opts.on("--primary-button-fg-color VALUE", String, "Primary button foreground color") do |val|
      params['primaryButtonFgColor'] = val
    end
    opts.on("--primary-button-hover-bg-color VALUE", String, "Primary button hover background color") do |val|
      params['primaryButtonHoverBgColor'] = val
    end
    opts.on("--primary-button-hover-fg-color VALUE", String, "Primary button hover foreground color") do |val|
      params['primaryButtonHoverFgColor'] = val
    end
    opts.on("--footer-bg-color VALUE", String, "Footer background color") do |val|
      params['footerBgColor'] = val
    end
    opts.on("--footer-fg-color VALUE", String, "Footer foreground color") do |val|
      params['footerFgColor'] = val
    end
    opts.on("--login-bg-color VALUE", String, "Login background color") do |val|
      params['loginBgColor'] = val
    end
    opts.on("--copyright TEXT", String, "Copyright String") do |val|
      params['copyrightString'] = val
    end
    opts.on("--css TEXT", String, "Override CSS") do |val|
      params['overrideCss'] = val
    end
    opts.on("--css-file FILE", String, "Override CSS from local file") do |val|
      options[:overrideCssFile] = val
    end
    opts.on("--terms TEXT", String, "Terms of use content") do |val|
      params['termsOfUse'] = val
    end
    opts.on("--terms-file FILE", String, "Terms of use content from local file") do |val|
      options[:termsOfUseFile] = val
    end
    opts.on("--privacy-policy TEXT", String, "Privacy policy content") do |val|
      params['privacyPolicy'] = val
    end
    opts.on("--privacy-policy-file FILE", String, "Privacy policy content from local file") do |val|
      options[:privacyPolicyFile] = val
    end
    opts.on('--support-menu-links LIST', Array, "Support menu links. Comma delimited list of menu links. Each menu link is pipe delimited url1|label1|code1,url2|label2|code2") do |val|
      options[:supportMenuLinks] = val
    end
    build_common_options(opts, options, [:json, :dry_run, :quiet, :remote])
  end

  optparse.parse!(args)
  connect(options)

  if args.count != 0
    raise_command_error "wrong number of arguments, expected 0 and got (#{args.count}) #{args}\n#{optparse}"
    return 1
  end

  begin
    payload = parse_payload(options)
    image_files = {}

    if !payload
      [:overrideCssFile, :termsOfUseFile, :privacyPolicyFile].each do |sym|
        if options[sym]
          filename = File.expand_path(options[sym])

          if filename && File.file?(filename)
            params[sym.to_s.delete_suffix('File')] = File.read(filename)
          else
            print_red_alert("File not found: #{filename}")
            exit 1
          end
        end
      end
      if options[:supportMenuLinks]
        params['supportMenuLinks'] = options[:supportMenuLinks].collect { |link|
          parts = link.split('|')
          {'url' => parts[0].strip, 'label' => (parts.count > 1 ? parts[1].strip : ''), 'labelCode' => (parts.count > 2 ? parts[2].strip : '')}
        }
      end
      payload = {'whitelabelSettings' => params}
    end

    @whitelabel_settings_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @whitelabel_settings_interface.dry.update(payload, image_files)
      return
    end
    json_response = @whitelabel_settings_interface.update(payload, image_files)

    if options[:json]
      puts as_json(json_response, options)
    elsif !options[:quiet]
      if json_response['success']
        print_green_success "Updated whitelabel settings"
        get([] + (options[:remote] ? ["-r",options[:remote]] : []))
      else
        print_red_alert "Error updating whitelabel settings: #{json_response['msg'] || json_response['errors']}"
      end
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#update_images(args) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/morpheus/cli/whitelabel_settings_command.rb', line 277

def update_images(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = opts.banner = subcommand_usage()
    opts.on("--header-logo FILE", String, "Header logo image. Local path of a file to upload (png|jpg|svg)") do |val|
      options[:headerLogo] = val
    end
    opts.on("--reset-header-logo", String, "Resets header logo to default header logo") do |val|
      params['resetHeaderLogo'] = true
    end
    opts.on("--footer-logo FILE", String, "Footer logo image. Local path of a file to upload (png|jpg|svg)") do |val|
      options[:footerLogo] = val
    end
    opts.on("--reset-footer-logo", String, "Resets footer logo to default footer logo") do |val|
      params['resetFooterLogo'] = true
    end
    opts.on("--login-logo FILE", String, "Login logo image. Local path of a file to upload (png|jpg|svg)") do |val|
      options[:loginLogo] = val
    end
    opts.on("--reset-login-logo", String, "Resets login logo to default login logo") do |val|
      params['resetLoginLogo'] = true
    end
    opts.on("--favicon FILE", String, "Favicon icon image. Local path of a file to upload") do |val|
      options[:favicon] = val
    end
    opts.on("--reset-favicon", String, "Resets favicon default favicon") do |val|
      params['resetFavicon'] = true
    end
    build_common_options(opts, options, [:json, :dry_run, :quiet, :remote])
    opts.footer = "Update your whitelabel images."
  end

  optparse.parse!(args)
  connect(options)

  if args.count != 0
    raise_command_error "wrong number of arguments, expected 0 and got (#{args.count}) #{args}\n#{optparse}"
    return 1
  end

  begin
    payload = parse_payload(options)

    if !payload
      payload = params

      [:headerLogo, :footerLogo, :loginLogo, :favicon].each do |sym|
        if options[sym]
          filename = File.expand_path(options[sym])

          if filename && File.file?(filename)
            payload["#{sym.to_s}.file"] = File.new(filename, 'rb')
          else
            print_red_alert("File not found: #{filename}")
            exit 1
          end
        end
      end
    end

    if payload.empty?
      print_green_success "Nothing to update"
      exit 1
    end

    @whitelabel_settings_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @whitelabel_settings_interface.dry.update_images(payload)
      return
    end

    json_response = @whitelabel_settings_interface.update_images(payload)

    if options[:json]
      puts as_json(json_response, options)
    elsif !options[:quiet]
      print_red_alert "Error updating whitelabel image: #{json_response['msg'] || json_response['errors']}" if json_response['success'] == false
      print_green_success "Updated whitelabel image" if json_response['success'] == true
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#view_image(args) ⇒ Object



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/morpheus/cli/whitelabel_settings_command.rb', line 406

def view_image(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = opts.banner = subcommand_usage("[image-type]")
    build_common_options(opts, options, [:json, :dry_run, :quiet, :remote])
    opts.footer = "View your image of specified [image-type].\n" +
        "[image-type] is required. This is the whitelabel image type (#{@image_types.collect {|k,v| k}.join('|')})\n" +
        "This opens the image url with a web browser."
  end

  optparse.parse!(args)
  connect(options)

  if args.count != 1
    raise_command_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args}\n#{optparse}"
    return 1
  end
  if !@image_types[args[0]]
    raise_command_error "Invalid image type specified: #{args[0]}. Must be one of the following (#{@image_types.collect {|k,v| k}.join('|')})"
    return 1
  end

  begin
    image_type = @image_types[args[0]]
    @whitelabel_settings_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @whitelabel_settings_interface.dry.get()
      return
    end

    whitelabel_settings = @whitelabel_settings_interface.get()['whitelabelSettings']

    if link = whitelabel_settings[image_type]
      if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
        system "start #{link}"
      elsif RbConfig::CONFIG['host_os'] =~ /darwin/
        system "open #{link}"
      elsif RbConfig::CONFIG['host_os'] =~ /linux|bsd/
        system "xdg-open #{link}"
      end
      return 0, nil
    else
      print_error red,"No image found for #{image_type}.",reset,"\n"
      return 1
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end