Class: Job

Inherits:
Object
  • Object
show all
Defined in:
lib/job.rb

Overview

Job describes a single BOSH job

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec:, namespace:, client:, client_stateful_set:, self_name: ENV['HOSTNAME']) ⇒ Job

Returns a new instance of Job.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/job.rb', line 7

def initialize(spec:, namespace:, client:, client_stateful_set:, self_name: ENV['HOSTNAME'])
  @spec = spec
  @namespace = namespace
  @client = client
  @self_name = self_name
  links = @spec['links'] = KubeLinkSpecs.new(@spec, @namespace, @client, client_stateful_set)

  # Figure out whether _this_ should bootstrap
  pods = @client.get_pods(namespace: @namespace, label_selector: "app.kubernetes.io/component=#{self_role}")
  pods_per_image = links.get_pods_per_image(pods)
  @spec['bootstrap'] = pods_per_image[self_pod..uid] < 2
end

Instance Attribute Details

#specObject (readonly)

Returns the value of attribute spec.



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

def spec
  @spec
end

Instance Method Details

#exported_propertiesObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/job.rb', line 22

def exported_properties
  @exported_properties ||= {}.tap do |exported_properties|
    spec['exported_properties'].each do |prop|
      src = spec['properties']
      dst = exported_properties
      keys = prop.split('.')
      leaf = keys.pop
      keys.each do |key|
        dst[key] ||= {}
        dst = dst[key]
        src = src.fetch(key, {})
      end
      dst[leaf] = src[leaf]
    end
  end
end

#generate(input_file_path, output_file_path, dns_encoder) ⇒ Object

Process the given template using a provided spec and output filename

Parameters:

  • input_file_path (String)

    The input filepath for the template

  • output_file_path (String)

    The output filepath

  • dns_encoder (KubeDNSEncoder)

    BOSH DNS encoder

Raises:



52
53
54
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
80
81
82
83
84
85
86
87
# File 'lib/job.rb', line 52

def generate(input_file_path, output_file_path, dns_encoder)
  # Make sure we're getting all the parameters we need
  raise NoDataProvided if spec.nil?
  raise NoInputFileProvided if input_file_path.nil?
  raise NoOutputFileProvided if output_file_path.nil?

  # Read the erb template
  begin
    perms = File.stat(input_file_path).mode
    erb_template = ERB.new(File.read(input_file_path), nil, '-')
    erb_template.filename = input_file_path
  rescue Errno::ENOENT
    raise "failed to read template file #{input_file_path}"
  end

  # Create a BOSH evaluation context
  evaluation_context = Bosh::Template::EvaluationContext.new(spec, dns_encoder)
  # Process the Template
  output = erb_template.result(evaluation_context.get_binding)

  begin
    # Open the output file
    output_dir = File.dirname(output_file_path)
    FileUtils.mkdir_p(output_dir)
    out_file = File.open(output_file_path, 'w')
    # Write results to the output file
    out_file.write(output)
    # Set the appropriate permissions on the output file
    out_file.chmod(perms)
  rescue Errno::ENOENT, Errno::EACCES => e
    out_file = nil
    raise "failed to open output file #{output_file_path}: #{e}"
  ensure
    out_file&.close
  end
end

#self_podObject



39
40
41
# File 'lib/job.rb', line 39

def self_pod
  @self_pod ||= @client.get_pod(@self_name, @namespace)
end

#self_roleObject



43
44
45
# File 'lib/job.rb', line 43

def self_role
  self_pod['metadata']['labels']['app.kubernetes.io/component']
end