Module: Chef::Knife::GithubBase

Defined in:
lib/chef/knife/github_base.rb

Class Method Summary (collapse)

Class Method Details

+ (Object) included(includer)



26
27
28
29
30
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
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
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
193
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/chef/knife/github_base.rb', line 26

def self.included(includer)
  includer.class_eval do

    deps do
      require 'chef/mixin/shell_out'
      require 'mixlib/versioning'
    end

    option :github_url,
           :long => "--github_url URL",
           :description => "URL of the github enterprise appliance"

    option :github_organizations,
           :long => "--github_org ORG:ORG",
           :description => "Lookup chef cookbooks in this colon-separated list of organizations",
           :proc => lambda { |o| o.split(":") }

    option :github_link,
           :long => "--github_link TYPE",
           :description => "Link type: ssh, http, git (dafault: ssh)"

    option :github_api_version,
           :long => "--github_api_ver VERSION",
           :description => "Version number of the API (default: v3)"

    option :github_ssl_verify_mode,
           :long => "--github_ssl_verify_mode",
           :description => "SSL verify mode: verify_peer, verify_none (default: verify_peer)",
           :boolean => true

    option :github_tmp,
           :long => "--github_tmp PATH",
           :description => "A path where temporary files for the diff function will be made default: /tmp/gitdiff)"

    option :github_no_update,
           :long => "--github_no_update",
           :description => "Turn github update checking off",
           :boolean => true
 
    def validate_base_options
      unless locate_config_value('github_url')
        ui.error "Github URL not specified"
        exit 1
      end
      unless locate_config_value('github_organizations')
        ui.error "Github organization(s) not specified"
        exit 1
      end
      unless locate_config_value('github_no_update')
        check_gem_version
      end

      @github_url             = locate_config_value("github_url")
      @github_organizations   = locate_config_value("github_organizations")
      @github_link            = locate_config_value("github_link") || 'ssh'
      @github_api_version     = locate_config_value("github_api_version") || 'v3'
      @github_ssl_verify_mode = locate_config_value("github_ssl_verify_mode") || 'verify_peer'
      @github_tmp             = locate_config_value("github_tmp") || '/var/tmp/gitdiff'
      @github_tmp             = "#{@github_tmp}#{Process.pid}"
    end

    def check_gem_version
      url  = 'http://rubygems.org/api/v1/gems/knife-github.json'
      result = `curl -L -s #{url}`
      begin
        json = JSON.parse(result)
        webversion = Mixlib::Versioning.parse(json['version'])
        thisversion = Mixlib::Versioning.parse(::Knife::Github::VERSION)
        if webversion > thisversion
          ui.info "INFO: New version (#{webversion.to_s}) of knife-github is available!"
          ui.info "INFO: Turn off this message with --github_no_update or add knife[:github_no_update] = true to your configuration"
        end 
        Chef::Log.debug("gem_local_version    : " + thisversion.to_s)
        Chef::Log.debug("gem_repo_version     : " + webversion.to_s)
        Chef::Log.debug("gem_downloads        : " + json['version_downloads'].to_s)
        Chef::Log.debug("gem_total_downloads  : " + json['downloads'].to_s)
 
      rescue
        ui.info "INFO: Cannot verify gem version information from rubygems.org"
        ui.info "INFO: Turn off this message with --github_no_update or add knife[:github_no_update] = true to your configuration"
      end
    end

    def display_debug_info
      Chef::Log.debug("github_url           : " + @github_url.to_s)
      Chef::Log.debug("github_org           : " + @github_organizations.to_s)
      Chef::Log.debug("github_api           : " + @github_api_version.to_s)
      Chef::Log.debug("github_link          : " + @github_link.to_s)
      Chef::Log.debug("github_ssl_mode      : " + @github_ssl_verify_mode.to_s)
    end

    def locate_config_value(key)
      key = key.to_sym
      config[key] || Chef::Config[:knife][key]
    end

    def  get_repo_clone_link
      link = locate_config_value('github_link')
      repo_link = case link
        when 'ssh' then 'ssh_url'
        when 'http' then 'clone_url'
        when 'https' then 'clone_url'
        when 'svn' then 'svn_url'
        when 'html' then 'html_url'
        when 'git' then 'git_url'
        else 'ssh_url'
      end
      return repo_link
    end

    def get_all_repos(orgs)
      # Parse every org and merge all into one hash
      repos = {}
      orgs.each do |org|
        get_repos(org).each { |repo| name = repo['name'] ; repos["#{name}"] = repo } 
      end
      repos
    end

    def get_repos(org)
      dns_name  = get_dns_name(@github_url)
      file_cache = "#{ENV['HOME']}/.chef/.#{dns_name.downcase}_#{org.downcase}" 

      if File.exists?(file_cache + ".json")
        json =  JSON.parse(File.read(file_cache + ".json"))
        json_updated = Time.parse(json['updated_at'])
        Chef::Log.info("#{org} - cache created at : " + json_updated.to_s)
        repo_updated = get_org_updated_time(org)
        Chef::Log.info("#{org} - repos updated at : " + repo_updated.to_s)
  
 unless json_updated >= repo_updated 
          # update cache file
          create_cache_file(file_cache + ".cache", org)
          create_cache_json(file_cache + ".json", org)
        end
      else
        create_cache_file(file_cache + ".cache", org)
        create_cache_json(file_cache + ".json", org)
      end
      
      # use cache files
      JSON.parse(File.read(file_cache + ".cache"))
    end

    def create_cache_json(file, org)
      Chef::Log.debug("Updating the cache file: #{file}")
      url  = @github_url + "/api/" + @github_api_version + "/orgs/" + org
      params = {'response' => 'json'} 
      result = send_request(url, params)
      File.open(file, 'w') { |f| f.write(JSON.pretty_generate(result)) }
    end

    def create_cache_file(file, org)
      Chef::Log.debug("Updating the cache file: #{file}")
      result = get_repos_github(org)
      File.open(file, 'w') { |f| f.write(JSON.pretty_generate(result)) }
    end
   
    def get_org_updated_time(org)
      url  = @github_url + "/api/" + @github_api_version + "/orgs/" + org
      params = {'response' => 'json'}
      result = send_request(url, params)
      Time.parse(result['updated_at'])
    end

    def get_repos_github(org)
      # Get all repo's for the org from github
      arr  = []
      page = 1
      url  = @github_url + "/api/" + @github_api_version + "/orgs/" + org + "/repos" 
      while true
        params = {'response' => 'json', 'page' => page }
        result = send_request(url, params)
        break if result.nil? || result.count < 1
        result.each { |key|
          if key['tags_url']
            tags = get_tags(key)
            key['tags'] = tags unless tags.nil? || tags.empty?
            key['latest_tag'] = get_latest_tag(tags)
            arr << key
          else 
            arr << key 
          end
        }
        page = page + 1
      end
      arr
    end

    def get_tags(repo)
      params = {'response' => 'json'}
      tags = send_request(repo['tags_url'], params)
      tags
    end

    def get_latest_tag(tags)
      return "" if tags.nil? || tags.empty?
      tags_arr =[]
      tags.each do |tag|
        tags_arr.push(Mixlib::Versioning.parse(tag['name'])) if tag['name'] =~ /^(\d*)\.(\d*)\.(\d*)$/
      end
      return "" if tags_arr.nil? || tags_arr.empty?
      return tags_arr.sort.last.to_s
    end

    def get_dns_name(url)
      url = url.downcase.gsub("http://","") if url.downcase.start_with?("http://")
      url = url.downcase.gsub("https://","") if url.downcase.start_with?("https://")
      url
    end

    def send_request(url, params = {})
      unless params.empty?
        params_arr = []
        params.sort.each { |elem|
          params_arr << elem[0].to_s + '=' + CGI.escape(elem[1].to_s).gsub('+', '%20').gsub(' ','%20')
        }
        data = params_arr.join('&')
        url = "#{url}?#{data}" 
      end

      if @github_ssl_verify_mode == "verify_none"
        config[:ssl_verify_mode] = :verify_none
      elsif @github_ssl_verify_mode == "verify_peer"
        config[:ssl_verify_mode] = :verify_peer
      end

      Chef::Log.debug("URL: " + url.to_s)

      uri = URI.parse(url)
      req_body = Net::HTTP::Get.new(uri.request_uri)
      request = Chef::REST::RESTRequest.new("GET", uri, req_body, headers={})
 
      response = request.call
    
      unless response.is_a?(Net::HTTPOK) then
        puts "Error #{response.code}: #{response.message}"
        puts JSON.pretty_generate(JSON.parse(response.body))
        puts "URL: #{url}"
        exit 1
      end

      begin
        json = JSON.parse(response.body)
      rescue
        ui.warn "The result on the RESTRequest is not in json format"
        ui.warn "Output: " + response.body
        exit 1
      end
      return json
    end

    def get_clone(url, cookbook)
        if ! File.directory? @github_tmp
           Dir.mkdir("#{@github_tmp}")
        end
        Dir.mkdir("#{@github_tmp}/git")
        ui.info("Getting #{@cookbook_name} from #{url}")
        output = `git clone #{url} #{@github_tmp}/git/#{cookbook} 2>&1`
        if $?.exitstatus != 0
           Chef::Log.error("Could not clone the repository for: #{cookbook}")
           FileUtils.remove_entry(@github_tmp)
           exit 1
        end
        return true
    end

    def add_tag(version)
        cpath = cookbook_path_valid?(@cookbook_name, false)
        Dir.chdir(cpath)
        Chef::Log.debug "Adding tag"
        output = `git tag -a "#{version}" -m "Added tag #{version}" 2>&1`
        if $?.exitstatus != 0
           Chef::Log.error("Could not add tag for: #{@cookbook_name}")
           FileUtils.remove_entry(@github_tmp)
           exit 1
        end
    end

    def cookbook_path_valid?(cookbook_name, check_exists)
      cookbook_path = config[:cookbook_path] || Chef::Config[:cookbook_path]
      if cookbook_path.nil? || cookbook_path.empty?
        Chef::Log.error("Please specify a cookbook path")
        exit 1
      end

      unless File.exists?(cookbook_path.first) && File.directory?(cookbook_path.first)
        Chef::Log.error("Cannot find the directory: #{cookbook_path.first}")
        exit 1
      end

      cookbook_path = File.join(cookbook_path.first,cookbook_name)
      if check_exists
          if File.exists?(cookbook_path)
            ui.info("Processing [S] #{cookbook_name}")
            Chef::Log.info("Path to #{cookbook_path} already exists, skipping.")
            return nil
          end
      else
          if ! File.exists?(cookbook_path)
            return nil
          end
      end
      return cookbook_path
    end

  end
end