knife-github 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/.gitignore +1 -0
  2. data/.yardopts +1 -0
  3. data/LICENSE +201 -201
  4. data/Rakefile +5 -0
  5. data/doc/Chef.html +132 -0
  6. data/doc/Chef/Knife.html +138 -0
  7. data/doc/Chef/Knife/GithubBase.html +784 -0
  8. data/doc/Chef/Knife/GithubBaseList.html +316 -0
  9. data/doc/Chef/Knife/GithubClone.html +341 -0
  10. data/doc/Chef/Knife/GithubCompare.html +299 -0
  11. data/doc/Chef/Knife/GithubDeploy.html +1449 -0
  12. data/doc/Chef/Knife/GithubDiff.html +473 -0
  13. data/doc/Chef/Knife/GithubList.html +299 -0
  14. data/doc/KnifeGithubCleanup.html +115 -0
  15. data/doc/KnifeGithubCleanup/GithubCleanup.html +425 -0
  16. data/doc/KnifeGithubSearch.html +115 -0
  17. data/doc/KnifeGithubSearch/GithubSearch.html +325 -0
  18. data/doc/_index.html +206 -0
  19. data/doc/class_list.html +54 -0
  20. data/doc/css/common.css +1 -0
  21. data/doc/css/full_list.css +57 -0
  22. data/doc/css/style.css +338 -0
  23. data/doc/file.README.html +118 -0
  24. data/doc/file_list.html +56 -0
  25. data/doc/frames.html +26 -0
  26. data/doc/index.html +118 -0
  27. data/doc/js/app.js +214 -0
  28. data/doc/js/full_list.js +178 -0
  29. data/doc/js/jquery.js +4 -0
  30. data/doc/method_list.html +197 -0
  31. data/doc/top-level-namespace.html +114 -0
  32. data/lib/chef/knife/github_base.rb +4 -4
  33. data/lib/chef/knife/github_baselist.rb +6 -3
  34. data/lib/chef/knife/github_cleanup.rb +1 -1
  35. data/lib/chef/knife/{github_download.rb → github_clone.rb} +8 -8
  36. data/lib/chef/knife/github_compare.rb +3 -4
  37. data/lib/chef/knife/github_deploy.rb +89 -67
  38. data/lib/chef/knife/github_diff.rb +50 -20
  39. data/lib/chef/knife/github_list.rb +1 -1
  40. data/lib/knife-github/version.rb +1 -1
  41. metadata +32 -3
@@ -95,10 +95,10 @@ class Chef
95
95
  ui.info "INFO: New version (#{webversion.to_s}) of knife-github is available!"
96
96
  ui.info "INFO: Turn off this message with --github_no_update or add knife[:github_no_update] = true to your configuration"
97
97
  end
98
- Chef::Log.debug("local_gem_version : " + thisversion.to_s)
99
- Chef::Log.debug("repo_gem_version : " + webversion.to_s)
100
- Chef::Log.debug("repo_downloads : " + json['version_downloads'].to_s)
101
- Chef::Log.debug("repo_total_downloads : " + json['downloads'].to_s)
98
+ Chef::Log.debug("gem_local_version : " + thisversion.to_s)
99
+ Chef::Log.debug("gem_repo_version : " + webversion.to_s)
100
+ Chef::Log.debug("gem_downloads : " + json['version_downloads'].to_s)
101
+ Chef::Log.debug("gem_total_downloads : " + json['downloads'].to_s)
102
102
 
103
103
  rescue
104
104
  ui.info "INFO: Cannot verify gem version information from rubygems.org"
@@ -57,10 +57,13 @@ class Chef
57
57
  config[:fields].downcase.split(',').each { |n| object_list << ((v["#{n}".strip]).to_s || 'n/a') }
58
58
  else
59
59
  color = :white
60
- if config[:mismatch] && !match.empty? && !config[:all]
60
+ if !match.empty? && !config[:all]
61
61
  matches = []; match.each { |m| matches << v[m].to_s }
62
- next if matches.uniq.count == 1
63
- color = :yellow
62
+ if matches.uniq.count == 1
63
+ next if config[:mismatch]
64
+ else
65
+ color = :yellow
66
+ end
64
67
  end
65
68
  columns.each { |c| r = c.split(","); object_list << ui.color((v["#{r.first}"]).to_s, color) || 'n/a' }
66
69
  end
@@ -39,7 +39,7 @@ module KnifeGithubCleanup
39
39
  require 'chef/mixin/shell_out'
40
40
  end
41
41
 
42
- banner "knife github cleanup [REPO] (options)"
42
+ banner "knife github cleanup [COOKBOOK] (options)"
43
43
  category "github"
44
44
 
45
45
  option :all,
@@ -21,7 +21,7 @@ require 'chef/knife'
21
21
  class Chef
22
22
  class Knife
23
23
 
24
- class GithubDownload < Knife
24
+ class GithubClone < Knife
25
25
 
26
26
  deps do
27
27
  require 'chef/knife/github_base'
@@ -29,19 +29,19 @@ class Chef
29
29
  require 'chef/mixin/shell_out'
30
30
  end
31
31
 
32
- banner "knife github download COOKBOOK (options)"
32
+ banner "knife github clone COOKBOOK (options)"
33
33
  category "github"
34
34
 
35
35
  option :all,
36
36
  :short => "-a",
37
37
  :long => "--all",
38
- :description => "Download all cookbooks from github.",
38
+ :description => "Clone all repo's from github.",
39
39
  :boolean => true
40
40
 
41
41
  option :force,
42
42
  :short => "-f",
43
43
  :long => "--force",
44
- :description => "Delete the existing cookbooks if exist.",
44
+ :description => "Delete the existing local repo if exist.",
45
45
  :boolean => true
46
46
 
47
47
  def run
@@ -65,17 +65,17 @@ class Chef
65
65
  @cookbook_name = name_args.first unless name_args.empty?
66
66
  if @cookbook_name
67
67
  repo = all_repos.select { |k,v| v["name"] == @cookbook_name }
68
- repo_download(repo, @cookbook_name)
68
+ repo_clone(repo, @cookbook_name)
69
69
  elsif config[:all]
70
70
  cookbooks.each do |c,v|
71
- repo_download(all_repos, c)
71
+ repo_clone(all_repos, c)
72
72
  end
73
73
  else
74
- Chef::Log.error("Please specify a cookbook name")
74
+ Chef::Log.error("Please specify a repository name")
75
75
  end
76
76
  end
77
77
 
78
- def repo_download(repo, cookbook)
78
+ def repo_clone(repo, cookbook)
79
79
  if repo.nil? || repo.empty?
80
80
  ui.info("Processing [?] #{cookbook}")
81
81
  Chef::Log.info("Cannot find the repository: #{cookbook} within github")
@@ -20,7 +20,6 @@ require 'chef/knife'
20
20
 
21
21
  class Chef
22
22
  class Knife
23
-
24
23
  class GithubCompare < Knife
25
24
 
26
25
  deps do
@@ -68,8 +67,8 @@ class Chef
68
67
  }
69
68
  else
70
69
  cookbooks.each { |k,v|
71
- get_all_repos[k].nil? || get_all_repos[k][git_link].nil? ? gh_url = ui.color("ERROR: Cannot find cookbook!", :red) : gh_url = get_all_repos[k][git_link]
72
- get_all_repos[k].nil? || get_all_repos[k]['latest_tag'].nil? ? gh_tag = ui.color("ERROR: No tags!", :red) : gh_tag = get_all_repos[k]['latest_tag']
70
+ get_all_repos[k].nil? || get_all_repos[k][git_link].nil? ? gh_url = ui.color("Cannot find cookbook!", :red) : gh_url = get_all_repos[k][git_link]
71
+ get_all_repos[k].nil? || get_all_repos[k]['latest_tag'].nil? || get_all_repos[k]['latest_tag'].empty? ? gh_tag = ui.color("No tags!", :red) : gh_tag = get_all_repos[k]['latest_tag']
73
72
  all_repos[k] = { 'name' => k, 'latest_cb_tag' => v['versions'][0]['version'], 'git_url' => gh_url, 'latest_gh_tag' => gh_tag }
74
73
  }
75
74
  end
@@ -83,7 +82,7 @@ class Chef
83
82
  repos = all_repos
84
83
  end
85
84
 
86
- columns = [ 'name,Name', 'latest_cb_tag,Tag', 'git_url,Link', 'latest_gh_tag,Tag' ]
85
+ columns = [ 'name,Chef Store', 'latest_cb_tag,Tag', 'git_url,Github Store', 'latest_gh_tag,Tag' ]
87
86
  match = [ 'latest_cb_tag', 'latest_gh_tag' ]
88
87
 
89
88
  if repos.nil? || repos.empty?
@@ -1,67 +1,57 @@
1
- #
2
- # Author:: Sander Botman (<sbotman@schubergphilis.com>)
3
- # Copyright:: Copyright (c) 2013 Sander Botman.
4
- # License:: Apache License, Version 2.0
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
- #
18
- # ---------------------------------------------------------------------------- #
19
- # Abstract
20
- # ---------------------------------------------------------------------------- #
21
- # This code is specific to our company workflow
22
- # When a cookbook is released it is tagged to a specific version
23
- # This version should match the cookbook version (from the metadata)
24
- # This version is then pinned against specific environments
25
- #
26
- # This class expects you to have pushed all your changes to github
27
- # It will then do the rest
28
- #
29
- # All modes presume you have used github download to download a cookbook or
30
- # are creating a new cookbook
31
- #
32
- # There are two modes of operation
33
- # Development (default)
34
- #
35
- # This will take a cookbook name
36
- # Do some basic version checks (if the current cookbook is frozen) and
37
- # upload it
38
- #
39
- # If the cookbook is frozen it will force you to choose a new version
40
- # and update the metadata accordingly
41
- #
42
- # Final (-f)
43
- #
44
- # You will be forced to select a new version.
45
- # You can choose via the options whether to increment the Major/minor or patch
46
- # revision numbers
47
- # The version will be tagged
48
- # Uploaded to the Chef server and frozen
49
- #
50
- # Version numbers
51
- #
52
- # You can choose a specific version number by specifying it on the command
53
- # line.
54
- #
55
- # If you do not specify a version, the version will be the version in your
56
- # cookbook's metadata
57
- #
58
- # A warning is issued if the version is lower than the version in github
59
- # ---------------------------------------------------------------------------- #
60
1
  require 'chef/knife'
61
2
 
62
3
  class Chef
63
4
  class Knife
64
-
5
+ # Implements the knife github deploy function
6
+ # @author:: Ian Southam (<isoutham@schubergphilis.com>)
7
+ # Copyright:: Copyright (c) 2013 Ian Southam.
8
+ # This code is specific to our company workflow
9
+ #
10
+ # == Overview
11
+ # All modes presume you have used github download to download a cookbook or
12
+ # are creating a new cookbook
13
+ #
14
+ # === Examples
15
+ # Deploy a development version of cookbook to your chef server
16
+ # knife github deploy cookbook_name
17
+ #
18
+ # Deploy a release version of cookbook to your chef server
19
+ # knife github deploy cookbook_name -f
20
+ #
21
+ # === Options
22
+ # -f Operate in final release mode
23
+ # -p Update the patch component of the version
24
+ # -m Update the minor component of the version
25
+ # -M Update the minor component of the version
26
+ #
27
+ # == Operation Modes
28
+ # Development (default)
29
+ #
30
+ # This will take a cookbook name
31
+ # Do some basic version checks (if the current cookbook is frozen) and
32
+ # upload it
33
+ #
34
+ # If the cookbook is frozen it will force you to choose a new version
35
+ # and update the metadata accordingly
36
+ #
37
+ # Release (-f)
38
+ #
39
+ # You will be forced to select a new version.
40
+ # You can choose via the options whether to increment the Major/minor or patch
41
+ # revision numbers
42
+ # The version will be tagged
43
+ # Uploaded to the Chef server and frozen
44
+ #
45
+ # == Version numbers
46
+ #
47
+ # You can choose a specific version number by specifying it on the command
48
+ # line.
49
+ #
50
+ # If you do not specify a version, the version will be the version in your
51
+ # cookbook's metadata
52
+ #
53
+ # A warning is issued if the version is lower than the version in github
54
+ #
65
55
  class GithubDeploy < Knife
66
56
  deps do
67
57
  require 'chef/knife/github_base'
@@ -102,11 +92,10 @@ class Chef
102
92
  :default => true
103
93
 
104
94
  def run
95
+ # Main run entry point for the class
105
96
 
106
- # validate base options from base module.
107
97
  validate_base_options
108
98
 
109
- # Display information if debug mode is on.
110
99
  display_debug_info
111
100
 
112
101
  # Gather all repo information from github.
@@ -135,7 +124,7 @@ class Chef
135
124
  # is the cookbook in the cookbook_path?
136
125
  if cookbook_path_valid?(@cookbook_name, false).nil?
137
126
  Chef::Log.error("Cookbook is not in cookbook path")
138
- ui.info("HINT: knife github download #{@cookbook_name}")
127
+ ui.info("HINT: knife github clone #{@cookbook_name}")
139
128
  exit 1
140
129
  end
141
130
 
@@ -161,6 +150,9 @@ class Chef
161
150
  if (config[:major] || config[:minor])
162
151
  config[:patch] = false
163
152
  end
153
+ if (config[:major] && config[:minor])
154
+ config[:minor] = false
155
+ end
164
156
 
165
157
  begin
166
158
  isFrozen = rest.get_rest("cookbooks/#{@cookbook_name}/#{cookbook_version}").frozen_version?
@@ -190,6 +182,7 @@ class Chef
190
182
  set_cookbook_version(cookbook_version)
191
183
  add_tag(cookbook_version)
192
184
  else
185
+ ui.confirm("Tag #{cookbook_version} exists - did you make this for this release?")
193
186
  checkout_tag(cookbook_version)
194
187
  set_cookbook_version(cookbook_version)
195
188
  end
@@ -210,12 +203,19 @@ class Chef
210
203
 
211
204
  end
212
205
 
206
+ # Ask user to increment current/desired version number
207
+ # Method will exit if the user chooses not to increment the version
208
+ #
209
+ # @param version [String] Version
210
+ # @return [String] New version number
211
+ #
213
212
  def up_version(version)
214
213
  while true do
215
214
  ui.info("Trying to deploy version #{version}")
216
215
  if @versions.include?(version)
217
216
  ui.info("Version #{version} is already in chef")
218
- ui.confirm("Shall I bump the version (No to Cancel)")
217
+ vt = choose_version(version)
218
+ ui.confirm("Shall I bump the version to #{vt} (No to Cancel)")
219
219
  version = choose_version(version)
220
220
  else
221
221
  break
@@ -224,10 +224,17 @@ class Chef
224
224
  version
225
225
  end
226
226
 
227
+ # Increment the current version according to the config
228
+ # options config[major] config[minor] config[patch]
229
+ # Method will exit if the user chooses not to increment the version
230
+ #
231
+ # @param version [String] Version
232
+ # @return [String] New version number
233
+ #
227
234
  def choose_version(version)
228
235
  if version =~ /(\d+)\.(\d+)\.(\d+)/
229
236
  major = $1
230
- minor = $1
237
+ minor = $2
231
238
  patch = $3
232
239
  major = major.to_i + 1 if config[:major]
233
240
  minor = minor.to_i + 1 if config[:minor]
@@ -241,6 +248,8 @@ class Chef
241
248
  version
242
249
  end
243
250
 
251
+ # Upload the cookbook to chef server
252
+ # If mode is final, freeze the cookbook
244
253
  def cookbook_upload()
245
254
  # Git meuk should not be uploaded use chefignore file instead
246
255
  # FileUtils.remove_entry("#{@github_tmp}/git/#{@cookbook_name}/.git")
@@ -254,6 +263,10 @@ class Chef
254
263
  upload.run
255
264
  end
256
265
 
266
+ # If a tag is available in github check it out
267
+ # Potentially quite dangerous as it could cause code to
268
+ # get rolled back
269
+ # @param version [String] Version
257
270
  def checkout_tag(version)
258
271
  ui.info "Checking out tag #{version}"
259
272
  cpath = get_cookbook_path(@cookbook_name)
@@ -265,6 +278,7 @@ class Chef
265
278
  end
266
279
  end
267
280
 
281
+ # Get a sorted array of version for the cookbook
268
282
  def get_cookbook_chef_versions ()
269
283
  cookbooks = rest.get_rest("/cookbooks/#{@cookbook_name}?num_version=all")
270
284
  cookbooks[@cookbook_name]['versions'].each do |v|
@@ -272,9 +286,8 @@ class Chef
272
286
  end
273
287
  end
274
288
 
275
- # ---------------------------------------------------------------------- #
276
289
  # Get the version number in the git version of the cookbook
277
- # ---------------------------------------------------------------------- #
290
+ # @param version [String] Version
278
291
  def get_cookbook_version()
279
292
  version = nil
280
293
  cpath = get_cookbook_path(@cookbook_name)
@@ -291,10 +304,17 @@ class Chef
291
304
  version
292
305
  end
293
306
 
307
+ # Determine if the current cookbook path is valid and that there
308
+ # is a cookbook of the correct name in there
309
+ # @param cookbook [String] cookbook name
310
+ # @return [String] Path to cookbook
294
311
  def get_cookbook_path(cookbook)
295
312
  return cookbook_path_valid?(cookbook, false)
296
313
  end
297
314
 
315
+ # Commit changes in git
316
+ # @param version [String] cookbook version
317
+ # @param push [Bool] true is the cookbook should also be pushed
298
318
  def do_commit(version, push)
299
319
  cpath = get_cookbook_path(@cookbook_name)
300
320
  Dir.chdir("#{cpath}")
@@ -318,6 +338,8 @@ class Chef
318
338
  end
319
339
 
320
340
 
341
+ # Set the version in metadata.rb
342
+ # @param version [String] cookbook version
321
343
  def set_cookbook_version(version)
322
344
  return unless get_cookbook_version() != version
323
345
  contents = ''
@@ -22,6 +22,15 @@ class Chef
22
22
  class Knife
23
23
 
24
24
  class GithubDiff < Knife
25
+ # Implements a diff function between your downloaded copy from git and what is in the Chef Server
26
+ #
27
+ # By default, it expects that you have already done knife github download COOKBOOK
28
+ #
29
+ # knife github diff cookbook_name [Version]
30
+ #
31
+ # You can also diff a cookbook against the github version bu using the -g option
32
+ #
33
+ # You can also optionally give a version on the command line
25
34
 
26
35
  deps do
27
36
  require 'chef/knife/github_base'
@@ -32,13 +41,15 @@ class Chef
32
41
  banner "knife github diff COOKBOOK [version] (options)"
33
42
  category "github"
34
43
 
35
- option :all,
36
- :short => "-a",
37
- :long => "--all",
38
- :description => "Diff all cookbooks from chef against github.",
39
- :boolean => true
44
+ option :github,
45
+ :short => "-g",
46
+ :long => "--github",
47
+ :description => "Diff against version in github",
48
+ :boolean => true,
49
+ :default => false
40
50
 
41
51
  def run
52
+ # The run method. The entry point into the class
42
53
 
43
54
  # validate base options from base module.
44
55
  validate_base_options
@@ -73,27 +84,46 @@ class Chef
73
84
  Chef::Log.error("Cannot find the link for the repository with the name: #{@cookbook_name}")
74
85
  exit 1
75
86
  end
76
- puts github_link
77
- get_clone(github_link, @cookbook_name)
78
- version = get_cookbook_copy(@cookbook_name, cookbook_version)
79
- do_diff(@cookbook_name, version)
87
+
88
+ if config[:github]
89
+ get_clone(github_link, @cookbook_name)
90
+ else # Copy downloaded version to #{@github_tmp}/git
91
+ cpath = cookbook_path_valid?(@cookbook_name, false)
92
+ if cpath.nil?
93
+ Chef::Log.error("Cannot find any local repository with the name: #{@cookbook_name}")
94
+ Chef::Log.error("Please use the option -g if you want to diff the github repository")
95
+ exit 1
96
+ end
97
+ tpath = "#{@github_tmp}/git"
98
+ if ! File.exists?(tpath)
99
+ FileUtils.makedirs(tpath)
100
+ end
101
+ FileUtils.cp_r cpath, tpath
102
+ end
103
+
104
+ version = get_cookbook_copy(@cookbook_name, cookbook_version)
105
+
106
+ do_diff(@cookbook_name, version)
80
107
  FileUtils.remove_entry(@github_tmp)
81
108
  end
82
109
 
83
110
  def do_diff(name, version)
84
111
  # Check to see if there is a tag matching the version
85
112
  Dir.chdir("#{@github_tmp}/git/#{name}")
86
- if `git tag`.split("\n").include?(version)
87
- ui.info("Tag version #{version} found, checking that out for diff")
88
- # Tag found so checkout that tag
89
- `git checkout -b #{version}`
90
- if !$?.exitstatus == 0
91
- ui.error("Failed to checkout branch #{version}")
92
- exit 1
93
- end
94
- else
95
- ui.info("Version #{version} of #{name} has no tag, using latest for diff")
96
- end
113
+ # Only checkout in github mode
114
+ if config[:github]
115
+ if `git tag`.split("\n").include?(version)
116
+ ui.info("Tag version #{version} found, checking that out for diff")
117
+ # Tag found so checkout that tag
118
+ `git checkout -b #{version}`
119
+ if !$?.exitstatus == 0
120
+ ui.error("Failed to checkout branch #{version}")
121
+ exit 1
122
+ end
123
+ else
124
+ ui.info("Version #{version} of #{name} has no tag, using latest for diff")
125
+ end
126
+ end
97
127
 
98
128
  FileUtils.remove_entry("#{@github_tmp}/git/#{name}/.git")
99
129
  output = `git diff --color #{@github_tmp}/git/#{name} #{@github_tmp}/cb/#{name}-#{version} 2>&1`