Module: VagrantPlugins::CookbookFetcher

Defined in:
lib/vagrant-cookbook-fetcher.rb,
lib/vagrant-cookbook-fetcher/guts.rb,
lib/vagrant-cookbook-fetcher/config.rb,
lib/vagrant-cookbook-fetcher/plugin.rb,
lib/vagrant-cookbook-fetcher/command.rb,
lib/vagrant-cookbook-fetcher/version.rb,
lib/vagrant-cookbook-fetcher/action_set_chef_paths.rb,
lib/vagrant-cookbook-fetcher/action_fetch_cookbooks.rb

Defined Under Namespace

Classes: CheckoutCommand, Config, FetchCookbooksAction, Plugin, SetChefPathsAction

Constant Summary collapse

NAME =
"vagrant-cookbook-fetcher"
VERSION =
"0.4.0"
AUTHOR =
"Clinton Wolfe"
AUTHOR_EMAIL =
"clintoncwolfe [at] gmail [dot] com"
SUMMARY =
"Fetch your Chef cookbooks whenever you provision"
DESCRIPTION =
"Whenever you run start, up, or provision, this plugin will dynamically fetch a list of checkouts from a URL; checkout each one; then create a combined roles directory, with symlinks."
URL =
"http://github.com/clintoncwolfe/vagrant-cookbook-fetcher"

Class Method Summary collapse

Class Method Details

.fetch_checkout_list(url, logger) ⇒ Object

Utility method, fetches checkout list, parses it, and writes cookbook order to a file in the current working directory.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/vagrant-cookbook-fetcher/guts.rb', line 31

def fetch_checkout_list (url, logger)
  require 'open-uri'

  checkouts = { :by_dir => {}, :cookbook_list => [] } 
  
  logger.info "Fetching checkout list from #{url}"

  # This is idiotic, but open-uri's open() fails on URLs like 'file:///...'
  # It does fine on absolute paths, though.
  url.gsub!(/^file:\/\//, '')

  open(url, {:ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE }) do |resp|
    resp.each do |line|
      line.chomp!
      if !line.empty? then
        pieces = line.split(/,/)
        branch = pieces[3]
        dir = pieces[2]

        # Build info hash
        checkouts[:by_dir][dir] = {
          :vcs => pieces[0],
          :repo => pieces[1],
          :dir => dir,
          :branch => pieces[3],
          :creds => pieces[4],          
        }
      
        # Build cookbook list.  Use first part of directory, and append cookbooks
        checkouts[:cookbook_list].push 'checkouts/' + (dir.split('/'))[0] + '/cookbooks'

        # Write cookbook order to a file, in case we are later disabled
        File.open('.cookbook-order', 'w') do |f|
          f.print(checkouts[:cookbook_list].join("\n"))
        end
      end
    end
  end
  return checkouts
end

.perform_checkouts(checkouts, logger) ⇒ Object

Utility method. Based on a parsed checkout list, performs each of the checkouts, creating the checkouts/ directory in the current directory.



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
133
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
# File 'lib/vagrant-cookbook-fetcher/guts.rb', line 98

def perform_checkouts (checkouts,logger)

  if !Dir.exists?("checkouts") then Dir.mkdir("checkouts") end
  
  Dir.chdir("checkouts") do  
    checkouts[:by_dir].each do |dir, info|
      logger.info "Updating checkout '#{dir}'"

      if info[:vcs] == 'git' then
        if Dir.exists?(info[:dir]) then
          # pull
          Dir.chdir(info[:dir]) do
            # TODO ignores git creds
            cmd = "git remote set-url origin #{info[:repo]}"
            safe_run(cmd, logger)
            cmd = "git fetch origin"
            safe_run(cmd, logger)

            local_branch = `git rev-parse --verify --symbolic-full-name #{info[:branch]} 2> /dev/null`.rstrip
            # no local branch
            if ! $?.success? then
              cmd = "git rev-parse --verify -q --symbolic-full-name origin/#{info[:branch]}"
              unless system cmd then raise "Could not find branch or commit #{info[:branch]}" end
              cmd = "git checkout -b #{info[:branch]} origin/#{info[:branch]}"
              safe_run(cmd, logger)

            elsif local_branch.empty? then
              # no branch
              cmd = "git checkout #{info[:branch]}"
              safe_run(cmd, logger)
            else
              # local branch already exists
              cmd = "git checkout #{info[:branch]}"
              safe_run(cmd, logger, /Already on '.+'/)
              cmd = "git merge origin/#{info[:branch]}"
              safe_run(cmd, logger, /Already up-to-date\./)
            end
          end
        else
          # clone
          # can't use --branch because it won't work with commit ids
          cmd = "git clone --no-checkout #{info[:repo]} #{info[:dir]}"
          safe_run(cmd, logger)
          Dir.chdir(info[:dir]) do
            cmd = "git rev-parse --verify -q origin/#{info[:branch]}"
            # branch
            if system cmd then
              current_branch=`git symbolic-ref HEAD 2> /dev/null`.rstrip
              if $?.success? && current_branch == "refs/heads/#{info[:branch]}" then
                cmd = "git checkout #{info[:branch]}"
                safe_run(cmd, logger)
              else
                cmd = "git checkout -B #{info[:branch]} origin/#{info[:branch]}"
                safe_run(cmd, logger)
              end
              #commit
            else
              cmd = "git checkout #{info[:branch]}"
              safe_run(cmd, logger)
            end
          end
        end
      else
        raise "Unsupported VCS '#{info[:vcs]}' in checkout list for entry '#{dir}'"
      end
    end
  end
end

.perform_fetch(args = {}) ⇒ Object

Utility method - reads the config, fetches the checkout list, does the checkout, and does the copying-in. Expects cwd to be the root_path.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/vagrant-cookbook-fetcher/guts.rb', line 8

def perform_fetch (args = {})
  url    = args[:url]
  logger = args[:logger]
  path   = args[:path]

  unless url then
    logger.warn "No cookbook_fetcher URL specified - skipping checkouts"
    return
  end

  Dir.chdir(path) do
    checkouts = CookbookFetcher.fetch_checkout_list(url,logger)
    CookbookFetcher.perform_checkouts(checkouts,logger)
    CookbookFetcher.update_copies(checkouts,logger)
  end
end

.safe_run(cmd, logger, ignore_regex = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/vagrant-cookbook-fetcher/guts.rb', line 73

def safe_run (cmd, logger, ignore_regex = nil)
  if logger.respond_to?(:debug) 
    logger.debug("Running #{cmd}")
  end
  output = `#{cmd} 2>&1`
  unless $?.success? then
    pwd = Dir.pwd
    logger.error("Got exit code #{$?.exitstatus} while running '#{cmd}' in '#{pwd}'")
    logger.error("Output: #{output}")
    exit $?.exitstatus
  end
  if (ignore_regex)
    output.gsub!(ignore_regex, '')
  end
  output.chomp!
  unless output.empty? 
    puts output
  end
end

.update_copies(checkouts, logger) ⇒ Object

Utility method - given a parsed checkout list, and assuming the checkout have already been performed, creates the combined/ directory in the current directory, and copies in the roles, nodes, etc.



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/vagrant-cookbook-fetcher/guts.rb', line 171

def update_copies (checkouts,logger) 
  things_to_link = [
    "roles",
    "nodes",
    "handlers",
    "data_bags",
  ]
  logger.info "Copying into combined/ #{things_to_link.sort.join(', ')}"

  if Dir.exists?("combined") then FileUtils.rm_rf("combined") end
  Dir.mkdir("combined")
  Dir.chdir("combined") do  
    # Create/clear the subdirs
    things_to_link.each do |thing|
      Dir.mkdir(thing)
    end
  end

  # Being careful to go in cookbook order, copy the files
  checkouts[:cookbook_list].each do |cookbook_dir|
    checkout_dir = (cookbook_dir.split('/'))[1]
    things_to_link.each do |thing|
      checkout_thing_dir = "checkouts/#{checkout_dir}/#{thing}"
      combined_dir = "combined/#{thing}"

      # If this checkout has anything to contribute
      if Dir.exists?(checkout_thing_dir) then
        FileUtils.cp_r("#{checkout_thing_dir}/.", combined_dir)
      end
    end
  end  
end