Top Level Namespace

Defined Under Namespace

Modules: ChefZero, Cheffish, Fhcap Classes: Chef, Thor

Instance Method Summary collapse

Instance Method Details

#aws_bootstrap_options(org_name, environment, instance_options) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cookbooks/provision/libraries/provision.rb', line 52

def aws_bootstrap_options(org_name, environment, instance_options)
  bootstrap_options = {
      image_id: instance_options[:image_id],
      instance_type: instance_options[:instance_type],
      key_name: key_pair_name_for(org_name)
  }
  bootstrap_options[:subnet] = subnet_name_for(org_name, environment, instance_options[:subnet]) if instance_options[:subnet]
  bootstrap_options[:security_group_ids] = instance_options[:security_groups].collect do |security_group|
    security_group_name_for(org_name, security_group)
  end if instance_options[:security_groups]
  bootstrap_options[:block_device_mappings] = instance_options[:block_device_mappings] if instance_options[:block_device_mappings]
  bootstrap_options
end

#aws_converge_options(org_name, environment, instance_options) ⇒ Object



66
67
68
# File 'lib/cookbooks/provision/libraries/provision.rb', line 66

def aws_converge_options(org_name, environment, instance_options)
  {:chef_version => '11.10.4'}
end

#aws_machine_options(org_name, environment, instance_name, instance_options) ⇒ Object

AWS



44
45
46
47
48
49
50
# File 'lib/cookbooks/provision/libraries/provision.rb', line 44

def aws_machine_options(org_name, environment, instance_name, instance_options)
  {
      :bootstrap_options => bootstrap_options_for('aws', org_name, environment, instance_options),
      :convergence_options => converge_options_for('aws', org_name, environment, instance_options),
      :aws_tags => {'Name' => instance_name, 'fh-tag-org' => org_name, 'fh-tag-environment' => [org_name,environment].join('-'), 'fh-tag-role' => instance_name.split('-').last.gsub(/\d+/, "")}
  }
end

#bootstrap_options_for(provider, org_name, environment, instance_options) ⇒ Object



34
35
36
# File 'lib/cookbooks/provision/libraries/provision.rb', line 34

def bootstrap_options_for(provider, org_name, environment, instance_options)
  send(:"#{provider}_bootstrap_options", org_name, environment, instance_options)
end

#cert_name_for(org_name, environment, cert_name) ⇒ Object



144
145
146
# File 'lib/cookbooks/provision/libraries/provision.rb', line 144

def cert_name_for(org_name, environment, cert_name)
  fh_name_for('fh-crt', org_name, environment, cert_name)
end

#cluster_config_for(node) ⇒ Object



154
155
156
# File 'lib/cookbooks/provision/libraries/provision.rb', line 154

def cluster_config_for(node)
  JSON.parse(node.normal.to_json, {:symbolize_names => true})
end

#converge_options_for(provider, org_name, environment, instance_options) ⇒ Object



38
39
40
# File 'lib/cookbooks/provision/libraries/provision.rb', line 38

def converge_options_for(provider, org_name, environment, instance_options)
  send(:"#{provider}_converge_options", org_name, environment, instance_options)
end

#fh_name_for(*args) ⇒ Object



148
149
150
151
152
# File 'lib/cookbooks/provision/libraries/provision.rb', line 148

def fh_name_for(*args)
  args.collect do |str|
    str.to_s.split('-')
  end.flatten.uniq.join('-')
end

#inbound_rules_for(org_name, ingress) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cookbooks/provision/libraries/provision.rb', line 14

def inbound_rules_for(org_name, ingress)
  ingress.collect do |ai|
    sources = ai[:sources] || []
    sources += ai[:groups].collect { |group| security_group_name_for(org_name, group) } if ai[:groups]

    protocols = ai[:protocols].collect { |p| p }
    if protocols.delete 'all'
      protocols += ['tcp', 'udp']
    end
    protocols.uniq!
    protocols.collect do |protocol|
      {port: ai[:start]..ai[:end], protocol: protocol, sources: sources}
    end
  end.flatten
end

#instance_name_for(org_name, environment, name) ⇒ Object



132
133
134
# File 'lib/cookbooks/provision/libraries/provision.rb', line 132

def instance_name_for(org_name, environment, name)
  fh_name_for(org_name, environment, name)
end

#internet_gateway_name_for(name) ⇒ Object



112
113
114
# File 'lib/cookbooks/provision/libraries/provision.rb', line 112

def internet_gateway_name_for(name)
  fh_name_for('fh-igw', name)
end

#key_pair_name_for(name) ⇒ Object



108
109
110
# File 'lib/cookbooks/provision/libraries/provision.rb', line 108

def key_pair_name_for(name)
  fh_name_for('fh-kp', name)
end

#load_balancer_name_for(org_name, environment, lb_name) ⇒ Object



140
141
142
# File 'lib/cookbooks/provision/libraries/provision.rb', line 140

def load_balancer_name_for(org_name, environment, lb_name)
  fh_name_for('fh-lb', org_name, environment, lb_name)[0..31] #elb names have a max length of 32
end

#machine_options_for(provider, org_name, environment, instance_name, instance_options) ⇒ Object



30
31
32
# File 'lib/cookbooks/provision/libraries/provision.rb', line 30

def machine_options_for(provider, org_name, environment, instance_name, instance_options)
  send(:"#{provider}_machine_options", org_name, environment, instance_name, instance_options)
end

#node_names_for(cluster_config) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'lib/cookbooks/provision/libraries/provision.rb', line 3

def node_names_for(cluster_config)
  org_name = cluster_config[:id]
  #If we are using chef-client in local mode (-z) we can't search :-(
  #node_names = search(:node, "chef_environment:*").map { |n| n.name }
  cluster_config[:environments].collect do |env_name, env_cfg|
    env_cfg[:instances].collect do |name, cfg|
      instance_name_for(org_name, env_name, name)
    end if env_cfg[:instances]
  end.flatten
end

#openstack_bootstrap_options(org_name, environment, instance_options) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/cookbooks/provision/libraries/provision.rb', line 81

def openstack_bootstrap_options(org_name, environment, instance_options)
  {
      flavor_ref: instance_options[:flavor_ref],
      image_ref: instance_options[:image_ref],
      key_name: key_pair_name_for(org_name),
      user_id: "ubuntu"
  }
end

#openstack_converge_options(org_name, environment, instance_options) ⇒ Object



90
91
92
# File 'lib/cookbooks/provision/libraries/provision.rb', line 90

def openstack_converge_options(org_name, environment, instance_options)
  {:chef_version => '11.10.4', :ohai_hints => {:openstack => {}}}
end

#openstack_machine_options(org_name, environment, instance_name, instance_options) ⇒ Object

OpenStack



72
73
74
75
76
77
78
79
# File 'lib/cookbooks/provision/libraries/provision.rb', line 72

def openstack_machine_options(org_name, environment, instance_name, instance_options)
  {
      :bootstrap_options => bootstrap_options_for('openstack', org_name, environment, instance_options),
      :convergence_options => converge_options_for('openstack', org_name, environment, instance_options),
      :floating_ip_pool => instance_options[:floating_ip_pool],
      :ssh_username => "ubuntu"
  }
end

#route_table_name_for(org_name, environment) ⇒ Object



124
125
126
# File 'lib/cookbooks/provision/libraries/provision.rb', line 124

def route_table_name_for(org_name, environment)
  fh_name_for('fh-rt', org_name, environment)
end

#security_group_name_for(name, environment) ⇒ Object



120
121
122
# File 'lib/cookbooks/provision/libraries/provision.rb', line 120

def security_group_name_for(name, environment)
  fh_name_for('fh-sg', name, environment)
end

#ssh_bootstrap_options(org_name, environment, instance_options) ⇒ Object



100
101
102
# File 'lib/cookbooks/provision/libraries/provision.rb', line 100

def ssh_bootstrap_options(org_name, environment, instance_options)
  {}
end

#ssh_converge_options(org_name, environment, instance_options) ⇒ Object



104
105
106
# File 'lib/cookbooks/provision/libraries/provision.rb', line 104

def ssh_converge_options(org_name, environment, instance_options)
  {:chef_version => '11.10.4'}
end

#ssh_machine_options(org_name, environment, instance_name, instance_options) ⇒ Object

SSH



96
97
98
# File 'lib/cookbooks/provision/libraries/provision.rb', line 96

def ssh_machine_options(org_name, environment, instance_name, instance_options)
  {}
end

#subnet_name_for(name, environment, suffix) ⇒ Object



128
129
130
# File 'lib/cookbooks/provision/libraries/provision.rb', line 128

def subnet_name_for(name, environment, suffix)
  fh_name_for('fh-net', name, environment, suffix)
end

#volume_name_for(*args) ⇒ Object



136
137
138
# File 'lib/cookbooks/provision/libraries/provision.rb', line 136

def volume_name_for(*args)
  fh_name_for('fh-vol', *args)
end

#vpc_name_for(name) ⇒ Object



116
117
118
# File 'lib/cookbooks/provision/libraries/provision.rb', line 116

def vpc_name_for(name)
  fh_name_for('fh-vpc', name)
end

#with_cluster_config(node, &block) ⇒ Object



158
159
160
161
# File 'lib/cookbooks/provision/libraries/provision.rb', line 158

def with_cluster_config(node, &block)
  cluster_config = cluster_config_for(node)
  block.call cluster_config
end

#with_cluster_instances(node, &block) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/cookbooks/provision/libraries/provision.rb', line 163

def with_cluster_instances(node, &block)
  env_regexp = node[:'env-regexp'] || '.'
  node_regexp = node[:'node-regexp'] || '.'
  with_cluster_config(node) do |cluster_config|
    org_name = cluster_config[:id]
    default_instance_options = cluster_config[:default_instance_options]
    instances = {}
    cluster_config[:environments].each do |env, env_config|
      chef_environment = fh_name_for(org_name, env)
      if chef_environment =~ /#{env_regexp}/
        instances[chef_environment] = {}

        env_config[:instances].each do |instance_name, instance_config|
          instance_name = instance_name_for(org_name, env, instance_name)
          if instance_name =~ /#{node_regexp}/
            instances[chef_environment][instance_name] = {}
            instances[chef_environment][instance_name][:instance_config] = instance_config

            machine_options = machine_options_for(cluster_config[:driver], org_name, env, instance_name, default_instance_options.merge(instance_config[cluster_config[:driver].to_sym] || {}))
            machine_options.deep_merge!(JSON.parse(node[:machine_options].to_json, {:symbolize_names => true})) if node[:machine_options]
            machine_options.deep_merge!(JSON.parse(instance_config[:machine_options].to_json, {:symbolize_names => true})) if instance_config[:machine_options]

            instances[chef_environment][instance_name][:machine_options] = machine_options
          end
        end
      end
    end
    block.call instances
  end
end