Module: Cuken::Api::Chef::Common

Includes:
Grit
Included in:
Cuken::Api::Chef
Defined in:
lib/cuken/api/chef/common.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#admin_client_nameObject

Returns the value of attribute admin_client_name.



9
10
11
# File 'lib/cuken/api/chef/common.rb', line 9

def admin_client_name
  @admin_client_name
end

#api_responseObject

Returns the value of attribute api_response.



9
10
11
# File 'lib/cuken/api/chef/common.rb', line 9

def api_response
  @api_response
end

#chef_argsObject

Returns the value of attribute chef_args.



9
10
11
# File 'lib/cuken/api/chef/common.rb', line 9

def chef_args
  @chef_args
end

#client_nameObject

Returns the value of attribute client_name.



9
10
11
# File 'lib/cuken/api/chef/common.rb', line 9

def client_name
  @client_name
end

#client_private_key_pathObject

Returns the value of attribute client_private_key_path.



9
10
11
# File 'lib/cuken/api/chef/common.rb', line 9

def client_private_key_path
  @client_private_key_path
end

#cookbookObject

Returns the value of attribute cookbook.



9
10
11
# File 'lib/cuken/api/chef/common.rb', line 9

def cookbook
  @cookbook
end

#cookbook_pathsObject

Returns the value of attribute cookbook_paths.



9
10
11
# File 'lib/cuken/api/chef/common.rb', line 9

def cookbook_paths
  @cookbook_paths
end

#cookbooks_pathsObject

Returns the value of attribute cookbooks_paths.



9
10
11
# File 'lib/cuken/api/chef/common.rb', line 9

def cookbooks_paths
  @cookbooks_paths
end

#cookbooks_uriObject

Returns the value of attribute cookbooks_uri.



9
10
11
# File 'lib/cuken/api/chef/common.rb', line 9

def cookbooks_uri
  @cookbooks_uri
end

#exceptionObject

Returns the value of attribute exception.



9
10
11
# File 'lib/cuken/api/chef/common.rb', line 9

def exception
  @exception
end

#gemserver_threadObject

Returns the value of attribute gemserver_thread.



9
10
11
# File 'lib/cuken/api/chef/common.rb', line 9

def gemserver_thread
  @gemserver_thread
end

#inflated_responseObject

Returns the value of attribute inflated_response.



9
10
11
# File 'lib/cuken/api/chef/common.rb', line 9

def inflated_response
  @inflated_response
end

#knife_config_fileObject

Returns the value of attribute knife_config_file.



9
10
11
# File 'lib/cuken/api/chef/common.rb', line 9

def knife_config_file
  @knife_config_file
end

#knife_debugObject

Returns the value of attribute knife_debug.



9
10
11
# File 'lib/cuken/api/chef/common.rb', line 9

def knife_debug
  @knife_debug
end

#local_chef_repoObject

Returns the value of attribute local_chef_repo.



9
10
11
# File 'lib/cuken/api/chef/common.rb', line 9

def local_chef_repo
  @local_chef_repo
end

#local_cookbook_repoObject

Returns the value of attribute local_cookbook_repo.



9
10
11
# File 'lib/cuken/api/chef/common.rb', line 9

def local_cookbook_repo
  @local_cookbook_repo
end

#local_site_cookbook_repoObject

Returns the value of attribute local_site_cookbook_repo.



9
10
11
# File 'lib/cuken/api/chef/common.rb', line 9

def local_site_cookbook_repo
  @local_site_cookbook_repo
end

#log_levelObject

Returns the value of attribute log_level.



9
10
11
# File 'lib/cuken/api/chef/common.rb', line 9

def log_level
  @log_level
end

#recipeObject

Returns the value of attribute recipe.



9
10
11
# File 'lib/cuken/api/chef/common.rb', line 9

def recipe
  @recipe
end

#remote_chef_repoObject

Returns the value of attribute remote_chef_repo.



9
10
11
# File 'lib/cuken/api/chef/common.rb', line 9

def remote_chef_repo
  @remote_chef_repo
end

#remote_cookbook_repoObject

Returns the value of attribute remote_cookbook_repo.



9
10
11
# File 'lib/cuken/api/chef/common.rb', line 9

def remote_cookbook_repo
  @remote_cookbook_repo
end

#root_dirObject

Returns the value of attribute root_dir.



9
10
11
# File 'lib/cuken/api/chef/common.rb', line 9

def root_dir
  @root_dir
end

#sandbox_urlObject

Returns the value of attribute sandbox_url.



9
10
11
# File 'lib/cuken/api/chef/common.rb', line 9

def sandbox_url
  @sandbox_url
end

#statusObject

Returns the value of attribute status.



9
10
11
# File 'lib/cuken/api/chef/common.rb', line 9

def status
  @status
end

#stderrObject

Returns the value of attribute stderr.



9
10
11
# File 'lib/cuken/api/chef/common.rb', line 9

def stderr
  @stderr
end

#stdoutObject

Returns the value of attribute stdout.



9
10
11
# File 'lib/cuken/api/chef/common.rb', line 9

def stdout
  @stdout
end

#uriObject

Returns the value of attribute uri.



9
10
11
# File 'lib/cuken/api/chef/common.rb', line 9

def uri
  @uri
end

Class Method Details

.ohaiObject



37
38
39
40
41
42
43
44
# File 'lib/cuken/api/chef/common.rb', line 37

def self.ohai
  # ohai takes a while, so only ever run it once.
  @ohai ||= begin
    o = Ohai::System.new
    o.all_plugins
    o
  end
end

Instance Method Details

#admin_clientObject



131
132
133
134
135
136
137
138
139
140
# File 'lib/cuken/api/chef/common.rb', line 131

def admin_client
  unless @admin_client
    r = ::Chef::REST.new(::Chef::Config[:registration_url], ::Chef::Config[:validation_client_name], ::Chef::Config[:validation_key])
    r.register("bobo", "#{tmpdir}/bobo.pem")
    c = ::Chef::ApiClient.cdb_load("bobo")
    c.admin(true)
    c.cdb_save
    @admin_client = c
  end
end

#check_chef_root_presence(directory, expect_presence = true) ⇒ Object



142
143
144
145
# File 'lib/cuken/api/chef/common.rb', line 142

def check_chef_root_presence(directory, expect_presence = true)
  chef.root_dir = directory
  check_placed_directory_presence([chef.root_dir], expect_presence)
end

#chefObject



50
51
52
53
54
55
56
# File 'lib/cuken/api/chef/common.rb', line 50

def chef
  @remote_chef_repo ||= "git://github.com/cookbooks/chef-repo.git"
  @knife_debug = true if @knife_debug.nil?
  @cookbooks_paths ||= []
  @cookbook_paths ||= []
  @chef ||= self
end

#clientObject



98
99
100
101
102
103
104
# File 'lib/cuken/api/chef/common.rb', line 98

def client
  @client ||= begin
    c = ::Chef::Client.new
    c.ohai = ohai
    c
  end
end

#create_client(client_name, admin = false) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/cuken/api/chef/common.rb', line 147

def create_client(client_name, admin = false)
  chef.client_private_key_path = chef.root_dir + "/.chef/#{client_name}.pem"
  data = {:name => client_name,
          :file => chef.client_private_key_path,
          :no_editor => true,
          :config_file => chef.knife_config_file}
  argv = ['client', 'create', data[:name], '--file', data[:file], '--config', data[:config_file],'--no-editor']
  argv << '--admin' if admin
  unless Pathname(chef.client_private_key_path).exist?
    with_args *argv do
      ::Chef::Application::Knife.new.run
    end
  else
    #TODO: Verify client exists on the Chef server, and has matching public key.
  end
end

#delete_client(client_name) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/cuken/api/chef/common.rb', line 164

def delete_client(client_name)
  chef.client_private_key_path = chef.root_dir + "/.chef/#{client_name}.pem"
  data = {:name => client_name,
          :file => chef.client_private_key_path,
          :no_editor => true,
          :yes => true,
          :print_after => true,
          :config_file => chef.knife_config_file}
  argv = ['client', 'delete', data[:name], '--no-editor', '--yes' ]
  with_args *argv do
    ::Chef::Application::Knife.new.run
  end
end

#in_chef_root(&block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cuken/api/chef/common.rb', line 58

def in_chef_root(&block)
  raise "You need to specify a Chef root directory." unless chef.root_dir
  if chef.root_dir.to_s[0] == '/'
    ::Dir.chdir(chef.root_dir, &block)
  else
    in_dir do
      chef.root_dir = ::File.expand_path(chef.root_dir)
      ::Dir.chdir(chef.root_dir, &block)
    end
  end
end

#knifeObject



70
71
72
# File 'lib/cuken/api/chef/common.rb', line 70

def knife
  ::Cuken::Api::Chef::Knife.new
end

#knife_commandObject



74
75
76
# File 'lib/cuken/api/chef/common.rb', line 74

def knife_command
  'knife '
end

#knife_config_file_error_handlingObject

Raises:

  • (RuntimeError)


87
88
89
90
91
92
# File 'lib/cuken/api/chef/common.rb', line 87

def knife_config_file_error_handling
  unless chef.knife_config_file && Pathname(chef.knife_config_file).exist?
    chef.knife_config_file = Pathname(chef.local_chef_repo).ascend { |d| h=d+'.chef'+'knife.rb'; break h if h.file? }
  end
  raise(RuntimeError, "chef.knife_config_file is required", caller) unless chef.knife_config_file
end

#make_adminObject



114
115
116
117
118
119
# File 'lib/cuken/api/chef/common.rb', line 114

def make_admin
  admin_client
  @rest = ::Chef::REST.new(Chef::Config[:registration_url], 'bobo', "#{tmpdir}/bobo.pem")
  #Chef::Config[:client_key] = "#{tmpdir}/bobo.pem"
  #Chef::Config[:node_name] = "bobo"
end

#make_non_adminObject



121
122
123
124
125
126
127
128
129
# File 'lib/cuken/api/chef/common.rb', line 121

def make_non_admin
  r = ::Chef::REST.new(::Chef::Config[:registration_url], ::Chef::Config[:validation_client_name], ::Chef::Config[:validation_key])
  r.register("not_admin", "#{tmpdir}/not_admin.pem")
  c = ::Chef::ApiClient.cdb_load("not_admin")
  c.cdb_save
  @rest = ::Chef::REST.new(::Chef::Config[:registration_url], 'not_admin', "#{tmpdir}/not_admin.pem")
  #Chef::Config[:client_key] = "#{tmpdir}/not_admin.pem"
  #Chef::Config[:node_name] = "not_admin"
end

#ohaiObject



46
47
48
# File 'lib/cuken/api/chef/common.rb', line 46

def ohai
  ::Cuken::Api::Chef::Common.ohai
end

#run_knife(argv = []) ⇒ Object

TODO: Once a method is spec’d refactor to use this and knife_config_file_error_handling



79
80
81
82
83
84
85
# File 'lib/cuken/api/chef/common.rb', line 79

def run_knife(argv=[])
  argv << '--config' << chef.knife_config_file.to_s
  with_args *argv do
    ::Chef::Application::Knife.new.run
  end
  ::Chef::Knife.cuken
end