Class: K8sflow::Docker::DockerImageBase

Inherits:
Clitopic::Command::Base
  • Object
show all
Defined in:
lib/k8sflow/command/docker/docker_image_base.rb

Direct Known Subclasses

Build, Dockerfile, Push

Class Method Summary collapse

Class Method Details

.create_dockerfileObject



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
# File 'lib/k8sflow/command/docker/docker_image_base.rb', line 55

def create_dockerfile
  tag = @options[:tag]
  dir = "#{@options[:path]}/#{@options[:tag]}"
  puts "- Remove previous dir: #{dir}"
  FileUtils.rm_rf dir
  puts "- Create dir: #{dir}"
  FileUtils.mkdir_p dir
  files.each do |file_list|
    puts "- Copy files: #{file_list}"
    FileUtils.cp(file_list, dir)
  end
  puts "- Read Dockerfile template: #{@options[:path]}/#{@options[:tpl]}"
  tpl = "#{@options[:path]}/#{@options[:tpl]}"
  File.open(tpl, 'rb') do |file|
    tpl = file.read
  end
  b = binding
  puts "- Write Dockerfile: #{dir}/Dockerfile"
  puts "  with vars:\n#{vars.map{|k,v| "   #{k}: #{v}"}.join("\n")}"
  File.open("#{dir}/Dockerfile", 'wb') do |file|
    file.write(ERB.new(tpl).result b)
  end

  puts  "-----------------------------------------------"
end

.dockerbuildObject



81
82
83
84
85
86
# File 'lib/k8sflow/command/docker/docker_image_base.rb', line 81

def dockerbuild
  dir = "#{@options[:path]}/#{@options[:tag]}"
  cmd = "docker -H #{@options[:docker_api]} build #{@options[:build_opts]} -t #{@options[:registry]}:#{tag}  #{dir} "
  puts cmd
  system(cmd)
end

.dockerpushObject



88
89
90
91
92
# File 'lib/k8sflow/command/docker/docker_image_base.rb', line 88

def dockerpush
  cmd = "docker -H #{@options[:docker_api]}  push #{@options[:registry]}:#{tag}"
  puts cmd
  system(cmd)
end

.filesObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/k8sflow/command/docker/docker_image_base.rb', line 36

def files
  f = []
  if @options.has_key?(:files)
    @options[:files].each do |file|
      if !file.start_with?("/")
        file = "#{@options[:path]}/#{file}"
      end
      file_list = Dir.glob(file)
      f << file_list
    end
  end
  return f
end

.kv_parse(list) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/k8sflow/command/docker/docker_image_base.rb', line 9

def kv_parse(list)
  vars = {}
  list.each do |e|
    sp = e.split("=")
    key = sp[0..-2].join("=")
    value = sp[-1]
    vars[key] = value
  end
  return vars
end

.tagObject



50
51
52
# File 'lib/k8sflow/command/docker/docker_image_base.rb', line 50

def tag
  @options[:tag]
end

.varsObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/k8sflow/command/docker/docker_image_base.rb', line 20

def vars
  if @vars.nil?
    @vars = {}
    if !options[:vars].nil?
      if options[:vars].is_a?(Hash)
        @vars.merge!(options[:vars])
      else
        @vars.merge!(kv_parse(options[:vars]))
      end
    end
  end

  @vars["tag"] = @options[:tag]
  return @vars
end