Class: VagrantPlugins::K3s::Provisioner

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

Instance Method Summary collapse

Constructor Details

#initialize(machine, config) ⇒ Provisioner

Returns a new instance of Provisioner.



10
11
12
13
# File 'lib/vagrant-k3s/provisioner.rb', line 10

def initialize(machine,config)
  super(machine,config)
  @logger = Log4r::Logger.new("vagrant::provisioners::k3s")
end

Instance Method Details

#build_outputsObject



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

def build_outputs
  outputs = {
    stdout: Vagrant::Util::LineBuffer.new { |line| handle_comm(:stdout, line) },
    stderr: Vagrant::Util::LineBuffer.new { |line| handle_comm(:stderr, line) },
  }
  block = proc { |type, data|
    outputs[type] << data if outputs[type]
  }
  [outputs, block]
end

#file_upload(local_file, remote_path, content) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/vagrant-k3s/provisioner.rb', line 146

def file_upload(local_file, remote_path, content)
  with_file(local_file, content) do |local_path|
    remote_tmp_dir = @machine.guest.capability :create_tmp_path, {:type => :directory}
    remote_tmp_path = [remote_tmp_dir, File.basename(remote_path)].join('/')
    @machine.communicate.upload(local_path, remote_tmp_path)
    @machine.communicate.sudo("install -v -DTZ #{remote_tmp_path} #{remote_path}") do |type, line|
      @machine.ui.info line.chomp, :color => {:stderr => :red, :stdout => :default}[type]
    end
  end
  @machine.ui.detail content.chomp, :color => :yellow
  remote_path
end

#handle_comm(type, data) ⇒ Object

This handles outputting the communication line back to the UI



122
123
124
125
126
127
128
129
130
# File 'lib/vagrant-k3s/provisioner.rb', line 122

def handle_comm(type, data)
  if [:stderr, :stdout].include?(type)
    # Output the line with the proper color based on the stream.
    options = {}
    options[:color] = type == :stdout ? :green : :red

    @machine.ui.detail(data.chomp, **options)
  end
end

#provisionObject

def cleanup end



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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
# File 'lib/vagrant-k3s/provisioner.rb', line 21

def provision
  args = ""
  if config.args.is_a?(String)
    args = " #{config.args.to_s}"
  elsif config.args.is_a?(Array)
    args = config.args.map { |a| quote_and_escape(a) }
    args = " #{args.join(" ")}"
  end

  unless @machine.guest.capability(:curl_installed)
    @machine.ui.info 'Installing Curl ...'
    @machine.guest.capability(:curl_install)
  end

  cfg_file = config.config_path.to_s
  cfg_yaml = config.config.is_a?(String) ? config.config : config.config.transform_keys(&:to_s).to_yaml
  file_upload "k3s-config.yaml", cfg_file, cfg_yaml

  env_file = config.env_path.to_s
  env_text = ""
  if config.env.is_a?(String)
    env_text = config.env.to_s
  end
  if config.env.is_a?(Array)
    config.env.each {|line| env_text << "#{line.to_s}\n"}
  end
  if config.env.is_a?(Hash)
    config.env.each {|key,value| env_text << "#{key.to_s}=#{quote_and_escape(value.to_s)}\n"}
  end
  file_upload "k3s-install.env", env_file, env_text

  skip_env = ""
  if config.skip_start
    skip_env = "INSTALL_K3S_SKIP_ENABLE=true"
  end
  
  prv_file = "/vagrant/k3s-provisioner.sh"
  prv_text = "    #/usr/bin/env bash\n    set -eu -o pipefail\n    chown \#{config.config_owner} \#{config.config_path}\n    chmod \#{config.config_mode} \#{config.config_path}\n    chown \#{config.env_owner} \#{config.env_path}\n    chmod \#{config.env_mode} \#{config.env_path}\n    set -o allexport\n    source \#{config.env_path}\n    set +o allexport\n    curl -fsL '\#{config.installer_url}' | \#{skip_env} sh -s - \#{args}\n  EOF\n  file_upload(\"k3s-install.sh\", prv_file, prv_text)\n  @machine.ui.info \"Invoking: \#{prv_file}\"\n\n  outputs, handler = build_outputs\n  begin\n    @machine.communicate.sudo(\"chmod +x \#{prv_file} && \#{prv_file}\", error_key: :ssh_bad_exit_status_muted, &handler)\n  ensure\n    outputs.values.map(&:close)\n  end\n\n  if not config.skip_complete\n    outputs, handler = build_outputs\n    begin\n      @machine.ui.info 'Enabling K3s autocompletion ...'\n      @machine.communicate.sudo(\"k3s completion -i bash\", error_key: :ssh_bad_exit_status_muted, &handler)\n    rescue Vagrant::Errors::VagrantError => e\n      @machine.ui.detail \"\#{e.extra_data[:stderr].chomp}\", :color => :yellow\n    ensure\n      outputs.values.map(&:close)\n    end\n  end\n\n  begin\n    exe = \"k3s\"\n    @machine.ui.info 'Checking the K3s version ...'\n    @machine.communicate.execute(\"which k3s\", :error_key => :ssh_bad_exit_status_muted) do |type, data|\n      exe = data.chomp if type == :stdout\n    end\n  rescue Vagrant::Errors::VagrantError => e\n    @machine.ui.detail \"\#{e.extra_data[:stderr].chomp}\", :color => :red\n  else\n    outputs, handler = build_outputs\n    begin\n      @machine.communicate.sudo(\"\#{exe} --version\", :error_key => :ssh_bad_exit_status_muted, &handler)\n    ensure\n      outputs.values.map(&:close)\n    end\n  end\nend\n"

#quote_and_escape(text, quote = '"') ⇒ Object



159
160
161
# File 'lib/vagrant-k3s/provisioner.rb', line 159

def quote_and_escape(text, quote = '"')
  "#{quote}#{text.to_s.gsub(/#{quote}/) { |m| "#{m}\\#{m}#{m}" }}#{quote}"
end

#with_file(name, content) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/vagrant-k3s/provisioner.rb', line 132

def with_file(name, content)
  file = Tempfile.new([name])
  file.binmode
  begin
    file.write(content)
    file.fsync
    file.close
    yield file.path
  ensure
    file.close
    file.unlink
  end
end