Class: Kitchen::Provisioner::Shell

Inherits:
Base
  • Object
show all
Defined in:
lib/kitchen/provisioner/shell.rb

Overview

Basic shell provisioner.

Author:

Instance Attribute Summary

Attributes included from Configurable

#instance

Instance Method Summary collapse

Methods inherited from Base

#call, #check_license, #cleanup_sandbox, #doctor, #initialize, #install_command, kitchen_provisioner_api_version, #sandbox_path

Methods included from Logging

#banner, #debug, #error, #fatal, #info, #warn

Methods included from Configurable

#[], #bourne_shell?, #calculate_path, #config_keys, #diagnose, #diagnose_plugin, #finalize_config!, included, #name, #powershell_shell?, #remote_path_join, #unix_os?, #verify_dependencies, #windows_os?

Constructor Details

This class inherits a constructor from Kitchen::Provisioner::Base

Instance Method Details

#create_sandboxObject

Creates a temporary directory on the local workstation into which provisioner related files and directories can be copied or created. The contents of this directory will be copied over to the instance before invoking the provisioner's run command. After this method completes, it is expected that the contents of the sandbox is complete and ready for copy to the remote instance.

*Note:* any subclasses would be well advised to call super first when overriding this method, for example:

Examples:

overriding `#create_sandbox`


class MyProvisioner < Kitchen::Provisioner::Base
  def create_sandbox
    super
    # any further file copies, preparations, etc.
  end
end

51
52
53
54
55
# File 'lib/kitchen/provisioner/shell.rb', line 51

def create_sandbox
  super
  prepare_data
  prepare_script
end

#init_commandString

Generates a command string which will perform any data initialization or configuration required after the provisioner software is installed but before the sandbox has been transferred to the instance. If no work is required, then `nil` will be returned.

Returns:

  • (String)

    a command string


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/kitchen/provisioner/shell.rb', line 58

def init_command
  return nil if config[:command]

  root = config[:root_path]
  data = remote_path_join(root, "data")

  code = if powershell_shell?
           Util.outdent!(<<-POWERSHELL)
      if (Test-Path "#{data}") {
        Remove-Item "#{data}" -Recurse -Force
      }
      if (-Not (Test-Path "#{root}")) {
        New-Item "#{root}" -ItemType directory | Out-Null
      }
           POWERSHELL
         else
           "#{sudo("rm")} -rf #{data} ; mkdir -p #{root}"
         end

  prefix_command(wrap_shell_code(code))
end

#prepare_commandString

Generates a command string which will perform any commands or configuration required just before the main provisioner run command but after the sandbox has been transferred to the instance. If no work is required, then `nil` will be returned.

Returns:

  • (String)

    a command string


81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/kitchen/provisioner/shell.rb', line 81

def prepare_command
  # On a windows host, the supplied script does not get marked as executable
  # due to windows not having the concept of an executable flag
  #
  # When the guest instance is *nix, `chmod +x` the script in the guest, prior to executing
  return unless unix_os? && config[:script] && !config[:command]

  debug "Marking script as executable"
  script = remote_path_join(
    config[:root_path],
    File.basename(config[:script])
  )
  prefix_command(wrap_shell_code(sudo("chmod +x #{script}")))
end

#run_commandString

Generates a command string which will invoke the main provisioner command on the prepared instance. If no work is required, then `nil` will be returned.

Returns:

  • (String)

    a command string


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/kitchen/provisioner/shell.rb', line 97

def run_command
  return prefix_command(wrap_shell_code(config[:command])) if config[:command]
  return unless config[:script]

  script = remote_path_join(
    config[:root_path],
    File.basename(config[:script])
  )

  if config[:arguments] && !config[:arguments].empty?
    if config[:arguments].is_a?(Array)
      if powershell_shell?
        script = ([script] + config[:arguments]).join(" ")
      else
        script = Shellwords.join([script] + config[:arguments])
      end
    else
      script.concat(" ").concat(config[:arguments].to_s)
    end
  end

  code = powershell_shell? ? %{& #{script}} : sudo(script)

  prefix_command(wrap_shell_code(code))
end