Class: Bosh::Cli::Resources::Job

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

Constant Summary collapse

BUILD_HOOK_FILES =
['prepare']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job_base, release_base, packages) ⇒ Job

Returns a new instance of Job


14
15
16
17
18
# File 'lib/cli/resources/job.rb', line 14

def initialize(job_base, release_base, packages)
  @release_base = Pathname.new(release_base)
  @job_base = Pathname.new(job_base)
  @package_dependencies = packages
end

Instance Attribute Details

#job_baseObject (readonly)

Returns the value of attribute job_base


12
13
14
# File 'lib/cli/resources/job.rb', line 12

def job_base
  @job_base
end

#package_dependenciesObject (readonly)

Returns the value of attribute package_dependencies


12
13
14
# File 'lib/cli/resources/job.rb', line 12

def package_dependencies
  @package_dependencies
end

#release_baseObject (readonly)

Returns the value of attribute release_base


12
13
14
# File 'lib/cli/resources/job.rb', line 12

def release_base
  @release_base
end

Class Method Details

.discover(release_base, packages) ⇒ Object

Parameters:

  • directory (String)

    base Release directory


6
7
8
9
10
# File 'lib/cli/resources/job.rb', line 6

def self.discover(release_base, packages)
  jobs_folder_contents = Dir[File.join(release_base, 'jobs', '*')]
  job_folders = jobs_folder_contents.select { |f| File.directory?(f) }
  job_folders.map { |job_base| new(job_base, release_base, packages) }
end

Instance Method Details

#additional_fingerprintsObject


30
31
32
# File 'lib/cli/resources/job.rb', line 30

def additional_fingerprints
  []
end

#dependenciesObject


34
35
36
# File 'lib/cli/resources/job.rb', line 34

def dependencies
  package_dependencies #TODO: should this be packages or package_dependencies?
end

#filesObject


46
47
48
49
50
# File 'lib/cli/resources/job.rb', line 46

def files
  files = (templates_files + monit_files).map { |absolute_path| [absolute_path, relative_path(absolute_path)] }
  files << [File.join(job_base, 'spec'), 'job.MF']
  files
end

#format_fingerprint(digest, filename, name, file_mode) ⇒ Object


89
90
91
# File 'lib/cli/resources/job.rb', line 89

def format_fingerprint(digest, filename, name, file_mode)
  "%s%s%s" % [File.basename(filename), digest, file_mode]
end

#nameObject


26
27
28
# File 'lib/cli/resources/job.rb', line 26

def name
  spec['name']
end

#plural_typeObject


42
43
44
# File 'lib/cli/resources/job.rb', line 42

def plural_type
  'jobs'
end

#propertiesObject


99
100
101
# File 'lib/cli/resources/job.rb', line 99

def properties
  spec['properties'] || {}
end

#run_script(script_name, *args) ⇒ Object


93
94
95
96
97
# File 'lib/cli/resources/job.rb', line 93

def run_script(script_name, *args)
  if BUILD_HOOK_FILES.include?(script_name.to_s)
    send(:"run_script_#{script_name}", *args)
  end
end

#singular_typeObject


38
39
40
# File 'lib/cli/resources/job.rb', line 38

def singular_type
  'job'
end

#specObject


20
21
22
23
24
# File 'lib/cli/resources/job.rb', line 20

def spec
  @spec ||= load_yaml_file(job_base.join('spec'))
rescue Exception => e
  raise Bosh::Cli::InvalidJob, "Job spec is missing or invalid: #{e.message}"
end

#validate!Object

TODO: check dependency packages


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/cli/resources/job.rb', line 53

def validate!
  if name.blank?
    raise Bosh::Cli::InvalidJob, 'Job name is missing'
  end

  unless name.bosh_valid_id?
    raise Bosh::Cli::InvalidJob, "'#{name}' is not a valid BOSH identifier"
  end

  unless  spec['templates'].nil? or spec['templates'].is_a?(Hash)
    raise Bosh::Cli::InvalidJob, "Incorrect templates section in '#{name}' job spec (Hash expected, #{spec['templates'].class} given)"
  end

  if extra_templates.size > 0
    raise Bosh::Cli::InvalidJob, "There are unused template files for job '#{name}': #{extra_templates.join(", ")}"
  end

  if missing_templates.size > 0
    raise Bosh::Cli::InvalidJob, "Some template files required by '#{name}' job are missing: #{missing_templates.join(", ")}"
  end

  if missing_packages.size > 0
    raise Bosh::Cli::InvalidJob, "Some packages required by '#{name}' job are missing: #{missing_packages.join(", ")}"
  end

  if spec.has_key?('properties')
    unless spec['properties'].is_a?(Hash)
      raise Bosh::Cli::InvalidJob, "Incorrect properties section in '#{name}' job spec (Hash expected, #{spec['properties'].class} given)"
    end
  end

  unless monit_files.size > 0
    raise Bosh::Cli::InvalidJob, "Cannot find monit file for '#{name}'"
  end
end