Class: Vanagon::Engine::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/vanagon/engine/base.rb

Direct Known Subclasses

AlwaysBeScheduling, Docker, Ec2, Hardware, Local, Pooler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(platform, target = nil, **opts) ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
14
15
# File 'lib/vanagon/engine/base.rb', line 9

def initialize(platform, target = nil, **opts)
  @platform = platform
  @required_attributes = ["ssh_port"]
  @target = target if target
  @target_user = @platform.target_user
  @remote_workdir_path = opts[:remote_workdir]
end

Instance Attribute Details

#remote_workdirObject

Returns the value of attribute remote_workdir.



7
8
9
# File 'lib/vanagon/engine/base.rb', line 7

def remote_workdir
  @remote_workdir
end

#targetObject

Returns the value of attribute target.



7
8
9
# File 'lib/vanagon/engine/base.rb', line 7

def target
  @target
end

Instance Method Details

#build_host_nameObject

Get the engine specific name of the host to build on

Raises:



23
24
25
# File 'lib/vanagon/engine/base.rb', line 23

def build_host_name
  raise Vanagon::Error, '#build_host_name has not been implemented for your engine.'
end

#dispatch(command, return_output = false) ⇒ Object

Dispatches the command for execution



34
35
36
# File 'lib/vanagon/engine/base.rb', line 34

def dispatch(command, return_output = false)
  Vanagon::Utilities.remote_ssh_command("#{@target_user}@#{@target}", command, @platform.ssh_port, return_command_output: return_output)
end

#get_remote_workdirObject



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/vanagon/engine/base.rb', line 60

def get_remote_workdir
  unless @remote_workdir
    if @remote_workdir_path
      dispatch("mkdir -p #{@remote_workdir_path}", true)
      @remote_workdir = @remote_workdir_path
    else
      @remote_workdir = dispatch("#{@platform.mktemp} 2>/dev/null", true)
    end
  end
  @remote_workdir
end

#nameObject

Get the engine name



18
19
20
# File 'lib/vanagon/engine/base.rb', line 18

def name
  'base'
end

#retrieve_built_artifact(artifacts_to_fetch, no_packaging) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/vanagon/engine/base.rb', line 76

def retrieve_built_artifact(artifacts_to_fetch, no_packaging)
  output_path = 'output/'
  FileUtils.mkdir_p(output_path)
  unless no_packaging
    artifacts_to_fetch << "#{@remote_workdir}/output/*"
  end
  artifacts_to_fetch.each do |path|
    Vanagon::Utilities.rsync_from(path, "#{@target_user}@#{@target}", output_path, @platform.ssh_port)
  end
end

#select_targetObject

This method is used to obtain a vm to build upon For the base class we just return the target that was passed in



29
30
31
# File 'lib/vanagon/engine/base.rb', line 29

def select_target
  @target or raise Vanagon::Error, '#select_target has not been implemented for your engine.'
end

#setupObject

Applies the steps needed to extend the system to build packages against the target system



44
45
46
47
48
49
# File 'lib/vanagon/engine/base.rb', line 44

def setup
  unless @platform.provisioning.empty?
    script = @platform.provisioning.join(' && ')
    dispatch(script)
  end
end

#ship_workdir(workdir) ⇒ Object



72
73
74
# File 'lib/vanagon/engine/base.rb', line 72

def ship_workdir(workdir)
  Vanagon::Utilities.rsync_to("#{workdir}/*", "#{@target_user}@#{@target}", @remote_workdir, @platform.ssh_port)
end

#startup(workdir) ⇒ Object

This method will take care of validation and target selection all at once as an easy shorthand to call from the driver



53
54
55
56
57
58
# File 'lib/vanagon/engine/base.rb', line 53

def startup(workdir)
  validate_platform
  select_target
  setup
  get_remote_workdir
end

#teardownObject

Steps needed to tear down or clean up the system after the build is complete



40
# File 'lib/vanagon/engine/base.rb', line 40

def teardown; end

#validate_platformObject

Ensures that the platform defines the attributes that the engine needs to function.

Raises:

  • (Vanagon::Error)

    an error is raised if a needed attribute is not defined



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/vanagon/engine/base.rb', line 90

def validate_platform
  missing_attrs = []
  @required_attributes.each do |attr|
    if !@platform.instance_variables.include?("@#{attr}".to_sym) || @platform.instance_variable_get("@#{attr}".to_sym).nil?
      missing_attrs << attr
    end
  end

  if missing_attrs.empty?
    return true
  else
    raise Vanagon::Error, "The following required attributes were not set in '#{@platform.name}': #{missing_attrs.join(', ')}."
  end
end