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
# 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)
end

Instance Attribute Details

#attributes ⇒ Object (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_cpi ⇒ Object (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_keys ⇒ Object



168
169
170
171
172
173
174
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 168

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

#available_attributes ⇒ Object



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

def available_attributes
  @attributes.keys
end

#available_deployment_sizes ⇒ Object



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

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_resources ⇒ Object

TODO move these validations into a "ValidatedSize" class or similar



137
138
139
140
141
142
143
144
145
146
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 137

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_password ⇒ Object



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

def common_password
  @attributes[:common_password]
end

#deployment_size ⇒ Object



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

def deployment_size
  @attributes[:deployment_size]
end

#dns ⇒ Object



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

def dns
  @attributes[:dns]
end

#format(attribute) ⇒ Object



126
127
128
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 126

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

#immutable_attributes ⇒ Object

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



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

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

#ip_addresses ⇒ Object



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

def ip_addresses
  @attributes[:ip_addresses]
end

#mutable_attribute?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 65

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

#mutable_attributes ⇒ Object

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



56
57
58
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 56

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

#name ⇒ Object



23
24
25
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 23

def name
  @attributes[:name]
end

#persistent_disk ⇒ Object



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

def persistent_disk
  @attributes[:persistent_disk]
end

#required_ports ⇒ Object

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


164
165
166
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 164

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

#security_group ⇒ Object



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

def security_group
  @attributes[:security_group]
end

#set(attribute, value) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 73

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



82
83
84
85
86
87
88
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 82

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

#set_unless_nil(attribute, value) ⇒ Object



69
70
71
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 69

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

#validate(attribute) ⇒ Object



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

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_size ⇒ Object



101
102
103
104
105
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 101

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



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 113

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_mapping ⇒ Object

FIXME only supports a single ip_address



108
109
110
111
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 108

def validate_dns_mapping
  validate_dns_a_record("api.#{dns}", ip_addresses.first)
  validate_dns_a_record("demoapp.#{dns}", ip_addresses.first)
end

#validated_color(attribute) ⇒ Object



130
131
132
133
134
# File 'lib/bosh/cloudfoundry/deployment_attributes.rb', line 130

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