Class: Formatron::Chef::Knife

Inherits:
Object
  • Object
show all
Defined in:
lib/formatron/chef/knife.rb

Overview

Wrapper for the knife cli rubocop:disable Metrics/ClassLength

Instance Method Summary collapse

Constructor Details

#initialize(keys:, chef_server_url:, username:, organization:, ssl_verify:, name:, databag_secret:, configuration:) ⇒ Knife

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



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/formatron/chef/knife.rb', line 12

def initialize(
  keys:,
  chef_server_url:,
  username:,
  organization:,
  ssl_verify:,
  name:,
  databag_secret:,
  configuration:
)
  @keys = keys
  @chef_server_url = chef_server_url
  @username = username
  @organization = organization
  @ssl_verify = ssl_verify
  @name = name
  @databag_secret = databag_secret
  @configuration = configuration
end

Instance Method Details

#bootstrap(guid:, bastion_hostname:, cookbook:, hostname:) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/formatron/chef/knife.rb', line 111

def bootstrap(
  guid:,
  bastion_hostname:,
  cookbook:,
  hostname:
)
  # rubocop:disable Metrics/LineLength
  command = "knife bootstrap #{hostname} --sudo -x ubuntu -i #{@keys.ec2_key} -E #{guid} -r #{cookbook} -N #{guid} -c #{@knife_file.path}#{@ssl_verify ? '' : ' --node-ssl-verify-mode none'} --secret-file #{@databag_secret_file.path}"
  command = "#{command} -G ubuntu@#{bastion_hostname}" unless bastion_hostname.eql? hostname
  fail "failed to bootstrap instance: #{guid}" unless Util::Shell.exec command
  # rubocop:enable Metrics/LineLength
end

#create_environment(environment:) ⇒ Object



87
88
89
90
91
# File 'lib/formatron/chef/knife.rb', line 87

def create_environment(environment:)
  # rubocop:disable Metrics/LineLength
  _attempt_to_create_environment environment unless _environment_exists environment
  # rubocop:enable Metrics/LineLength
end

#delete_client(client:) ⇒ Object



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

def delete_client(client:)
  # rubocop:disable Metrics/LineLength
  command = "knife client delete #{client} -y -c #{@knife_file.path}"
  fail "failed to delete client: #{client}" unless Util::Shell.exec command
  # rubocop:enable Metrics/LineLength
end

#delete_databagObject



124
125
126
127
128
129
# File 'lib/formatron/chef/knife.rb', line 124

def delete_databag
  # rubocop:disable Metrics/LineLength
  command = "knife data bag delete formatron #{@name} -y -c #{@knife_file.path}"
  fail "failed to delete data bag item: #{@name}" unless Util::Shell.exec command
  # rubocop:enable Metrics/LineLength
end

#delete_environment(environment:) ⇒ Object



143
144
145
146
147
148
# File 'lib/formatron/chef/knife.rb', line 143

def delete_environment(environment:)
  # rubocop:disable Metrics/LineLength
  command = "knife environment delete #{environment} -y -c #{@knife_file.path}"
  fail "failed to delete environment: #{environment}" unless Util::Shell.exec command
  # rubocop:enable Metrics/LineLength
end

#delete_node(node:) ⇒ Object



131
132
133
134
# File 'lib/formatron/chef/knife.rb', line 131

def delete_node(node:)
  command = "knife node delete #{node} -y -c #{@knife_file.path}"
  fail "failed to delete node: #{node}" unless Util::Shell.exec command
end

#deploy_databagObject

rubocop:enable Metrics/MethodLength



56
57
58
59
# File 'lib/formatron/chef/knife.rb', line 56

def deploy_databag
  _attempt_to_create_databag unless _databag_exists
  _attempt_to_create_databag_item
end

#initObject

rubocop:disable Metrics/MethodLength



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/formatron/chef/knife.rb', line 35

def init
  @knife_file = Tempfile.new 'formatron-knife-'
  @knife_file.write "    chef_server_url '\#{@chef_server_url}'\n    validation_client_name '\#{@organization}-validator'\n    validation_key '\#{@keys.organization_key}'\n    node_name '\#{@username}'\n    client_key '\#{@keys.user_key}'\n    verify_api_cert \#{@ssl_verify}\n    ssl_verify_mode \#{@ssl_verify ? ':verify_peer' : ':verify_none'}\n  EOH\n  @knife_file.close\n  @databag_secret_file = Tempfile.new 'formatron-databag-secret-'\n  @databag_secret_file.write @databag_secret\n  @databag_secret_file.close\n  @databag_file = Tempfile.new ['formatron-databag-', '.json']\n  @databag_file.write @configuration.merge(id: @name).to_json\n  @databag_file.close\nend\n".gsub(/^ {10}/, '')


150
151
152
153
154
# File 'lib/formatron/chef/knife.rb', line 150

def unlink
  @knife_file.unlink unless @knife_file.nil?
  @databag_secret_file.unlink unless @databag_secret_file.nil?
  @databag_file.unlink unless @databag_file.nil?
end