|
| 1 | +require 'erb' |
| 2 | +require 'ostruct' |
| 3 | +require 'fileutils' |
| 4 | +require 'json' |
| 5 | +require 'bundler' |
| 6 | + |
| 7 | +class Benchmarker |
| 8 | + include FileUtils |
| 9 | + |
| 10 | + def initialize(target, size='medium') |
| 11 | + @target = target |
| 12 | + @size = 'large' |
| 13 | + end |
| 14 | + |
| 15 | + def check_submodule |
| 16 | + submodule = File.join('benchmarks', 'full_catalog', 'puppetlabs-puppetserver_perf_control') |
| 17 | + unless File.exist?(File.join(submodule, 'Puppetfile')) |
| 18 | + raise RuntimeError, 'The perf control repo is not readable. Make sure to initialize submodules by running: git submodule update --init --recursive' |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + def setup |
| 23 | + require 'puppet' |
| 24 | + config = File.join(@target, 'puppet.conf') |
| 25 | + environment = File.join(@target, 'environments', 'perf_control') |
| 26 | + Puppet.initialize_settings(['--config', config]) |
| 27 | + FileUtils.cd(@target) do |
| 28 | + Bundler::with_clean_env do |
| 29 | + system("bundle install") |
| 30 | + system("bundle exec r10k puppetfile install --puppetfile #{File.join(environment, 'Puppetfile')} --moduledir #{File.join(environment, 'modules')} --config r10k.yaml") |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + # Loading the base system affects the first run a lot so burn one run here |
| 35 | + run |
| 36 | + end |
| 37 | + |
| 38 | + def run(args=nil) |
| 39 | + # Probably could pare down these facts a lot |
| 40 | + facts = Puppet::Node::Facts.new("testing", { |
| 41 | + 'pe_concat_basedir' => '/tmp/file', |
| 42 | + 'platform_symlink_writable' => true, |
| 43 | + 'pe_build' => '2016.4.4', |
| 44 | + 'identity' => { |
| 45 | + 'privileged' => true, |
| 46 | + }, |
| 47 | + 'mountpoints' => { |
| 48 | + '/tmp' => { |
| 49 | + 'options' => ['rw'], |
| 50 | + } |
| 51 | + }, |
| 52 | + 'puppet_files_dir_present' => true, |
| 53 | + 'puppetversion' => '4.10.4', |
| 54 | + 'aio_agent_version' => '1.10.4', |
| 55 | + 'aio_agent_build' => '1.10.4', |
| 56 | + 'memory' => { |
| 57 | + 'system' => { |
| 58 | + 'total_bytes' => 8589934592, |
| 59 | + } |
| 60 | + }, |
| 61 | + 'memorysize' => '8 GiB', |
| 62 | + 'osfamily' => 'RedHat', |
| 63 | + 'operatingsystem' => 'CentOS', |
| 64 | + 'operatingsystemrelease' => '6.8', |
| 65 | + 'processorcount' => '2', |
| 66 | + 'fake_domain' => 'pgtomcat.mycompany.org', |
| 67 | + 'function' => 'app', |
| 68 | + 'group' => 'pgtomcat', |
| 69 | + 'stage' => 'prod', |
| 70 | + 'whereami' => 'portland', |
| 71 | + 'hostname' => 'pgtomcat', |
| 72 | + 'fqdn' => 'pgtomcat.mycompany.org', |
| 73 | + 'lsbdistcodename' => 'n/a', |
| 74 | + 'processor0' => 'AMD Ryzen 7 1700 Eight-Core Processor', |
| 75 | + }) |
| 76 | + |
| 77 | + env = Puppet.lookup(:environments).get('perf_control') |
| 78 | + node = Puppet::Node.indirection.find("testing", :environment => env, :facts => facts) |
| 79 | + |
| 80 | + Puppet.push_context({:current_environment => env}, 'current env for benchmark') |
| 81 | + Puppet::Resource::Catalog.indirection.find("testing", :use_node => node) |
| 82 | + Puppet.pop_context |
| 83 | + Puppet.lookup(:environments).clear('perf_control') |
| 84 | + end |
| 85 | + |
| 86 | + def generate |
| 87 | + check_submodule |
| 88 | + templates = File.join('benchmarks', 'full_catalog') |
| 89 | + source = File.join(templates, "puppetlabs-puppetserver_perf_control") |
| 90 | + environment = File.join(@target, 'environments', 'perf_control') |
| 91 | + mkdir_p(File.join(@target, 'environments')) |
| 92 | + FileUtils.cp_r(source, environment) |
| 93 | + |
| 94 | + render(File.join(templates, 'puppet.conf.erb'), |
| 95 | + File.join(@target, 'puppet.conf'), |
| 96 | + :codedir => File.join(@target, 'environments'), |
| 97 | + :target => @target) |
| 98 | + |
| 99 | + render(File.join(templates, 'site.pp.erb'), |
| 100 | + File.join(environment, 'manifests', 'site.pp'), |
| 101 | + :size => @size) |
| 102 | + |
| 103 | + render(File.join(templates, 'hiera.yaml.erb'), |
| 104 | + File.join(@target, 'hiera.yaml'), |
| 105 | + :datadir => File.join(environment, 'hieradata')) |
| 106 | + |
| 107 | + FileUtils.cp(File.join(templates, 'Gemfile'), @target) |
| 108 | + FileUtils.cp(File.join(templates, 'r10k.yaml'), @target) |
| 109 | + end |
| 110 | + |
| 111 | + def render(erb_file, output_file, bindings) |
| 112 | + site = ERB.new(File.read(erb_file)) |
| 113 | + File.open(output_file, 'w') do |fh| |
| 114 | + fh.write(site.result(OpenStruct.new(bindings).instance_eval { binding })) |
| 115 | + end |
| 116 | + end |
| 117 | +end |
0 commit comments