class Umami::Client

Attributes

config_path[R]
policyfile[R]
staging_dir[R]

Public Class Methods

new(policyfile = nil) click to toggle source
# 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

Public Instance Methods

apply_config!() click to toggle source
# 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() click to toggle source
# File lib/chef-umami/client.rb, line 94
def build_config
  create_client_rb
  cp_fake_client_key
end
client() click to toggle source
# File lib/chef-umami/client.rb, line 38
def client
  @client ||= Chef::Client.new
end
client_rb_staging_path() click to toggle source
# File lib/chef-umami/client.rb, line 58
def client_rb_staging_path
  File.join(dot_chef_staging_dir, 'config.rb')
end
compile() click to toggle source

Execute the compile phase of a Chef client run.

# File lib/chef-umami/client.rb, line 115
def compile
  prep
  client.setup_run_context
end
cp_fake_client_key() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/chef-umami/client.rb, line 42
def fake_client_key
  File.join(staging_dir, 'umami.pem')
end
policy_name() click to toggle source
# File lib/chef-umami/client.rb, line 34
def policy_name
  @policy_name ||= Umami::Policyfile::PolicyfileLock.new(policyfile).name
end
prep() click to toggle source

Perform the steps required prior to compiling resources, including running Ohai and building up the node object.

# 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() click to toggle source

TODO: This can only be called after prep completes successfully. Add some check to determine if the client is actually prepped.

# File lib/chef-umami/client.rb, line 122
def resource_collection
  client.run_status.run_context.resource_collection
end