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/winrm.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, WinRM

Instance Method Summary collapse

Constructor Details

#initialize(directory:, aws:, bucket:, name:, target:, ec2_key:, administrator_name:, administrator_password:, 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 rubocop:disable Metrics/AbcSize



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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/formatron/chef.rb', line 16

def initialize(
  directory:,
  aws:,
  bucket:,
  name:,
  target:,
  ec2_key:,
  administrator_name:,
  administrator_password:,
  username:,
  organization:,
  ssl_verify:,
  chef_sub_domain:,
  bastions:,
  hosted_zone_name:,
  server_stack:,
  guid:,
  configuration:,
  databag_secret:
)
  @working_directory = File.join directory, guid
  @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(
    directory: @working_directory,
    aws: @aws,
    bucket: bucket,
    name: server_stack,
    target: @target,
    guid: guid,
    ec2_key: ec2_key
  )
  @knife = Knife.new(
    directory: @working_directory,
    keys: @keys,
    administrator_name: administrator_name,
    administrator_password: administrator_password,
    chef_server_url: chef_server_url,
    username: username,
    organization: organization,
    ssl_verify: ssl_verify,
    databag_secret: databag_secret,
    configuration: configuration
  )
  @berkshelf = Berkshelf.new(
    directory: @working_directory,
    keys: @keys,
    chef_server_url: chef_server_url,
    username: username,
    ssl_verify: ssl_verify
  )
  @ssh = SSH.new(
    keys: @keys
  )
  @winrm = WinRM.new(
    administrator_name: administrator_name,
    administrator_password: administrator_password
  )
end

Instance Method Details

#destroy(guid:) ⇒ Object

rubocop:disable Metrics/MethodLength



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/formatron/chef.rb', line 242

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/AbcSize rubocop:enable Metrics/ParameterLists rubocop:enable Metrics/MethodLength



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/formatron/chef.rb', line 86

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

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

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



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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/formatron/chef.rb', line 100

def provision(
  os:,
  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(
      os: os,
      bastion_hostname: bastion_hostname,
      guid: guid,
      cookbook_name: cookbook_name,
      hostname: hostname
    )
  else
    _bootstrap_node(
      os: os,
      bastion_hostname: bastion_hostname,
      guid: guid,
      cookbook_name: cookbook_name,
      hostname: hostname
    )
  end
end