class ChefFiles

Public Class Methods

new(config, ap_host, chef_dir) click to toggle source
# File lib/gaddygaddy-client/chef_files.rb, line 21
def initialize(config, ap_host, chef_dir)
  @config = config
  @ap_host = ap_host
  @chef_dir = chef_dir
end

Public Instance Methods

cookbook_path(prod_version, version) click to toggle source

Will return cookbook_uri depending on cookbook version -1 is not supported in production

# File lib/gaddygaddy-client/chef_files.rb, line 55
def cookbook_path(prod_version, version)
  if prod_version
    '/' + URI.encode("pkg/gg_chef_#{version}.tar.gz")
  else
    '/chef/latest_cookbook/1'
  end
end
create_version_file(version) click to toggle source
# File lib/gaddygaddy-client/chef_files.rb, line 34
def create_version_file(version)
  File.open(File.join(@chef_dir,'cookbooks_version_' + version.to_s), "w") do |f|
    f.puts "Version: #{version}"
    f.puts "Created at #{Time.now}"
    f.close
  end
end
get_cookbook_version() click to toggle source
# File lib/gaddygaddy-client/chef_files.rb, line 42
def get_cookbook_version
  path = '/chef/cookbooks_version/1'
  if @config.exist?
    path += "/#{@config.user_id_salt}/#{@config.device_id}/#{@config.token}"
  end
  response = Request.client_service_get @ap_host, path
  raise "Could not get cookbook version" unless response['cookbooks_version']
  logger.debug "Got cookbook version: #{response}"
  response['cookbooks_version']
end
get_cookbooks(file_host, cookbooks_version) click to toggle source

Will check version of installed cookbooks and update if it’s old

# File lib/gaddygaddy-client/chef_files.rb, line 66
def get_cookbooks(file_host, cookbooks_version)
  version = cookbooks_version ? cookbooks_version : get_cookbook_version.to_s
  installed_version = installed_cookbook_version.to_s
  prod_version = version.split('_')[0].to_s != '0'
  return if installed_version.size > 0 && (installed_version.to_s == version.to_s) && prod_version
  tmp_file = "/tmp/cookbooks-#{version}.tar.gz"
  port = DEFAULT_FILE_HOST_PORT
  file_host = prod_version ?  file_host : @ap_host
  # The file host should be without http or port
  file_host = file_host.split("://")[1] if file_host.index("://")
  file_host,port = file_host.split(":") if file_host.index(":")
  Net::HTTP.start(file_host, port) do |http|
    begin
      http.read_timeout = 500
      file = open(tmp_file, 'wb')
      logger.debug "Will request the cookbooks files from http://#{file_host}:#{port}#{cookbook_path(prod_version, version)}"
      result = http.request_get(cookbook_path(prod_version, version)) do |response|
        response.read_body do |segment|
          file.write(segment)
        end
      end
      logger.debug "The tar cookbook request response was #{result.inspect}"
    ensure
      file.close
    end
  end
  logger.debug "Will untar the file to #{@chef_dir} and then remove file #{tmp_file}"
  FileUtils.mkdir_p @chef_dir
  # Check that the tar file is valid first
  cmd = "tar --test-label -zvf #{tmp_file} && rm -rf #{@chef_dir}/* && tar -C #{@chef_dir} -zxvf #{tmp_file}"
  run_cmd cmd
  create_version_file version
  cmd_remove = "rm #{tmp_file}"
  run_cmd cmd_remove
end
installed_cookbook_version() click to toggle source

Get the version of cookbook installed at the system

# File lib/gaddygaddy-client/chef_files.rb, line 29
def installed_cookbook_version
  installed_versions = Dir.glob(File.join(@chef_dir, 'cookbooks_version_[0-9]*'))
  installed_versions.size > 0 ? installed_versions.map{|file| File.basename(file.split('_')[2])}.max : 0
end