Module: Cuken::Api::Chef

Includes:
Aruba::Api, Common
Defined in:
lib/cuken/api/chef.rb,
lib/cuken/api/chef/role.rb,
lib/cuken/api/chef/knife.rb,
lib/cuken/api/chef/common.rb,
lib/cuken/api/chef/cookbook.rb,
lib/cuken/api/chef/data_bag.rb

Defined Under Namespace

Modules: Common, Cookbook, DataBag, Knife, Role

Constant Summary

Constants included from Aruba::Api

Aruba::Api::DEFAULT_IO_WAIT_SECONDS, Aruba::Api::DEFAULT_TIMEOUT_SECONDS

Instance Attribute Summary collapse

Attributes included from Common

#admin_client_name, #api_response, #chef_args, #client_name, #client_private_key_path, #cookbook, #cookbook_paths, #cookbooks_paths, #cookbooks_uri, #exception, #gemserver_thread, #inflated_response, #knife_config_file, #knife_debug, #local_chef_repo, #local_cookbook_repo, #local_site_cookbook_repo, #log_level, #recipe, #remote_chef_repo, #remote_cookbook_repo, #root_dir, #sandbox_url, #status, #stderr, #stdout, #uri

Instance Method Summary collapse

Methods included from Aruba::Api

#_create_file, #_ensure_newline, #_mkdir, #_write_interactive, #all_output, #all_stderr, #all_stdout, #announce_or_puts, #append_to_file, #assert_exact_output, #assert_exit_status_and_output, #assert_exit_status_and_partial_output, #assert_exiting_with, #assert_failing_with, #assert_partial_output, #assert_passing_with, #cd, #check_directory_presence, #check_exact_file_content, #check_file_content, #check_file_presence, #create_dir, #current_dir, #current_ruby, #detect_ruby, #dirs, #exit_timeout, #get_process, #in_dir, #io_wait, #only_processes, #original_env, #output_from, #overwrite_file, #prep_for_fs_check, #processes, #regexp, #register_process, #remove_file, #restore_env, #run, #run_interactive, #run_simple, #set_env, #stderr_from, #stdout_from, #stop_processes!, #type, #unescape, #unset_bundler_env_vars, #use_clean_gemset, #write_file

Methods included from Common

#admin_client, #check_chef_root_presence, #chef, #client, #create_client, #delete_client, #in_chef_root, #knife, #knife_command, #knife_config_file_error_handling, #make_admin, #make_non_admin, #ohai, ohai, #run_knife

Instance Attribute Details

#apt_server_threadObject

Returns the value of attribute apt_server_thread.



265
266
267
# File 'lib/cuken/api/chef.rb', line 265

def apt_server_thread
  @apt_server_thread
end

Instance Method Details

#admin_restObject



277
278
279
280
# File 'lib/cuken/api/chef.rb', line 277

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

#append_cookbook_path(cookbook, lp, lrp) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cuken/api/chef.rb', line 51

def append_cookbook_path(cookbook, lp, lrp)
  if lrp.exist?
    announce_or_puts(%{Adding cookbook path: #{lp}}) if @announce_env && cookbook
    chef.cookbook_paths << lp if cookbook
    announce_or_puts(%{Adding cookbooks path: #{lp.parent}}) if @announce_env && cookbook
    chef.cookbooks_paths << lp.parent if cookbook
    lrp
  else
    announce_or_puts(%{WARNING: cookbook(s) path: #{lp} is not a Git repository.}) if @announce_env && cookbook
  end
end

#apt_serverObject



267
268
269
270
271
272
273
274
275
# File 'lib/cuken/api/chef.rb', line 267

def apt_server
  @apt_server ||= ::WEBrick::HTTPServer.new(
    :Port         => 9000,
    :DocumentRoot => datadir + "/apt/var/www/apt",
    # Make WEBrick STFU
    :Logger       => ::Logger.new(StringIO.new),
    :AccessLog    => [ ::StringIO.new, ::WEBrick::AccessLog::COMMON_LOG_FORMAT ]
  )
end

#chef_clone_repo(ckbk_path, cookbook = false, repo = chef.remote_chef_repo, type = {'branch' => 'master'}, deep_clone = true) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/cuken/api/chef.rb', line 90

def chef_clone_repo(ckbk_path, cookbook = false, repo = chef.remote_chef_repo, type = {'branch' => 'master'}, deep_clone = true)
  in_dir do
    pth = Pathname(ckbk_path).expand_path
    gritty = ::Grit::Git.new((pth + '.git').to_s)
    announce_or_puts gritty.inspect
    clone_opts = {:depth => 1, :quiet => false, :verbose => true, :progress => true}
    type['branch'] = type['branch'].nil? ? '' : type['branch']
    type['tag'] = type['tag'].nil? ? '' : type['tag']
    type['ref'] = type['ref'].nil? ? '' : type['ref']
    clone_opts[:depth] = 1 unless deep_clone
    if pth.directory?
      announce_or_puts "Pulling: #{repo} into #{pth}"
      FileUtils.cd(pth.to_s) do
        res = gritty.pull(clone_opts, repo)
      end
      clone_opts[:branch] = type['branch'].empty? ? 'master' : type['branch']
    else
      clone_opts[:branch] = type['branch'].empty? ? 'master' : type['branch']
      announce_or_puts "Cloning: #{repo} into #{pth}"
      res = gritty.clone(clone_opts, repo, pth.to_s)
      Dir.chdir(pth.to_s) do
        if ::File.exist?('.gitmodules')
          cmd = "git submodule init"
          announce_or_puts `#{cmd}`
          cmd = "git submodule update"
          announce_or_puts `#{cmd}`
        end
      end
    end
    clone_pull_error_check(res) if res
    update_cookbook_paths(pth, cookbook)
    unless chef.cookbooks_paths.empty?   # is empty after cloning default chef-repo
      grotty = ::Grit::Git.new((pth + '.git').to_s)
      grotty.checkout( { :B => clone_opts[:branch]||'master' } )
      if !type['tag'].empty? || !type['ref'].empty?
        grotty.checkout( { :B => true }, 'cuken', type['tag']||type['ref'] )
      else
        grotty.checkout( { :B => true }, 'cuken' )
      end
    end
    pth
  end
end

#chef_submodule_repo(ckbk_path, cookbook = false, repo = chef.remote_chef_repo, type = {'branch' => 'master'}, deep_clone = true) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/cuken/api/chef.rb', line 134

def chef_submodule_repo(ckbk_path, cookbook = false, repo = chef.remote_chef_repo, type = {'branch' => 'master'}, deep_clone = true)
  in_dir do
    pth = (Pathname(chef.root_dir).expand_path)+ckbk_path
    gritty = ::Grit::Git.new((pth + '.git').to_s)
    announce_or_puts gritty.inspect
    clone_opts = {:quiet => false, :verbose => true, :progress => true}
    clone_opts[:depth] = 1 unless deep_clone
    checkout = type['ref']||type['tag']||type['branch']||'master'
    type['branch'] = type['branch'].nil? ? '' : type['branch']
    type['tag'] = type['tag'].nil? ? '' : type['tag']
    type['ref'] = type['ref'].nil? ? '' : type['ref']
    res = nil
    if pth.directory?
         announce_or_puts "Updating submodule: #{repo} at #{pth}"
        FileUtils.cd(pth.to_s) do
          cmd = "git fetch"
          res = `#{cmd}`
          announce_or_puts res
        end
      clone_opts[:branch] = type['branch'].empty? ? 'master' : type['branch']
    else
      clone_opts[:branch] = type['branch'].empty? ? 'master' : type['branch']
      announce_or_puts "Adding submodule: #{repo} at #{pth}"
      FileUtils.cd(chef.root_dir) do
        cmd = "git submodule add #{repo} #{pth.to_s}"
        announce_or_puts `#{cmd}`
      end
      FileUtils.cd(pth.to_s) do
        cmd = "git submodule init"
        res = `#{cmd}`
        announce_or_puts res
        cmd = "git submodule update"
        res = `#{cmd}`
        announce_or_puts res
      end
    end
    clone_pull_error_check(res) if res
    update_cookbook_paths(pth, cookbook)
    unless chef.cookbooks_paths.empty?   # is empty after cloning default chef-repo
      grotty = ::Grit::Git.new((pth + '.git').to_s)
      cmd = "git submodule add #{repo} #{pth.to_s}"
      announce_or_puts `#{cmd}`
      FileUtils.cd(pth.to_s) do
        cmd = "git submodule init"
        res = `#{cmd}`
        announce_or_puts res
        cmd = "git submodule update"
        res = `#{cmd}`
        announce_or_puts res
        if !type['tag'].empty? || !type['ref'].empty?
          grotty.checkout( { :B => true }, 'cuken', type['tag']||type['ref'] )
        else
          grotty.checkout( { :B => true }, 'cuken' )
        end
      end
    end
    pth
  end
end

#cleanup_dirsObject



247
248
249
# File 'lib/cuken/api/chef.rb', line 247

def cleanup_dirs
  @cleanup_dirs ||= ::Array.new
end

#cleanup_filesObject



243
244
245
# File 'lib/cuken/api/chef.rb', line 243

def cleanup_files
  @cleanup_files ||= ::Array.new
end

#clone_pull_error_check(res) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/cuken/api/chef.rb', line 81

def clone_pull_error_check(res)
  if repo = res[/Could not find Repository /]
    raise RuntimeError, "Could not find Repository #{repo}", caller
  end
  if repo = res[/ERROR: (.*) doesn't exist. Did you enter it correctly?/,1]
    raise RuntimeError, "ERROR: #{repo} doesn't exist. Did you enter it correctly? #{repo}", caller
  end
end

#configdirObject



239
240
241
# File 'lib/cuken/api/chef.rb', line 239

def configdir
  @configdir ||= ::File.join(File.dirname(__FILE__), "..", "data", "config")
end

#couchdb_rest_clientObject



282
283
284
# File 'lib/cuken/api/chef.rb', line 282

def couchdb_rest_client
  ::Chef::REST.new('http://localhost:5984/chef_integration', false, false)
end

#datadirObject



235
236
237
# File 'lib/cuken/api/chef.rb', line 235

def datadir
  @datadir ||= ::File.join(File.dirname(__FILE__), "..", "data")
end

#gemserverObject



255
256
257
258
259
260
261
262
263
# File 'lib/cuken/api/chef.rb', line 255

def gemserver
  @gemserver ||= ::WEBrick::HTTPServer.new(
    :Port         => 8000,
    :DocumentRoot => datadir + "/gems/",
    # Make WEBrick STFU
    :Logger       => ::Logger.new(StringIO.new),
    :AccessLog    => [ ::StringIO.new, ::WEBrick::AccessLog::COMMON_LOG_FORMAT ]
  )
end

#restObject



223
224
225
# File 'lib/cuken/api/chef.rb', line 223

def rest
  @rest ||= ::Chef::REST.new('http://localhost:4000', nil, nil)
end

#run_knife_command(cmd, interactive = false) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/cuken/api/chef.rb', line 194

def run_knife_command(cmd, interactive=false)
  no_ckbk_pth_opt = ['cookbook delete', 'role from file'].each{|str| break true if cmd[/#{str}/]}
  no_ckbk_pth = chef.cookbooks_paths.empty?
  if no_ckbk_pth
    ckbk_pth_opt = false
  else
    ckbk_pth = (chef.cookbooks_paths.collect { |pn| pn.expand_path.to_s }).join(':')
    ckbk_pth_opt = true
  end
  ckbk_pth_opt = false if no_ckbk_pth_opt.is_a? TrueClass
  in_dir do
    unless chef.knife_config_file
      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
  cmd += " -c #{chef.knife_config_file.expand_path.to_s}" if chef.knife_config_file.expand_path.exist?
  cmd += " -o #{ckbk_pth}" if ckbk_pth_opt
  # cmd += " --log_level debug" if chef.knife_debug
  chef.root_dir ||= current_dir
  in_chef_root do
    if interactive
      run_interactive(unescape("#{chef.knife_command}" + cmd))
    else
      run_simple(unescape("#{chef.knife_command}" + cmd))
    end
  end
end

#server_tmpdirObject



231
232
233
# File 'lib/cuken/api/chef.rb', line 231

def server_tmpdir
  @server_tmpdir ||= ::File.expand_path(File.join(datadir, "tmp"))
end

#stashObject



251
252
253
# File 'lib/cuken/api/chef.rb', line 251

def stash
  @stash ||= ::Hash.new
end

#tmpdirObject



227
228
229
# File 'lib/cuken/api/chef.rb', line 227

def tmpdir
  @tmpdir ||= ::File.join(Dir.tmpdir, "chef_integration")
end

#update_cookbook_paths(ckbk_path, cookbook) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cuken/api/chef.rb', line 64

def update_cookbook_paths(ckbk_path, cookbook)
  lp = Pathname(ckbk_path).expand_path.realdirpath
  lrp = lp + '.git'
  if cookbook
    append_cookbook_path(cookbook, lp, lrp)
  else
    lp.each_child do |pth|
      rpth = pth + '.git'
      append_cookbook_path(true, pth, rpth) if rpth.directory?
    end
    chef.cookbooks_paths.uniq!
    if chef.cookbooks_paths.empty?
      announce_or_puts(%{WARNING: cookbooks path: #{lp} does not contain any Git repositories.}) if @announce_env
    end
  end
end