"Fossies" - the Fresh Open Source Software Archive

Member "vagrant-2.2.14/plugins/providers/docker/action.rb" (20 Nov 2020, 11235 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 module VagrantPlugins
    2   module DockerProvider
    3     module Action
    4       # Include the built-in modules so we can use them as top-level things.
    5       include Vagrant::Action::Builtin
    6 
    7       # This action starts another container just like the real one running
    8       # but only for the purpose of running a single command rather than
    9       # to exist long-running.
   10       def self.action_run_command
   11         Vagrant::Action::Builder.new.tap do |b|
   12           # We just call the "up" action. We create a separate action
   13           # to hold this though in case we modify it in the future, and
   14           # so that we can switch on the "machine_action" env var.
   15           b.use action_up
   16         end
   17       end
   18 
   19       # This action brings the "machine" up from nothing, including creating the
   20       # container, configuring metadata, and booting.
   21       def self.action_up
   22         Vagrant::Action::Builder.new.tap do |b|
   23           b.use Call, IsState, :not_created do |env, b2|
   24             if env[:result]
   25               b2.use HandleBox
   26             end
   27           end
   28 
   29           b.use ConfigValidate
   30           b.use HostMachine
   31 
   32           # Yeah, this is supposed to be here twice (once more above). This
   33           # catches the case when the container was supposed to be created,
   34           # but the host state was unknown, and now we know its not actually
   35           # created.
   36           b.use Call, IsState, :not_created do |env, b2|
   37             if env[:result]
   38               b2.use HandleBox
   39               b2.use DestroyBuildImage
   40             end
   41           end
   42 
   43           b.use action_start
   44         end
   45       end
   46 
   47       def self.action_package
   48         lambda do |env|
   49           raise Errors::PackageNotSupported
   50         end
   51       end
   52 
   53       # This action just runs the provisioners on the machine.
   54       def self.action_provision
   55         Vagrant::Action::Builder.new.tap do |b|
   56           b.use ConfigValidate
   57           b.use Call, IsState, :not_created do |env, b2|
   58             if env[:result]
   59               b2.use Message, I18n.t("docker_provider.messages.not_created")
   60               next
   61             end
   62 
   63             b2.use Call, IsState, :running do |env2, b3|
   64               if !env2[:result]
   65                 b3.use Message, I18n.t("docker_provider.messages.not_running")
   66                 next
   67               end
   68 
   69               b3.use Call, HasProvisioner do |env3, b4|
   70                 b4.use Provision
   71               end
   72             end
   73           end
   74         end
   75       end
   76 
   77       # This is the action that is primarily responsible for halting
   78       # the virtual machine, gracefully or by force.
   79       def self.action_halt
   80         Vagrant::Action::Builder.new.tap do |b|
   81           b.use Call, IsState, :host_state_unknown do |env, b2|
   82             if env[:result]
   83               b2.use HostMachine
   84             end
   85           end
   86 
   87           b.use Call, IsState, :not_created do |env, b2|
   88             if env[:result]
   89               b2.use Message, I18n.t("docker_provider.messages.not_created")
   90               next
   91             end
   92 
   93             b2.use Stop
   94           end
   95         end
   96       end
   97 
   98       # This action is responsible for reloading the machine, which
   99       # brings it down, sucks in new configuration, and brings the
  100       # machine back up with the new configuration.
  101       def self.action_reload
  102         Vagrant::Action::Builder.new.tap do |b|
  103           b.use ConfigValidate
  104           b.use Call, IsState, :not_created do |env, b2|
  105             if env[:result]
  106               b2.use Message, I18n.t("docker_provider.messages.not_created")
  107               next
  108             end
  109 
  110             b2.use action_halt
  111 
  112             b2.use Call, IsBuild do |env2, b3|
  113               if env2[:result]
  114                 b3.use EnvSet, force_halt: true
  115                 b3.use action_halt
  116                 b3.use HostMachineSyncFoldersDisable
  117                 b3.use Destroy
  118                 b3.use ProvisionerCleanup
  119               end
  120             end
  121 
  122             b2.use action_start
  123           end
  124         end
  125       end
  126 
  127       # This is the action that is primarily responsible for completely
  128       # freeing the resources of the underlying virtual machine.
  129       def self.action_destroy
  130         Vagrant::Action::Builder.new.tap do |b|
  131           b.use Call, IsHostMachineCreated do |env, b2|
  132             if !env[:result]
  133               b2.use Message, I18n.t("docker_provider.messages.not_created")
  134               next
  135             end
  136 
  137             b2.use Call, IsState, :host_state_unknown do |env2, b3|
  138               if env2[:result]
  139                 b3.use HostMachine
  140               end
  141             end
  142 
  143             b2.use Call, IsState, :not_created do |env2, b3|
  144               if env2[:result]
  145                 b3.use Message,
  146                   I18n.t("docker_provider.messages.not_created")
  147                 next
  148               end
  149 
  150               b3.use Call, DestroyConfirm do |env3, b4|
  151                 if env3[:result]
  152                   b4.use ConfigValidate
  153                   b4.use ProvisionerCleanup, :before
  154                   b4.use EnvSet, force_halt: true
  155                   b4.use action_halt
  156                   b4.use HostMachineSyncFoldersDisable
  157                   b4.use Destroy
  158                   b4.use DestroyNetwork
  159                   b4.use DestroyBuildImage
  160                 else
  161                   b4.use Message,
  162                     I18n.t("docker_provider.messages.will_not_destroy")
  163                 end
  164               end
  165             end
  166           end
  167         end
  168       end
  169 
  170       # This is the action that will exec into an SSH shell.
  171       def self.action_ssh
  172         Vagrant::Action::Builder.new.tap do |b|
  173           b.use Call, IsState, :not_created do |env, b2|
  174             if env[:result]
  175               raise Errors::ContainerNotCreatedError
  176             end
  177 
  178             b2.use Call, IsState, :running do |env2, b3|
  179               if !env2[:result]
  180                 raise Errors::ContainerNotRunningError
  181               end
  182 
  183               b3.use PrepareSSH
  184               b3.use SSHExec
  185             end
  186           end
  187         end
  188       end
  189 
  190       # This is the action that will run a single SSH command.
  191       def self.action_ssh_run
  192         Vagrant::Action::Builder.new.tap do |b|
  193           b.use Call, IsState, :not_created do |env, b2|
  194             if env[:result]
  195               raise Errors::ContainerNotCreatedError
  196             end
  197 
  198             b2.use Call, IsState, :running do |env2, b3|
  199               if !env2[:result]
  200                 raise Errors::ContainerNotRunningError
  201               end
  202 
  203               b3.use SSHRun
  204             end
  205           end
  206         end
  207       end
  208 
  209       def self.action_start
  210         Vagrant::Action::Builder.new.tap do |b|
  211           b.use Call, IsState, :running do |env, b2|
  212             if env[:machine_action] != :run_command
  213               b2.use Call, HasProvisioner do |env2, b3|
  214                 b3.use Provision
  215               end
  216             end
  217 
  218             # If the container is running and we're doing a run, we're done
  219             next if env[:result] && env[:machine_action] != :run_command
  220 
  221             b2.use Call, IsState, :not_created do |env2, b3|
  222               if env2[:result]
  223                 # First time making this thing, set to the "preparing" state
  224                 b3.use InitState
  225               else
  226                 b3.use EnvSet, host_machine_sync_folders: false
  227               end
  228             end
  229 
  230             b2.use HostMachineBuildDir
  231             b2.use HostMachineSyncFolders
  232             b2.use PrepareNFSValidIds
  233             b2.use SyncedFolderCleanup
  234             b2.use PrepareNFSSettings
  235             b2.use PrepareNetworks
  236             b2.use Login
  237             b2.use Build
  238 
  239             if env[:machine_action] != :run_command
  240               # If the container is NOT created yet, then do some setup steps
  241               # necessary for creating it.
  242 
  243               b2.use Call, IsState, :preparing do |env2, b3|
  244                 if env2[:result]
  245                   b3.use EnvSet, port_collision_repair: true
  246                   b3.use HostMachinePortWarning
  247                   b3.use HostMachinePortChecker
  248                   b3.use ForwardedPorts # This action converts the `ports` param into proper network configs
  249                   b3.use PrepareForwardedPortCollisionParams
  250                   b3.use HandleForwardedPortCollisions
  251                   b3.use SyncedFolders
  252                   b3.use Pull
  253                   b3.use Create
  254                   b3.use WaitForRunning
  255                 else
  256                   b3.use CompareSyncedFolders
  257                 end
  258               end
  259 
  260               b2.use ConnectNetworks
  261               b2.use Start
  262               b2.use WaitForRunning
  263 
  264               b2.use Call, HasSSH do |env2, b3|
  265                 if env2[:result]
  266                   b3.use WaitForCommunicator
  267                 end
  268               end
  269             else
  270               # We're in a run command, so we do things a bit differently.
  271               b2.use SyncedFolders
  272               b2.use Create
  273             end
  274           end
  275         end
  276       end
  277 
  278       def self.action_suspend
  279         lambda do |env|
  280           raise Errors::SuspendNotSupported
  281         end
  282       end
  283 
  284       # The autoload farm
  285       action_root = Pathname.new(File.expand_path("../action", __FILE__))
  286       autoload :Build, action_root.join("build")
  287       autoload :CompareSyncedFolders, action_root.join("compare_synced_folders")
  288       autoload :ConnectNetworks, action_root.join("connect_networks")
  289       autoload :Create, action_root.join("create")
  290       autoload :Destroy, action_root.join("destroy")
  291       autoload :DestroyBuildImage, action_root.join("destroy_build_image")
  292       autoload :DestroyNetwork, action_root.join("destroy_network")
  293       autoload :ForwardedPorts, action_root.join("forwarded_ports")
  294       autoload :HasSSH, action_root.join("has_ssh")
  295       autoload :HostMachine, action_root.join("host_machine")
  296       autoload :HostMachineBuildDir, action_root.join("host_machine_build_dir")
  297       autoload :HostMachinePortChecker, action_root.join("host_machine_port_checker")
  298       autoload :HostMachinePortWarning, action_root.join("host_machine_port_warning")
  299       autoload :HostMachineRequired, action_root.join("host_machine_required")
  300       autoload :HostMachineSyncFolders, action_root.join("host_machine_sync_folders")
  301       autoload :HostMachineSyncFoldersDisable, action_root.join("host_machine_sync_folders_disable")
  302       autoload :InitState, action_root.join("init_state")
  303       autoload :IsBuild, action_root.join("is_build")
  304       autoload :IsHostMachineCreated, action_root.join("is_host_machine_created")
  305       autoload :Login, action_root.join("login")
  306       autoload :PrepareForwardedPortCollisionParams, action_root.join("prepare_forwarded_port_collision_params")
  307       autoload :PrepareNetworks, action_root.join("prepare_networks")
  308       autoload :PrepareNFSValidIds, action_root.join("prepare_nfs_valid_ids")
  309       autoload :PrepareNFSSettings, action_root.join("prepare_nfs_settings")
  310       autoload :PrepareSSH, action_root.join("prepare_ssh")
  311       autoload :Pull, action_root.join("pull")
  312       autoload :Start, action_root.join("start")
  313       autoload :Stop, action_root.join("stop")
  314       autoload :WaitForRunning, action_root.join("wait_for_running")
  315     end
  316   end
  317 end