How to use Chef LWRP to specify GIT Branch

355 views Asked by At

What we really want is for the deploy LWRP to be able to specify the branch to the GIT. Ideally this is pulled off of an environment attirbute.

we are calling it like this :

my_deploy 'install my-client-portal' do
       repo 'https://hqdevgit01.my.lan/sites/my-client-portal.git'
       destination '/var/sites/my-client-portal'
 action :installNodeFromGit
        branch node[:my_deploy][:branch_name]
end

The branch above is not working..

LWRP Resource

actions :installNodeFromGit

default_action :installNodeFromGit if defined?(default_action)

attribute :repo, :kind_of => String, :required => true
attribute :destination, :kind_of => String, :required => true
attribute :branch, :kind_of => String, :required => false, :default => 'master'

LWRP provider

use_inline_resources

action :installNodeFromGit do

    converge_by("Installing.") do
      resp = install
      @new_resource.updated_by_last_action(resp)  
    end

end

def load_current_resource

  @current_resource = Chef::Resource::MyDeploy.new(@new_resource.name)
  @current_resource.repo(@new_resource.repo)
  @current_resource.destination(@new_resource.destination)
  @current_resource.branch(@new_resource.branch)

end

def install
    ENV['GIT_SSL_NO_VERIFY']="true"

    directory new_resource.destination do
        owner 'root'
        group 'root'
        mode '0755'
         action :create
       recursive true
    end

    git new_resource.destination do
         repository new_resource.repo
         action :sync
         revision new_resource.branch
    end

    if new_resource.destination
       path = new_resource.destination
      cmd  = "npm install"
   execute "npm install at #{path}" do
              cwd path
                  command cmd
         end
    end
end
1

There are 1 answers

0
Tom Wilson On

Problem here is you are using an LWRP (that you must already have available in your Chef environment) and then hoping to run it as one or more other GIT branches (that are possibly not loaded in the Chef environment.)

If you set up your branches right, you could include each branch as a different cookbook (see docs for Berkshelf or Librarian for details on spec'ing a branch.) For example, you might have berkshelf lines like this:

cookbook "mycookbook", git: "https://github.com/my_git/mycookbook.git", branch: "master"
cookbook "mycookbook_branch1", git: "https://github.com/my_git/mycookbook.git", branch: "branch1"
# etc

I think the branch cookbooks would have to have these unique names internally (e.g. "mycookbook_branch1".) It's messy but if you make each branch accessible in your Chef environment, you might have a fighting chance to select the desired (branched) resource.

Obviously I don't know the details, but on the surface it seems simpler to just create each of the possible resources (with different names) in the master branch and select the right one in your recipe. If that becomes really un-DRY, you can always extract a module, etc.