Top Level Namespace

Includes:
FileUtils

Defined Under Namespace

Modules: Facter, Kernel, MediaWikiVagrant, SharedData Classes: AlsoString, Apr1Md5, BasicTTLCache, Class, DNSCacheEntry, DNSCached, Hiera, Object

Constant Summary

UNSUPPORTED_PLATFORMS =
[]

Instance Method Summary collapse

Instance Method Details

#build_isoObject



147
148
149
150
151
152
153
154
155
# File 'support/packager/package.rb', line 147

def build_iso
  puts 'Creating iso image to distribute...'
  # -r: Rock Ridge with recommended values for permissions, etc.
  if system('which genisoimage >/dev/null 2>&1')
    system('genisoimage', '-quiet', '-r', '-o', $iso_file.to_s, $contents_dir.to_s)
  else
    puts '"genisoimage" not found. Iso image not created.'
  end
end

#commonObject



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
# File 'support/packager/package.rb', line 81

def common
  old_cwd = Dir.pwd

  mkdir_p($contents_dir)

  download_file($contents_dir, $url_config['Base Box'])

  mediawiki_vagrant_dir = $packager_dir.parent.parent
  core_dir = mediawiki_vagrant_dir + 'mediawiki'

  Dir.chdir(mediawiki_vagrant_dir)
  puts 'Creating git bundle for mediawiki-vagrant...'
  system('git', 'bundle', 'create', ($contents_dir + 'mediawiki_vagrant.bundle').to_s, 'master')

  Dir.chdir(core_dir)
  puts 'Creating git bundle for mediawiki-core...'
  system('git', 'bundle', 'create', ($contents_dir + 'mediawiki_core.bundle').to_s, 'master')

  Dir.chdir(mediawiki_vagrant_dir)
  puts 'Copying cache...'
  system('cp', '-R', 'cache', $contents_dir.to_s)
  # Ensure that a 'partial' directory is present for the apt cache
  mkdir_p(($contents_dir + 'cache' + 'apt' + 'partial').to_s)
  # An apt lock file may not exist but make sure that it it removed from the
  # copy just in case as it will cause annoying problems otherwise.
  rm_f(($contents_dir + 'cache' + 'apt' + 'lock').to_s)

  Dir.chdir(old_cwd)

  download_file($contents_dir, $url_config['GPLv2'])
  download_file($contents_dir, $url_config['Vagrant License'])

  template = $packager_dir + 'template'
  puts 'Copying template files...'
  cp(template + 'README.txt', $contents_dir)
  cp(template + 'LICENSE', $contents_dir)
end

#config_to_hash(conf) ⇒ Object

Function: merge_config(string|hash main_conf, string|hash service_conf)

Merges the service-specific service_conf into main_conf. Both arguments can be either hashes or YAML-formatted strings. It returns the merged configuration hash.



8
9
10
11
# File 'puppet/modules/service/lib/puppet/parser/functions/merge_config.rb', line 8

def config_to_hash(conf)
  return YAML.load(conf) unless conf.is_a?(Hash)
  conf
end

#count_pending_tests_in(group) ⇒ Object



57
58
59
# File 'puppet/modules/stdlib/spec/acceptance/build_csv.rb', line 57

def count_pending_tests_in(group)
  count_test_types_in('pending_tests',group)
end

#count_test_types_in(type, group) ⇒ Object



46
47
48
49
50
51
52
53
# File 'puppet/modules/stdlib/spec/acceptance/build_csv.rb', line 46

def count_test_types_in(type,group)
  return 0 if group.nil?
  group.inject(0) do |m,(k,v)|
    m += v.length if k == type
    m += count_tests_in(v) if v.is_a?(Hash)
    m
  end
end

#count_tests_in(group) ⇒ Object



54
55
56
# File 'puppet/modules/stdlib/spec/acceptance/build_csv.rb', line 54

def count_tests_in(group)
  count_test_types_in('tests',group)
end

#dedent_string(string) ⇒ Object



37
38
39
40
41
42
43
# File 'puppet/modules/wmflib/lib/puppet/parser/functions/ordered_yaml.rb', line 37

def dedent_string(string)
  lines = string.split("\n")
  return string if lines.empty?
  min_indent = lines.map { |line| line.start_with?(" ") ? line.match(/^ +/).offset(0)[1] : 0 }.min
  return string if min_indent.zero?
  lines.map { |line| line.gsub(/^ {#{min_indent}}/, "") }.join("\n")
end

#download_file(target_dir_pathname, url_info) ⇒ Object

Checks the SHA256. If it matches, doesn't need to be downloaded again



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'support/packager/package.rb', line 25

def download_file(target_dir_pathname, url_info)
  url = url_info['url']

  filename = target_dir_pathname
  if url_info.key?('filename')
    filename += url_info['filename']
  else
    # Use the initial URL's basename, rather than following redirects
    # first, since one of them redirects to a URL with a long unreadable
    # basename.
    filename += Pathname.new(url).basename
  end

  puts "Checking #{filename}..."
  if filename.exist? && url_info.include?('sha256')
    $sha256.reset
    $sha256.file(filename)
    if $sha256.hexdigest === url_info['sha256']
      puts "File '#{filename}' up to date."
      return
    else
      puts "File '#{filename}' exists, but the hash did not match.  Re-downloading."
    end
  end

  target_dir_pathname.mkpath
  download_file_to_filename(filename, url)
end

#download_file_to_filename(filename, url) ⇒ Object

Based on ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net.html#label-Streaming+Response+Bodies Not sure if there's a simpler way to do this without reading the whole file into memory.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'support/packager/package.rb', line 56

def download_file_to_filename(filename, url)
  puts "Downloading #{url}..."
  uri = URI(url)

  Net::HTTP.start(
      uri.host,
      uri.port,
      use_ssl: uri.scheme == 'https'
  ) do |http|
    request = Net::HTTP::Get.new uri.request_uri

    http.request request do |response|
      if response.is_a?(Net::HTTPRedirection) || response.is_a?(Net::HTTPFound)
        return download_file_to_filename(filename, response['location'])
      end

      open filename, 'wb' do |io|
        response.read_body do |chunk|
          io.write(chunk)
        end
      end
    end
  end
end

#escape(str) ⇒ Object



31
32
33
# File 'puppet/modules/wmflib/lib/puppet/parser/functions/make_url.rb', line 31

def escape(str)
  URI.escape(str.to_s, ":/?#[]@!$&'()*+,;= ")
end

#fact(*args) ⇒ Object



15
# File 'puppet/modules/stdlib/spec/acceptance/build_csv.rb', line 15

def fact(*args) [] end

#get_puppet_versionObject



36
37
38
# File 'puppet/modules/stdlib/spec/spec_helper_acceptance.rb', line 36

def get_puppet_version
  (on default, puppet('--version')).output.chomp
end

#get_tests(children) ⇒ Object

Collect up tests and example groups into a hash



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'puppet/modules/stdlib/spec/acceptance/build_csv.rb', line 32

def get_tests(children)
  children.inject({}) do |memo,c|
    memo[c.description] = Hash.new
    memo[c.description]["groups"] = get_tests(c.children) unless c.children.empty?
    memo[c.description]["tests"] = c.examples.collect { |e|
      e.description unless e.pending?
    }.compact unless c.examples.empty?
    memo[c.description]["pending_tests"] = c.examples.collect { |e|
      e.description if e.pending?
    }.compact unless c.examples.empty?
    memo
  end
end

#hiera_config_fixtureObject



13
14
15
16
17
18
19
20
21
22
23
# File 'puppet/spec/spec_helper.rb', line 13

def hiera_config_fixture
  fixture_path = File.expand_path(File.join(__FILE__, '..', 'fixtures'))
  conf = File.read(File.join(puppet_path, 'hiera.yaml'))
  conf.gsub!(%r{/vagrant/puppet}, puppet_path)

  FileUtils.mkdir_p(fixture_path)
  fixture = File.join(fixture_path, 'hiera.yaml')
  File.write(fixture, conf)

  fixture
end

#ini_cast(v) ⇒ Object



24
25
26
# File 'puppet/modules/wmflib/lib/puppet/parser/functions/ini.rb', line 24

def ini_cast(v)
  v.include?('.') ? Float(v) : Integer(v) rescue v
end

#ini_flatten(map, prefix = nil) ⇒ Object

Function: php_ini( hash $ini_settings [, hash $… ] )

Serialize a hash into php.ini-style format. Takes one or more hashes as arguments. If the argument list contains more than one hash, they are merged together. In case of duplicate keys, hashes to the right win.

Example

php_ini({'server' => {'port' => 80}}) # => server.port = 80


17
18
19
20
21
22
# File 'puppet/modules/wmflib/lib/puppet/parser/functions/ini.rb', line 17

def ini_flatten(map, prefix = nil)
  map.reduce({}) do |flat, (k, v)|
    k = [prefix, k].compact.join('.')
    flat.merge! v.is_a?(Hash) ? ini_flatten(v, k) : Hash[k, v]
  end
end

#is_future_parser_enabled?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
# File 'puppet/modules/stdlib/spec/spec_helper_acceptance.rb', line 27

def is_future_parser_enabled?
  if default[:type] == 'aio' || ENV['PUPPET_INSTALL_TYPE'] == 'agent'
    return true
  elsif default[:default_apply_opts]
    return default[:default_apply_opts][:parser] == 'future'
  end
  return false
end

#linuxObject



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'support/packager/package.rb', line 119

def linux
  linux_dir = $contents_dir + 'Linux'

  $url_config['VirtualBox']['Linux'].each do |distro, url|
    distro_dir = linux_dir + distro
    download_file(distro_dir, url)
  end

  $url_config['Vagrant']['Linux'].each do |format, url|
    vagrant_format_dir = linux_dir + format
    download_file(vagrant_format_dir, url)
  end
end

#macObject



133
134
135
136
137
138
# File 'support/packager/package.rb', line 133

def mac
  mac_dir = $contents_dir + 'Mac'
  download_file(mac_dir, $url_config['VirtualBox']['Mac'])
  download_file(mac_dir, $url_config['Vagrant']['Mac'])
  download_file(mac_dir, $url_config['Git']['Mac'])
end

#make_url(query_params) ⇒ Object



24
25
26
27
28
29
# File 'puppet/modules/wmflib/lib/puppet/parser/functions/make_url.rb', line 24

def make_url(query_params)
  query_params
    .delete_if { |k, v| v.nil? }
    .collect { |k, v| escape(k) + '=' + escape(v) }
    .join('&')
end

#ordered_json(o) ⇒ Object

Function: ordered_json( hash $data [, hash $… ] )

Serialize a hash into JSON with lexicographically sorted keys.

Because the order of keys in Ruby 1.8 hashes is undefined, 'to_pson' is not idempotent: i.e., the serialized form of the same hash object can vary from one invocation to the next. This causes problems whenever a JSON-serialized hash is included in a file template, because the variations in key order are picked up as file updates by Puppet, causing Puppet to replace the file and refresh dependent resources on every run.

Examples

# Render a Puppet hash as a configuration file:
$options = { 'useGraphite' => true, 'minVal' => '0.1' }
file { '/etc/kibana/config.json':
  content => ordered_json($options),
}


21
22
23
24
25
26
27
28
29
30
# File 'puppet/modules/wmflib/lib/puppet/parser/functions/ordered_json.rb', line 21

def ordered_json(o)
  case o
  when Array
    '[' + o.map { |x| ordered_json(x) }.join(', ') + ']'
  when Hash
    '{' + o.sort.map { |k, v| k.to_pson + ': ' + ordered_json(v) }.join(', ') + '}'
  else
    o.include?('.') ? Float(o).to_s : Integer(o).to_s rescue o.to_pson
  end
end

#param_value(subject, type, title, param) ⇒ Object



5
6
7
# File 'puppet/modules/nginx/spec/spec_helper.rb', line 5

def param_value(subject, type, title, param)
  subject.resource(type, title).send(:parameters)[param.to_sym]
end

#puppet_pathObject



4
5
6
# File 'puppet/spec/spec_helper.rb', line 4

def puppet_path
  File.expand_path(File.join(__FILE__, '../..'))
end

#shellescape(str) ⇒ Object



21
22
23
# File 'puppet/modules/wmflib/lib/puppet/parser/functions/shellescape.rb', line 21

def shellescape(str)
  Shellwords.shellescape(str.to_s)
end

#sort_keys_recursive(value) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'puppet/modules/wmflib/lib/puppet/parser/functions/ordered_yaml.rb', line 15

def sort_keys_recursive(value)
  # Prepare a value for YAML serialization by sorting its keys (if it is
  # a hash) and the keys of any hash object that is contained within the
  # value. Returns a new value.
  case value
  when Array
    value.map { |elem| sort_keys_recursive(elem) }
  when Hash
    map = {}
    def map.each_pair
      map.sort.each { |p| yield p }
    end
    value.sort.reduce(map) { |h, (k, v)| h[k] = sort_keys_recursive(v); h }
  when 'true', 'false'
    value == 'true'
  when :undef
    nil
  else
    value.include?('.') ? Float(value) : Integer(value) rescue value
  end
end

#to_csv(function_list, tests) ⇒ Object

Convert tests hash to csv format



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'puppet/modules/stdlib/spec/acceptance/build_csv.rb', line 62

def to_csv(function_list,tests)
  function_list.collect do |function_name|
    if v = tests["#{function_name} function"]
      positive_tests = count_tests_in(v["groups"]["success"])
      negative_tests = count_tests_in(v["groups"]["failure"])
      pending_tests  =
        count_pending_tests_in(v["groups"]["failure"]) +
        count_pending_tests_in(v["groups"]["failure"])
    else
      positive_tests = 0
      negative_tests = 0
      pending_tests  = 0
    end
    sprintf("%-25s, %-9d, %-9d, %-9d", function_name,positive_tests,negative_tests,pending_tests)
  end.compact
end

#windowsObject



140
141
142
143
144
145
# File 'support/packager/package.rb', line 140

def windows
  windows_dir = $contents_dir + 'Windows'
  download_file(windows_dir, $url_config['VirtualBox']['Windows'])
  download_file(windows_dir, $url_config['Vagrant']['Windows'])
  download_file(windows_dir, $url_config['Git']['Windows'])
end