Class: Kitchen::Provisioner::SaltSolo

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

Overview

Basic Salt Masterless Provisioner, based on work by

Author:

Constant Summary collapse

RETCODE_VERSION =

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

'0.17.5'

Instance Method Summary collapse

Instance Method Details

#create_sandboxObject



152
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
# File 'lib/kitchen/provisioner/salt_solo.rb', line 152

def create_sandbox
  super
  prepare_data
  prepare_minion
  prepare_state_top
  prepare_pillars
  prepare_grains
  if config[:state_collection] || config[:is_file_root]
    prepare_state_collection
  else
    prepare_formula config[:kitchen_root], config[:formula]

    deps = if Pathname.new(config[:vendor_path]).absolute?
      Dir["#{config[:vendor_path]}/*"]
    else
      Dir["#{config[:kitchen_root]}/#{config[:vendor_path]}/*"]
    end

    deps.each do |d|
      prepare_formula "#{config[:kitchen_root]}/#{config[:vendor_path]}", File.basename(d)
    end

    config[:dependencies].each do |formula|
      prepare_formula formula[:path], formula[:name]
    end
  end
end

#init_commandObject



180
181
182
183
# File 'lib/kitchen/provisioner/salt_solo.rb', line 180

def init_command
  debug("Initialising Driver #{self.name} by cleaning #{config[:root_path]}")
  "#{sudo('rm')} -rf #{config[:root_path]} ; mkdir -p #{config[:root_path]}"
end

#install_commandObject



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
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/kitchen/provisioner/salt_solo.rb', line 66

def install_command
  debug(diagnose())

  # 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 ((config[:salt_version] != 'latest') && (config[:salt_install] == 'bootstrap') && config[:salt_bootstrap_options].empty?)
    debug("Using bootstrap git to install #{config[:salt_version]}")
    config[:salt_bootstrap_options] = "-P git v#{config[:salt_version]}"
  end

  salt_install = config[:salt_install]

  salt_url = config[:salt_bootstrap_url]
  chef_url = config[:chef_bootstrap_url]
  bootstrap_options = config[:salt_bootstrap_options]

  salt_version = config[:salt_version]
  salt_apt_repo = config[:salt_apt_repo]
  salt_apt_repo_key = config[:salt_apt_repo_key]

  omnibus_download_dir = config[:omnibus_cachier] ? "/tmp/vagrant-cache/omnibus_chef" : "/tmp"

  <<-INSTALL
    sh -c '
    #{Util.shell_helpers}

    # what version of salt is installed?
    SALT_VERSION=`salt-call --version | cut -d " " -f 2`


    if [ -z "${SALT_VERSION}" -a "#{salt_install}" = "bootstrap" ]
    then
      do_download #{salt_url} /tmp/bootstrap-salt.sh
      #{sudo('sh')} /tmp/bootstrap-salt.sh #{bootstrap_options}
    elif [ -z "${SALT_VERSION}" -a "#{salt_install}" = "apt" ]
    then
      . /etc/lsb-release

      echo "deb #{salt_apt_repo}/salt-#{salt_version} ${DISTRIB_CODENAME} main" | #{sudo('tee')} /etc/apt/sources.list.d/salt-#{salt_version}.list

      do_download #{salt_apt_repo_key} /tmp/repo.key
      #{sudo('apt-key')} add /tmp/repo.key

      #{sudo('apt-get')} update
      #{sudo('apt-get')} install -y salt-minion
    fi

    # check again, now that an install of some form should have happened
    SALT_VERSION=`salt-call --version | cut -d " " -f 2`

    if [ -z "${SALT_VERSION}" ]
    then
      echo "No salt-minion installed, install must have failed!!"
      echo "salt_install = #{salt_install}"
      echo "salt_url = #{salt_url}"
      echo "bootstrap_options = #{bootstrap_options}"
      echo "salt_version = #{salt_version}"
      echo "salt_apt_repo = #{salt_apt_repo}"
      echo "salt_apt_repo_key = #{salt_apt_repo_key}"
      exit 2
    elif [ "${SALT_VERSION}" = "#{salt_version}" -o "#{salt_version}" = "latest" ]
    then
      echo "You asked for #{salt_version} and you have ${SALT_VERSION} installed, sweet!"
    elif [ ! -z "${SALT_VERSION}" -a "#{salt_install}" = "bootstrap" ]
    then
      echo "You asked for bootstrap install and you have got ${SALT_VERSION}, hope thats ok!"
    else
      echo "You asked for #{salt_version} and you have got ${SALT_VERSION} installed, dunno how to fix that, sorry!"
      exit 2
    fi

    if [ ! -d "/opt/chef" ]
    then
      echo "-----> Installing Chef Omnibus"
      mkdir -p #{omnibus_download_dir}
      if [ ! -x #{omnibus_download_dir}/install.sh ]
      then
        do_download #{chef_url} #{omnibus_download_dir}/install.sh
      fi
      #{sudo('sh')} #{omnibus_download_dir}/install.sh -d #{omnibus_download_dir}
    fi

    '
  INSTALL
end

#run_commandObject



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/kitchen/provisioner/salt_solo.rb', line 185

def run_command
  debug("running driver #{self.name}")
  # sudo(File.join(config[:root_path], File.basename(config[:script])))
  debug(diagnose())
  if config[:salt_run_highstate]
    cmd = sudo("salt-call --config-dir=#{File.join(config[:root_path], config[:salt_config])} --local state.highstate")
  end

  cmd << " --log-level=#{config[:log_level]}"

  # 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 && config[:salt_version] != 'latest'
    # hope for the best and hope it works eventually
    cmd = cmd + " --retcode-passthrough"
  end

  # 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 ; ' << cmd
  # 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
end