"Fossies" - the Fresh Open Source Software Archive

Member "vagrant-2.2.14/plugins/providers/virtualbox/action.rb" (20 Nov 2020, 15658 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.9_vs_2.2.10.

    1 require "vagrant/action/builder"
    2 
    3 module VagrantPlugins
    4   module ProviderVirtualBox
    5     module Action
    6       autoload :Boot, File.expand_path("../action/boot", __FILE__)
    7       autoload :CheckAccessible, File.expand_path("../action/check_accessible", __FILE__)
    8       autoload :CheckCreated, File.expand_path("../action/check_created", __FILE__)
    9       autoload :CheckGuestAdditions, File.expand_path("../action/check_guest_additions", __FILE__)
   10       autoload :CheckRunning, File.expand_path("../action/check_running", __FILE__)
   11       autoload :CheckVirtualbox, File.expand_path("../action/check_virtualbox", __FILE__)
   12       autoload :CleanMachineFolder, File.expand_path("../action/clean_machine_folder", __FILE__)
   13       autoload :ClearForwardedPorts, File.expand_path("../action/clear_forwarded_ports", __FILE__)
   14       autoload :ClearNetworkInterfaces, File.expand_path("../action/clear_network_interfaces", __FILE__)
   15       autoload :Created, File.expand_path("../action/created", __FILE__)
   16       autoload :Customize, File.expand_path("../action/customize", __FILE__)
   17       autoload :Destroy, File.expand_path("../action/destroy", __FILE__)
   18       autoload :DestroyUnusedNetworkInterfaces, File.expand_path("../action/destroy_unused_network_interfaces", __FILE__)
   19       autoload :DiscardState, File.expand_path("../action/discard_state", __FILE__)
   20       autoload :Export, File.expand_path("../action/export", __FILE__)
   21       autoload :ForcedHalt, File.expand_path("../action/forced_halt", __FILE__)
   22       autoload :ForwardPorts, File.expand_path("../action/forward_ports", __FILE__)
   23       autoload :Import, File.expand_path("../action/import", __FILE__)
   24       autoload :ImportMaster, File.expand_path("../action/import_master", __FILE__)
   25       autoload :IsPaused, File.expand_path("../action/is_paused", __FILE__)
   26       autoload :IsRunning, File.expand_path("../action/is_running", __FILE__)
   27       autoload :IsSaved, File.expand_path("../action/is_saved", __FILE__)
   28       autoload :MatchMACAddress, File.expand_path("../action/match_mac_address", __FILE__)
   29       autoload :MessageAlreadyRunning, File.expand_path("../action/message_already_running", __FILE__)
   30       autoload :MessageNotCreated, File.expand_path("../action/message_not_created", __FILE__)
   31       autoload :MessageNotRunning, File.expand_path("../action/message_not_running", __FILE__)
   32       autoload :MessageWillNotDestroy, File.expand_path("../action/message_will_not_destroy", __FILE__)
   33       autoload :Network, File.expand_path("../action/network", __FILE__)
   34       autoload :NetworkFixIPv6, File.expand_path("../action/network_fix_ipv6", __FILE__)
   35       autoload :Package, File.expand_path("../action/package", __FILE__)
   36       autoload :PackageSetupFiles, File.expand_path("../action/package_setup_files", __FILE__)
   37       autoload :PackageSetupFolders, File.expand_path("../action/package_setup_folders", __FILE__)
   38       autoload :PackageVagrantfile, File.expand_path("../action/package_vagrantfile", __FILE__)
   39       autoload :PrepareCloneSnapshot, File.expand_path("../action/prepare_clone_snapshot", __FILE__)
   40       autoload :PrepareNFSSettings, File.expand_path("../action/prepare_nfs_settings", __FILE__)
   41       autoload :PrepareNFSValidIds, File.expand_path("../action/prepare_nfs_valid_ids", __FILE__)
   42       autoload :PrepareForwardedPortCollisionParams, File.expand_path("../action/prepare_forwarded_port_collision_params", __FILE__)
   43       autoload :Resume, File.expand_path("../action/resume", __FILE__)
   44       autoload :SaneDefaults, File.expand_path("../action/sane_defaults", __FILE__)
   45       autoload :SetDefaultNICType, File.expand_path("../action/set_default_nic_type", __FILE__)
   46       autoload :SetName, File.expand_path("../action/set_name", __FILE__)
   47       autoload :SnapshotDelete, File.expand_path("../action/snapshot_delete", __FILE__)
   48       autoload :SnapshotRestore, File.expand_path("../action/snapshot_restore", __FILE__)
   49       autoload :SnapshotSave, File.expand_path("../action/snapshot_save", __FILE__)
   50       autoload :Suspend, File.expand_path("../action/suspend", __FILE__)
   51 
   52       # @deprecated use {PackageSetupFiles} instead
   53       autoload :SetupPackageFiles, File.expand_path("../action/setup_package_files", __FILE__)
   54 
   55       # Include the built-in modules so that we can use them as top-level
   56       # things.
   57       include Vagrant::Action::Builtin
   58 
   59       # This action boots the VM, assuming the VM is in a state that requires
   60       # a bootup (i.e. not saved).
   61       def self.action_boot
   62         Vagrant::Action::Builder.new.tap do |b|
   63           b.use CheckAccessible
   64           b.use CleanMachineFolder
   65           b.use SetName
   66           b.use ClearForwardedPorts
   67           b.use Provision
   68           b.use EnvSet, port_collision_repair: true
   69           b.use PrepareForwardedPortCollisionParams
   70           b.use HandleForwardedPortCollisions
   71           b.use PrepareNFSValidIds
   72           b.use SyncedFolderCleanup
   73           b.use SyncedFolders
   74           b.use PrepareNFSSettings
   75           b.use SetDefaultNICType
   76           b.use ClearNetworkInterfaces
   77           b.use Network
   78           b.use NetworkFixIPv6
   79           b.use ForwardPorts
   80           b.use SetHostname
   81           b.use SaneDefaults
   82           b.use Call, IsEnvSet, :cloud_init do |env, b2|
   83             if env[:result]
   84               b2.use CloudInitSetup
   85             end
   86           end
   87           b.use CleanupDisks
   88           b.use Disk
   89           b.use Customize, "pre-boot"
   90           b.use Boot
   91           b.use Customize, "post-boot"
   92           b.use WaitForCommunicator, [:starting, :running]
   93           b.use Call, IsEnvSet, :cloud_init do |env, b2|
   94             if env[:result]
   95               b2.use CloudInitWait
   96             end
   97           end
   98           b.use Customize, "post-comm"
   99           b.use CheckGuestAdditions
  100         end
  101       end
  102 
  103       # This is the action that is primarily responsible for completely
  104       # freeing the resources of the underlying virtual machine.
  105       def self.action_destroy
  106         Vagrant::Action::Builder.new.tap do |b|
  107           b.use CheckVirtualbox
  108           b.use Call, Created do |env1, b2|
  109             if !env1[:result]
  110               b2.use MessageNotCreated
  111               next
  112             end
  113 
  114             b2.use Call, DestroyConfirm do |env2, b3|
  115               if env2[:result]
  116                 b3.use ConfigValidate
  117                 b3.use ProvisionerCleanup, :before
  118                 b3.use CheckAccessible
  119                 b3.use EnvSet, force_halt: env2[:force_halt]
  120                 b3.use action_halt
  121                 b3.use Destroy
  122                 b3.use CleanMachineFolder
  123                 b3.use DestroyUnusedNetworkInterfaces
  124                 b3.use PrepareNFSValidIds
  125                 b3.use SyncedFolderCleanup
  126               else
  127                 b3.use MessageWillNotDestroy
  128               end
  129             end
  130           end
  131         end
  132       end
  133 
  134       # This is the action that is primarily responsible for halting
  135       # the virtual machine, gracefully or by force.
  136       def self.action_halt
  137         Vagrant::Action::Builder.new.tap do |b|
  138           b.use CheckVirtualbox
  139           b.use Call, Created do |env, b2|
  140             if env[:result]
  141               b2.use CheckAccessible
  142               b2.use DiscardState
  143 
  144               b2.use Call, IsPaused do |env2, b3|
  145                 next if !env2[:result]
  146                 b3.use Resume
  147               end
  148 
  149               b2.use Call, GracefulHalt, :poweroff, :running do |env2, b3|
  150                 if !env2[:result]
  151                   b3.use ForcedHalt
  152                 end
  153               end
  154             else
  155               b2.use MessageNotCreated
  156             end
  157           end
  158         end
  159       end
  160 
  161       # This action packages the virtual machine into a single box file.
  162       def self.action_package
  163         Vagrant::Action::Builder.new.tap do |b|
  164           b.use CheckVirtualbox
  165           b.use Call, Created do |env1, b2|
  166             if !env1[:result]
  167               b2.use MessageNotCreated
  168               next
  169             end
  170 
  171             b2.use PackageSetupFolders
  172             b2.use PackageSetupFiles
  173             b2.use CheckAccessible
  174             b2.use action_halt
  175             b2.use ClearForwardedPorts
  176             b2.use PrepareNFSValidIds
  177             b2.use SyncedFolderCleanup
  178             b2.use Package
  179             b2.use Export
  180             b2.use PackageVagrantfile
  181           end
  182         end
  183       end
  184 
  185       # This action just runs the provisioners on the machine.
  186       def self.action_provision
  187         Vagrant::Action::Builder.new.tap do |b|
  188           b.use CheckVirtualbox
  189           b.use ConfigValidate
  190           b.use Call, Created do |env1, b2|
  191             if !env1[:result]
  192               b2.use MessageNotCreated
  193               next
  194             end
  195 
  196             b2.use Call, IsRunning do |env2, b3|
  197               if !env2[:result]
  198                 b3.use MessageNotRunning
  199                 next
  200               end
  201 
  202               b3.use CheckAccessible
  203               b3.use Provision
  204             end
  205           end
  206         end
  207       end
  208 
  209       # This action is responsible for reloading the machine, which
  210       # brings it down, sucks in new configuration, and brings the
  211       # machine back up with the new configuration.
  212       def self.action_reload
  213         Vagrant::Action::Builder.new.tap do |b|
  214           b.use CheckVirtualbox
  215           b.use Call, Created do |env1, b2|
  216             if !env1[:result]
  217               b2.use MessageNotCreated
  218               next
  219             end
  220 
  221             b2.use ConfigValidate
  222             b2.use action_halt
  223             b2.use action_start
  224           end
  225         end
  226       end
  227 
  228       # This is the action that is primarily responsible for resuming
  229       # suspended machines.
  230       def self.action_resume
  231         Vagrant::Action::Builder.new.tap do |b|
  232           b.use CheckVirtualbox
  233           b.use Call, Created do |env, b2|
  234             if env[:result]
  235               b2.use CheckAccessible
  236               b2.use EnvSet, port_collision_repair: false
  237               b2.use PrepareForwardedPortCollisionParams
  238               b2.use HandleForwardedPortCollisions
  239               b2.use Resume
  240               b2.use Provision
  241               b2.use WaitForCommunicator, [:restoring, :running]
  242             else
  243               b2.use MessageNotCreated
  244             end
  245           end
  246         end
  247       end
  248 
  249       def self.action_snapshot_delete
  250         Vagrant::Action::Builder.new.tap do |b|
  251           b.use CheckVirtualbox
  252           b.use Call, Created do |env, b2|
  253             if env[:result]
  254               b2.use SnapshotDelete
  255             else
  256               b2.use MessageNotCreated
  257             end
  258           end
  259         end
  260       end
  261 
  262       # This is the action that is primarily responsible for restoring a snapshot
  263       def self.action_snapshot_restore
  264         Vagrant::Action::Builder.new.tap do |b|
  265           b.use CheckVirtualbox
  266           b.use Call, Created do |env, b2|
  267             if !env[:result]
  268               raise Vagrant::Errors::VMNotCreatedError
  269             end
  270 
  271             b2.use CheckAccessible
  272             b2.use EnvSet, force_halt: true
  273             b2.use action_halt
  274             b2.use SnapshotRestore
  275 
  276             b2.use Call, IsEnvSet, :snapshot_delete do |env2, b3|
  277               if env2[:result]
  278                 b3.use action_snapshot_delete
  279               end
  280             end
  281 
  282             b2.use Call, IsEnvSet, :snapshot_start do |env2, b3|
  283               if env2[:result]
  284                 b3.use action_start
  285               end
  286             end
  287           end
  288         end
  289       end
  290 
  291       # This is the action that is primarily responsible for saving a snapshot
  292       def self.action_snapshot_save
  293         Vagrant::Action::Builder.new.tap do |b|
  294           b.use CheckVirtualbox
  295           b.use Call, Created do |env, b2|
  296             if env[:result]
  297               b2.use SnapshotSave
  298             else
  299               b2.use MessageNotCreated
  300             end
  301           end
  302         end
  303       end
  304 
  305       # This is the action that will exec into an SSH shell.
  306       def self.action_ssh
  307         Vagrant::Action::Builder.new.tap do |b|
  308           b.use CheckVirtualbox
  309           b.use CheckCreated
  310           b.use CheckAccessible
  311           b.use CheckRunning
  312           b.use SSHExec
  313         end
  314       end
  315 
  316       # This is the action that will run a single SSH command.
  317       def self.action_ssh_run
  318         Vagrant::Action::Builder.new.tap do |b|
  319           b.use CheckVirtualbox
  320           b.use CheckCreated
  321           b.use CheckAccessible
  322           b.use CheckRunning
  323           b.use SSHRun
  324         end
  325       end
  326 
  327       # This action starts a VM, assuming it is already imported and exists.
  328       # A precondition of this action is that the VM exists.
  329       def self.action_start
  330         Vagrant::Action::Builder.new.tap do |b|
  331           b.use CheckVirtualbox
  332           b.use ConfigValidate
  333           b.use BoxCheckOutdated
  334           b.use Call, IsRunning do |env, b2|
  335             # If the VM is running, run the necessary provisioners
  336             if env[:result]
  337               b2.use action_provision
  338               next
  339             end
  340 
  341             b2.use Call, IsSaved do |env2, b3|
  342               if env2[:result]
  343                 # The VM is saved, so just resume it
  344                 b3.use action_resume
  345                 next
  346               end
  347 
  348               b3.use Call, IsPaused do |env3, b4|
  349                 if env3[:result]
  350                   b4.use Resume
  351                   next
  352                 end
  353 
  354                 # The VM is not saved, so we must have to boot it up
  355                 # like normal. Boot!
  356                 b4.use action_boot
  357               end
  358             end
  359           end
  360         end
  361       end
  362 
  363       # This is the action that is primarily responsible for suspending
  364       # the virtual machine.
  365       def self.action_suspend
  366         Vagrant::Action::Builder.new.tap do |b|
  367           b.use CheckVirtualbox
  368           b.use Call, Created do |env, b2|
  369             if env[:result]
  370               b2.use CheckAccessible
  371               b2.use Suspend
  372             else
  373               b2.use MessageNotCreated
  374             end
  375           end
  376         end
  377       end
  378 
  379       # This is the action that is called to sync folders to a running
  380       # machine without a reboot.
  381       def self.action_sync_folders
  382         Vagrant::Action::Builder.new.tap do |b|
  383           b.use PrepareNFSValidIds
  384           b.use SyncedFolders
  385           b.use PrepareNFSSettings
  386         end
  387       end
  388 
  389       # This action brings the machine up from nothing, including importing
  390       # the box, configuring metadata, and booting.
  391       def self.action_up
  392         Vagrant::Action::Builder.new.tap do |b|
  393           b.use CheckVirtualbox
  394 
  395           # Handle box_url downloading early so that if the Vagrantfile
  396           # references any files in the box or something it all just
  397           # works fine.
  398           b.use Call, Created do |env, b2|
  399             if !env[:result]
  400               b2.use HandleBox
  401             end
  402           end
  403 
  404           b.use ConfigValidate
  405           b.use Call, Created do |env, b2|
  406             # If the VM is NOT created yet, then do the setup steps
  407             if !env[:result]
  408               b2.use CheckAccessible
  409               b2.use Customize, "pre-import"
  410 
  411               if env[:machine].provider_config.linked_clone
  412                 # We are cloning from the box
  413                 b2.use ImportMaster
  414               end
  415 
  416               b2.use PrepareClone
  417               b2.use PrepareCloneSnapshot
  418               b2.use Import
  419               b2.use DiscardState
  420               b2.use MatchMACAddress
  421             end
  422           end
  423 
  424           b.use EnvSet, cloud_init: true
  425           b.use action_start
  426         end
  427       end
  428     end
  429   end
  430 end