balanced-cookbooks / balanced-ci

Internal configuration for the Jekins CI environment
4 stars 1 forks source link

How to hardcode includes #8

Open mjallday opened 10 years ago

mjallday commented 10 years ago

how does chef know how to include templates?

if i change this line https://github.com/balanced-cookbooks/balanced-ci/blob/new-jobs/libraries/balanced_ci_job.rb#L16

to source 'job-balanced.xml.erb'

i get the error


Chef::Exceptions::FileNotFound
------------------------------
balanced_ci_job[balanced-test] (/tmp/vagrant-chef-1/chef-solo-1/cookbooks/balanced-ci/libraries/balanced_ci_pipeline.rb line 65) had an error: Chef::Exceptions::FileNotFound: jenkins_job[balanced-test] (/tmp/
vagrant-chef-1/chef-solo-1/cookbooks/balanced-ci/libraries/balanced_ci_job.rb line 15) had an error: Chef::Exceptions::FileNotFound: template[/var/lib/jenkins/jobs/balanced-test/config.xml] (/tmp/vagrant-chef
-1/chef-solo-1/cookbooks/jenkins/libraries/jenkins_job.rb line 77) had an error: Chef::Exceptions::FileNotFound: Cookbook 'ci' (1.0.12) does not contain a file at any of these locations:
  templates/ubuntu-12.04/job-balanced.xml.erb
  templates/ubuntu/job-balanced.xml.erb
  templates/default/job-balanced.xml.erb

This cookbook _does_ contain: ['/tmp/vagrant-chef-1/chef-solo-1/cookbooks/ci/templates/default/git.xml.erb','/tmp/vagrant-chef-1/chef-solo-1/cookbooks/ci/templates/default/google_auth.xml.erb','/tmp/vagrant-c
hef-1/chef-solo-1/cookbooks/ci/templates/default/job-config.xml.erb','/tmp/vagrant-chef-1/chef-solo-1/cookbooks/ci/templates/default/slave_user.xml.erb','/tmp/vagrant-chef-1/chef-solo-1/cookbooks/ci/templates
/default/ssh_config.erb']
coderanger commented 10 years ago

I think this was resolved with the template_content changes?

coderanger commented 10 years ago

Ahh okay, I see what you were referencing. Time for an infodump:

So to render a template, Chef needs two bits of information: the template name and which cookbook the template is in (hereafter template source and template cookbook). For the template source, you either have to pass that when you create the resource instance, or optionally give a default_source when defining the resource. For the template cookbook you have four options (checked in order):

  1. Pass an explicit cookbook when creating the resource.
  2. If an explicit source in the resource, it will use the cookbook the resource was instantiated in.
  3. If given when the resource was defined, default_cookbook is used.
  4. Magic fallback: if none of the above cases triggered, poise tries to figure out what cookbook the resource was defined in.

Most of the time we want to be using case 4, but there is a hitch. What 4 is actually computing is the cookbook of the thing that called attribute('', template: true, ...). This means that when you subclass ci_job, it was still looking for templates in the ci cookbook even though the new class is in balanced-ci.

So how do we fix this? I just pushed a change to poise that should provide flexible enough semantics. In short the change is that if you define the same template_content attribute again in a subclass, it inherits the options from the original. This means that in balanced_ci_job you can just add attribute('', template:true) and that will leave the default_source and default_options from the base class intact, but reset the cookbook detected in case 4.

This has been a 2AM braindump. We now return you to your regularly scheduled programming.

mahmoudimus commented 10 years ago

@coderanger this is no longer an issue given https://github.com/balanced-cookbooks/jenkins/pull/2 rght?