Class: Bosh::Cloudfoundry::DeploymentAttributes

Inherits:
Object
  • Object
show all
Includes:
Bosh::Cli::Validation, BoshExtensions
Defined in:
lib/bosh/cloudfoundry/deployment_attributes.rb

Overview

Each version/CPI/size combination of Cloud Foundry deployment template has input attributes that can or must be provided by a user.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(director_client, bosh_status, release_version_cpi, attributes = {}) ⇒ DeploymentAttributes

Returns a new instance of DeploymentAttributes.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 11

def initialize(director_client, bosh_status, release_version_cpi, attributes = {})
  @director_client = director_client
  @bosh_status = bosh_status
  @release_version_cpi = release_version_cpi
  @attributes = attributes
  @attributes[:name] ||= default_name
  @attributes[:deployment_size] ||= default_deployment_size
  @attributes[:persistent_disk] ||= default_persistent_disk
  @attributes[:security_group] ||= default_security_group
  @attributes[:common_password] ||= random_string(12, :common)
  @attributes[:skip_dns_validation] ||= default_skip_dns_validation
  @attributes[:dea_server_ram] ||= default_dea_server_ram
  @attributes[:dea_container_depot_disk] ||= default_dea_container_depot_disk
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



8
9
10
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 8

def attributes
  @attributes
end

#release_version_cpiObject (readonly)

Returns the value of attribute release_version_cpi.



9
10
11
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 9

def release_version_cpi
  @release_version_cpi
end

Instance Method Details

#attributes_with_string_keysObject



181
182
183
184
185
186
187
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 181

def attributes_with_string_keys
  attributes.inject({}) do |mem, key_value|
    key, value = key_value
    mem[key.to_s] = value
    mem
  end
end

#available_attributesObject



62
63
64
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 62

def available_attributes
  @attributes.keys
end

#available_deployment_sizesObject



161
162
163
164
165
166
167
168
169
170
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 161

def available_deployment_sizes
  @available_deployment_sizes ||= begin
    deployment_sizes = release_version_cpi.spec["deployment_sizes"]
    if deployment_sizes && deployment_sizes.is_a?(Array) && deployment_sizes.first.is_a?(String)
      deployment_sizes
    else
      err "template spec needs 'deployment_sizes' key with list of deployment sizes names available; found #{deployment_sizes.spec.inspect}"
    end
  end
end

#available_resourcesObject

TODO move these validations into a “ValidatedSize” class or similar



150
151
152
153
154
155
156
157
158
159
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 150

def available_resources
  @available_resources ||= begin
    resources = release_version_cpi.spec["resources"]
    if resources && resources.is_a?(Array) && resources.first.is_a?(String)
      resources
    else
      err "template spec needs 'resources' key with list of resource pool names available; found #{release_version_cpi.spec.inspect}"
    end
  end
end

#common_passwordObject



42
43
44
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 42

def common_password
  @attributes[:common_password]
end

#dea_container_depot_diskObject



58
59
60
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 58

def dea_container_depot_disk
  @attributes[:dea_container_depot_disk]
end

#dea_server_ramObject



54
55
56
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 54

def dea_server_ram
  @attributes[:dea_server_ram]
end

#deployment_sizeObject



30
31
32
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 30

def deployment_size
  @attributes[:deployment_size]
end

#dnsObject



50
51
52
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 50

def dns
  @attributes[:dns]
end

#format(attribute) ⇒ Object



139
140
141
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 139

def format(attribute)
  value = attributes[attribute.to_sym].to_s
end

#immutable_attributesObject

Attributes & their values that are not to be changed over time



72
73
74
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 72

def immutable_attributes
  release_version_cpi.immutable_attributes.map(&:to_sym)
end

#ip_addressesObject



46
47
48
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 46

def ip_addresses
  @attributes[:ip_addresses]
end

#mutable_attribute?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 76

def mutable_attribute?(attribute)
  mutable_attributes.include?(attribute.to_sym)
end

#mutable_attributesObject

Attributes & their values that can be changed via setters & deployment re-deployed successfully



67
68
69
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 67

def mutable_attributes
  release_version_cpi.mutable_attributes.map(&:to_sym)
end

#nameObject



26
27
28
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 26

def name
  @attributes[:name]
end

#persistent_diskObject



34
35
36
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 34

def persistent_disk
  @attributes[:persistent_disk]
end

#required_portsObject

If using security groups, the following ports must be opened for external access:

  • 22 - ssh to all servers

  • 80 - http traffic to routers

  • 443 - https traffic to routers

  • 4222 - access to nats server



177
178
179
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 177

def required_ports
  [22, 80, 443, 4222]
end

#security_groupObject



38
39
40
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 38

def security_group
  @attributes[:security_group]
end

#set(attribute, value) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 84

def set(attribute, value)
  attributes[attribute.to_sym] = value

  case attribute.to_sym
  when :ip_addresses
    set_default_dns
  end
end

#set_mutable(attribute, value) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 93

def set_mutable(attribute, value)
  if mutable_attribute?(attribute)
    set(attribute, value)
  else
    false
  end
end

#set_unless_nil(attribute, value) ⇒ Object



80
81
82
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 80

def set_unless_nil(attribute, value)
  attributes[attribute.to_sym] = value if value
end

#validate(attribute) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 101

def validate(attribute)
  value = attributes[attribute.to_sym]
  if attribute.to_s == "deployment_size"
    available_deployment_sizes.include?(value)
  elsif attribute.to_s =~ /size$/
    available_resources.include?(value)
  else
    true
  end
end

#validate_deployment_sizeObject



112
113
114
115
116
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 112

def validate_deployment_size
  step("Validating deployment size", "Available deployment sizes are #{available_deployment_sizes.join(', ')}", :fatal) do
    validate(:deployment_size)
  end
end

#validate_dns_a_record(domain, expected_ip_address) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 126

def validate_dns_a_record(domain, expected_ip_address)
  dns_mapping = Bosh::Cloudfoundry::Validations::DnsIpMappingValidation.new(domain, expected_ip_address)
  if dns_mapping.valid?
    say "`#{dns_mapping.domain}' maps to #{dns_mapping.ip_address}".make_green
  else
    say "Validation errors:"
    dns_mapping.errors.each do |error|
      say "- %s" % [error]
    end
    err "`#{dns_mapping.domain}' does not map to #{dns_mapping.ip_address}"
  end
end

#validate_dns_mappingObject

FIXME only supports a single ip_address



119
120
121
122
123
124
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 119

def validate_dns_mapping
  unless @attributes[:skip_dns_validation]
    validate_dns_a_record("api.#{dns}", ip_addresses.first)
    validate_dns_a_record("demoapp.#{dns}", ip_addresses.first)
  end
end

#validated_color(attribute) ⇒ Object



143
144
145
146
147
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 143

def validated_color(attribute)
  validate(attribute) ?
    format(attribute).make_green :
    format(attribute).make_red
end