Class: Kontena::Cli::Vault::ImportCommand

Inherits:
Kontena::Command show all
Includes:
Common, GridOptions
Defined in:
lib/kontena/cli/vault/import_command.rb

Instance Attribute Summary

Attributes inherited from Kontena::Command

#arguments, #exit_code, #result

Instance Method Summary collapse

Methods included from GridOptions

included

Methods included from Common

#access_token=, #add_master, #any_key_to_continue, #any_key_to_continue_with_timeout, #api_url, #api_url=, #caret, #clear_current_grid, #client, #cloud_auth?, #cloud_client, #config, #confirm, #confirm_command, #current_grid, #current_master_index, #debug?, #display_account_login_info, #display_login_info, display_logo, #display_master_login_info, #error, exit_with_error, #kontena_account, #logger, #pastel, #print, #prompt, #puts, #require_api_url, #require_token, #reset_client, #reset_cloud_client, #running_quiet?, #running_silent?, #running_verbose?, #spin_if, #spinner, #sprint, #sputs, #stdin_input, #use_refresh_token, #vfakespinner, #vputs, #vspinner, #warning

Methods inherited from Kontena::Command

banner, callback_matcher, #help_requested?, inherited, #instance, load_subcommand, requires_current_account_token, requires_current_account_token?, requires_current_grid, requires_current_grid?, requires_current_master, requires_current_master?, requires_current_master_token, requires_current_master_token?, #run, #run_callbacks, #verify_current_account_token, #verify_current_grid, #verify_current_master, #verify_current_master_token

Instance Method Details

#executeObject



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
# File 'lib/kontena/cli/vault/import_command.rb', line 26

def execute
  require_current_grid

  updates = []
  deletes = []

  parsed_input.map do |k,v|
    case v
    when String, Numeric, TrueClass, FalseClass
      if empty_is_null? && v.to_s.empty?
        deletes << k.to_s
      else
        updates << [k.to_s, v.to_s]
      end
    when NilClass
      deletes << k.to_s
    else
      exit_with_error "Invalid value type #{v.class} for #{k}."
    end
  end

  if updates.empty? && deletes.empty?
    exit_with_error "No secrets loaded"
  end

  unless forced?
    puts "About to.."
    puts "  * #{Kontena.pastel.yellow("IMPORT")} #{updates.size} secret#{"s" if updates.size > 1}" unless updates.empty?
    puts "  * #{Kontena.pastel.red("DELETE")} #{deletes.size} secret#{"s" if deletes.size > 1}" unless deletes.empty?
    confirm
  end

  unless updates.empty?
    spinner "Updating #{updates.size} secrets" do |spin|
      updates.each do |key_value_pair|
        spin.fail! unless Kontena.run(['vault', 'update', '--upsert', '--silent'] + key_value_pair)
      end
    end
  end

  unless deletes.empty? || skip_null?
    spinner "Deleting #{deletes.size} secrets" do |spin|
      deletes.map(&:shellescape).each do |key_to_delete|
        spin.fail! unless Kontena.run(['vault', 'rm', '--silent', '--force', key_to_delete])
      end
    end
  end
end

#inputObject



22
23
24
# File 'lib/kontena/cli/vault/import_command.rb', line 22

def input
  path ? File.read(path) : stdin_input("Enter secrets YAML", :multiline)
end

#parsed_inputObject



17
18
19
20
# File 'lib/kontena/cli/vault/import_command.rb', line 17

def parsed_input
  require "json"
  json? ? JSON.load(input) : YAML.safe_load(input, [], [], true, path)
end