Class: Formatron::Chef

Inherits:
Object
  • Object
show all
Defined in:
lib/formatron/chef.rb,
lib/formatron/chef/ssh.rb,
lib/formatron/chef/keys.rb,
lib/formatron/chef/knife.rb,
lib/formatron/chef/berkshelf.rb

Overview

manage the instance provisioning with Chef rubocop:disable Metrics/ClassLength

Defined Under Namespace

Classes: Berkshelf, Keys, Knife, SSH

Instance Method Summary collapse

Constructor Details

#initialize(aws:, bucket:, name:, target:, ec2_key:, username:, organization:, ssl_verify:, chef_sub_domain:, bastions:, hosted_zone_name:, server_stack:, guid:, configuration:, databag_secret:) ⇒ Chef

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/ParameterLists



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/formatron/chef.rb', line 14

def initialize(
  aws:,
  bucket:,
  name:,
  target:,
  ec2_key:,
  username:,
  organization:,
  ssl_verify:,
  chef_sub_domain:,
  bastions:,
  hosted_zone_name:,
  server_stack:,
  guid:,
  configuration:,
  databag_secret:
)
  @aws = aws
  @name = name
  @target = target
  @chef_sub_domain = chef_sub_domain
  @hosted_zone_name = hosted_zone_name
  @organization = organization
  @server_stack = server_stack
  @bastions = bastions
  chef_server_url = _chef_server_url
  @keys = Keys.new(
    aws: @aws,
    bucket: bucket,
    name: server_stack,
    target: @target,
    guid: guid,
    ec2_key: ec2_key
  )
  @knife = Knife.new(
    keys: @keys,
    chef_server_url: chef_server_url,
    username: username,
    organization: organization,
    ssl_verify: ssl_verify,
    databag_secret: databag_secret,
    configuration: configuration
  )
  @berkshelf = Berkshelf.new(
    keys: @keys,
    chef_server_url: chef_server_url,
    username: username,
    ssl_verify: ssl_verify
  )
  @ssh = SSH.new(
    keys: @keys
  )
end

Instance Method Details

#destroy(guid:) ⇒ Object

rubocop:disable Metrics/MethodLength



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/formatron/chef.rb', line 194

def destroy(guid:)
  Formatron::LOG.info do
    "Delete Chef configuration for node: #{guid}"
  end
  Formatron::LOG.info do
    "Deleting data bag item '#{guid}' from chef server: #{@chef_sub_domain}"
  end
  @knife.delete_databag name: guid
  Formatron::LOG.info do
    "Deleting node '#{guid}' from chef server: #{@chef_sub_domain}"
  end
  @knife.delete_node node: guid
  Formatron::LOG.info do
    "Deleting client '#{guid}' from chef server: #{@chef_sub_domain}"
  end
  @knife.delete_client client: guid
  Formatron::LOG.info do
    "Deleting environment '#{guid}' from chef server: #{@chef_sub_domain}"
  end
  @knife.delete_environment environment: guid
end

#initObject

rubocop:enable Metrics/ParameterLists rubocop:enable Metrics/MethodLength



70
71
72
73
74
75
76
77
78
79
# File 'lib/formatron/chef.rb', line 70

def init
  CloudFormation.stack_ready!(
    aws: @aws,
    name: @server_stack,
    target: @target
  )
  @keys.init
  @knife.init
  @berkshelf.init
end

#provision(sub_domain:, guid:, cookbook:, bastion:) ⇒ Object

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/formatron/chef.rb', line 83

def provision(
  sub_domain:,
  guid:,
  cookbook:,
  bastion:
)
  Formatron::LOG.info do
    "Provision #{guid} with Chef cookbook: #{cookbook}"
  end
  bastion ||= @bastions.keys[0]
  bastion_hostname = _hostname(
    sub_domain: @bastions[bastion]
  )
  CloudFormation.stack_ready!(
    aws: @aws,
    name: @name,
    target: @target
  )
  cookbook_name = File.basename cookbook
  hostname = _hostname(
    sub_domain: sub_domain
  )
  Formatron::LOG.info do
    "Deploying data bag item '#{guid}' to chef server: #{@chef_sub_domain}"
  end
  @knife.deploy_databag name: guid
  Formatron::LOG.info do
    "Lock cookbook versions for environment #{guid}"
  end
  @knife.create_environment environment: guid
  @berkshelf.upload environment: guid, cookbook: cookbook
  if @knife.node_exists? guid: guid
    _reprovision_node(
      bastion_hostname: bastion_hostname,
      guid: guid,
      cookbook_name: cookbook_name,
      hostname: hostname
    )
  else
    _bootstrap_node(
      bastion_hostname: bastion_hostname,
      guid: guid,
      cookbook_name: cookbook_name,
      hostname: hostname
    )
  end
end

rubocop:enable Metrics/MethodLength



217
218
219
220
221
# File 'lib/formatron/chef.rb', line 217

def unlink
  @keys.unlink
  @knife.unlink
  @berkshelf.unlink
end