Class: MediaWikiVagrant::Environment
- Defined in:
- lib/mediawiki-vagrant/environment.rb
Overview
Represents the current environment from which MediaWiki-Vagrant commands are executed.
Constant Summary collapse
- STALENESS =
604800
Class Method Summary collapse
-
.operating_system ⇒ :osx, ...
Host operating system.
-
.total_cpus ⇒ Fixnum
Total host OS CPU cores.
-
.total_memory ⇒ Fixnum
Total host OS memory in MB.
Instance Method Summary collapse
-
#boxname ⇒ String
name of MWV root directory, sanitized to be usable in a domain name.
-
#cancel_reload ⇒ Object
Removes the reload trigger for the next provision.
-
#commit ⇒ Object
The HEAD commit of local master branch, if we're executing from within a cloned Git repo.
-
#configure_settings {|settings| ... } ⇒ Object
Configures settings for this environment using the given block.
-
#hiera_delete(key) ⇒ Object
Deletes the given entry from the vagrant-managed hiera file.
-
#hiera_get(key) ⇒ Object
Returns the value of the given entry in the vagrant-managed hiera file, or nil if none exists.
-
#hiera_set(key, value) ⇒ Hash
Saves the given key/value pair to the vagrant-managed hiera file.
-
#initialize(directory) ⇒ Environment
constructor
Initialize a new environment for the given directory.
-
#load_settings(roles = roles_enabled) ⇒ Settings
Loads and returns settings for this environment.
-
#path(*subpaths) ⇒ Pathname
Returns an absolute path from the given relative path.
-
#prune_roles ⇒ Object
Removes all enabled roles that are no longer available.
-
#purge_puppet_created_files ⇒ Object
Removes files created by the puppet provisioner.
-
#reload? ⇒ true, false
Whether the environment is set to reload upon the next provision.
-
#role_docstring(role) ⇒ Object
Get comment header for a role.
-
#role_settings(roles = roles_enabled) ⇒ {String => Settings}
Settings for the given or currently enabled roles.
-
#roles_available ⇒ Array
Returns all available Puppet roles.
-
#roles_enabled ⇒ Array
Returns enabled Puppet roles.
-
#trigger_reload ⇒ Object
Triggers a reload upon the next provision.
-
#update ⇒ Object
If it has been a week or more since remote commits have been fetched, run 'git fetch origin', unless the user disabled automatic fetching.
-
#update_roles(roles) ⇒ Object
Updates the enabled Puppet roles to the given set.
-
#valid? ⇒ Boolean
Whether this is a valid MediaWiki-Vagrant environment.
Constructor Details
#initialize(directory) ⇒ Environment
Initialize a new environment for the given directory.
77 78 79 80 |
# File 'lib/mediawiki-vagrant/environment.rb', line 77 def initialize(directory) @directory = directory @path = Pathname.new(@directory) end |
Class Method Details
.operating_system ⇒ :osx, ...
Host operating system.
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/mediawiki-vagrant/environment.rb', line 19 def case RbConfig::CONFIG['host_os'] when /mac|darwin/i :osx when /linux/i :linux when /mswin|mingw|cygwin/i :windows else :unknown end end |
.total_cpus ⇒ Fixnum
Total host OS CPU cores.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/mediawiki-vagrant/environment.rb', line 36 def total_cpus cores = case when :osx `sysctl -n hw.ncpu`.to_i when :linux `nproc`.to_i when :windows require 'win32ole' wmi = WIN32OLE.connect('winmgmts://') q = wmi.ExecQuery('select NumberOfCores from Win32_Processor') q.to_enum.reduce(0) { |cores, processor| cores + processor.NumberOfCores } else 1 end [1, cores].max end |
.total_memory ⇒ Fixnum
Total host OS memory in MB.
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/mediawiki-vagrant/environment.rb', line 61 def total_memory case when :osx `sysctl -n hw.memsize`.to_i / 1024 / 1024 when :linux `awk '$1 == "MemTotal:" { print $2 }' /proc/meminfo`.to_i / 1024 when :windows `wmic OS get TotalVisibleMemorySize | more +1`.to_i / 1024 else 0 end end |
Instance Method Details
#boxname ⇒ String
name of MWV root directory, sanitized to be usable in a domain name
285 286 287 288 |
# File 'lib/mediawiki-vagrant/environment.rb', line 285 def boxname @path.basename.to_s.downcase .gsub(/[^a-z0-9-]+/, '-')[0..62].gsub(/^-|-$/, '') end |
#cancel_reload ⇒ Object
Removes the reload trigger for the next provision.
84 85 86 |
# File 'lib/mediawiki-vagrant/environment.rb', line 84 def cancel_reload reload_trigger.delete if reload_trigger.exist? end |
#commit ⇒ Object
The HEAD commit of local master branch, if we're executing from within a cloned Git repo.
91 92 93 94 |
# File 'lib/mediawiki-vagrant/environment.rb', line 91 def commit master = path('.git/refs/heads/master') master.read[0..8] if master.exist? end |
#configure_settings {|settings| ... } ⇒ Object
Configures settings for this environment using the given block.
103 104 105 |
# File 'lib/mediawiki-vagrant/environment.rb', line 103 def configure_settings(&blk) Settings.configure(settings_path, &blk) end |
#hiera_delete(key) ⇒ Object
Deletes the given entry from the vagrant-managed hiera file.
246 247 248 249 250 251 252 253 254 |
# File 'lib/mediawiki-vagrant/environment.rb', line 246 def hiera_delete(key) if hiera_data.exist? hiera = hiera_load if hiera.key?(key) hiera.delete(key) hiera_save(hiera) end end end |
#hiera_get(key) ⇒ Object
Returns the value of the given entry in the vagrant-managed hiera file, or nil if none exists.
263 264 265 |
# File 'lib/mediawiki-vagrant/environment.rb', line 263 def hiera_get(key) hiera_load[key] end |
#hiera_set(key, value) ⇒ Hash
Saves the given key/value pair to the vagrant-managed hiera file.
274 275 276 277 278 279 |
# File 'lib/mediawiki-vagrant/environment.rb', line 274 def hiera_set(key, value) hiera_load.tap do |hiera| hiera[key] = value hiera_save(hiera) end end |
#load_settings(roles = roles_enabled) ⇒ Settings
Loads and returns settings for this environment. Settings for the given or currently enabled roles will augment those loaded from `.settings.yaml`.
115 116 117 118 119 120 121 122 |
# File 'lib/mediawiki-vagrant/environment.rb', line 115 def load_settings(roles = roles_enabled) settings = Settings.new settings.load(settings_path) if settings_path.exist? role_settings(roles).each { |_, rsettings| settings.combine(rsettings) } settings end |
#path(*subpaths) ⇒ Pathname
Returns an absolute path from the given relative path.
128 129 130 |
# File 'lib/mediawiki-vagrant/environment.rb', line 128 def path(*subpaths) @path.join(*subpaths) end |
#prune_roles ⇒ Object
Removes all enabled roles that are no longer available.
134 135 136 137 |
# File 'lib/mediawiki-vagrant/environment.rb', line 134 def prune_roles migrate_roles update_roles(roles_enabled & roles_available) end |
#purge_puppet_created_files ⇒ Object
Removes files created by the puppet provisioner.
233 234 235 236 237 238 239 240 |
# File 'lib/mediawiki-vagrant/environment.rb', line 233 def purge_puppet_created_files FileUtils.rm_f Dir[path('settings.d/puppet-managed/*.php')] FileUtils.rm_rf path('settings.d/multiwiki') FileUtils.rm_rf path('settings.d/wikis') FileUtils.rm_rf path('settings.d/composer') FileUtils.rm_rf path('vagrant.d') FileUtils.rm_f path('mediawiki/LocalSettings.php') end |
#reload? ⇒ true, false
Whether the environment is set to reload upon the next provision.
143 144 145 |
# File 'lib/mediawiki-vagrant/environment.rb', line 143 def reload? reload_trigger.exist? end |
#role_docstring(role) ⇒ Object
Get comment header for a role
175 176 177 178 179 180 181 182 |
# File 'lib/mediawiki-vagrant/environment.rb', line 175 def role_docstring(role) role_file = module_path("role/manifests/#{role}.pp") return nil unless role_file.exist? role_file.each_line.take_while { |line| line =~ /^#( |$)/ }.inject('') do |doc, line| doc << line.sub(/^# ?/, '') end end |
#role_settings(roles = roles_enabled) ⇒ {String => Settings}
Settings for the given or currently enabled roles.
190 191 192 193 194 |
# File 'lib/mediawiki-vagrant/environment.rb', line 190 def role_settings(roles = roles_enabled) roles.each.with_object({}) do |role, settings| settings[role] = role_settings_load(role) end end |
#roles_available ⇒ Array
Returns all available Puppet roles.
151 152 153 154 155 |
# File 'lib/mediawiki-vagrant/environment.rb', line 151 def roles_available manifests = Dir[module_path('role/manifests/*.pp')] manifests.map! { |file| File.read(file, encoding: 'utf-8').match(/^class\s*role::(\w+)/) { |m| m[1] } } manifests.compact.sort.uniq - ['generic', 'mediawiki', 'labs_initial_content'] end |
#roles_enabled ⇒ Array
Returns enabled Puppet roles.
161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/mediawiki-vagrant/environment.rb', line 161 def roles_enabled migrate_roles hiera = hiera_load return [] unless hiera.key?('classes') roles = hiera['classes'].map do |r| r.match(/^role::(\S+)/) { |m| m[1] } end roles.compact.sort.uniq end |
#trigger_reload ⇒ Object
Triggers a reload upon the next provision.
198 199 200 201 202 |
# File 'lib/mediawiki-vagrant/environment.rb', line 198 def trigger_reload dir = reload_trigger.dirname dir.mkdir unless dir.exist? reload_trigger.open('w') { |io| io.puts } end |
#update ⇒ Object
If it has been a week or more since remote commits have been fetched, run 'git fetch origin', unless the user disabled automatic fetching. You can disable automatic fetching by creating an empty 'no-updates' file in the root directory of your repository.
218 219 220 221 222 |
# File 'lib/mediawiki-vagrant/environment.rb', line 218 def update if stale_head? && !(ENV.include?('MWV_NO_UPDATE') || path('no-update').exist?) system('git fetch origin', chdir: @directory) end end |
#update_roles(roles) ⇒ Object
Updates the enabled Puppet roles to the given set.
206 207 208 209 210 211 |
# File 'lib/mediawiki-vagrant/environment.rb', line 206 def update_roles(roles) classes = roles.sort.uniq.map do |r| "role::#{r.sub(/^role::/, '')}" end hiera_set('classes', classes) end |
#valid? ⇒ Boolean
Whether this is a valid MediaWiki-Vagrant environment. This should be used as a guard in middleware.
227 228 229 |
# File 'lib/mediawiki-vagrant/environment.rb', line 227 def valid? path('lib/mediawiki-vagrant.rb').exist? end |