Module: Chef::Knife::Bootstrap::Bootstrapper

Included in:
AzureServerCreate, AzurermServerCreate, Chef::Knife::BootstrapAzure, Chef::Knife::BootstrapAzurerm
Defined in:
lib/chef/knife/bootstrap/bootstrapper.rb

Instance Method Summary collapse

Instance Method Details

#create_node_and_client_pemObject



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/chef/knife/bootstrap/bootstrapper.rb', line 107

def create_node_and_client_pem
  client_builder ||= begin
    require "chef/knife/bootstrap/client_builder"
    Chef::Knife::Bootstrap::ClientBuilder.new(
      chef_config: Chef::Config,
      config: config,
      ui: ui
    )
  end
  client_builder.run
  client_builder.client_path
end

#default_hint_optionsObject



34
35
36
37
38
39
40
# File 'lib/chef/knife/bootstrap/bootstrapper.rb', line 34

def default_hint_options
  %w{
    vm_name
    public_fqdn
    platform
  }
end

#get_chef_extension_nameObject



26
27
28
# File 'lib/chef/knife/bootstrap/bootstrapper.rb', line 26

def get_chef_extension_name
  is_image_windows? ? "ChefClient" : "LinuxChefClient"
end

#get_chef_extension_private_paramsObject



120
121
122
123
124
125
126
127
128
129
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
# File 'lib/chef/knife/bootstrap/bootstrapper.rb', line 120

def get_chef_extension_private_params
  pri_config = {}
  # validator less bootstrap support for bootstrap protocol cloud-api
  if Chef::Config[:validation_key] && File.exist?(File.expand_path(Chef::Config[:validation_key]))
    pri_config[:validation_key] = File.read(File.expand_path(Chef::Config[:validation_key]))
  else
    if Chef::VERSION.split(".").first.to_i == 11
      ui.error("Unable to find validation key. Please verify your configuration file for validation_key config value.")
      exit 1
    end
    if config[:server_count].to_i > 1
      node_name = config[:chef_node_name]
      0.upto(config[:server_count].to_i - 1) do |count|
        config[:chef_node_name] = node_name + count.to_s
        key_path = create_node_and_client_pem
        pri_config[("client_pem" + count.to_s).to_sym] = File.read(key_path)
      end
      config[:chef_node_name] = node_name
    else
      key_path = create_node_and_client_pem
      if File.exist?(key_path)
        pri_config[:client_pem] = File.read(key_path)
      else
        ui.error('Unable to find client.pem at given path #{key_path}')
        exit 1
      end
    end
  end

  # SSL cert bootstrap support
  if config[:cert_path]
    if File.exist?(File.expand_path(config[:cert_path]))
      pri_config[:chef_server_crt] = File.read(File.expand_path(config[:cert_path]))
    else
      ui.error("Specified SSL certificate does not exist.")
      exit 1
    end
  end

  # encrypted_data_bag_secret key for encrypting/decrypting the data bags
  pri_config[:encrypted_data_bag_secret] = load_correct_secret

  pri_config
end

#get_chef_extension_public_paramsObject



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
# File 'lib/chef/knife/bootstrap/bootstrapper.rb', line 70

def get_chef_extension_public_params
  pub_config = {}
  if config[:azure_extension_client_config]
    pub_config[:client_rb] = File.read(File.expand_path(config[:azure_extension_client_config]))
  else
    pub_config[:client_rb] = "chef_server_url \t #{Chef::Config[:chef_server_url].to_json}\nvalidation_client_name\t#{Chef::Config[:validation_client_name].to_json}"
  end

  pub_config[:runlist] = config[:run_list].empty? ? "" : config[:run_list].join(",").to_json
  pub_config[:custom_json_attr] = config[:json_attributes] || {}
  pub_config[:extendedLogs] = config[:extended_logs] ? "true" : "false"
  pub_config[:hints] = ohai_hints if @service.instance_of?(Azure::ResourceManagement::ARMInterface) && !config[:ohai_hints].nil?
  pub_config[:chef_daemon_interval] = config[:chef_daemon_interval] if config[:chef_daemon_interval]
  pub_config[:daemon] = config[:daemon] if config[:daemon]

  # bootstrap attributes
  pub_config[:bootstrap_options] = {}
  pub_config[:bootstrap_options][:environment] = config[:environment] if config[:environment]
  pub_config[:bootstrap_options][:chef_node_name] = config[:chef_node_name] if config[:chef_node_name]
  pub_config[:bootstrap_options][:chef_server_url] = Chef::Config[:chef_server_url] if Chef::Config[:chef_server_url]
  pub_config[:bootstrap_options][:validation_client_name] = Chef::Config[:validation_client_name] if Chef::Config[:validation_client_name]
  pub_config[:bootstrap_options][:node_verify_api_cert] = config[:node_verify_api_cert] ? "true" : "false" if config.key?(:node_verify_api_cert)
  pub_config[:bootstrap_options][:bootstrap_version] = config[:bootstrap_version] if config[:bootstrap_version]
  pub_config[:bootstrap_options][:node_ssl_verify_mode] = config[:node_ssl_verify_mode] if config[:node_ssl_verify_mode]
  pub_config[:bootstrap_options][:bootstrap_proxy] = config[:bootstrap_proxy] if config[:bootstrap_proxy]
  pub_config
end

#get_chef_extension_publisherObject



30
31
32
# File 'lib/chef/knife/bootstrap/bootstrapper.rb', line 30

def get_chef_extension_publisher
  "Chef.Bootstrap.WindowsAzure"
end

#get_chef_extension_version(chef_extension_name = nil) ⇒ Object

get latest version



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/chef/knife/bootstrap/bootstrapper.rb', line 43

def get_chef_extension_version(chef_extension_name = nil)
  if config[:azure_chef_extension_version]
    config[:azure_chef_extension_version]
  else
    chef_extension_name ||= get_chef_extension_name
    if @service.instance_of? Azure::ResourceManagement::ARMInterface
      service.get_latest_chef_extension_version(
        azure_service_location: config[:azure_service_location],
        chef_extension_publisher: get_chef_extension_publisher,
        chef_extension: chef_extension_name
      )
    elsif @service.instance_of? Azure::ServiceManagement::ASMInterface
      extensions = service.get_extension(chef_extension_name, get_chef_extension_publisher)
      extensions.css("Version").max.text.split(".").first + ".*"
    end
  end
end

#load_correct_secretObject



98
99
100
101
102
103
104
105
# File 'lib/chef/knife/bootstrap/bootstrapper.rb', line 98

def load_correct_secret
  secret_file = config[:encrypted_data_bag_secret_file]
  secret = config[:encrypted_data_bag_secret]

  secret_file = Chef::EncryptedDataBagItem.load_secret(secret_file) unless secret_file.nil?

  secret_file || secret
end

#ohai_hintsObject



61
62
63
64
65
66
67
68
# File 'lib/chef/knife/bootstrap/bootstrapper.rb', line 61

def ohai_hints
  hint_values = config[:ohai_hints]
  if hint_values.casecmp("default") == 0
    default_hint_options
  else
    hint_values.split(",")
  end
end