"Fossies" - the Fresh Open Source Software Archive

Member "vagrant-2.2.14/plugins/providers/hyperv/action.rb" (20 Nov 2020, 9788 Bytes) of package /linux/misc/vagrant-2.2.14.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "action.rb": 2.2.10_vs_2.2.11.

    1 require "pathname"
    2 
    3 require "vagrant/action/builder"
    4 
    5 module VagrantPlugins
    6   module HyperV
    7     module Action
    8       # Include the built-in modules so we can use them as top-level things.
    9       include Vagrant::Action::Builtin
   10 
   11       def self.action_reload
   12         Vagrant::Action::Builder.new.tap do |b|
   13           b.use ConfigValidate
   14           b.use Call, IsState, :not_created do |env, b2|
   15             if env[:result]
   16               b2.use Message, I18n.t("vagrant_hyperv.message_not_created")
   17               next
   18             end
   19 
   20             b2.use action_halt
   21             b2.use action_start
   22           end
   23         end
   24       end
   25 
   26       def self.action_destroy
   27         Vagrant::Action::Builder.new.tap do |b|
   28           b.use Call, IsState, :not_created do |env1, b1|
   29             if env1[:result]
   30               b1.use Message, I18n.t("vagrant_hyperv.message_not_created")
   31               next
   32             end
   33 
   34             b1.use Call, DestroyConfirm do |env2, b2|
   35               if !env2[:result]
   36                 b2.use MessageWillNotDestroy
   37                 next
   38               end
   39 
   40               b2.use ConfigValidate
   41               b2.use ProvisionerCleanup, :before
   42               b2.use StopInstance
   43               b2.use DeleteVM
   44               b2.use SyncedFolderCleanup
   45             end
   46           end
   47         end
   48       end
   49 
   50       def self.action_halt
   51         Vagrant::Action::Builder.new.tap do |b|
   52           b.use ConfigValidate
   53           b.use Call, IsState, :not_created do |env, b2|
   54             if env[:result]
   55               b2.use Message, I18n.t("vagrant_hyperv.message_not_created")
   56               next
   57             end
   58 
   59             b2.use Call, GracefulHalt, :off, :running do |env2, b3|
   60               if !env2[:result]
   61                 b3.use StopInstance
   62               end
   63             end
   64           end
   65         end
   66       end
   67 
   68       # This action packages the virtual machine into a single box file.
   69       def self.action_package
   70         Vagrant::Action::Builder.new.tap do |b|
   71           b.use CheckEnabled
   72           b.use Call, IsState, :not_created do |env1, b2|
   73             if env1[:result]
   74               b2.use Message, I18n.t("vagrant_hyperv.message_not_created")
   75               next
   76             end
   77 
   78             b2.use PackageSetupFolders
   79             b2.use PackageSetupFiles
   80             b2.use action_halt
   81             b2.use SyncedFolderCleanup
   82             b2.use Package
   83             b2.use PackageVagrantfile
   84             b2.use PackageMetadataJson
   85             b2.use Export
   86           end
   87         end
   88       end
   89 
   90       def self.action_provision
   91         Vagrant::Action::Builder.new.tap do |b|
   92           b.use ConfigValidate
   93           b.use Call, IsState, :not_created do |env, b2|
   94             if env[:result]
   95               b2.use Message, I18n.t("vagrant_hyperv.message_not_created")
   96               next
   97             end
   98 
   99             b2.use Call, IsState, :running do |env1, b3|
  100               if !env1[:result]
  101                 b3.use Message, I18n.t("vagrant_hyperv.message_not_running")
  102                 next
  103               end
  104 
  105               b3.use Provision
  106             end
  107           end
  108         end
  109       end
  110 
  111       def self.action_resume
  112         Vagrant::Action::Builder.new.tap do |b|
  113           b.use HandleBox
  114           b.use ConfigValidate
  115           b.use Call, IsState, :not_created do |env, b1|
  116             if env[:result]
  117               b1.use Message, I18n.t("vagrant_hyperv.message_not_created")
  118               next
  119             end
  120 
  121             b1.use ResumeVM
  122             b1.use WaitForIPAddress
  123             b1.use WaitForCommunicator, [:running]
  124           end
  125         end
  126       end
  127 
  128       def self.action_start
  129         Vagrant::Action::Builder.new.tap do |b|
  130           b.use Call, IsState, :running do |env1, b1|
  131             if env1[:result]
  132               b1.use action_provision
  133               next
  134             end
  135 
  136             b1.use Call, IsState, :paused do |env2, b2|
  137               if env2[:result]
  138                 b2.use action_resume
  139                 next
  140               end
  141 
  142               b2.use Call, IsState, :saved do |env3, b3|
  143                 # When state is `:saved` it is a snapshot being restored
  144                 if !env3[:result]
  145                   b3.use Provision
  146                   b3.use Configure
  147                   b3.use SetName
  148                   b3.use NetSetVLan
  149                   b3.use NetSetMac
  150                 end
  151 
  152                 b3.use CleanupDisks
  153                 b3.use Disk
  154                 b3.use SyncedFolderCleanup
  155                 b3.use StartInstance
  156                 b3.use WaitForIPAddress
  157                 b3.use WaitForCommunicator, [:running]
  158                 b3.use SyncedFolders
  159                 b3.use SetHostname
  160               end
  161             end
  162           end
  163         end
  164       end
  165 
  166       def self.action_up
  167         Vagrant::Action::Builder.new.tap do |b|
  168           b.use CheckEnabled
  169           b.use CheckAccess
  170           b.use HandleBox
  171           b.use ConfigValidate
  172           b.use Call, IsState, :not_created do |env1, b1|
  173             if env1[:result]
  174               b1.use Import
  175             end
  176 
  177             b1.use action_start
  178           end
  179         end
  180       end
  181 
  182       def self.action_read_state
  183         Vagrant::Action::Builder.new.tap do |b|
  184           b.use ConfigValidate
  185           b.use ReadState
  186         end
  187       end
  188 
  189       def self.action_ssh
  190         Vagrant::Action::Builder.new.tap do |b|
  191           b.use ConfigValidate
  192           b.use Call, IsState, :not_created do |env, b2|
  193             if env[:result]
  194               raise Vagrant::Errors::VMNotCreatedError
  195             end
  196 
  197             b2.use Call, IsState, :running do |env1, b3|
  198               if !env1[:result]
  199                 raise Vagrant::Errors::VMNotRunningError
  200               end
  201 
  202               b3.use SSHExec
  203             end
  204           end
  205         end
  206       end
  207 
  208       def self.action_ssh_run
  209         Vagrant::Action::Builder.new.tap do |b|
  210           b.use ConfigValidate
  211           b.use Call, IsState, :not_created do |env, b2|
  212             if env[:result]
  213               raise Vagrant::Errors::VMNotCreatedError
  214             end
  215 
  216             b2.use Call, IsState, :running do |env1, b3|
  217               if !env1[:result]
  218                 raise Vagrant::Errors::VMNotRunningError
  219               end
  220 
  221               b3.use SSHRun
  222             end
  223           end
  224         end
  225       end
  226 
  227       def self.action_suspend
  228         Vagrant::Action::Builder.new.tap do |b|
  229           b.use ConfigValidate
  230           b.use Call, IsState, :not_created do |env, b2|
  231             if env[:result]
  232               b2.use Message, I18n.t("vagrant_hyperv.message_not_created")
  233               next
  234             end
  235 
  236             b2.use SuspendVM
  237           end
  238         end
  239       end
  240 
  241       def self.action_snapshot_delete
  242         Vagrant::Action::Builder.new.tap do |b|
  243           b.use ConfigValidate
  244           b.use Call, IsState, :not_created do |env, b2|
  245             if env[:result]
  246               b2.use Message, I18n.t("vagrant_hyperv.message_not_created")
  247               next
  248             end
  249 
  250             b2.use SnapshotDelete
  251 
  252           end
  253         end
  254       end
  255 
  256       def self.action_snapshot_restore
  257         Vagrant::Action::Builder.new.tap do |b|
  258           b.use ConfigValidate
  259           b.use Call, IsState, :not_created do |env, b2|
  260             if env[:result]
  261               b2.use Message, I18n.t("vagrant_hyperv.message_not_created")
  262               next
  263             end
  264 
  265             b2.use action_halt
  266             b2.use SnapshotRestore
  267 
  268             b2.use Call, IsEnvSet, :snapshot_delete do |env2, b3|
  269               if env2[:result]
  270                 b3.use action_snapshot_delete
  271               end
  272             end
  273 
  274             b2.use action_start
  275 
  276           end
  277         end
  278       end
  279 
  280       def self.action_snapshot_save
  281         Vagrant::Action::Builder.new.tap do |b|
  282           b.use ConfigValidate
  283           b.use Call, IsState, :not_created do |env, b2|
  284             if env[:result]
  285               b2.use Message, I18n.t("vagrant_hyperv.message_not_created")
  286               next
  287             end
  288             b2.use SnapshotSave
  289           end
  290         end
  291       end
  292 
  293       # The autoload farm
  294       action_root = Pathname.new(File.expand_path("../action", __FILE__))
  295       autoload :PackageSetupFolders, action_root.join("package_setup_folders")
  296       autoload :PackageSetupFiles, action_root.join("package_setup_files")
  297       autoload :PackageVagrantfile, action_root.join("package_vagrantfile")
  298       autoload :PackageMetadataJson, action_root.join("package_metadata_json")
  299       autoload :Export, action_root.join("export")
  300 
  301       autoload :CheckEnabled, action_root.join("check_enabled")
  302       autoload :CheckAccess, action_root.join("check_access")
  303       autoload :Configure, action_root.join("configure")
  304       autoload :DeleteVM, action_root.join("delete_vm")
  305       autoload :Import, action_root.join("import")
  306       autoload :Package, action_root.join("package")
  307       autoload :IsWindows, action_root.join("is_windows")
  308       autoload :ReadState, action_root.join("read_state")
  309       autoload :ResumeVM, action_root.join("resume_vm")
  310       autoload :StartInstance, action_root.join('start_instance')
  311       autoload :StopInstance, action_root.join('stop_instance')
  312       autoload :SuspendVM, action_root.join("suspend_vm")
  313       autoload :WaitForIPAddress, action_root.join("wait_for_ip_address")
  314       autoload :NetSetVLan, action_root.join("net_set_vlan")
  315       autoload :NetSetMac, action_root.join("net_set_mac")
  316       autoload :MessageWillNotDestroy, action_root.join("message_will_not_destroy")
  317       autoload :SnapshotDelete, action_root.join("snapshot_delete")
  318       autoload :SnapshotRestore, action_root.join("snapshot_restore")
  319       autoload :SnapshotSave, action_root.join("snapshot_save")
  320       autoload :SetName, action_root.join("set_name")
  321     end
  322   end
  323 end