Class: Fhcap::Tasks::Chef::Server::CreateUser

Inherits:
ChefTaskBase show all
Defined in:
lib/fhcap/tasks/chef/server/create_user.rb

Instance Attribute Summary collapse

Attributes inherited from TaskBase

#config, #options, #thor, #verbose

Instance Method Summary collapse

Methods inherited from ChefTaskBase

#knife_config_dir, #knife_config_file_for, #run_chef_client_cmd, #run_knife_cmd, #run_knife_ssh_cmd

Methods inherited from TaskBase

#ask_config, #color_pad, #exit_with_error, #set_color, #suppress_stdout, #table_header, #table_row, #with_progress

Methods included from KnifeHelper

#chef_server_config_for, #chef_server_config_hash_for, #chef_server_names, #delete_chef_object, #knife_config, #knife_config_dir, #knife_config_file_for, #knife_data_bag_delete, #knife_download, #knife_environment_delete, #knife_upload, #local_chef_server?, #with_chef_server, #with_local_chef_server

Methods included from ProvidersHelper

#provider_availability_zones, #provider_config, #provider_credentials, #provider_for, #provider_names, #provider_names_for, #provider_regions, #provider_type, #providers_config

Methods included from ReposHelper

#find_cluster, #find_cluster_pwds, #find_cluster_pwds_with_repo, #find_cluster_with_repo, #find_data_bag, #find_data_bag_item, #find_environment, #find_repo_item, #find_role, #get_cookbook_deps, #get_cookbook_meta, #get_cookbook_versions, #get_cookbooks, #get_dirty_cookbooks, #get_entry_dependencies, #get_modified_cookbooks, #git_diff, #is_dirty?, #modified?, #repo_cfg, #repo_clusters_dir, #repo_cookbook_paths, #repo_dir, #repo_names, #repo_paths, #repos_config, #repos_dir, #run_inside_repo_dir

Methods included from FhcapHelper

#cluster_template_names

Constructor Details

#initialize(options) ⇒ CreateUser

Returns a new instance of CreateUser.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/fhcap/tasks/chef/server/create_user.rb', line 13

def initialize(options)
  super
  @chef_server_version = options[:'chef-server-version'] || 'osc'
  @user_config = {
      chef_server: options[:'chef-server'],
      username: options[:username],
      display_name: options[:'display-name'] || @username,
      first_name: options[:'first-name'] || @username,
      last_name: options[:'last-name'] || @username,
      email: options[:email],
      password: options[:password] || SecureRandom.urlsafe_base64(6),
      validation_client_name: @validation_client_name,
  }
end

Instance Attribute Details

#chef_server_versionObject (readonly)

Returns the value of attribute chef_server_version.



11
12
13
# File 'lib/fhcap/tasks/chef/server/create_user.rb', line 11

def chef_server_version
  @chef_server_version
end

#user_configObject (readonly)

Returns the value of attribute user_config.



11
12
13
# File 'lib/fhcap/tasks/chef/server/create_user.rb', line 11

def user_config
  @user_config
end

Instance Method Details

#runObject



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
# File 'lib/fhcap/tasks/chef/server/create_user.rb', line 28

def run
  thor.say "Knife::CreateUser", :yellow

  ask_config(required_config, user_config)

  if @chef_server_version == 'osc'
    cmd = "osc_user create #{user_config[:username]} -p #{user_config[:password]} -y -d -a"
  else
    cmd = "user create #{user_config[:username]} #{user_config[:display_name]} #{user_config[:first_name]} #{user_config[:last_name]} #{user_config[:email]} #{user_config[:password]} -y"
  end

  import_cfg = {}

  #ToDo [RHMAP-2898] Use knife object
  private_key = run_knife_cmd(cmd, user_config[:chef_server])

  if $?.exitstatus == 0
    knife_config_dir = config[:knife_dir] || File.join(config.default_dir, '.chef')
    knife_config_file = File.join(knife_config_dir, "knife-#{user_config[:chef_server]}.rb")
    ::Chef::Config.from_file(knife_config_file)
    file = File.open(::Chef::Config[:validation_key], "rb")
    validation_key = file.read

    import_cfg[:chef_server_url] = ::Chef::Config[:chef_server_url]
    import_cfg[:validation_client_name] = ::Chef::Config[:validation_client_name]
    import_cfg[:node_name] = user_config[:username]
    import_cfg[:private_key] = private_key
    import_cfg[:validation_key] = validation_key

    import_file = File.join(config.tmp_dir,"#{user_config[:username]}-#{user_config[:chef_server]}_import.json")
    thor.create_file(import_file, JSON.pretty_generate(import_cfg), :force => true)

    table_contents = [table_header('Username', 'Password', 'Chef Server URL')]
    table_contents << table_row(user_config[:username], user_config[:password], ::Chef::Config[:chef_server_url])
    thor.print_table table_contents

    table_contents = [table_header('Private Key')]
    table_contents << table_row(private_key)
    thor.print_table table_contents

    table_contents = [table_header('Validation Key')]
    table_contents << table_row(validation_key)
    thor.print_table table_contents

    thor.say_status 'warning', "The validation key shown above is the validation key used by you for '#{user_config[:chef_server]}'. If you don't normally share this key between users, you should create a new one manually for '#{user_config[:username]}'", :yellow

    thor.say_status 'Import Command:', "fhcap knife add --name  #{user_config[:chef_server]} --import-file #{import_file}", :cyan
  end
end