Class: Kitchen::Provisioner::SaltSolo

Inherits:
Base
  • Object
show all
Includes:
Salt::Pillars, Salt::States, Salt::Util
Defined in:
lib/kitchen/provisioner/salt_solo.rb

Overview

Basic Salt Masterless Provisioner, based on work by

Author:

Constant Summary collapse

DEFAULT_CONFIG =
{
  bootstrap_url: 'https://raw.githubusercontent.com/saltstack/kitchen-salt/master/assets/install.sh',
  cache_commands: [],
  chef_bootstrap_url: 'https://www.chef.io/chef/install.sh',
  dependencies: [],
  dry_run: false,
  gpg_home: '~/.gnupg/',
  gpg_key: nil,
  install_after_init_environment: false,
  is_file_root: false,
  local_salt_root: nil,
  omnibus_cachier: false,
  pillars_from_directories: [],
  pip_bin: 'pip',
  pip_editable: false,
  pip_extra_index_url: [],
  pip_index_url: 'https://pypi.python.org/simple/',
  pip_pkg: 'salt==%s',
  remote_states: nil,
  require_chef: true,
  salt_apt_repo_key: 'https://repo.saltstack.com/apt/ubuntu/16.04/amd64/latest/SALTSTACK-GPG-KEY.pub',
  salt_apt_repo: 'https://repo.saltstack.com/apt/ubuntu/16.04/amd64/',
  salt_bootstrap_options: '',
  salt_bootstrap_url: 'https://bootstrap.saltstack.com',
  salt_config: '/etc/salt',
  salt_copy_filter: [],
  salt_env: 'base',
  salt_file_root: '/srv/salt',
  salt_force_color: false,
  salt_enable_color: true,
  salt_install: 'bootstrap',
  salt_minion_config_dropin_files: [],
  salt_minion_config_template: nil,
  salt_minion_config: '/etc/salt/minion',
  salt_minion_extra_config: {},
  salt_minion_id: nil,
  salt_pillar_root: '/srv/pillar',
  salt_ppa: 'ppa:saltstack/salt',
  salt_spm_root: '/srv/spm',
  salt_state_top: '/srv/salt/top.sls',
  salt_version: 'latest',
  salt_yum_repo_key: 'https://repo.saltstack.com/yum/redhat/$releasever/$basearch/archive/%s/SALTSTACK-GPG-KEY.pub',
  salt_yum_repo_latest: 'https://repo.saltstack.com/yum/redhat/salt-repo-latest-2.el7.noarch.rpm',
  salt_yum_repo: 'https://repo.saltstack.com/yum/redhat/$releasever/$basearch/archive/%s',
  salt_yum_rpm_key: 'https://repo.saltstack.com/yum/redhat/7/x86_64/archive/%s/SALTSTACK-GPG-KEY.pub',
  state_collection: false,
  state_top_from_file: false,
  state_top: {},
  vendor_path: nil,
  vendor_repo: {},
  run_salt_call: true
}.freeze
WIN_DEFAULT_CONFIG =
{
  chef_bootstrap_url: 'https://omnitruck.chef.io/install.ps1',
  salt_bootstrap_url: 'https://raw.githubusercontent.com/saltstack/salt-bootstrap/develop/bootstrap-salt.ps1'
}.freeze
RETCODE_VERSION =

salt-call version that supports the undocumented –retcode-passthrough command

'0.17.5'.freeze

Instance Method Summary collapse

Instance Method Details

#create_sandboxObject



196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/kitchen/provisioner/salt_solo.rb', line 196

def create_sandbox
  super
  prepare_data
  prepare_gpg_key
  prepare_install
  prepare_minion
  prepare_pillars
  prepare_grains
  prepare_states
  prepare_state_top
  prepare_cache_commands
  # upload scripts, cached formulas, and setup system repositories
  prepare_dependencies
end

#init_commandObject



237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/kitchen/provisioner/salt_solo.rb', line 237

def init_command
  debug("Initialising Driver #{name}")
  cmd = if windows_os?
          'mkdir -Force -Path '"#{config[:root_path]}""\n"
        else
          "mkdir -p '#{config[:root_path]}';"
        end
  cmd += <<-INSTALL
    #{config[:init_environment]}
  INSTALL
  cmd
end

#install_chefObject



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
186
187
188
189
190
191
192
193
194
# File 'lib/kitchen/provisioner/salt_solo.rb', line 155

def install_chef
  return unless config[:require_chef]
  chef_url = config[:chef_bootstrap_url]
  if windows_os?
    <<-POWERSHELL
      if (-Not $(test-path c:\\opscode\\chef)) {
        if (-Not $(Test-Path c:\\temp)) {
          New-Item -Path c:\\temp -itemtype directory
        }
        [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
        (New-Object net.webclient).DownloadFile("#{chef_url}", "c:\\temp\\chef_bootstrap.ps1")
        write-host "-----> Installing Chef Omnibus (for busser/serverspec ruby support)"
        #{sudo('powershell')} c:\\temp\\chef_bootstrap.ps1
      }
    POWERSHELL
  else
    omnibus_download_dir = config[:omnibus_cachier] ? '/tmp/vagrant-cache/omnibus_chef' : '/tmp'
    bootstrap_url = config[:bootstrap_url]
    bootstrap_download_dir = '/tmp'
    <<-INSTALL
        echo "-----> Trying to install ruby(-dev) using assets.sh from kitchen-salt"
          mkdir -p #{bootstrap_download_dir}
          if [ ! -x #{bootstrap_download_dir}/install.sh ]
          then
            do_download #{bootstrap_url} #{bootstrap_download_dir}/install.sh
          fi
          #{sudo('sh')} #{bootstrap_download_dir}/install.sh -d #{bootstrap_download_dir}
        if [ $? -ne 0 ] || [ ! -d "/opt/chef" ]
        then
          echo "Failed install ruby(-dev) using assets.sh from kitchen-salt"
          echo "-----> Fallback to Chef Bootstrap script (for busser/serverspec ruby support)"
          mkdir -p "#{omnibus_download_dir}"
          if [ ! -x #{omnibus_download_dir}/install.sh ]
          then
              #{sudo('sh')} #{omnibus_download_dir}/install.sh -d #{omnibus_download_dir}
          fi;
        fi
    INSTALL
  end
end

#install_commandObject



109
110
111
112
113
# File 'lib/kitchen/provisioner/salt_solo.rb', line 109

def install_command
  unless not config[:salt_install] || config[:salt_install] == 'pip' || config[:install_after_init_environment]
    setup_salt
  end
end

#os_join(*args) ⇒ Object



250
251
252
253
254
255
256
# File 'lib/kitchen/provisioner/salt_solo.rb', line 250

def os_join(*args)
  if windows_os?
     File.join(*args).tr('/', '\\')
  else
     File.join(*args)
  end
end

#prepare_commandObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/kitchen/provisioner/salt_solo.rb', line 115

def prepare_command
  cmd = ''
  unless windows_os?
    cmd += <<-CHOWN
      #{sudo('chown')} -R "${SUDO_USER:-$USER}" "#{config[:root_path]}/#{config[:salt_file_root]}"
    CHOWN
  end
  if config[:prepare_salt_environment]
    cmd += <<-PREPARE
      #{config[:prepare_salt_environment]}
    PREPARE
  end
  if config[:salt_install] == 'pip' || config[:install_after_init_environment]
    cmd << setup_salt
  end
  cmd
end

#prepare_installObject



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/kitchen/provisioner/salt_solo.rb', line 211

def prepare_install
  salt_version = config[:salt_version]
  if config[:salt_install] == 'pip'
    debug('Using pip to install')
    if File.exist?(config[:pip_pkg])
      debug('Installing with pip from sdist')
      sandbox_pip_path = File.join(sandbox_path, 'pip')
      FileUtils.mkdir_p(sandbox_pip_path)
      FileUtils.cp_r(config[:pip_pkg], sandbox_pip_path)
      config[:pip_install] = File.join(config[:root_path], 'pip', File.basename(config[:pip_pkg]))
    else
      debug('Installing with pip from download')
      if salt_version != 'latest'
        config[:pip_install] = format(config[:pip_pkg], salt_version)
      else
        config[:pip_pkg].slice!('==%s')
        config[:pip_install] = config[:pip_pkg]
      end
    end
  elsif config[:salt_install] == 'bootstrap'
    if File.exist?(config[:salt_bootstrap_url])
      FileUtils.cp_r(config[:salt_bootstrap_url], File.join(sandbox_path, 'bootstrap.sh'))
    end
  end
end

#run_commandObject



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/kitchen/provisioner/salt_solo.rb', line 290

def run_command
  debug("running driver #{name}")
  debug(diagnose)

  return unless config[:run_salt_call]

  # config[:salt_version] can be 'latest' or 'x.y.z', 'YYYY.M.x' etc
  # error return codes are a mess in salt:
  #  https://github.com/saltstack/salt/pull/11337
  # Unless we know we have a version that supports --retcode-passthrough
  # attempt to scan the output for signs of failure
  if "#{config[:salt_version]}" <= RETCODE_VERSION
    # scan the output for signs of failure, there is a risk of false negatives
    fail_grep = 'grep -e Result.*False -e Data.failed.to.compile -e No.matching.sls.found.for'
    # capture any non-zero exit codes from the salt-call | tee pipe
    cmd = 'set -o pipefail ; ' << salt_command
    # Capture the salt-call output & exit code
    cmd << ' 2>&1 | tee /tmp/salt-call-output ; SC=$? ; echo salt-call exit code: $SC ;'
    # check the salt-call output for fail messages
    cmd << " (sed '/#{fail_grep}/d' /tmp/salt-call-output | #{fail_grep} ; EC=$? ; echo salt-call output grep exit code ${EC} ;"
    # use the non-zer exit code from salt-call, then invert the results of the grep for failures
    cmd << ' [ ${SC} -ne 0 ] && exit ${SC} ; [ ${EC} -eq 0 ] && exit 1 ; [ ${EC} -eq 1 ] && exit 0)'
    cmd
  else
    salt_command
  end
end

#salt_commandObject



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/kitchen/provisioner/salt_solo.rb', line 258

def salt_command
  salt_version = config[:salt_version]

  cmd = ''
  if windows_os?
    salt_config_path = config[:salt_config]
    cmd << "(get-content #{os_join(config[:root_path], salt_config_path, 'minion')}) -replace '\\$env:TEMP', $env:TEMP | set-content #{os_join(config[:root_path], salt_config_path, 'minion')} ;"
  else
    # install/update dependencies
    cmd << sudo("chmod +x #{config[:root_path]}/*.sh;")
    cmd << sudo("#{config[:root_path]}/dependencies.sh;")
    cmd << sudo("#{config[:root_path]}/gpgkey.sh;") if config[:gpg_key]
    salt_config_path = config[:salt_config]
  end

  if config[:pre_salt_command]
    cmd << "#{config[:pre_salt_command]} && "
  end
  cmd << sudo("#{salt_call} --state-output=changes --config-dir=#{os_join(config[:root_path], salt_config_path)} state.highstate")
  cmd << " --log-level=#{config[:log_level]}" if config[:log_level]
  cmd << " --id=#{config[:salt_minion_id]}" if config[:salt_minion_id]
  cmd << " test=#{config[:dry_run]}" if config[:dry_run]
  cmd << ' --force-color' if config[:salt_force_color]
  cmd << ' --no-color' if not config[:salt_enable_color]
  if "#{salt_version}" > RETCODE_VERSION || salt_version == 'latest'
    # hope for the best and hope it works eventually
    cmd << ' --retcode-passthrough'
  end
  cmd << ' 2>&1 ; exit $LASTEXITCODE' if windows_os?
  cmd
end

#setup_saltObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/kitchen/provisioner/salt_solo.rb', line 133

def setup_salt
  debug(diagnose)
  salt_version = config[:salt_version]

  # if salt_verison is set, bootstrap is being used & bootstrap_options is empty,
  # set the bootstrap_options string to git install the requested version
  if (salt_version != 'latest') && (config[:salt_install] == 'bootstrap') && config[:salt_bootstrap_options].empty?
    debug("Using bootstrap git to install #{salt_version}")
    config[:salt_bootstrap_options] = "-P git v#{salt_version}"
  end

  install_template = if windows_os?
                       File.expand_path('./../install_win.erb', __FILE__)
                     else
                       File.expand_path('./../install.erb', __FILE__)
                     end

  erb = ERB.new(File.read(install_template)).result(binding)
  debug('Install Command:' + erb.to_s)
  erb
end