Class: Kitchen::Provisioner::Yansible

Inherits:
Base
  • Object
show all
Includes:
Yansible::Tools, Yansible::Tools::Dependencies, Yansible::Tools::Exec, Yansible::Tools::Files
Defined in:
lib/kitchen/provisioner/yansible.rb

Constant Summary collapse

DEFAULT_CONFIG =
{
  remote_executor: false,
  remote_install_path: '/tmp/ansible',
  sandboxed_executor: false,
  playbook: 'default.yml',
  ansible_binary: 'ansible-playbook',
  ansible_version: nil,
  ansible_config: nil,
  ansible_extra_arguments: nil,
  ansible_force_color: true,
  ansible_host_key_checking: false,
  ansible_winrm_auth_transport: nil,
  ansible_winrm_cert_validation: 'ignore',
  ansible_verbose: false,
  ansible_verbosity: 1,
  ansible_roles_path: 'roles',
  dependencies: [],
}

Constants included from Yansible::Tools::Files

Yansible::Tools::Files::ANSIBLE_INVENTORY

Instance Method Summary collapse

Methods included from Yansible::Tools::Dependencies

#git_check_ref, #git_checkout, #git_clean, #git_clone, #git_get_origin, #prepare_dependencies

Methods included from Yansible::Tools::Files

#copy_dirs, #copy_dirs_to_sandbox, #copy_files, #dependencies_tmp_dir, #executor_tmp_dir, #generate_inventory, #generate_sandbox_path, #instance_tmp_dir, #inventory_file, #prepare_ansible_config, #prepare_inventory_file, #prepare_playbook_file, #remote_file_path, #venv_root

Methods included from Yansible::Tools::Exec

#check_command, #command_exists, #detect_platform, #execute_local_command, #local_command_exists, #local_command_path, #print_cmd_error, #print_cmd_parameters, #sudo, #sudo_env, #unindent

Instance Method Details

#commandObject



209
210
211
212
213
214
215
# File 'lib/kitchen/provisioner/yansible.rb', line 209

def command
  return @command if defined? @command

  @command = @config[:ansible_binary]
  debug("Ansible command: #{@command}")
  @command
end

#command_argsObject



233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/kitchen/provisioner/yansible.rb', line 233

def command_args
  return @command_args if defined? @command_args

  @command_args = []
  @config[:ansible_extra_arguments].each { |arg| @command_args.push(arg) } if @config[:ansible_extra_arguments]
  @config[:ansible_verbose] ? @command_args.push('-' + 'v' * @config[:ansible_verbosity]) : ''
  @command_args.push("--inventory='#{remote_file_path(ANSIBLE_INVENTORY, fallback: inventory_file)}'")
  @command_args.push("--limit='#{@instance.name}'")
  @command_args.push(remote_file_path(@config[:playbook]))

  @command_args
end

#command_envObject



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/kitchen/provisioner/yansible.rb', line 217

def command_env
  return @command_env if defined? @command_env

  # noinspection RubyStringKeysInHashInspection
  @command_env = {
    'ANSIBLE_FORCE_COLOR' => @config[:ansible_force_color].to_s,
    'ANSIBLE_HOST_KEY_CHECKING' => @config[:ansible_host_key_checking].to_s,
    'ANSIBLE_INVENTORY_ENABLED' => 'yaml',
    'ANSIBLE_RETRY_FILES_ENABLED' => false.to_s,
    'ANSIBLE_ROLES_PATH' => remote_file_path('roles', fallback: generate_sandbox_path('roles')),
  }
  @command_env['ANSIBLE_CONFIG'] = @config[:ansible_config] if @config[:ansible_config]

  @command_env
end

#create_sandboxObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/kitchen/provisioner/yansible.rb', line 153

def create_sandbox
  super

  directories = %w[
    roles
    host_vars
    group_vars
    module_utils
    library
    callback_plugins
    connection_plugins
    filter_plugins
    lookup_plugins
  ]

  prepare_dependencies(@config[:dependencies])
  generate_inventory(inventory_file, remote: true)

  info("Copy dependencies to sandbox")
  copy_dirs_to_sandbox(dependencies_tmp_dir, dst: 'roles')
  directories.each do |directory|
    if File.directory?(directory)
      info("Copy #{directory} to sandbox")
      copy_dirs_to_sandbox(directory)
    end
  end
  info("Prepare config")
  prepare_ansible_config
  info("Prepare playbook")
  prepare_playbook_file
  info("Prepare inventory file")
  prepare_inventory_file
end

#init_commandObject



139
140
141
142
# File 'lib/kitchen/provisioner/yansible.rb', line 139

def init_command
  info("Initializing provisioner software.")
  "mkdir #{windows_os? ? '-Force' : '-p'} #{config[:root_path]}"
end

#install_commandObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/kitchen/provisioner/yansible.rb', line 61

def install_command
  info("Installing provisioner software.")
  info("Working with '#{@instance.platform.os_type}' platform.")
  debug("Driver info: '#{@instance.driver.diagnose}'.")
  debug("Transport info: '#{@instance.transport.diagnose}'.")
  debug("Platform info: '#{@instance.platform.diagnose}'.")
  instance_platform = detect_platform

  if @config[:remote_executor]
    if windows_os?
      message = unindent(<<-MSG)

        ===============================================================================
         We can't use Windows platform with remote installation.
         Abandon ship!
        ===============================================================================
      MSG
      raise UserError, message
    end
    info('Using remote executor.')

    """
      #{Install.make(@config, instance_platform).remote_install}
    """
  else
    info('Using local executor.')
    if command_exists(command) and !@config[:sandboxed_executor]
      info('Ansible is installed already - proceeding further steps.')
    else
      if RbConfig::CONFIG["host_os"] =~ /mswin|mingw/
        message = unindent(<<-MSG)

          ===============================================================================
           We can't use Windows platform as a Host system for sandboxing.
           Abandon ship!
          ===============================================================================
        MSG
        raise UserError, message
      else
        additional_packages = []
        info('Checking for sandboxed Ansible version.')

        if windows_os?
          # ok, adding pywinrm
          info('==> Windows target platform may be tested only using local Ansible installation! <==')
          additional_packages.push('pywinrm')
        end

        # create sandbox
        if command_exists("#{venv_root}/bin/ansible")
          info("Ansible is installed at '#{venv_root}'.")
        else
          info("Ansible is not installed - will try to create local sandbox for execution")
          if command_exists('virtualenv')
            system("virtualenv #{venv_root}")
            system("#{venv_root}/bin/pip install " +
                     "#{Install.make(@config, instance_platform).pip_required_packages.join(' ')}"
            )
          else
            message = unindent(<<-MSG)

              ===============================================================================
               Couldn't find virtualenv binary for sandboxing.
               Please make sure execution host has Python and VirtualEnv packages installed.
              ===============================================================================
            MSG
            raise UserError, message
          end
        end
      end
    end

    """
      #{Install.make(@config, instance_platform).local_install}
    """
  end
end

#prepare_commandObject



144
145
146
147
148
149
150
151
# File 'lib/kitchen/provisioner/yansible.rb', line 144

def prepare_command
  info("Preparing configuration for provisioner.")
  unless @config[:remote_executor]
    generate_inventory(inventory_file)
  end

  ""
end

#run_commandObject



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/kitchen/provisioner/yansible.rb', line 187

def run_command
  if @config[:remote_executor]
    info("Execute Ansible remotely.")

    command_env_script = []
    if %w(darwin mac macos macosx).include? detect_platform
      command_env_script.push('source ~/.profile')
    end

    command_env.each {|k,v| command_env_script.push(shell_env_var(k, v))}

    """
      #{command_env_script.join('; ')}; #{command} #{command_args.join(' ')}
    """
  else
    info("Execute Ansible locally.")
    execute_local_command("#{command} #{command_args.join(' ')}", env: command_env, print_stdout: true)

    ""
  end
end