Class: Batali::Source::ChefServer

Inherits:
Batali::Source show all
Includes:
Utility::Chef, Bogo::Memoization
Defined in:
lib/batali/source/chef_server.rb

Overview

Chef Server based source

Instance Attribute Summary collapse

Attributes inherited from Batali::Source

#cache_path

Instance Method Summary collapse

Methods included from Utility::Chef

included, #init_chef!

Methods inherited from Batali::Source

#==, build, #clean_asset, #diff

Constructor Details

#initialize(args = {}) ⇒ self

Extract extra info before allowing super to load data

Parameters:

  • args (Hash) (defaults to: {})


25
26
27
28
29
# File 'lib/batali/source/chef_server.rb', line 25

def initialize(args={})
  @deps = args.delete(:dependencies) || {}
  super
  init_chef!
end

Instance Attribute Details

#cacheString

Returns local cache path.

Returns:

  • (String)

    local cache path



15
16
17
# File 'lib/batali/source/chef_server.rb', line 15

def cache
  @cache
end

#dependenciesArray<Hash> (readonly)

Returns dependency strings.

Returns:

  • (Array<Hash>)

    dependency strings



13
14
15
# File 'lib/batali/source/chef_server.rb', line 13

def dependencies
  @dependencies
end

#identifierstring (readonly)

Returns unique identifier.

Returns:

  • (string)

    unique identifier



17
18
19
# File 'lib/batali/source/chef_server.rb', line 17

def identifier
  @identifier
end

Instance Method Details

#api_serviceChef::Rest

Returns:

  • (Chef::Rest)


32
33
34
35
36
# File 'lib/batali/source/chef_server.rb', line 32

def api_service
  memoize(:api_service) do
    Chef::Rest.new(endpoint)
  end
end

#assetString

Returns directory.

Returns:

  • (String)

    directory



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/batali/source/chef_server.rb', line 57

def asset
  path = File.join(cache_directory, name, version)
  begin
    FileUtils.mkdir_p(path)
    cookbook = rest.get_rest("cookbooks/#{name}/#{version}")
    manifest = cookbook.manifest
    Chef::CookbookVersion::COOKBOOK_SEGMENTS.each do |segement|
      if(manifest.key?(segment))
        manifest[segement].each do |s_file|
          new_path = File.join(path, s_file['path'].gsub('/', File::SEPARATOR))
          FileUtils.mkdir_p(File.dirname(new_path))
          api_service.sign_on_redirect = false
          t_file = api_service.get_rest(s_file['url'], true)
          FilUtils.mv(t_file.path, new_path)
        end
      end
    end
  rescue => e
    debug "Failed to fully download cookbook [#{name}<#{version}>] - #{e.class}: #{e}"
    FileUtils.rm_rf(path)
    raise
  end
  path
end

#cache_directoryString

Returns path to cache.

Returns:

  • (String)

    path to cache



49
50
51
52
53
54
# File 'lib/batali/source/chef_server.rb', line 49

def cache_directory
  memoize(:cache_directory) do
    @cache ||= File.join(cache_path, 'chef_server', endpoint)
    cache
  end
end

#unit_dependenciesArray<Array<name, constraints>>

Returns:

  • (Array<Array<name, constraints>>)


44
45
46
# File 'lib/batali/source/chef_server.rb', line 44

def unit_dependencies
  deps.to_a
end

#unit_versionString

Returns:

  • (String)


39
40
41
# File 'lib/batali/source/chef_server.rb', line 39

def unit_version
  version
end