Class: PuppetFixtures::Fixtures

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_fixtures.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_dir: Dir.pwd, max_thread_limit: 10) ⇒ Fixtures



23
24
25
26
# File 'lib/puppet_fixtures.rb', line 23

def initialize(source_dir: Dir.pwd, max_thread_limit: 10)
  @source_dir = source_dir
  @max_thread_limit = max_thread_limit
end

Instance Attribute Details

#source_dirObject (readonly)

Returns the value of attribute source_dir.



21
22
23
# File 'lib/puppet_fixtures.rb', line 21

def source_dir
  @source_dir
end

Instance Method Details

#cleanObject



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/puppet_fixtures.rb', line 242

def clean
  repositories.each_value do |opts|
    target = opts[:target]
    logger.debug("Removing repository #{target}")
    FileUtils.rm_rf(target)
  end

  forge_modules.each_value do |opts|
    target = opts[:target]
    logger.debug("Removing forge module #{target}")
    FileUtils.rm_rf(target)
  end

  symlinks.each_value do |symlink|
    logger.debug("Removing symlink #{symlink}")
    symlink.remove
  end
end

#downloadObject



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
# File 'lib/puppet_fixtures.rb', line 185

def download
  logger.debug("Downloading to #{module_target_dir}")
  FileUtils.mkdir_p(module_target_dir)

  if symlinks.empty?
    logger.debug('No symlinks to create')
  else
    symlinks.each_value do |symlink|
      logger.info("Creating symlink #{symlink}")
      symlink.create
    end
  end

  queue = Queue.new

  repositories.each do |remote, opts|
    queue << [:repository, remote, opts]
  end
  forge_modules.each do |remote, opts|
    queue << [:forge, remote, opts]
  end

  if queue.empty?
    logger.debug('Nothing to download')
    return
  end

  instance = self

  thread_count = [@max_thread_limit, queue.size].min
  logger.debug("Download queue size: #{queue.size}; using #{thread_count} threads")

  threads = Array.new(thread_count) do |_i|
    Thread.new do
      loop do
        begin
          type, remote, opts = queue.pop(true)
        rescue ThreadError
          break # Queue is empty
        end
        case type
        when :repository
          instance.download_repository(remote, **opts)
        when :forge
          instance.download_module(remote, **opts)
        end
      end
    end
  end

  begin
    threads.map(&:join)
  rescue Interrupt
    # pass
  end
end

#download_module(remote, ref:, target:, flags:) ⇒ Boolean



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
# File 'lib/puppet_fixtures.rb', line 151

def download_module(remote, ref:, target:, flags:)
  if File.directory?(target)
    if !ref || ref.empty?
      logger.debug("Module #{target} already up to date")
      return false
    end

    begin
      version = PuppetFixtures::.new(File.join(target, 'metadata.json')).version
    rescue ArgumentError
      logger.warn "Unable to detect module version for #{target}; updating"
    else
      if ref == version
        logger.debug("Module #{target} already up to date (#{ref})")
        return false
      else
        logger.debug("Module #{target} version #{version} != #{ref}; updating")
      end
    end
  end

  command = %w[puppet module install]
  command << '--version' << ref if ref
  command += flags if flags
  command += ['--ignore-dependencies', '--force', '--target-dir', module_target_dir, remote]

  Dir.mktmpdir do |working_dir|
    command += ['--module_working_dir', working_dir]
    raise "Failed to install module #{remote} to #{module_target_dir}" unless run_command(command)
  end

  true
end

#download_repository(remote, target:, scm:, subdir:, ref:, branch:, flags:) ⇒ Boolean



135
136
137
138
# File 'lib/puppet_fixtures.rb', line 135

def download_repository(remote, target:, scm:, subdir:, ref:, branch:, flags:)
  repository = PuppetFixtures::Repository.factory(scm: scm, remote: remote, target: target, branch: branch, ref: ref)
  repository.download(flags, subdir)
end

#fixture_pathObject



261
262
263
264
265
266
267
268
269
# File 'lib/puppet_fixtures.rb', line 261

def fixture_path
  if ENV['FIXTURES_YML']
    ENV['FIXTURES_YML']
  elsif File.exist?('.fixtures.yml')
    '.fixtures.yml'
  elsif File.exist?('.fixtures.yaml')
    '.fixtures.yaml'
  end
end

#fixturesObject



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
# File 'lib/puppet_fixtures.rb', line 66

def fixtures
  @fixtures ||= begin
    categories = read_fixtures_file['fixtures']

    categories['symlinks'] ||= begin
       = PuppetFixtures::.new(File.join(source_dir, 'metadata.json'))
      { .name.split('-').last => source_dir }
    rescue ArgumentError
      {}
    end
    categories['forge_modules'] ||= {}
    categories['repositories'] ||= {}

    defaults = { 'target' => module_target_dir }

    %w[symlinks forge_modules repositories].to_h do |category|
      # load defaults from the `.fixtures.yml` `defaults` section
      # for the requested category and merge them into my defaults
      category_defaults = if (category_defaults = categories.dig('defaults', category))
                            defaults.merge(category_defaults)
                          else
                            defaults
                          end

      entries = categories[category].to_h do |fixture, opts|
        # convert a simple string fixture to a hash, by
        # using the string fixture as the `repo` option of the hash.
        opts = { 'repo' => opts } if opts.instance_of?(String)
        # there should be a warning or something if it's not a hash...
        next unless opts.instance_of?(Hash)

        # merge our options into the defaults to get the
        # final option list
        opts = category_defaults.merge(opts)

        next unless include_repo?(opts['puppet_version'])

        entry = validate_fixture_hash!(
          target: File.join(opts['target'], fixture),
          ref: opts['ref'] || opts['tag'],
          branch: opts['branch'],
          scm: opts.fetch('scm', 'git'),
          flags: opts['flags'],
          subdir: opts['subdir'],
        )

        case category
        when 'forge_modules'
          entry.delete(:scm)
          entry.delete(:branch)
          entry.delete(:subdir)
        when 'symlinks'
          entry = PuppetFixtures::Symlink.new(link: entry[:target], target: opts['repo'])
        end

        [opts['repo'], entry]
      end

      [category, entries]
    end
  end
end

#forge_modulesHash

Returns A hash of all the fixture forge modules.

Examples:

{
  "puppetlabs-stdlib"=>{
    "target"=>"spec/fixtures/modules/stdlib",
    "ref"=>nil,
    "branch"=>nil,
    "scm"=>nil,
    "flags"=>"--module_repository=https://myforge.example.com/",
    "subdir"=>nil,
  }
}


56
57
58
# File 'lib/puppet_fixtures.rb', line 56

def forge_modules
  @forge_modules ||= fixtures['forge_modules']
end

#module_target_dirString



142
143
144
145
# File 'lib/puppet_fixtures.rb', line 142

def module_target_dir
  # TODO: relative to source_dir?
  @module_target_dir ||= File.expand_path(File.join('spec', 'fixtures', 'modules'))
end

#repositoriesHash

Returns A hash of all the fixture repositories.

Examples:

{
  "puppetlabs-stdlib"=>{
    "target"=>"https://gitlab.com/puppetlabs/puppet-stdlib.git",
    "ref"=>nil,
    "branch"=>"main",
    "scm"=>nil,
  }
}


39
40
41
# File 'lib/puppet_fixtures.rb', line 39

def repositories
  @repositories ||= fixtures['repositories']
end


62
63
64
# File 'lib/puppet_fixtures.rb', line 62

def symlinks
  @symlinks ||= fixtures['symlinks']
end