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

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

Instance Method Details

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



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/formatron/chef/knife.rb', line 114

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



90
91
92
93
94
# File 'lib/formatron/chef/knife.rb', line 90

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



139
140
141
142
143
144
# File 'lib/formatron/chef/knife.rb', line 139

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_databag(name:) ⇒ Object



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

def delete_databag(name:)
  # 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



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

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



134
135
136
137
# File 'lib/formatron/chef/knife.rb', line 134

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_databag(name:) ⇒ Object

rubocop:enable Metrics/MethodLength



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/formatron/chef/knife.rb', line 51

def deploy_databag(name:)
  databag_file = Tempfile.new ['formatron-databag-', '.json']
  databag_file.write @configuration.merge(id: name).to_json
  databag_file.close
  _attempt_to_create_databag unless _databag_exists
  _attempt_to_create_databag_item(
    name: name,
    databag_file: databag_file
  )
ensure
  databag_file.unlink unless databag_file.nil?
end

#initObject

rubocop:disable Metrics/MethodLength



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/formatron/chef/knife.rb', line 33

def init
  @knife_file = Tempfile.new 'formatron-knife-'
  @knife_file.write <<-EOH.gsub(/^ {10}/, '')
    chef_server_url '#{@chef_server_url}'
    validation_client_name '#{@organization}-validator'
    validation_key '#{@keys.organization_key}'
    node_name '#{@username}'
    client_key '#{@keys.user_key}'
    verify_api_cert #{@ssl_verify}
    ssl_verify_mode #{@ssl_verify ? ':verify_peer' : ':verify_none'}
  EOH
  @knife_file.close
  @databag_secret_file = Tempfile.new 'formatron-databag-secret-'
  @databag_secret_file.write @databag_secret
  @databag_secret_file.close
end

#node_exists?(guid:) ⇒ Boolean

Returns:

  • (Boolean)


153
154
155
156
# File 'lib/formatron/chef/knife.rb', line 153

def node_exists?(guid:)
  command = "knife node show #{guid} -c #{@knife_file.path}"
  Util::Shell.exec command
end


158
159
160
161
# File 'lib/formatron/chef/knife.rb', line 158

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