Class: Dapp::Dimg::Builder::Chef::Cookbook

Inherits:
Object
  • Object
show all
Defined in:
lib/dapp/dimg/builder/chef/cookbook.rb

Overview

Локальный cookbook сборщик с правилами сборки dimg образов для chef сборщика.

Constant Summary collapse

CACHE_VERSION =
1
CHECKSUM_PATTERNS =
%w(
  attributes/**/*
  recipes/**/*
  files/**/*
  templates/**/*
).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(builder, path:, berksfile:, metadata:, enabled_recipes:, enabled_modules:) ⇒ Cookbook

rubocop:disable Metrics/ParameterLists



25
26
27
28
29
30
31
32
# File 'lib/dapp/dimg/builder/chef/cookbook.rb', line 25

def initialize(builder, path:, berksfile:, metadata:, enabled_recipes:, enabled_modules:)
  @builder = builder
  @path = Pathname.new(path)
  @berksfile = berksfile
  @metadata = 
  @enabled_recipes = enabled_recipes
  @enabled_modules = enabled_modules
end

Instance Attribute Details

#berksfileObject (readonly)

Returns the value of attribute berksfile.



19
20
21
# File 'lib/dapp/dimg/builder/chef/cookbook.rb', line 19

def berksfile
  @berksfile
end

#builderObject (readonly)

Returns the value of attribute builder.



15
16
17
# File 'lib/dapp/dimg/builder/chef/cookbook.rb', line 15

def builder
  @builder
end

#enabled_modulesObject (readonly)

Returns the value of attribute enabled_modules.



22
23
24
# File 'lib/dapp/dimg/builder/chef/cookbook.rb', line 22

def enabled_modules
  @enabled_modules
end

#enabled_recipesObject (readonly)

Returns the value of attribute enabled_recipes.



21
22
23
# File 'lib/dapp/dimg/builder/chef/cookbook.rb', line 21

def enabled_recipes
  @enabled_recipes
end

#metadataObject (readonly)

Returns the value of attribute metadata.



20
21
22
# File 'lib/dapp/dimg/builder/chef/cookbook.rb', line 20

def 
  @metadata
end

#pathObject (readonly)

Returns the value of attribute path.



17
18
19
# File 'lib/dapp/dimg/builder/chef/cookbook.rb', line 17

def path
  @path
end

#vendor_pathObject (readonly)

Returns the value of attribute vendor_path.



18
19
20
# File 'lib/dapp/dimg/builder/chef/cookbook.rb', line 18

def vendor_path
  @vendor_path
end

Instance Method Details

#checksumObject

TODO: Сookbook’и зависимости на локальной файловой системе TODO: должны обрабатываться рекурсивно. Чтобы контрольная сумма TODO: учитывала все зависимости всех зависимых локальных cookbook’ов.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/dapp/dimg/builder/chef/cookbook.rb', line 50

def checksum
  @checksum ||= begin
    all_local_paths = local_paths
                      .product(CHECKSUM_PATTERNS)
                      .map {|cb, dir| Dir[cb.join(dir)]}
                      .flatten
                      .map(&Pathname.method(:new))

    builder.dimg.hashsum [
      CACHE_VERSION,
      builder.dimg.paths_content_hashsum(all_local_paths),
      *all_local_paths.map {|p| p.relative_path_from(path).to_s}.sort,
      berksfile.dump,
      .dump
    ].compact
  end
end

#local_pathsObject



39
40
41
42
43
44
45
# File 'lib/dapp/dimg/builder/chef/cookbook.rb', line 39

def local_paths
  @local_paths ||= [].tap do |paths|
    paths << path
    paths.push(*berksfile.local_cookbooks.values.map {|cookbook| cookbook[:path]})
    paths.uniq!
  end
end

#nameObject

rubocop:enable Metrics/ParameterLists



35
36
37
# File 'lib/dapp/dimg/builder/chef/cookbook.rb', line 35

def name
  .name
end

#stage_checksum(stage) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'lib/dapp/dimg/builder/chef/cookbook.rb', line 144

def stage_checksum(stage)
  paths = Dir[stage_cookbooks_path(stage).join('**/*')].map(&Pathname.method(:new))

  builder.dimg.hashsum [
    builder.dimg.paths_content_hashsum(paths),
    *paths.map {|p| p.relative_path_from(stage_cookbooks_path(stage)).to_s}.sort,
    stage == :before_install ? builder.chefdk_image : nil
  ].compact
end

#stage_cookbooks_path(stage) ⇒ Object

rubocop:enable Metrics/AbcSize



127
128
129
130
131
132
# File 'lib/dapp/dimg/builder/chef/cookbook.rb', line 127

def stage_cookbooks_path(stage)
  _stage_cookbooks_path(stage).tap do |_cookbooks_path|
    @stage_cookbooks_installed ||= {}
    @stage_cookbooks_installed[stage] ||= true.tap {install_stage_cookbooks(stage)}
  end
end

#stage_enabled_modules(stage) ⇒ Object



134
135
136
137
# File 'lib/dapp/dimg/builder/chef/cookbook.rb', line 134

def stage_enabled_modules(stage)
  @stage_enabled_modules ||= {}
  @stage_enabled_modules[stage] ||= enabled_modules.select {|cookbook| stage_entry_exist?(stage, cookbook, stage)}
end

#stage_enabled_recipes(stage) ⇒ Object



139
140
141
142
# File 'lib/dapp/dimg/builder/chef/cookbook.rb', line 139

def stage_enabled_recipes(stage)
  @stage_enabled_recipes ||= {}
  @stage_enabled_recipes[stage] ||= enabled_recipes.select {|recipe| stage_entry_exist?(stage, name, recipe)}
end

#vendor!Object

rubocop:disable Metrics/AbcSize



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
# File 'lib/dapp/dimg/builder/chef/cookbook.rb', line 76

def vendor!
  volumes_from = [builder.dimg.dapp.base_container, builder.chefdk_container]

  builder.dimg.dapp.log_secondary_process(builder.dimg.dapp.t(code: _vendor_process_name)) do
    volumes_from = [builder.dimg.dapp.base_container, builder.chefdk_container]

    tmp_berksfile_path = builder.dimg.tmp_path.join("Berksfile.#{SecureRandom.uuid}")
    File.open(tmp_berksfile_path, 'w') {|file| file.write berksfile.dump}

     = builder.dimg.tmp_path.join("metadata.rb.#{SecureRandom.uuid}")
    File.open(, 'w') {|file| file.write .dump}

    vendor_commands = [
      "#{builder.dimg.dapp.mkdir_bin} -p ~/.ssh",
      'echo "Host *" >> ~/.ssh/config',
      'echo "    StrictHostKeyChecking no" >> ~/.ssh/config',
      *local_paths.map {|path| "#{builder.dimg.dapp.rsync_bin} --archive --relative #{path} /tmp/local_cookbooks"},
      "cd /tmp/local_cookbooks/#{path}",
      "cp #{tmp_berksfile_path} Berksfile",
      "cp #{} metadata.rb",
      '/.dapp/deps/chefdk/bin/berks vendor /tmp/cookbooks',
      ["#{builder.dimg.dapp.find_bin} /tmp/cookbooks -type d -exec #{builder.dimg.dapp.bash_bin} -ec '",
       "#{builder.dimg.dapp.install_bin} -o #{Process.uid} -g #{Process.gid} ",
       "--mode $(#{builder.dimg.dapp.stat_bin} -c %a {}) -d ",
       "#{_vendor_path}/$(echo {} | #{builder.dimg.dapp.sed_bin} -e \"s/^\\/tmp\\/cookbooks//\")' \\;"].join,
      ["#{builder.dimg.dapp.find_bin} /tmp/cookbooks -type f -exec #{builder.dimg.dapp.bash_bin} -ec '",
       "#{builder.dimg.dapp.install_bin} -o #{Process.uid} -g #{Process.gid} ",
       "--mode $(#{builder.dimg.dapp.stat_bin} -c %a {}) {} ",
       "#{_vendor_path}/$(echo {} | #{builder.dimg.dapp.sed_bin} -e \"s/\\/tmp\\/cookbooks//\")' \\;"].join,
      ["#{builder.dimg.dapp.install_bin} -o #{Process.uid} -g #{Process.gid} ",
       "--mode 0644 <(#{builder.dimg.dapp.date_bin} +%s.%N) #{_vendor_path.join('.created_at')}"].join
    ]

    builder.dimg.dapp.shellout!(
      [ "#{builder.dimg.dapp.host_docker_bin} run --rm",
        volumes_from.map {|container| "--volumes-from #{container}"}.join(' '),
        *local_paths.map {|path| "--volume #{path}:#{path}"},
        "--volume #{builder.dimg.tmp_path}:#{builder.dimg.tmp_path}",
        ("--volume #{builder.dimg.dapp.ssh_auth_sock}:/tmp/dapp-ssh-agent" if builder.dimg.dapp.ssh_auth_sock),
        "--volume #{_vendor_path.tap(&:mkpath)}:#{_vendor_path}",
        ('--env SSH_AUTH_SOCK=/tmp/dapp-ssh-agent' if builder.dimg.dapp.ssh_auth_sock),
        ''.tap do |cmd|
          cmd << "dappdeps/berksdeps:0.1.0 #{builder.dimg.dapp.bash_bin}"
          cmd << " -ec '#{builder.dimg.dapp.shellout_pack(vendor_commands.join(' && '))}'"
        end ].compact.join(' '),
      verbose: true
    )
  end
end