Class: Umami::Client
- Inherits:
-
Object
- Object
- Umami::Client
- Defined in:
- lib/chef-umami/client.rb
Instance Attribute Summary collapse
-
#config_path ⇒ Object
readonly
Returns the value of attribute config_path.
-
#policyfile ⇒ Object
readonly
Returns the value of attribute policyfile.
-
#staging_dir ⇒ Object
readonly
Where Umami will stage files.
Instance Method Summary collapse
- #apply_config! ⇒ Object
- #build_config ⇒ Object
- #client ⇒ Object
- #client_rb_staging_path ⇒ Object
-
#compile ⇒ Object
Execute the compile phase of a Chef client run.
- #cp_fake_client_key ⇒ Object
- #create_client_rb ⇒ Object
- #dot_chef_staging_dir ⇒ Object
- #fake_client_key ⇒ Object
-
#initialize(policyfile = nil) ⇒ Client
constructor
A new instance of Client.
- #policy_name ⇒ Object
-
#prep ⇒ Object
Perform the steps required prior to compiling resources, including running Ohai and building up the node object.
-
#resource_collection ⇒ Object
TODO: This can only be called after #prep completes successfully.
Constructor Details
#initialize(policyfile = nil) ⇒ Client
Returns a new instance of Client.
26 27 28 29 30 31 32 |
# File 'lib/chef-umami/client.rb', line 26 def initialize(policyfile = nil) @client = client @policy_name = nil @policyfile = policyfile @staging_dir = Dir.mktmpdir('umami-') @config_path = client_rb_staging_path end |
Instance Attribute Details
#config_path ⇒ Object (readonly)
Returns the value of attribute config_path.
23 24 25 |
# File 'lib/chef-umami/client.rb', line 23 def config_path @config_path end |
#policyfile ⇒ Object (readonly)
Returns the value of attribute policyfile.
24 25 26 |
# File 'lib/chef-umami/client.rb', line 24 def policyfile @policyfile end |
#staging_dir ⇒ Object (readonly)
Where Umami will stage files.
25 26 27 |
# File 'lib/chef-umami/client.rb', line 25 def staging_dir @staging_dir end |
Instance Method Details
#apply_config! ⇒ Object
99 100 101 102 103 104 |
# File 'lib/chef-umami/client.rb', line 99 def apply_config! build_config Chef::Config.from_file(config_path) # Define Chef::Config['config_file'] lest Ohai complain. Chef::Config['config_file'] = config_path end |
#build_config ⇒ Object
94 95 96 97 |
# File 'lib/chef-umami/client.rb', line 94 def build_config create_client_rb cp_fake_client_key end |
#client ⇒ Object
38 39 40 |
# File 'lib/chef-umami/client.rb', line 38 def client @client ||= Chef::Client.new end |
#client_rb_staging_path ⇒ Object
58 59 60 |
# File 'lib/chef-umami/client.rb', line 58 def client_rb_staging_path File.join(dot_chef_staging_dir, 'config.rb') end |
#compile ⇒ Object
Execute the compile phase of a Chef client run.
115 116 117 118 |
# File 'lib/chef-umami/client.rb', line 115 def compile prep client.setup_run_context end |
#cp_fake_client_key ⇒ Object
46 47 48 49 50 |
# File 'lib/chef-umami/client.rb', line 46 def cp_fake_client_key # Create a fake client cert based on a dummy cert we have laying around. fake_client_key_src = File.join(File.dirname(__FILE__), %w(.. .. support umami.pem)) FileUtils.cp(fake_client_key_src, fake_client_key) end |
#create_client_rb ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/chef-umami/client.rb', line 62 def create_client_rb File.open(client_rb_staging_path, 'wb+') do |f| f.print(<<~CONFIG) ### Chef Client Configuration ### # The settings in this file will configure chef to apply the exported policy in # this directory. To use it, run: # # chef-client -z # policy_name '#{policy_name}' policy_group 'local' use_policyfile true policy_document_native_api true chef_server_url 'http://127.0.0.1:8889' node_name 'umami-node' client_key '#{fake_client_key}' # In order to use this repo, you need a version of Chef Client and Chef Zero # that supports policyfile "native mode" APIs: current_version = Gem::Version.new(Chef::VERSION) unless Gem::Requirement.new(">= 12.7").satisfied_by?(current_version) puts("!" * 80) puts(<<-MESSAGE) This Chef Repo requires features introduced in Chef 12.7, but you are using Chef \#{Chef::VERSION}. Please upgrade to Chef 12.7 or later. MESSAGE puts("!" * 80) exit!(1) end CONFIG end end |
#dot_chef_staging_dir ⇒ Object
52 53 54 55 56 |
# File 'lib/chef-umami/client.rb', line 52 def dot_chef_staging_dir dot_dir = File.join(staging_dir, '.chef') FileUtils.mkdir_p(dot_dir) dot_dir end |
#fake_client_key ⇒ Object
42 43 44 |
# File 'lib/chef-umami/client.rb', line 42 def fake_client_key File.join(staging_dir, 'umami.pem') end |
#policy_name ⇒ Object
34 35 36 |
# File 'lib/chef-umami/client.rb', line 34 def policy_name @policy_name ||= Umami::Policyfile::PolicyfileLock.new(policyfile).name end |
#prep ⇒ Object
Perform the steps required prior to compiling resources, including running Ohai and building up the node object.
108 109 110 111 112 |
# File 'lib/chef-umami/client.rb', line 108 def prep client.run_ohai client.load_node # from the server client.build_node end |
#resource_collection ⇒ Object
TODO: This can only be called after #prep completes successfully. Add some check to determine if the client is actually prepped.
122 123 124 |
# File 'lib/chef-umami/client.rb', line 122 def resource_collection client.run_status.run_context.resource_collection end |