Dduvall has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/194948

Change subject: plugin: Support auto-configuration as a setting feature
......................................................................

plugin: Support auto-configuration as a setting feature

Support auto-configuration in `Setting` itself instead of as an ad hoc
feature of `vagrant_cores` and `vagrant_ram`, and perform it for unset
settings upon running `vagrant config`.

Bug: T87684
Change-Id: I6b5e53c5aa1b2177a378bda223476203727df391
---
M lib/mediawiki-vagrant/config.rb
M lib/mediawiki-vagrant/setting.rb
M lib/mediawiki-vagrant/settings/definitions.rb
M spec/mediawiki_vagrant/setting_spec.rb
M spec/mediawiki_vagrant/settings/definitions_spec.rb
5 files changed, 63 insertions(+), 9 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/vagrant 
refs/changes/48/194948/1

diff --git a/lib/mediawiki-vagrant/config.rb b/lib/mediawiki-vagrant/config.rb
index ffd5af6..2e8884e 100644
--- a/lib/mediawiki-vagrant/config.rb
+++ b/lib/mediawiki-vagrant/config.rb
@@ -114,6 +114,9 @@
           Settings.definitions.include?(name) && !setting.internal?
         end
 
+        # Automatically configure any unset settings that support it
+        scope.each { |_, setting| setting.auto! }
+
         if options[:required]
           scope = scope.reject { |_, setting| setting.default? || setting.set? 
}
         end
diff --git a/lib/mediawiki-vagrant/setting.rb b/lib/mediawiki-vagrant/setting.rb
index ee21afc..ee41245 100644
--- a/lib/mediawiki-vagrant/setting.rb
+++ b/lib/mediawiki-vagrant/setting.rb
@@ -6,6 +6,7 @@
   # @attr description [String] Brief description of the setting
   # @attr help [String] Additional help text
   # @attr default [Object] Default value
+  # @attr auto [Proc] Used to automatically configure the setting
   # @attr coercion [Proc] Used to process a given setting value
   # @attr internal [true, false] Whether the setting is only used internally
   # @attr allows_empty [true, false] Whether to allow empty string values
@@ -15,7 +16,7 @@
   #
   class Setting
     attr_reader :name
-    attr_accessor :description, :help, :default, :coercion, :internal, 
:allows_empty
+    attr_accessor :description, :help, :default, :auto, :coercion, :internal, 
:allows_empty
 
     def initialize(name, value = nil)
       @name = name
@@ -27,6 +28,10 @@
 
     alias allows_empty? allows_empty
 
+    def auto!
+      self.value = auto.call unless auto.nil? || set?
+    end
+
     alias internal? internal
 
     def default?
diff --git a/lib/mediawiki-vagrant/settings/definitions.rb 
b/lib/mediawiki-vagrant/settings/definitions.rb
index 6d8a8d7..304b9a1 100644
--- a/lib/mediawiki-vagrant/settings/definitions.rb
+++ b/lib/mediawiki-vagrant/settings/definitions.rb
@@ -12,19 +12,15 @@
       description: "Amount of RAM (in MB) allocated to the guest VM",
       help: "Specify 'auto' to automatically allocate 1/4 of your system's 
memory",
       default: 1536,
-      coercion: ->(setting, new) do
-        new = (new == "auto") ? (Environment.total_memory / 4) : new.to_i
-        [setting.default, new].max
-      end
+      auto: -> { Environment.total_memory / 4 },
+      coercion: ->(setting, new) { [setting.default, new.to_i].max }
 
     setting :vagrant_cores,
       description: "CPU cores allocated to the guest VM",
       help: "If you're on a single-core system, be sure to enter '1'. Specify 
'auto' to automatically allocate all of your host system's cores.",
       default: 2,
-      coercion: ->(setting, new) do
-        new = (new == "auto") ? Environment.total_cpus : new.to_i
-        new && new.to_i
-      end
+      auto: -> { Environment.total_cpus },
+      coercion: ->(setting, new) { new && new.to_i }
 
     setting :static_ip,
       description: "IP address assigned to the guest VM",
diff --git a/spec/mediawiki_vagrant/setting_spec.rb 
b/spec/mediawiki_vagrant/setting_spec.rb
index 2465152..5c30242 100644
--- a/spec/mediawiki_vagrant/setting_spec.rb
+++ b/spec/mediawiki_vagrant/setting_spec.rb
@@ -8,6 +8,39 @@
     let(:name) { :foo }
     let(:value) { nil }
 
+    describe '#auto!' do
+      subject { setting.auto! }
+
+      context 'where `auto=` is set' do
+        before { setting.auto = -> { :bar } }
+
+        context 'and no value is set' do
+          it 'sets the value using the auto result' do
+            subject
+            expect(setting.value).to eq(:bar)
+          end
+        end
+
+        context 'but a value is set' do
+          let(:value) { :baz }
+
+          it 'sets the value using the auto result' do
+            subject
+            expect(setting.value).to eq(:baz)
+          end
+        end
+      end
+
+      context 'where `auto=` is not set' do
+        context 'and no value is set' do
+          it 'the value remains unset' do
+            subject
+            expect(setting).not_to be_set
+          end
+        end
+      end
+    end
+
     describe '#set?' do
       subject { setting.set? }
 
diff --git a/spec/mediawiki_vagrant/settings/definitions_spec.rb 
b/spec/mediawiki_vagrant/settings/definitions_spec.rb
index dd59be1..e18e4d2 100644
--- a/spec/mediawiki_vagrant/settings/definitions_spec.rb
+++ b/spec/mediawiki_vagrant/settings/definitions_spec.rb
@@ -1,4 +1,5 @@
 require 'spec_helper'
+require 'mediawiki-vagrant/environment'
 require 'mediawiki-vagrant/settings/definitions'
 
 module MediaWikiVagrant
@@ -34,6 +35,14 @@
             end
           end
         end
+
+        context 'auto configuration' do
+          it 'allocates 1/4 of total system memory' do
+            expect(Environment).to receive(:total_memory).and_return(8192)
+            subject.auto!
+            expect(subject.value).to eq(2048)
+          end
+        end
       end
 
       describe 'vagrant_cores' do
@@ -48,6 +57,14 @@
             expect(subject.value).to eq(4)
           end
         end
+
+        context 'auto configuration' do
+          it 'uses all available cores/CPUs' do
+            expect(Environment).to receive(:total_cpus).and_return(8)
+            subject.auto!
+            expect(subject.value).to eq(8)
+          end
+        end
       end
 
       describe 'static_ip' do

-- 
To view, visit https://gerrit.wikimedia.org/r/194948
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6b5e53c5aa1b2177a378bda223476203727df391
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/vagrant
Gerrit-Branch: master
Gerrit-Owner: Dduvall <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to