Class: ChefFiles

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/gaddygaddy-client/chef_files.rb

Instance Method Summary collapse

Methods included from Logging

create_logger, #logger, logger, set_log_conf, #set_log_file, #set_log_level

Constructor Details

#initialize(config, ap_host, chef_dir) ⇒ ChefFiles

Returns a new instance of ChefFiles.



21
22
23
24
25
# 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

Instance Method Details

#cookbook_path(prod_version, version) ⇒ Object

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



55
56
57
58
59
60
61
# 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'
  end
end

#create_version_file(version) ⇒ Object



34
35
36
37
38
39
40
# 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_versionObject



42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Object

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



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
# 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_versionObject

Get the version of cookbook installed at the system



29
30
31
32
# 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