Class: Formatron::Chef

Inherits:
Object
  • Object
show all
Defined in:
lib/formatron/chef.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

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



13
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
# File 'lib/formatron/chef.rb', line 13

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

Instance Method Details

#destroy(guid:) ⇒ Object

rubocop:disable Metrics/MethodLength



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/formatron/chef.rb', line 124

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



66
67
68
69
70
71
72
73
74
75
# File 'lib/formatron/chef.rb', line 66

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



79
80
81
82
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
# File 'lib/formatron/chef.rb', line 79

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
  Formatron::LOG.info do
    "Bootstrap node #{guid}"
  end
  @knife.bootstrap(
    bastion_hostname: bastion_hostname,
    guid: guid,
    cookbook: cookbook_name,
    hostname: hostname
  )
end

rubocop:enable Metrics/MethodLength



147
148
149
150
151
# File 'lib/formatron/chef.rb', line 147

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