Class: Athlete::Build

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/athlete/build.rb

Constant Summary collapse

@@valid_properties =

Define valid properties

%w{
  name
  registry
  version
}

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#debug, #fatal, #get_loglevel, #info, #loglevel, #warn

Constructor Details

#initializeBuild

Returns a new instance of Build.



18
19
20
21
22
23
24
25
26
27
# File 'lib/athlete/build.rb', line 18

def initialize
  @@valid_properties.each do |property|
    self.class.class_eval {
      define_method(property) do |arg|
        instance_variable_set("@#{property}", arg)
        self.class.class_eval{attr_reader property.to_sym}
      end
    }
  end
end

Class Attribute Details

.buildsObject

Returns the value of attribute builds.



8
9
10
# File 'lib/athlete/build.rb', line 8

def builds
  @builds
end

Class Method Details

.define(name, &block) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/athlete/build.rb', line 40

def self.define(name, &block)
  build = Athlete::Build.new
  build.name name
  build.instance_eval(&block)
  build.fill_default_values
  @builds[build.name] = build
end

Instance Method Details

#buildObject

Build the Docker image



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/athlete/build.rb', line 90

def build
  info "Building with image name as: '#{final_image_name}', tagged #{determined_version}"
  command = "docker build -t #{final_image_name} ."
  logged_command = get_loglevel == Logger::INFO  ? 'docker build' : command
  retval = Utils::Subprocess.run command do |stdout, stderr, thread|
    info "[#{logged_command}] [stdout] #{stdout}"
    info "[#{logged_command}] [stderr] #{stderr}" if stderr != nil
  end
  if retval.exitstatus != 0
    raise Athlete::CommandExecutionFailed, "The command #{command} exited with non-zero status #{retval.exitstatus}"
  end
end

#determined_versionObject



56
57
58
59
60
61
62
63
# File 'lib/athlete/build.rb', line 56

def determined_version
  case @version
  when :git_head
    return git_tag
  else
    return @version.to_s
  end
end

#fill_default_valuesObject



48
49
50
# File 'lib/athlete/build.rb', line 48

def fill_default_values
  @version_method ||= :git_head
end

#final_image_nameObject



52
53
54
# File 'lib/athlete/build.rb', line 52

def final_image_name
  @final_image_name ||= @registry.nil? ? "#{@name}:#{determined_version}" : "#{@registry}/#{@name}:#{determined_version}"
end

#git_tagObject

Figure out the short hash of the current HEAD



66
67
68
69
70
71
72
73
# File 'lib/athlete/build.rb', line 66

def git_tag
  return @git_tag if @git_tag
  @git_tag = `git rev-parse --short HEAD 2>&1`.chomp
  if $? != 0
    raise Athlete::BuildFailedException, "Could not determine git hash of HEAD, output was: #{@git_tag}"
  end
  @git_tag
end

#image_name_with_specified_version(version) ⇒ Object

Create the image name with a specified git tag



76
77
78
# File 'lib/athlete/build.rb', line 76

def image_name_with_specified_version(version)
  @registry.nil? ? "#{@name}:#{version}" : "#{@registry}/#{@name}:#{version}"
end

#perform(should_push) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/athlete/build.rb', line 80

def perform(should_push)
  build
  if should_push
    push
  else
    info "Skipping push of image as --no-push was specified"
  end
end

#pushObject

Push image to remote registry (Docker Hub or private registry)



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/athlete/build.rb', line 104

def push
  if @registry.nil?
    info "Preparing to push image to the Docker Hub"
  else
    info "Preparing to push image to '#{@registry}'"
  end
  
  command = "docker push #{final_image_name}"
  retval = Utils::Subprocess.run "docker push #{final_image_name}" do |stdout, stderr, thread|
    info "[#{logged_command}] [stdout] #{stdout}"
    info "[#{logged_command}] [stderr] #{stderr}" if stderr != nil
  end
  
  if retval.exitstatus != 0
    raise Athlete::CommandExecutionFailed, "The command #{command} exited with non-zero status #{retval.exitstatus}"
  end
end

#readable_outputObject



122
123
124
125
126
127
128
129
130
# File 'lib/athlete/build.rb', line 122

def readable_output
  lines = []
  lines << "  Build name: #{@name}"
  @@valid_properties.sort.each do |property|
    next if property == 'name'
    lines << sprintf("    %-10s: %s", property, instance_variable_get("@#{property}")) if instance_variable_get("@#{property}")
  end
  puts lines.join("\n")
end

#setup_dsl_methodsObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/athlete/build.rb', line 29

def setup_dsl_methods
  @@valid_properties.each do |property|
    self.class.class_eval {
      define_method(property) do |arg|
        instance_variable_set("@#{property}", arg)
        self.class.class_eval{attr_reader property.to_sym}
      end
    }
  end
end