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
63
# 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,
    name: @name,
    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

#delete_databagObject



85
86
87
88
89
90
# File 'lib/formatron/chef.rb', line 85

def delete_databag
  Formatron::LOG.info do
    "Deleting data bag from chef server: #{@chef_sub_domain}"
  end
  @knife.delete_databag
end

#deploy_databagObject



78
79
80
81
82
83
# File 'lib/formatron/chef.rb', line 78

def deploy_databag
  Formatron::LOG.info do
    "Deploying data bag to chef server: #{@chef_sub_domain}"
  end
  @knife.deploy_databag
end

#destroy(guid:) ⇒ Object

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



127
128
129
130
131
132
133
134
# File 'lib/formatron/chef.rb', line 127

def destroy(guid:)
  Formatron::LOG.info do
    "Delete Chef configuration for node: #{guid}"
  end
  @knife.delete_node node: guid
  @knife.delete_client client: guid
  @knife.delete_environment environment: guid
end

#initObject

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



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

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



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

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
  )
  @knife.create_environment environment: guid
  @berkshelf.upload environment: guid, cookbook: cookbook
  @knife.bootstrap(
    bastion_hostname: bastion_hostname,
    guid: guid,
    cookbook: cookbook_name,
    hostname: hostname
  )
end


136
137
138
139
140
# File 'lib/formatron/chef.rb', line 136

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