Class: Kdeploy::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/kdeploy/template.rb

Class Method Summary collapse

Class Method Details

.render(template_path, variables = {}) ⇒ Object



9
10
11
12
13
# File 'lib/kdeploy/template.rb', line 9

def self.render(template_path, variables = {})
  template_content = File.read(template_path)
  context = OpenStruct.new(variables)
  ERB.new(template_content).result(context.instance_eval { binding })
end

.render_and_upload(executor, template_path, destination, variables = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/kdeploy/template.rb', line 15

def self.render_and_upload(executor, template_path, destination, variables = {})
  rendered_content = render(template_path, variables)

  # 创建临时文件
  temp_file = Tempfile.new('kdeploy')
  begin
    temp_file.write(rendered_content)
    temp_file.close

    # 上传渲染后的文件
    executor.upload(temp_file.path, destination)
  ensure
    temp_file.unlink
  end
end