Sha256: bdf637cf0bc15812f31c5140db1dac8ecc3d199ff51082b359c611769d845ae1

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

module VagrantPlugins
  module Exec
    class Command < Vagrant.plugin(2, :command)

      def execute
        cmd, cmd_args = parse_args
        cmd && cmd_args or return nil

        # Execute the actual SSH
        with_target_vms(nil, single_target: true) do |vm|
          vm.config.exec.finalize! # TODO: do we have to call it explicitly?

          plain = "#{cmd} " << cmd_args.join(' ')

          command = "source ~/.profile && "
          command << "cd #{vm.config.exec.folder} && "
          command << add_env(vm.config.exec.env)
          command << add_bundler(vm.config.exec.bundler, plain)
          command << plain

          @logger.info("Executing single command on remote machine: #{command}")
          env = vm.action(:ssh_run, ssh_run_command: command)

          status = env[:ssh_run_exit_status] || 0
          return status
        end
      end

      private

      def parse_args
        opts = OptionParser.new do |o|
          o.banner = 'Usage: vagrant exec [options] <command>'
          o.separator ''

          o.on('-h', '--help', 'Print this help') do
            safe_puts(opts.help)
          end
        end

        argv = split_main_and_subcommand(@argv.dup)
        exec_args, cmd, cmd_args = argv[0], argv[1], argv[2]

        # show help
        if !cmd || exec_args.any? { |a| a == '-h' || a == '--help' }
          safe_puts(opts.help)
          return nil
        end

        return cmd, cmd_args
      end

      def add_env(env)
        ''.tap do |command|
          if env.any?
            env.each do |key, value|
              command << "export #{key}=#{value} && "
            end
          end
        end
      end

      def add_bundler(bundler, plain_command)
        ''.tap do |command|
          if bundler && plain_command !~ /^bundle /
            command << 'bundle exec '
          end
        end
      end

    end # Command
  end # Exec
end # VagrantPlugins

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vagrant-exec-0.2.1 lib/vagrant-exec/command.rb