Class: Bosh::Director::Models::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh/director/models/instance.rb

Instance Method Summary collapse

Instance Method Details

#cloud_properties_hashObject



42
43
44
45
46
47
48
49
# File 'lib/bosh/director/models/instance.rb', line 42

def cloud_properties_hash
  if cloud_properties.nil?
    return {} if spec.nil? || spec['vm_type'].nil?
    spec['vm_type']['cloud_properties'] || {}
  else
    JSON.parse(cloud_properties)
  end
end

#cloud_properties_hash=(hash) ⇒ Object



51
52
53
# File 'lib/bosh/director/models/instance.rb', line 51

def cloud_properties_hash=(hash)
  self.cloud_properties = JSON.dump(hash)
end

#credentialsObject



124
125
126
# File 'lib/bosh/director/models/instance.rb', line 124

def credentials
  object_or_nil(credentials_json)
end

#credentials=(spec) ⇒ Object



128
129
130
# File 'lib/bosh/director/models/instance.rb', line 128

def credentials=(spec)
  self.credentials_json = json_encode(spec)
end

#dns_record_namesObject



55
56
57
58
59
# File 'lib/bosh/director/models/instance.rb', line 55

def dns_record_names
  return nil if dns_records.nil?

  JSON.parse(dns_records)
end

#dns_record_names=(list) ⇒ Object



61
62
63
# File 'lib/bosh/director/models/instance.rb', line 61

def dns_record_names=(list)
  self.dns_records = JSON.dump(list)
end

#latest_rendered_templates_archiveObject



29
30
31
# File 'lib/bosh/director/models/instance.rb', line 29

def latest_rendered_templates_archive
  rendered_templates_archives_dataset.order(:created_at).last
end

#nameObject



65
66
67
# File 'lib/bosh/director/models/instance.rb', line 65

def name
  "#{self.job}/#{self.uuid}"
end

#persistent_diskObject



18
19
20
21
# File 'lib/bosh/director/models/instance.rb', line 18

def persistent_disk
  # Currently we support only 1 persistent disk.
  self.persistent_disks.find { |disk| disk.active }
end

#persistent_disk_cidObject



23
24
25
26
27
# File 'lib/bosh/director/models/instance.rb', line 23

def persistent_disk_cid
  disk = persistent_disk
  return disk.disk_cid if disk
  nil
end

#specObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/bosh/director/models/instance.rb', line 73

def spec
  return nil if spec_json.nil?

  begin
    result = JSON.parse(spec_json)
  rescue JSON::ParserError
    return 'error'
  end
  if result['resource_pool'].nil?
    result
  else
    if result['vm_type'].nil?
      result['vm_type'] = {
        'name' => result['resource_pool']['name'],
        'cloud_properties' => result['resource_pool']['cloud_properties']
      }
    end

    if result['resource_pool']['stemcell'] && result['stemcell'].nil?
      result['stemcell'] = result['resource_pool']['stemcell']
      result['stemcell']['alias'] = result['resource_pool']['name']
    end

    result.delete('resource_pool')

    result
  end
end

#spec=(spec) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/bosh/director/models/instance.rb', line 102

def spec=(spec)
  begin
    self.spec_json = spec.nil? ? nil : JSON.generate(spec)
  rescue JSON::GeneratorError
    self.spec_json = 'error'
  end
end

#spec_p(property_path) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/bosh/director/models/instance.rb', line 110

def spec_p(property_path)
  current_prop = spec
  property_path.split('.').each do |prop|
    return nil if current_prop.nil? || !current_prop.is_a?(Hash)
    current_prop = current_prop[prop]
  end
  current_prop
end

#stale_rendered_templates_archivesObject



33
34
35
36
37
38
39
40
# File 'lib/bosh/director/models/instance.rb', line 33

def stale_rendered_templates_archives
  stale_archives = rendered_templates_archives_dataset
  if latest = latest_rendered_templates_archive
    stale_archives.exclude(id: latest.id)
  else
    stale_archives
  end
end

#to_sObject



69
70
71
# File 'lib/bosh/director/models/instance.rb', line 69

def to_s
  "#{self.job}/#{self.index} (#{self.uuid})"
end

#validateObject



11
12
13
14
15
16
# File 'lib/bosh/director/models/instance.rb', line 11

def validate
  validates_presence [:deployment_id, :job, :index, :state]
  validates_unique [:deployment_id, :job, :index]
  validates_integer :index
  validates_includes %w(started stopped detached), :state
end

#vm_envObject



119
120
121
122
# File 'lib/bosh/director/models/instance.rb', line 119

def vm_env
  return {} if spec.nil?
  spec['env'] || {}
end