Class: Chef::Knife::Setup
- Inherits:
-
Chef::Knife
- Object
- Chef::Knife
- Chef::Knife::Setup
- Defined in:
- lib/chef/knife/knife-setup.rb
Instance Method Summary collapse
- #bootstrap_client ⇒ Object
- #get_node ⇒ Object
- #knife_ssh(command) ⇒ Object
- #knife_ssh_with_password_auth(command) ⇒ Object
- #load_template(template = nil) ⇒ Object
- #post ⇒ Object
- #render_template(template = nil) ⇒ Object
- #run ⇒ Object
- #server_name ⇒ Object
- #set_env(node, env) ⇒ Object
- #set_run_list(node, run_list) ⇒ Object
- #ssh_command ⇒ Object
- #validate_name_args! ⇒ Object
Instance Method Details
#bootstrap_client ⇒ Object
167 168 169 170 171 172 173 174 175 176 |
# File 'lib/chef/knife/knife-setup.rb', line 167 def bootstrap_client begin knife_ssh(ssh_command).run rescue Net::SSH::AuthenticationFailed unless config[:ssh_password] puts "Failed to authenticate #{config[:ssh_user]} - trying password auth" knife_ssh_with_password_auth(ssh_command).run end end end |
#get_node ⇒ Object
146 147 148 149 150 151 152 153 |
# File 'lib/chef/knife/knife-setup.rb', line 146 def get_node node = Chef::Node.load(server_name) if node.nil? ui.error("Could not find a node named #{@server_name}") exit 1 end return node end |
#knife_ssh(command) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/chef/knife/knife-setup.rb', line 207 def knife_ssh(command) ssh = Chef::Knife::Ssh.new ssh.ui = ui ssh.name_args = [ @node_name, command] ssh.config[:ssh_user] = config[:ssh_user] ssh.config[:ssh_password] = config[:ssh_password] ssh.config[:ssh_port] = Chef::Config[:knife][:ssh_port] || config[:ssh_port] ssh.config[:identity_file] = config[:identity_file] ssh.config[:manual] = true ssh.config[:no_host_key_verify] = config[:no_host_key_verify] ssh.config[:on_error] = :raise ssh end |
#knife_ssh_with_password_auth(command) ⇒ Object
221 222 223 224 225 226 |
# File 'lib/chef/knife/knife-setup.rb', line 221 def knife_ssh_with_password_auth(command) ssh = knife_ssh(command) ssh.config[:identity_file] = nil ssh.config[:ssh_password] = ssh.get_password ssh end |
#load_template(template = nil) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/chef/knife/knife-setup.rb', line 113 def load_template(template=nil) # Are we bootstrapping using an already shipped template? if config[:template_file] bootstrap_files = config[:template_file] else bootstrap_files = [] bootstrap_files << File.join(File.dirname(__FILE__), 'bootstrap', "#{config[:distro]}.erb") bootstrap_files << File.join(@@chef_config_dir, "bootstrap", "#{config[:distro]}.erb") bootstrap_files << File.join(ENV['HOME'], '.chef', 'bootstrap', "#{config[:distro]}.erb") bootstrap_files << Gem.find_files(File.join("chef","knife","bootstrap","#{config[:distro]}.erb")) bootstrap_files.flatten! end template = Array(bootstrap_files).find do |bootstrap_template| Chef::Log.debug("Looking for bootstrap template in #{File.dirname(bootstrap_template)}") File.exists?(bootstrap_template) end unless template ui.info("Can not find bootstrap definition for #{config[:distro]}") raise Errno::ENOENT end ui.info("Found bootstrap template in #{File.dirname(template)}") IO.read(template).chomp end |
#post ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/chef/knife/knife-setup.rb', line 238 def post url = Chef::Config[:post_url] = { :fqdn => get_node.to_hash["fqdn"] } body = JSON.generate() uri = URI.parse(url) request = Net::HTTP::Post.new(uri.request_uri) http = Net::HTTP.new(uri.host, uri.port) request.add_field('Content-Type', 'application/json-rpc') request.body = body response = http.request(request) raise "HTTP Error: #{response.code} on #{url}" unless response.code == "200" || response.code == "204" end |
#render_template(template = nil) ⇒ Object
141 142 143 144 |
# File 'lib/chef/knife/knife-setup.rb', line 141 def render_template(template=nil) context = Knife::Core::BootstrapContext.new(config, config[:run_list], Chef::Config) Erubis::Eruby.new(template).evaluate(context) end |
#run ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/chef/knife/knife-setup.rb', line 178 def run validate_name_args! @node_name = Array(@name_args).first # back compat--templates may use this setting: config[:server_name] = @node_name $stdout.sync = true ui.info("Setup Chef on #{ui.color(@node_name, :bold)}") bootstrap_client unless config[:nobootstrap] set_run_list(get_node, config[:run_list]) if config[:run_list] set_env(get_node, config[:environment]) unless config[:environment] == "_default" knife_ssh("chef-client").run if config[:norunchef] post if Chef::Config[:post_url] end |
#server_name ⇒ Object
203 204 205 |
# File 'lib/chef/knife/knife-setup.rb', line 203 def server_name config[:chef_node_name] || Array(@name_args).first end |
#set_env(node, env) ⇒ Object
161 162 163 164 165 |
# File 'lib/chef/knife/knife-setup.rb', line 161 def set_env(node, env) ui.info("Setting environment to #{env} for #{node.name}") node.chef_environment(env) node.save end |
#set_run_list(node, run_list) ⇒ Object
155 156 157 158 159 |
# File 'lib/chef/knife/knife-setup.rb', line 155 def set_run_list(node, run_list) ui.info("Setting run_list to #{run_list.inspect} for #{node.name}") node.run_list.reset!(run_list) node.save end |
#ssh_command ⇒ Object
228 229 230 231 232 233 234 235 236 |
# File 'lib/chef/knife/knife-setup.rb', line 228 def ssh_command command = render_template(load_template(config[:bootstrap_template])) if config[:use_sudo] command = "sudo #{command}" end command end |
#validate_name_args! ⇒ Object
196 197 198 199 200 201 |
# File 'lib/chef/knife/knife-setup.rb', line 196 def validate_name_args! if Array(@name_args).first.nil? ui.error("Must pass an FQDN or ip to bootstrap") exit 1 end end |