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 =
{
  dry_run: false,
  salt_version: 'latest',
  salt_install: 'bootstrap',
  salt_bootstrap_url: 'https://bootstrap.saltstack.com',
  salt_bootstrap_options: '',
  salt_apt_repo: 'https://repo.saltstack.com/apt/ubuntu/16.04/amd64/',
  salt_apt_repo_key: 'https://repo.saltstack.com/apt/ubuntu/16.04/amd64/latest/SALTSTACK-GPG-KEY.pub',
  salt_ppa: 'ppa:saltstack/salt',
  bootstrap_url: 'https://raw.githubusercontent.com/saltstack/kitchen-salt/master/assets/install.sh',
  chef_bootstrap_url: 'https://www.getchef.com/chef/install.sh',
  salt_config: '/etc/salt',
  salt_minion_config: '/etc/salt/minion',
  salt_minion_config_template: nil,
  salt_minion_id: nil,
  salt_env: 'base',
  salt_file_root: '/srv/salt',
  salt_pillar_root: '/srv/pillar',
  salt_spm_root: '/srv/spm',
  salt_state_top: '/srv/salt/top.sls',
  salt_force_color: false,
  state_collection: false,
  state_top: {},
  state_top_from_file: false,
  salt_copy_filter: [],
  is_file_root: false,
  remote_states: nil,
  require_chef: true,
  dependencies: [],
  vendor_path: nil,
  vendor_repo: {},
  omnibus_cachier: false,
  local_salt_root: nil,
  pillars_from_directories: [],
  salt_yum_rpm_key: 'https://repo.saltstack.com/yum/redhat/7/x86_64/archive/%s/SALTSTACK-GPG-KEY.pub',
  salt_yum_repo: 'https://repo.saltstack.com/yum/redhat/$releasever/$basearch/archive/%s',
  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',
  pip_pkg: 'salt==%s',
  pip_editable: false,
  pip_index_url: 'https://pypi.python.org/simple/',
  pip_extra_index_url: [],
  pip_bin: 'pip',
  install_after_init_environment: false,
}
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



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/kitchen/provisioner/salt_solo.rb', line 164

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

#init_commandObject



203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/kitchen/provisioner/salt_solo.rb', line 203

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

#install_chefObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/kitchen/provisioner/salt_solo.rb', line 124

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

#install_commandObject



90
91
92
93
94
# File 'lib/kitchen/provisioner/salt_solo.rb', line 90

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

#prepare_commandObject



96
97
98
99
100
# File 'lib/kitchen/provisioner/salt_solo.rb', line 96

def prepare_command
  if config[:salt_install] == 'pip' || config[:install_after_init_environment]
    setup_salt
  end
end

#prepare_installObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/kitchen/provisioner/salt_solo.rb', line 177

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] = 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.exists?(config[:salt_bootstrap_url])
      FileUtils.cp_r(config[:salt_bootstrap_url], File.join(sandbox_path, 'bootstrap.sh'))
    end
  end
end

#run_commandObject



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/kitchen/provisioner/salt_solo.rb', line 246

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

  # 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



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/kitchen/provisioner/salt_solo.rb', line 216

def salt_command
  salt_version = config[:salt_version]

  cmd = ""
  if windows_os?
    salt_call = "c:\\salt\\salt-call.bat"
    salt_config_path = config[:salt_config].gsub('/', '\\')
    cmd << "(get-content #{File.join(config[:root_path], salt_config_path, 'minion').gsub('/', '\\')}).replace(\"`$env`:TEMP\", $env:TEMP) | set-content #{File.join(config[:root_path], salt_config_path, 'minion').gsub('/', '\\')} ;"
  else
    # install/update dependencies
    cmd << sudo("chmod +x #{config[:root_path]}/*.sh;")
    cmd << sudo("#{config[:root_path]}/dependencies.sh;")
    salt_config_path = config[:salt_config]
    salt_call = "salt-call"
  end
  cmd << sudo("#{salt_call} --state-output=changes --config-dir=#{File.join(config[:root_path], salt_config_path)} --local 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]
  if salt_version > RETCODE_VERSION || salt_version == 'latest'
    # hope for the best and hope it works eventually
    cmd << ' --retcode-passthrough'
  end
  if windows_os?
    cmd << ' ; exit $LASTEXITCODE'
  end
  cmd
end

#setup_saltObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/kitchen/provisioner/salt_solo.rb', line 102

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

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

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