[DRE-commits] [vagrant-libvirt] 12/104: Halt action added. This resulted into action.rb file rewrite. There was a need to split up actions into more subactions + more checking of VM state - if it's up when trying to ssh etc..

Antonio Terceiro terceiro at moszumanska.debian.org
Sun Apr 24 13:55:39 UTC 2016


This is an automated email from the git hooks/post-receive script.

terceiro pushed a commit to annotated tag 0.0.11
in repository vagrant-libvirt.

commit 72f1e41d9a869f3407039a844ba5e704e9bd0818
Author: pradels <les.pradels at gmail.com>
Date:   Mon Apr 1 17:10:35 2013 +0200

    Halt action added. This resulted into action.rb file rewrite. There
    was a need to split up actions into more subactions + more checking
    of VM state - if it's up when trying to ssh etc..
---
 lib/vagrant-libvirt/action.rb                      | 132 +++++++++++++++++----
 .../{destroy_domain.rb => cleanup_data_dir.rb}     |  12 +-
 lib/vagrant-libvirt/action/destroy_domain.rb       |   7 +-
 .../action/{start_domain.rb => halt_domain.rb}     |  25 ++--
 lib/vagrant-libvirt/action/is_running.rb           |  21 ++++
 lib/vagrant-libvirt/action/is_suspended.rb         |  21 ++++
 lib/vagrant-libvirt/action/message_not_running.rb  |  16 +++
 .../action/message_not_suspended.rb                |  16 +++
 lib/vagrant-libvirt/action/start_domain.rb         |   6 +-
 locales/en.yml                                     |   6 +
 10 files changed, 208 insertions(+), 54 deletions(-)

diff --git a/lib/vagrant-libvirt/action.rb b/lib/vagrant-libvirt/action.rb
index 9b3c01b..22312c6 100644
--- a/lib/vagrant-libvirt/action.rb
+++ b/lib/vagrant-libvirt/action.rb
@@ -12,28 +12,77 @@ module VagrantPlugins
           b.use ConfigValidate
           b.use ConnectLibvirt
           b.use Call, IsCreated do |env, b2|
-            if env[:result]
-              b2.use Call, ReadState do |env2,b3|
-                if env2[:machine_state_id] == 'paused'
-                  b3.use StartDomain
-                  return true
-                end
+            # Create VM if not yet created.
+            if !env[:result]
+              b2.use SetNameOfDomain
+              b2.use HandleStoragePool
+              b2.use HandleBoxImage
+              b2.use CreateDomainVolume
+              b2.use CreateDomain
+              b2.use CreateNetworkInterfaces
+
+              b2.use TimedProvision
+              b2.use StartDomain
+              b2.use WaitTillUp
+              b2.use SyncFolders
+            else
+              b2.use action_start
+            end
+          end
+        end
+      end
+
+      # Assuming VM is created, just start it. This action is not called
+      # directly by any subcommand. VM can be suspended, already running or in
+      # poweroff state.
+      def self.action_start
+        Vagrant::Action::Builder.new.tap do |b|
+          b.use ConfigValidate
+          b.use ConnectLibvirt
+          b.use Call, IsRunning do |env, b2|
+            # If the VM is running, then our work here is done, exit
+            next if env[:result]
+
+            b2.use Call, IsSuspended do |env2, b3|
+              if env2[:result]
+                b3.use ResumeDomain
+                next
               end
-              b2.use MessageAlreadyCreated
+
+              # VM is not running or suspended. Start it.. Machine should gain
+              # IP address when comming up, so wait for dhcp lease and store IP
+              # into machines data_dir.
+              b3.use StartDomain
+              b3.use WaitTillUp
+            end
+          end
+        end
+      end
+
+      # This is the action that is primarily responsible for halting the
+      # virtual machine.
+      def self.action_halt
+        Vagrant::Action::Builder.new.tap do |b|
+          b.use ConfigValidate
+          b.use ConnectLibvirt
+          b.use Call, IsCreated do |env, b2|
+            if !env[:result]
+              b2.use MessageNotCreated
               next
             end
 
-            b2.use SetNameOfDomain
-            b2.use HandleStoragePool
-            b2.use HandleBoxImage
-            b2.use CreateDomainVolume
-            b2.use CreateDomain
-            b2.use CreateNetworkInterfaces
-
-            b2.use TimedProvision
-            b2.use StartDomain
-            b2.use WaitTillUp
-            b2.use SyncFolders
+            b2.use Call, IsSuspended do |env2, b3|
+              b3.use ResumeDomain if env2[:result]
+            end
+
+            b2.use Call, IsRunning do |env2, b3|
+              next if !env2[:result]
+
+              # VM is running, halt it.. Cleanup running instance data. Now
+              # only IP address is stored.
+              b3.use HaltDomain
+              b3.use CleanupDataDir
+            end
           end
         end
       end
@@ -51,6 +100,9 @@ module VagrantPlugins
 
             b2.use ConnectLibvirt
             b2.use DestroyDomain
+
+            # Cleanup running instance data. Now only IP address is stored.
+            b2.use CleanupDataDir
           end
         end
       end
@@ -65,7 +117,15 @@ module VagrantPlugins
               next
             end
 
-            b2.use SSHExec
+            b2.use ConnectLibvirt
+            b2.use Call, IsRunning do |env2, b3|
+              if !env2[:result]
+                b3.use MessageNotRunning
+                next
+              end
+
+              b3.use SSHExec
+            end
           end
         end
       end
@@ -80,8 +140,16 @@ module VagrantPlugins
               next
             end
 
-            b2.use Provision
-            b2.use SyncFolders
+            b2.use ConnectLibvirt
+            b2.use Call, IsRunning do |env2, b3|
+              if !env2[:result]
+                b3.use MessageNotRunning
+                next
+              end
+
+              b3.use Provision
+              b3.use SyncFolders
+            end
           end
         end
       end
@@ -98,7 +166,13 @@ module VagrantPlugins
             end
 
             b2.use ConnectLibvirt
-            b2.use SuspendDomain
+            b2.use Call, IsRunning do |env2, b3|
+              if !env2[:result]
+                b3.use MessageNotRunning
+                next
+              end
+              b3.use SuspendDomain
+            end
           end
         end
       end
@@ -115,7 +189,13 @@ module VagrantPlugins
             end
 
             b2.use ConnectLibvirt
-            b2.use ResumeDomain
+            b2.use Call, IsSuspended do |env2, b3|
+              if !env2[:result]
+                b3.use MessageNotSuspended
+                next
+              end
+              b3.use ResumeDomain
+            end
           end
         end
       end
@@ -144,8 +224,12 @@ module VagrantPlugins
       action_root = Pathname.new(File.expand_path("../action", __FILE__))
       autoload :ConnectLibvirt, action_root.join("connect_libvirt")
       autoload :IsCreated, action_root.join("is_created")
+      autoload :IsRunning, action_root.join("is_running")
+      autoload :IsSuspended, action_root.join("is_suspended")
       autoload :MessageAlreadyCreated, action_root.join("message_already_created")
       autoload :MessageNotCreated, action_root.join("message_not_created")
+      autoload :MessageNotRunning, action_root.join("message_not_running")
+      autoload :MessageNotSuspended, action_root.join("message_not_suspended")
       autoload :HandleStoragePool, action_root.join("handle_storage_pool")
       autoload :HandleBoxImage, action_root.join("handle_box_image")
       autoload :SetNameOfDomain, action_root.join("set_name_of_domain")
@@ -154,8 +238,10 @@ module VagrantPlugins
       autoload :CreateNetworkInterfaces, action_root.join("create_network_interfaces")
       autoload :DestroyDomain, action_root.join("destroy_domain")
       autoload :StartDomain, action_root.join("start_domain")
+      autoload :HaltDomain, action_root.join("halt_domain")
       autoload :SuspendDomain, action_root.join("suspend_domain")
       autoload :ResumeDomain, action_root.join("resume_domain")
+      autoload :CleanupDataDir, action_root.join("cleanup_data_dir")
       autoload :ReadState, action_root.join("read_state")
       autoload :ReadSSHInfo, action_root.join("read_ssh_info")
       autoload :TimedProvision, action_root.join("timed_provision")
diff --git a/lib/vagrant-libvirt/action/destroy_domain.rb b/lib/vagrant-libvirt/action/cleanup_data_dir.rb
similarity index 55%
copy from lib/vagrant-libvirt/action/destroy_domain.rb
copy to lib/vagrant-libvirt/action/cleanup_data_dir.rb
index 7e18521..57b8749 100644
--- a/lib/vagrant-libvirt/action/destroy_domain.rb
+++ b/lib/vagrant-libvirt/action/cleanup_data_dir.rb
@@ -3,21 +3,13 @@ require 'log4r'
 module VagrantPlugins
   module Libvirt
     module Action
-      class DestroyDomain
+      class CleanupDataDir
         def initialize(app, env)
-          @logger = Log4r::Logger.new("vagrant_libvirt::action::destroy_domain")
+          @logger = Log4r::Logger.new("vagrant_libvirt::action::cleanup_data_dir")
           @app = app
         end
 
         def call(env)
-          # Destroy the server, remove the tracking ID and file holding IP
-          # address.
-          env[:ui].info(I18n.t("vagrant_libvirt.destroy_domain"))
-
-          domain = env[:libvirt_compute].servers.get(env[:machine].id.to_s)
-          domain.destroy(:destroy_volumes => true)
-          env[:machine].id = nil
-
           # Remove file holding IP address
           ip_file_path = env[:machine].data_dir + 'ip'
           File.delete(ip_file_path) if File.exists?(ip_file_path)
diff --git a/lib/vagrant-libvirt/action/destroy_domain.rb b/lib/vagrant-libvirt/action/destroy_domain.rb
index 7e18521..91cdac1 100644
--- a/lib/vagrant-libvirt/action/destroy_domain.rb
+++ b/lib/vagrant-libvirt/action/destroy_domain.rb
@@ -10,18 +10,13 @@ module VagrantPlugins
         end
 
         def call(env)
-          # Destroy the server, remove the tracking ID and file holding IP
-          # address.
+          # Destroy the server, remove the tracking ID
           env[:ui].info(I18n.t("vagrant_libvirt.destroy_domain"))
 
           domain = env[:libvirt_compute].servers.get(env[:machine].id.to_s)
           domain.destroy(:destroy_volumes => true)
           env[:machine].id = nil
 
-          # Remove file holding IP address
-          ip_file_path = env[:machine].data_dir + 'ip'
-          File.delete(ip_file_path) if File.exists?(ip_file_path)
-
           @app.call(env)
         end
       end
diff --git a/lib/vagrant-libvirt/action/start_domain.rb b/lib/vagrant-libvirt/action/halt_domain.rb
similarity index 50%
copy from lib/vagrant-libvirt/action/start_domain.rb
copy to lib/vagrant-libvirt/action/halt_domain.rb
index 1acfd2b..3648101 100644
--- a/lib/vagrant-libvirt/action/start_domain.rb
+++ b/lib/vagrant-libvirt/action/halt_domain.rb
@@ -3,29 +3,34 @@ require 'log4r'
 module VagrantPlugins
   module Libvirt
     module Action
-
-      # Just start the domain.
-      class StartDomain
+      # Halt the domain.
+      class HaltDomain
         def initialize(app, env)
-          @logger = Log4r::Logger.new("vagrant_libvirt::action::start_domain")
+          @logger = Log4r::Logger.new("vagrant_libvirt::action::halt_domain")
           @app = app
         end
 
         def call(env)
-          env[:ui].info(I18n.t("vagrant_libvirt.starting_domain"))
+          env[:ui].info(I18n.t("vagrant_libvirt.halt_domain"))
 
           domain = env[:libvirt_compute].servers.get(env[:machine].id.to_s)
           raise Errors::NoDomainError if domain == nil
-          if domain.state.to_s == 'paused'
-            domain.resume
-          else
-            domain.start
+
+          @logger.info("Trying gracefull shutdown.")
+          domain.shutdown
+          begin
+            domain.wait_for(30) {
+              !ready?
+            }
+          rescue Fog::Errors::TimeoutError
+            @logger.info("VM is still running. Calling force poweroff.")
+            domain.poweroff
           end
 
           @app.call(env)
         end
       end
-
     end
   end
 end
+
diff --git a/lib/vagrant-libvirt/action/is_running.rb b/lib/vagrant-libvirt/action/is_running.rb
new file mode 100644
index 0000000..a0bf4ec
--- /dev/null
+++ b/lib/vagrant-libvirt/action/is_running.rb
@@ -0,0 +1,21 @@
+module VagrantPlugins
+  module Libvirt
+    module Action
+      # This can be used with "Call" built-in to check if the machine
+      # is running and branch in the middleware.
+      class IsRunning
+        def initialize(app, env)
+          @app = app
+        end
+
+        def call(env)
+          domain = env[:libvirt_compute].servers.get(env[:machine].id.to_s)
+          raise Errors::NoDomainError if domain == nil
+          env[:result] = domain.state.to_s == 'running'
+
+          @app.call(env)
+        end
+      end
+    end
+  end
+end
diff --git a/lib/vagrant-libvirt/action/is_suspended.rb b/lib/vagrant-libvirt/action/is_suspended.rb
new file mode 100644
index 0000000..1162429
--- /dev/null
+++ b/lib/vagrant-libvirt/action/is_suspended.rb
@@ -0,0 +1,21 @@
+module VagrantPlugins
+  module Libvirt
+    module Action
+      # This can be used with "Call" built-in to check if the machine
+      # is suspended and branch in the middleware.
+      class IsSuspended
+        def initialize(app, env)
+          @app = app
+        end
+
+        def call(env)
+          domain = env[:libvirt_compute].servers.get(env[:machine].id.to_s)
+          raise Errors::NoDomainError if domain == nil
+          env[:result] = domain.state.to_s == 'paused'
+
+          @app.call(env)
+        end
+      end
+    end
+  end
+end
diff --git a/lib/vagrant-libvirt/action/message_not_running.rb b/lib/vagrant-libvirt/action/message_not_running.rb
new file mode 100644
index 0000000..2f8b977
--- /dev/null
+++ b/lib/vagrant-libvirt/action/message_not_running.rb
@@ -0,0 +1,16 @@
+module VagrantPlugins
+  module Libvirt
+    module Action
+      class MessageNotRunning
+        def initialize(app, env)
+          @app = app
+        end
+
+        def call(env)
+          env[:ui].info(I18n.t("vagrant_libvirt.not_running"))
+          @app.call(env)
+        end
+      end
+    end
+  end
+end
diff --git a/lib/vagrant-libvirt/action/message_not_suspended.rb b/lib/vagrant-libvirt/action/message_not_suspended.rb
new file mode 100644
index 0000000..40654eb
--- /dev/null
+++ b/lib/vagrant-libvirt/action/message_not_suspended.rb
@@ -0,0 +1,16 @@
+module VagrantPlugins
+  module Libvirt
+    module Action
+      class MessageNotSuspended
+        def initialize(app, env)
+          @app = app
+        end
+
+        def call(env)
+          env[:ui].info(I18n.t("vagrant_libvirt.not_suspended"))
+          @app.call(env)
+        end
+      end
+    end
+  end
+end
diff --git a/lib/vagrant-libvirt/action/start_domain.rb b/lib/vagrant-libvirt/action/start_domain.rb
index 1acfd2b..5ea9332 100644
--- a/lib/vagrant-libvirt/action/start_domain.rb
+++ b/lib/vagrant-libvirt/action/start_domain.rb
@@ -16,11 +16,7 @@ module VagrantPlugins
 
           domain = env[:libvirt_compute].servers.get(env[:machine].id.to_s)
           raise Errors::NoDomainError if domain == nil
-          if domain.state.to_s == 'paused'
-            domain.resume
-          else
-            domain.start
-          end
+          domain.start
 
           @app.call(env)
         end
diff --git a/locales/en.yml b/locales/en.yml
index 49bb09c..5b7e8cf 100644
--- a/locales/en.yml
+++ b/locales/en.yml
@@ -4,6 +4,10 @@ en:
       The domain is already created.
     not_created: |-
       Domain is not created. Please run `vagrant up` first.
+    not_running: |-
+      Domain is not running. Please run `vagrant up` or `vagrant resume` first.
+    not_suspended: |-
+      Domain is not suspended.
     finding_volume: |-
       Checking if volume is available.
     creating_domain: |-
@@ -22,6 +26,8 @@ en:
       Poweroff domain.
     destroy_domain: |-
       Removing domain...
+    halt_domain: |-
+      Halting domain...
     resuming_domain: |-
       Resuming domain...
     suspending_domain: |-

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-ruby-extras/vagrant-libvirt.git



More information about the Pkg-ruby-extras-commits mailing list