Class: Umami::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-umami/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_pathObject (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

#policyfileObject (readonly)

Returns the value of attribute policyfile.



24
25
26
# File 'lib/chef-umami/client.rb', line 24

def policyfile
  @policyfile
end

#staging_dirObject (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_configObject



94
95
96
97
# File 'lib/chef-umami/client.rb', line 94

def build_config
  create_client_rb
  cp_fake_client_key
end

#clientObject



38
39
40
# File 'lib/chef-umami/client.rb', line 38

def client
  @client ||= Chef::Client.new
end

#client_rb_staging_pathObject



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

#compileObject

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_keyObject



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_rbObject



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_dirObject



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_keyObject



42
43
44
# File 'lib/chef-umami/client.rb', line 42

def fake_client_key
  File.join(staging_dir, 'umami.pem')
end

#policy_nameObject



34
35
36
# File 'lib/chef-umami/client.rb', line 34

def policy_name
  @policy_name ||= Umami::Policyfile::PolicyfileLock.new(policyfile).name
end

#prepObject

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_collectionObject

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