Skip to content

Commit 6aa55e9

Browse files
committed
Document classes and methods, as part of the template rendering documentation review
1 parent 970d6ac commit 6aa55e9

File tree

10 files changed

+130
-16
lines changed

10 files changed

+130
-16
lines changed

src/bosh-director-core/lib/bosh/director/core/templates/job_instance_renderer.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,39 @@
33
require 'bosh/director/formatter_helper'
44

55
module Bosh::Director::Core::Templates
6+
7+
# @param [Array<DeploymentPlan::Job>] instance_jobs
8+
# @param [JobTemplateLoader] job_template_loader
69
class JobInstanceRenderer
710
def initialize(templates, job_template_loader)
811
@templates = templates
912
@job_template_loader = job_template_loader
1013
end
1114

15+
# Render all templates for a Bosh instance.
16+
#
17+
# From a list of instance jobs (typically comming from a single instance
18+
# plan, so they cover all templates of some instance) this method is
19+
# responsible for orchestrating several tasks.
20+
#
21+
# Lazily-delegated work to a 'JobTemplateLoader' object:
22+
# - Load all templates of the release job that the instance job is
23+
# referring to
24+
# - Convert each of these to a 'JobTemplateRenderer' object
25+
#
26+
# Work done here on top of this:
27+
# - Render each template with the necessary bindings (comming from
28+
# deployment manifest properties) for building the special 'spec'
29+
# object that the ERB rendring code can use.
30+
#
31+
# The actual rendering of each template is delegated to its related
32+
# 'JobTemplateRenderer' object, as created in the first place by the
33+
# 'JobTemplateLoader' object.
34+
#
35+
# @param [Hash] spec_object A hash of properties that will finally result
36+
# in the `spec` object exposed to ERB templates
37+
# @return [RenderedJobInstance] An object containing the rendering results
38+
# (when successful)
1239
def render(spec)
1340
errors = []
1441

src/bosh-director-core/lib/bosh/director/core/templates/job_template_loader.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,24 @@ def initialize(logger, template_blob_cache, link_provider_intents, dns_encoder)
1515
@dns_encoder = dns_encoder
1616
end
1717

18+
# Perform these operations in order:
19+
#
20+
# 1. Download (or fetch from cache) a single job blob tarball.
21+
#
22+
# 2. Extract the job blob tarball in a temporary directory.
23+
#
24+
# 3. Access the extracted 'job.MF' manifest from the job blob.
25+
#
26+
# 4. Load all ERB templates (including the 'monit' file and all other
27+
# declared files within the 'templates' subdir) and create one
28+
# 'SourceErb' object for each of these.
29+
#
30+
# 5. Create and return a 'JobTemplateRenderer' object, populated with
31+
# all created 'SourceErb' objects.
32+
#
33+
# @param [DeploymentPlan::Job] instance_job The job whose templates
34+
# should be rendered
35+
# @return [JobTemplateRenderer] Object that can render the templates
1836
def process(job_template)
1937
template_dir = extract_template(job_template)
2038
manifest = Psych.load_file(File.join(template_dir, 'job.MF'), aliases: true)

src/bosh-director/lib/bosh/director/config_server/variables_interpolator.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,19 @@ def initialize
77
@cache_by_job_name = {}
88
end
99

10-
# @param [Hash] template_spec_properties Hash to be interpolated
11-
# @param [Hash] deployment_name The deployment context in-which the interpolation will occur
12-
# @param [VariableSet] variable_set The variable set which the interpolation will use.
13-
# @return [Hash] A Deep copy of the interpolated template_spec_properties
10+
# From a `job_name => job_properties` hash of instance group properties,
11+
# resolve the `((var_name))`` placeholders, fetching their values from the
12+
# config server. Most often, these placeholders refers to secrets stored
13+
# in CredHub.
14+
#
15+
# @param [Hash] instance_group_properties a hash of per-job properties, to
16+
# be interpolated
17+
# @param [String] deployment_name The name of the deployment, as context
18+
# in-which the interpolation will occur
19+
# @param [String] instance_group_name The name of the instance group
20+
# @param [VariableSet] variable_set The variable set which the
21+
# interpolation will use.
22+
# @return [Hash] A Deep copy of the interpolated instance_group_properties
1423
def interpolate_template_spec_properties(template_spec_properties, deployment_name, instance_name, variable_set)
1524
if template_spec_properties.nil?
1625
return template_spec_properties

src/bosh-director/lib/bosh/director/deployment_plan/instance_group.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,19 @@ def create_desired_instances(count, deployment)
409409

410410
private
411411

412+
# Returns the aggregated list of package names, considering all jobs
413+
# desired on this instance group, with no duplicate.
414+
#
415+
# @return [Array<String>] The list of package names
412416
def run_time_dependencies
413417
run_time_packages.map(&:name)
414418
end
415419

420+
# The aggregated list of models for all packages to install on instances
421+
# of the group, considering all jobs desired on this instance group,
422+
# with no duplicate.
423+
#
424+
# @return [Array<Models::Package>] The list of packages
416425
def run_time_packages
417426
jobs.flat_map(&:package_models).uniq
418427
end

src/bosh-director/lib/bosh/director/deployment_plan/instance_plan.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,11 @@ def spec
379379
InstanceSpec.create_from_instance_plan(self)
380380
end
381381

382+
# Returns the desired jobs for the instance
383+
#
384+
# Here “template” is the old Bosh v1 name for “job”.
385+
#
386+
# @return [Array<DeploymentPlan::Job>] List of desired jobs
382387
def templates
383388
@desired_instance.instance_group.jobs
384389
end

src/bosh-director/lib/bosh/director/deployment_plan/instance_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def self.create_from_instance_plan(instance_plan)
1717

1818
spec = {
1919
'deployment' => deployment_name,
20-
'job' => instance_group.spec,
20+
'job' => instance_group.spec, # <- here 'job' is old Bosh v1 naming, meaning 'instance group'
2121
'index' => instance.index,
2222
'bootstrap' => instance.bootstrap?,
2323
'lifecycle' => instance_group.lifecycle,

src/bosh-director/lib/bosh/director/deployment_plan/job.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ def initialize(release, name)
3434
@properties = {}
3535
end
3636

37-
# Looks up template model and its package models in DB
37+
# Looks up job model and its package models in DB.
38+
#
39+
# Here “template” is the old Bosh v1 name for “job”.
40+
#
3841
# @return [void]
3942
def bind_models
4043
@model = @release.get_template_model_by_name(@name)

src/bosh-director/lib/bosh/director/deployment_plan/release_version.rb

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,11 @@ def spec
9191
}
9292
end
9393

94-
# Looks up up template model by template name
95-
# @param [String] name Template name
94+
# Looks up up job model by job name.
95+
#
96+
# Here “template” is the old Bosh v1 name for “job”.
97+
#
98+
# @param [String] name Job name
9699
# @return [Models::Template]
97100
def get_template_model_by_name(name)
98101
@all_jobs ||= @model.templates.each_with_object({}) do |job, all_jobs|
@@ -109,25 +112,38 @@ def get_package_model_by_name(name)
109112
@model.package_by_name(name)
110113
end
111114

112-
# Adds template to a list of templates used by this release for the
113-
# current deployment
115+
# Adds a job to a list of jobs used by this release for the current
116+
# deployment.
117+
#
118+
# Here “template” is the old Bosh v1 name for “job”.
119+
#
114120
# @param [String] options Template name
115121
def get_or_create_template(name)
116122
@jobs[name] ||= Job.new(self, name)
117123
end
118124

125+
# Return a given job, identified by name.
126+
#
127+
# Here “template” is the old Bosh v1 name for “job”.
128+
#
119129
# @param [String] name Job name
120130
# @return [DeploymentPlan::Job] Job with given name used by this
121-
# release (if any)
131+
# release (if any)
122132
def template(name)
123133
@jobs[name]
124134
end
125135

126-
# Returns a list of job templates that need to be included into this
127-
# release. Note that this is not just a list of all templates existing
128-
# in the release but rather a list of templates for jobs that are included
129-
# into current deployment plan.
130-
# @return [Array<DeploymentPlan::Job>] List of job templates
136+
# Returns a list of jobs from the release that are used by the
137+
# deployment.
138+
#
139+
# Note that this is not the full list of all jobs existing in the
140+
# release, but a subset list of the jobs defined in that release that
141+
# are used by the current deployment, and thus included in its plan.
142+
#
143+
# Here “template” is the old Bosh v1 name for “job”.
144+
#
145+
# @return [Array<DeploymentPlan::Job>] List of the release jobs used by
146+
# the deployment
131147
def templates
132148
@jobs.values
133149
end

src/bosh-director/lib/bosh/director/job_renderer.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44

55
module Bosh::Director
66
class JobRenderer
7+
8+
# Render the related job templates for each instance plan passed as
9+
# argument in the 'instance_plans' array.
10+
#
11+
# @param [Logging::Logger] logger A logger where to log activity
12+
# @param [Array<InstancePlan>] instance_plans A list of instance plans
13+
# @param [TemplateBlobCache] cache A cache through which job blobs are to
14+
# be fetched
15+
# @param [DnsEncoder] dns_encoder A DNS encoder for generating Bosh DNS
16+
# queries out of context and criteria
17+
# @param [Array<LinkProviderIntent>] link_provider_intents Relevant
18+
# context-dependant
19+
# link provider
20+
# intents
721
def self.render_job_instances_with_cache(logger, instance_plans, cache, dns_encoder, link_provider_intents)
822
job_template_loader = Core::Templates::JobTemplateLoader.new(
923
logger,
@@ -17,6 +31,15 @@ def self.render_job_instances_with_cache(logger, instance_plans, cache, dns_enco
1731
end
1832
end
1933

34+
# For one instance plan, create a 'JobInstanceRenderer' object that will
35+
# lazily load the ERB templates for all desired jobs on the instance, then
36+
# render these templates with the bindings populated by the
37+
# 'spec' properties of the instance plan.
38+
#
39+
# @param [DeploymentPlan::InstancePlan] instance_plan An instance plan
40+
# @param [JobTemplateLoader] loader The object that will load the ERB
41+
# templates
42+
# @param [Logging::Logger] logger A logger where to log activity
2043
def self.render_job_instance(instance_plan, loader, logger)
2144
instance = instance_plan.instance
2245

src/bosh-director/lib/bosh/director/models/template.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
module Bosh::Director::Models
2+
3+
# This class models a job, as defined within a Bosh Release.
4+
#
5+
# Here “template” is the old Bosh v1 name for “job”.
26
class Template < Sequel::Model(Bosh::Director::Config.db)
37
many_to_one :release
48
many_to_many :release_versions

0 commit comments

Comments
 (0)