Class: Vanagon::Driver

Inherits:
Object
  • Object
show all
Includes:
Utilities
Defined in:
lib/vanagon/driver.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utilities

#erb_file, #erb_string, #ex, #find_program_on_path, #get_md5sum, #get_sum, #http_request, #http_request_code, #http_request_generic, #local_command, #remote_ssh_command, #retry_with_timeout, #rsync_from, #rsync_to, #ssh_command

Constructor Details

#initialize(platform, project, options = {}) ⇒ Driver

rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity



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
# File 'lib/vanagon/driver.rb', line 24

def initialize(platform, project, options = {}) # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
  @options = options
  @verbose = options[:verbose] || false
  @preserve = options[:preserve] || false
  @workdir = options[:workdir] || Dir.mktmpdir

  @@configdir = options[:configdir] || File.join(Dir.pwd, "configs")
  components = options[:components] || []
  only_build = options[:only_build]

  @platform = Vanagon::Platform.load_platform(platform, File.join(@@configdir, "platforms"))
  @project = Vanagon::Project.load_project(
    project, File.join(@@configdir, "projects"), @platform, components
  )
  @project.settings[:verbose] = options[:verbose]
  @project.settings[:skipcheck] = options[:skipcheck] || false
  if only_build && !only_build.empty?
    filter_out_components(only_build)
  end
  loginit('vanagon_hosts.log')

  @remote_workdir = options[:'remote-workdir']

  engine = pick_engine(options)
  load_engine_object(engine, @platform, options[:target])
end

Instance Attribute Details

#platformObject

Returns the value of attribute platform.



14
15
16
# File 'lib/vanagon/driver.rb', line 14

def platform
  @platform
end

#preserveObject

Returns the value of attribute preserve.



14
15
16
# File 'lib/vanagon/driver.rb', line 14

def preserve
  @preserve
end

#projectObject

Returns the value of attribute project.



14
15
16
# File 'lib/vanagon/driver.rb', line 14

def project
  @project
end

#remote_workdirObject

Returns the value of attribute remote_workdir.



14
15
16
# File 'lib/vanagon/driver.rb', line 14

def remote_workdir
  @remote_workdir
end

#targetObject

Returns the value of attribute target.



14
15
16
# File 'lib/vanagon/driver.rb', line 14

def target
  @target
end

#verboseObject

Returns the value of attribute verbose.



14
15
16
# File 'lib/vanagon/driver.rb', line 14

def verbose
  @verbose
end

#workdirObject

Returns the value of attribute workdir.



14
15
16
# File 'lib/vanagon/driver.rb', line 14

def workdir
  @workdir
end

Class Method Details

.configdirObject



95
96
97
# File 'lib/vanagon/driver.rb', line 95

def self.configdir
  @@configdir
end

.loggerObject



99
100
101
# File 'lib/vanagon/driver.rb', line 99

def self.logger
  @@logger
end

Instance Method Details

#build_host_infoObject



103
104
105
# File 'lib/vanagon/driver.rb', line 103

def build_host_info
  { "name" => @engine.build_host_name, "engine" => @engine.name }
end

#camelize(string) ⇒ Object



85
86
87
88
89
# File 'lib/vanagon/driver.rb', line 85

def camelize(string)
  string.gsub(/(?:^|_)([a-z])?/) do |match|
    (Regexp.last_match[1] || '').capitalize
  end
end

#cleanup_workdirObject



91
92
93
# File 'lib/vanagon/driver.rb', line 91

def cleanup_workdir
  FileUtils.rm_rf(workdir)
end

#dependenciesObject



187
188
189
190
191
192
193
194
195
196
# File 'lib/vanagon/driver.rb', line 187

def dependencies
  # Simple sanity check for the project
  if @project.version.nil? || @project.version.empty?
    raise Vanagon::Error, "Project requires a version set, all is lost."
  end

  VanagonLogger.info "creating dependencies list"
  @project.fetch_sources(workdir, retry_count, timeout)
  @project.cli_manifest_json(@platform)
end

#filter_out_components(only_build) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/vanagon/driver.rb', line 51

def filter_out_components(only_build)
  # map each element in the only_build array to it's set of filtered components, then
  # flatten all the results in to one array and set project.components to that.
  @project.components = only_build.flat_map { |comp| @project.filter_component(comp) }.uniq
  if @verbose
    VanagonLogger.info "Only building:"
    @project.components.each { |comp| VanagonLogger.info comp.name }
  end
end

#install_build_dependenciesObject

rubocop:disable Metrics/AbcSize



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/vanagon/driver.rb', line 114

def install_build_dependencies # rubocop:disable Metrics/AbcSize
  unless list_build_dependencies.empty?
    if @platform.build_dependencies && @platform.build_dependencies.command && !@platform.build_dependencies.command.empty?
      @engine.dispatch("#{@platform.build_dependencies.command} #{list_build_dependencies.join(' ')} #{@platform.build_dependencies.suffix}")
    elsif @platform.respond_to?(:install_build_dependencies)
      @engine.dispatch(@platform.install_build_dependencies(list_build_dependencies))
    else
      raise Vanagon::Error, "No method defined to install build dependencies for #{@platform.name}"
    end
  end
end

#list_build_dependenciesObject

Returns the set difference between the build_requires and the components to get a list of external dependencies that need to be installed.



110
111
112
# File 'lib/vanagon/driver.rb', line 110

def list_build_dependencies
  @project.components.map(&:build_requires).flatten.uniq - @project.components.map(&:name)
end

#load_engine_object(engine_type, platform, target) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/vanagon/driver.rb', line 77

def load_engine_object(engine_type, platform, target)
  require "vanagon/engine/#{engine_type}"
  @engine = Object::const_get("Vanagon::Engine::#{camelize(engine_type)}")
    .new(platform, target, remote_workdir: remote_workdir)
rescue StandardError, ScriptError => e
  raise Vanagon::Error.wrap(e, "Could not load the desired engine '#{engine_type}'")
end

#pick_engine(options) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/vanagon/driver.rb', line 61

def pick_engine(options)
  if options[:engine] && !options[:target]
    options[:engine]
  elsif @platform.build_hosts
    'hardware'
  elsif @platform.aws_ami
    'ec2'
  elsif @platform.docker_image
    'docker'
  elsif options[:target]
    'base'
  else
    'always_be_scheduling'
  end
end

#renderObject



174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/vanagon/driver.rb', line 174

def render
  # Simple sanity check for the project
  if @project.version.nil? || @project.version.empty?
    raise Vanagon::Error, "Project requires a version set, all is lost."
  end

  VanagonLogger.info "rendering Makefile"
  @project.fetch_sources(workdir, retry_count, timeout)
  @project.make_bill_of_materials(workdir)
  @project.generate_packaging_artifacts(workdir)
  @project.make_makefile(workdir)
end

#retry_countObject



20
21
22
# File 'lib/vanagon/driver.rb', line 20

def retry_count
  @retry_count ||= @project.retry_count || ENV["VANAGON_RETRY_COUNT"] || 1
end

#runObject

rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/vanagon/driver.rb', line 126

def run # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
  # Simple sanity check for the project
  if @project.version.nil? or @project.version.empty?
    raise Vanagon::Error, "Project requires a version set, all is lost."
  end

  # if no_packaging has been set in the project, don't execute the
  # whole makefile. Instead just perform the installation.
  make_target = ''
  if @project.no_packaging
    make_target = "#{@project.name}-project"
  end

  @engine.startup(workdir)
  VanagonLogger.info "Target is #{@engine.target}"
  Vanagon::Utilities.retry_with_timeout(retry_count, timeout) do
    install_build_dependencies
  end
  @project.fetch_sources(workdir, retry_count, timeout)

  @project.make_makefile(workdir)
  @project.make_bill_of_materials(workdir)
  # Don't generate packaging artifacts if no_packaging is set
  @project.generate_packaging_artifacts(workdir) unless @project.no_packaging
  @project.save_manifest_json(@platform, workdir)
  @engine.ship_workdir(workdir)
  @engine.dispatch("(cd #{@engine.remote_workdir}; #{@platform.make} #{make_target})")
  @engine.retrieve_built_artifact(@project.artifacts_to_fetch, @project.no_packaging)
  @project.publish_yaml_settings(@platform)

  if %i[never on-failure].include? @preserve
    @engine.teardown
    cleanup_workdir
  end
rescue StandardError => e
  if [:never].include? @preserve
    @engine.teardown
    cleanup_workdir
  end
  VanagonLogger.error(e)
  VanagonLogger.error e.backtrace.join("\n")
  raise e
ensure
  if ["hardware", "ec2"].include?(@engine.name)
    @engine.teardown
  end
end

#timeoutObject



16
17
18
# File 'lib/vanagon/driver.rb', line 16

def timeout
  @timeout ||= @project.timeout || ENV["VANAGON_TIMEOUT"] || 7200
end