Module: Kubes::Docker::Strategy::ImageName

Extended by:
Memoist
Included in:
Utils
Defined in:
lib/kubes/docker/strategy/image_name.rb

Constant Summary collapse

@@image_name =
nil
@@timestamp =
Time.now.strftime('%Y-%m-%dT%H-%M-%S')

Instance Method Summary collapse

Instance Method Details

#argsObject



62
63
64
65
# File 'lib/kubes/docker/strategy/image_name.rb', line 62

def args
  # base at end in case of redirection. IE: command > /path
  custom.args + default.args
end

#customObject



67
68
69
70
71
# File 'lib/kubes/docker/strategy/image_name.rb', line 67

def custom
  custom = Kubes::Args::Custom.new(@name, "#{Kubes.root}/.kubes/config/args/docker.rb")
  custom.build
  custom
end

#defaultObject



74
75
76
# File 'lib/kubes/docker/strategy/image_name.rb', line 74

def default
  Kubes::Docker::Args::Default.new(@name, image_name, @options)
end

#generate_nameObject



44
45
46
47
# File 'lib/kubes/docker/strategy/image_name.rb', line 44

def generate_name
  # IE: tongueroo/demo:kubes-
  ["#{repo}:kubes-#{@@timestamp}", git_sha].compact.join('-')
end

#git_shaObject



53
54
55
56
57
58
59
60
# File 'lib/kubes/docker/strategy/image_name.rb', line 53

def git_sha
  return @git_sha if @git_sha
  # always call this and dont use the execute method because of the noop option
  git_installed = system("type git > /dev/null 2>&1")
  return unless git_installed && File.exist?("#{Kubes.root}/.git")
  @git_sha = `cd #{Kubes.root} && git rev-parse --short HEAD`
  @git_sha.strip!
end

#image_nameObject

full_image - Includes the tag. Examples:

123456789.dkr.ecr.us-west-2.amazonaws.com/myapp:kubes-2018-04-20T09-29-08-b7d51df
tongueroo/demo-kubes:kubes-2018-04-20T09-29-08-b7d51df


30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/kubes/docker/strategy/image_name.rb', line 30

def image_name
  return generate_name if @options[:generate]
  return @@image_name if @@image_name
  return "tongueroo/demo-kubes:kubes-12345678" if ENV['TEST']

  unless File.exist?(image_state_path)
    logger.error "ERROR: Unable to find #{image_state_path} which contains the last docker image name built with kubes build.  Please run `kubes docker build` first."
    exit 1
  end
  data = JSON.load(IO.read(image_state_path))
  data['image']
end

#image_state_pathObject

output can get entirely wiped so dont use that folder



23
24
25
# File 'lib/kubes/docker/strategy/image_name.rb', line 23

def image_state_path
  Kubes.config.state.path
end

#repoObject



49
50
51
# File 'lib/kubes/docker/strategy/image_name.rb', line 49

def repo
  Kubes.config.repo
end

#reserve_image_nameObject



8
9
10
# File 'lib/kubes/docker/strategy/image_name.rb', line 8

def reserve_image_name
  @@image_name = generate_name
end

#store_image_nameObject

Store this in a file because this name gets reference in other tasks later and we want the image name to stay the same when the commands are run separate in different processes. So we store the state in a file. Only when a new docker build command gets run will the image name state be updated.



16
17
18
19
20
# File 'lib/kubes/docker/strategy/image_name.rb', line 16

def store_image_name
  FileUtils.mkdir_p(File.dirname(image_state_path))
  text = JSON.pretty_generate(image: @@image_name)
  IO.write(image_state_path, text)
end