Class: VagrantPlugins::Salt::Provisioner

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-salt/provisioner.rb

Instance Method Summary collapse

Instance Method Details

#accept_keysObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/vagrant-salt/provisioner.rb', line 194

def accept_keys
  if @config.accept_keys
    if !@machine.communicate.test("which salt-key")
      @machine.env.ui.info "Salt-key not installed!"
    else
      @machine.env.ui.info "Waiting for minion key..."
      key_staged = false
      attempts = 0
      while !key_staged
        attempts += 1 
        @machine.communicate.sudo("salt-key -l pre | wc -l") do |type, output|
          begin
            output = Integer(output)
            if output > 1
              key_staged = true
            end
          rescue
          end
        end
        sleep 1
        if attempts > 10
          raise Salt::Errors::SaltError, :not_received_minion_key
        end
      end

      @machine.env.ui.info "Accepting minion key."
      @machine.communicate.sudo("salt-key -A")
    end
  end
end

#binaries_foundObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/vagrant-salt/provisioner.rb', line 19

def binaries_found
  ## Determine States, ie: install vs configure
  desired_binaries = []
  if !@config.no_minion
    desired_binaries.push('salt-minion')
    desired_binaries.push('salt-call')
  end

  if @config.install_master
    desired_binaries.push('salt-master')
  end

  if @config.install_syndic
    desired_binaries.push('salt-syndic')
  end

  found = true
  for binary in desired_binaries
    @machine.env.ui.info "Checking if %s is installed" % binary
    if !@machine.communicate.test("which %s" % binary)
      @machine.env.ui.info "%s was not found." % binary
      found = false
    else
      @machine.env.ui.info "%s found" % binary
    end
  end

  return found
end

#bootstrap_options(install, configure, config_dir) ⇒ Object

Generates option string for bootstrap script



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
# File 'lib/vagrant-salt/provisioner.rb', line 66

def bootstrap_options(install, configure, config_dir)
  options = ""

  ## Any extra options passed to bootstrap
  if @config.bootstrap_options
    options = "%s %s" % [options, @config.bootstrap_options]
  end

  if configure
    options = "%s -c %s" % [options, config_dir]
  end

  if configure and !install
    options = "%s -C" % options
  else

    if @config.install_master
      options = "%s -M" % options
    end

    if @config.install_syndic
      options = "%s -S" % options
    end

    if @config.no_minion
      options = "%s -N" % options
    end

    if @config.install_type
      options = "%s %s" % [options, @config.install_type]
    end

    if @config.install_args
      options = "%s %s" % [options, @config.install_args]
    end 
  end
  if @config.verbose
    @machine.env.ui.info "Using Bootstrap Options: %s" % options
  end
  return options
end

#call_highstateObject



225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/vagrant-salt/provisioner.rb', line 225

def call_highstate
  if @config.run_highstate
    @machine.env.ui.info "Calling state.highstate... (this may take a while)"
    @machine.communicate.sudo("salt-call saltutil.sync_all")
    @machine.communicate.sudo("salt-call state.highstate -l debug") do |type, data|
      if @config.verbose
        @machine.env.ui.info(data)
      end
    end
  else
    @machine.env.ui.info "run_highstate set to false. Not running state.highstate."
  end
end

#expanded_path(rel_path) ⇒ Object

Utilities



15
16
17
# File 'lib/vagrant-salt/provisioner.rb', line 15

def expanded_path(rel_path)
  Pathname.new(rel_path).expand_path(@machine.env.root_path)
end

#get_bootstrapObject

Get bootstrap file location, bundled or custom



139
140
141
142
143
144
145
146
# File 'lib/vagrant-salt/provisioner.rb', line 139

def get_bootstrap
  if @config.bootstrap_script
    bootstrap_abs_path = expanded_path(@config.bootstrap_script)
  else
    bootstrap_abs_path = Pathname.new("../../../scripts/bootstrap-salt.sh").expand_path(__FILE__)
  end
  return bootstrap_abs_path
end

#need_configureObject



49
50
51
# File 'lib/vagrant-salt/provisioner.rb', line 49

def need_configure
  @config.minion_config or @config.minion_key
end

#need_installObject



53
54
55
56
57
58
59
# File 'lib/vagrant-salt/provisioner.rb', line 53

def need_install
  if @config.always_install
    return true
  else
    return !binaries_found()
  end
end

#provisionObject



6
7
8
9
10
11
12
# File 'lib/vagrant-salt/provisioner.rb', line 6

def provision
  upload_configs
  upload_keys
  run_bootstrap_script
  accept_keys
  call_highstate
end

#run_bootstrap_scriptObject

Determine if we are configure and/or installing, then do either



149
150
151
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/vagrant-salt/provisioner.rb', line 149

def run_bootstrap_script
  install = need_install()
  configure = need_configure()
  config_dir = temp_config_dir()
  options = bootstrap_options(install, configure, config_dir)

  if configure or install

    if configure and !install
      @machine.env.ui.info "Salt binaries found. Configuring only."
    else
      @machine.env.ui.info "Bootstrapping Salt... (this may take a while)"
    end
    
    bootstrap_path = get_bootstrap()
    bootstrap_destination = File.join(config_dir, "bootstrap_salt.sh")
    @machine.communicate.upload(bootstrap_path.to_s, bootstrap_destination)
    @machine.communicate.sudo("chmod +x %s" % bootstrap_destination)
    bootstrap = @machine.communicate.sudo("%s %s" % [bootstrap_destination, options]) do |type, data|
      if data[0] == "\n"
        # Remove any leading newline but not whitespace. If we wanted to
        # remove newlines and whitespace we would have used data.lstrip
        data = data[1..-1]
      end
      if @config.verbose
        @machine.env.ui.info(data.rstrip)
      end
    end
    if !bootstrap
      raise Salt::Errors::SaltError, :bootstrap_failed
    end

    if configure and !install
      @machine.env.ui.info "Salt successfully configured!"
    elsif configure and install
      @machine.env.ui.info "Salt successfully configured and installed!"
    elsif !configure and install
      @machine.env.ui.info "Salt successfully installed!"
    end
  
  else
    @machine.env.ui.info "Salt did not need installing or configuring."
  end
end

#temp_config_dirObject



61
62
63
# File 'lib/vagrant-salt/provisioner.rb', line 61

def temp_config_dir
  return @config.temp_config_dir || "/tmp"
end

#upload_configsObject

Actions Copy master and minion configs to VM



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/vagrant-salt/provisioner.rb', line 111

def upload_configs
  if @config.minion_config
    @machine.env.ui.info "Copying salt minion config to vm."
    @machine.communicate.upload(expanded_path(@config.minion_config).to_s, temp_config_dir + "/minion")
  end

  if @config.master_config
    @machine.env.ui.info "Copying salt master config to vm."
    @machine.communicate.upload(expanded_path(@config.master_config).to_s, temp_config_dir + "/master")
  end
end

#upload_keysObject

Copy master and minion keys to VM



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/vagrant-salt/provisioner.rb', line 124

def upload_keys
  if @config.minion_key and @config.minion_pub
    @machine.env.ui.info "Uploading minion keys."
    @machine.communicate.upload(expanded_path(@config.minion_key).to_s, temp_config_dir + "/minion.pem")
    @machine.communicate.upload(expanded_path(@config.minion_pub).to_s, temp_config_dir + "/minion.pub")
  end

  if @config.master_key and @config.master_pub
    @machine.env.ui.info "Uploading master keys."
    @machine.communicate.upload(expanded_path(@config.master_key).to_s, temp_config_dir + "/master.pem")
    @machine.communicate.upload(expanded_path(@config.master_pub).to_s, temp_config_dir + "/master.pub")
  end
end