Method: Configure#generate_and_queue_upload_template

Defined in:
lib/maws/commands/configure.rb

#generate_and_queue_upload_template(instance, configuration) ⇒ Object



147
148
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/maws/commands/configure.rb', line 147

def generate_and_queue_upload_template(instance, configuration)
  locations = [configuration.location].flatten
  locations.each do |location|
    # prepare params for config file interpolation
    resolved_params = {}
    configuration.template_params ||= {}

    configuration.template_params.each do |param_name, param_config|
      resolved_params[param_name] = resolve_template_param(instance, configuration.template, param_name, param_config)
    end
    resolved_params['instance'] = instance
    resolved_params['instances'] = instances
    resolved_params['settings'] = @profile.settings

    # generate config file
    template_path = File.join(TEMPLATES_PATH, configuration.template + ".erb")
    template = File.read(template_path)
    generated_config =  Erubis::Eruby.new(template).result(resolved_params)

    Dir.mkdir(TEMPLATE_OUTPUT_DIR) if !File.directory?(TEMPLATE_OUTPUT_DIR)
    config_output_path = File.join(TEMPLATE_OUTPUT_DIR, "#{instance.name}--#{instance.aws_id}." + configuration.template)
    File.open(config_output_path, "w") {|f| f.write(generated_config)}
    info "generated  '#{config_output_path}'"

    if options.copy_to_local
      info "copying to local path: #{location}"
      FileUtils.cp(config_output_path, location)
    end

    queue_ssh_action(instance) do |ssh|
      ensure_output :info, "configuring #{configuration.template} for #{instance.name}"

      if options.dump
        ensure_output :info, "\n\n------- BEGIN #{config_output_path} -------"
        ensure_output :info, generated_config
        ensure_output :info, "-------- END #{config_output_path} --------\n\n"
      end

      if configuration.copy_as_user
        remote_tmp_path = File.join("/tmp/", "#{instance.name}--#{instance.aws_id}." + configuration.template)
        ssh.scp.upload!(config_output_path, remote_tmp_path)
        cp_command = "sudo su - #{configuration.copy_as_user} -c 'cp -f #{remote_tmp_path} #{location}' && sudo rm #{remote_tmp_path}"
        do_remote_command(ssh, instance, 'copy_as_user', cp_command)
      else
        ssh.scp.upload!(config_output_path, location)
      end

      if configuration.owner
        chown_command = "sudo su - -c 'chown -R #{configuration.owner} #{location}'"
        do_remote_command(ssh, instance, 'permissions', chown_command)
      end

      if configuration.permissions
        chmod_command = "sudo su - -c 'chmod -R #{configuration.permissions} #{location}'"
        do_remote_command(ssh, instance, 'permissions', chmod_command)
      end

      timestamp = ssh.exec!("sudo stat -c %y #{location}")
      ensure_output :info, "            new timestamp for #{location}: " + timestamp
    end
  end
end