Created
January 20, 2012 18:54
-
-
Save groknaut/1648988 to your computer and use it in GitHub Desktop.
chef client.rb to that grabs config from userdata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # credit to opscode: | |
| # https://gist.github.com/319106 | |
| # credit to makuk66 | |
| #https://gist.github.com/1111078 | |
| @client_key_file="/etc/chef/client.pem" | |
| @client_rb_std_filename = "/etc/chef/client.rb" | |
| @client_rb_firstboot_filename = "/etc/chef/client.firstboot.rb" | |
| def first_boot | |
| require 'ohai' | |
| require 'json' | |
| log_level :debug | |
| log_location STDOUT | |
| warn "HIGGS-BOSON: executing #{@client_rb_firstboot_filename} for first boot" | |
| o = Ohai::System.new | |
| o.all_plugins | |
| # allow userdata.json to override EC2 metadata | |
| userdata_file="/etc/chef/userdata.json" | |
| if ::File.exists? userdata_file then | |
| warn "HIGGS-BOSON: obtaining user_data from #{userdata_file}" | |
| userdata_content = IO.read(userdata_file) | |
| else | |
| warn "HIGGS-BOSON: obtaining userdata from ohai" | |
| userdata_content = o[:ec2][:userdata] | |
| if userdata_content.kind_of?(Array) | |
| userdata_content = userdata_content[o[:ec2][:ami_launch_index]] | |
| end | |
| end | |
| raise "missing userdata" if userdata_content.nil? or userdata_content.empty? | |
| warn "HIGGS-BOSON: got user data: #{userdata_content}" | |
| warn "HIGGS-BOSON: parsing userdata" | |
| userdata = JSON.parse(userdata_content) | |
| warn "HIGGS-BOSON: parsed userdata" | |
| raise "missing chef_server" if userdata["chef_server"].nil? | |
| raise "missing validation_key" if userdata["validation_key"].nil? | |
| chef_server_url userdata["chef_server"] | |
| warn "HIGGS-BOSON: chef_server_url #{chef_server_url}" | |
| if userdata.has_key?("attributes") | |
| if userdata["attributes"].has_key?("node_name") | |
| my_node_name = userdata["attributes"]["node_name"] | |
| end | |
| attrs_file = "/etc/chef/attributes.json" | |
| Chef::Log.info "writing and loading #{attrs_file}" | |
| File.open(attrs_file, "w") do |f| | |
| f.print(JSON.pretty_generate(userdata["attributes"])) | |
| end | |
| json_attribs attrs_file | |
| end | |
| node_name my_node_name | |
| warn "HIGGS-BOSON: node_name #{node_name}" | |
| ssl_verify_mode :verify_none | |
| file_cache_path "/var/cache/chef" | |
| file_backup_path "/var/lib/chef/backup" | |
| pid_file "/var/run/chef/client.pid" | |
| cache_options({ :path => "/var/cache/chef/checksums", :skip_expires => true}) | |
| signing_ca_user "chef" | |
| Mixlib::Log::Formatter.show_time = true | |
| unless File.exists?("/etc/chef/validation.pem") | |
| File.open("/etc/chef/validation.pem", "w", 0600) do |f| | |
| f.print(userdata["validation_key"]) | |
| end | |
| end | |
| warn "HIGGS-BOSON: writing to #{@client_rb_std_filename}" | |
| new_client_rb = File.open(@client_rb_std_filename, 'w') | |
| new_client_rb.write <<END | |
| log_level :info | |
| log_location STDOUT | |
| chef_server_url "#{chef_server_url}" | |
| node_name "#{node_name}" | |
| ssl_verify_mode :verify_none | |
| file_cache_path "/var/cache/chef" | |
| file_backup_path "/var/lib/chef/backup" | |
| pid_file "/var/run/chef/client.pid" | |
| cache_options({ :path => "/var/cache/chef/checksums", :skip_expires => true}) | |
| signing_ca_user "chef" | |
| Mixlib::Log::Formatter.show_time = true | |
| Ohai::Config[:disabled_plugins] = ["aix", "darwin", "freebsd", "hpux", "netbsd", "openbsd", "rackspace", "solaris2", "sigar", "lua", "groovy", "mono", "erlang", "eucalyptus"] | |
| END | |
| if userdata.has_key?("attributes") | |
| if userdata["attributes"].has_key?("environment") | |
| my_environment = userdata["attributes"]["environment"] | |
| warn "HIGGS-BOSON: got environment: #{my_environment}" | |
| environment my_environment | |
| new_client_rb.write("environment #{environment}\n") | |
| else | |
| my_environment = "dev" | |
| warn "HIGGS-BOSON: HIGGS-BOSON: userdata doesn't have environment key. Writing #{my_environment} as env." | |
| environment my_environment | |
| new_client_rb.write("environment #{environment}\n") | |
| end | |
| end | |
| new_client_rb.close | |
| warn "HIGGS-BOSON: closed #{client_rb_std_filename}" | |
| runlist_file = "/etc/chef/first-boot.json" | |
| if userdata.has_key?("run_list") | |
| runlist_content = userdata_content["run_list"] | |
| warn "HIGGS-BOSON: got runlist: #{runlist_content}" | |
| Chef::Log.info "writing and loading #{runlist_file}" | |
| File.open(runlist_file, "w") do |f| | |
| f.print(JSON.pretty_generate(userdata["run_list"])) | |
| end | |
| json_attribs "/etc/chef/first-boot.json" | |
| else | |
| warn "HIGGS-BOSON: HIGGS-BOSON: userdata NOT has_key run_list" | |
| end | |
| end | |
| # end of first boot def | |
| if ::File.exists? @client_key_file and ::File.exists? @client_rb_std_filename | |
| # we are already registered; use existing config | |
| self.class_eval(IO.read(@client_rb_std_filename), @client_rb_std_filename, 1) | |
| else | |
| first_boot | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment