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

Constant Summary collapse

CONFIG_FILE =
'knife.rb'
DATABAG_SECRET_FILE =
'databag_secret'
DATABAG_DIRECTORY =
'databag'
DATABAG_ITEM_SUFFIX =
'.json'

Instance Method Summary collapse

Constructor Details

#initialize(directory:, keys:, administrator_name:, administrator_password:, chef_server_url:, username:, organization:, ssl_verify:, databag_secret:, configuration:) ⇒ Knife

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



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

def initialize(
  directory:,
  keys:,
  administrator_name:,
  administrator_password:,
  chef_server_url:,
  username:,
  organization:,
  ssl_verify:,
  databag_secret:,
  configuration:
)
  @knife_file = File.join directory, CONFIG_FILE
  @databag_secret_file = File.join directory, DATABAG_SECRET_FILE
  @databag_directory = File.join directory, DATABAG_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
end

Instance Method Details

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

rubocop:disable Metrics/MethodLength



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/formatron/chef/knife.rb', line 122

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

#create_environment(environment:) ⇒ Object



97
98
99
100
101
# File 'lib/formatron/chef/knife.rb', line 97

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



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

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

#delete_databag(name:) ⇒ Object

rubocop:enable Metrics/MethodLength



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

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

#delete_environment(environment:) ⇒ Object



160
161
162
163
164
165
# File 'lib/formatron/chef/knife.rb', line 160

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

#delete_node(node:) ⇒ Object



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

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

#deploy_databag(name:) ⇒ Object

rubocop:enable Metrics/MethodLength



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/formatron/chef/knife.rb', line 61

def deploy_databag(name:)
  databag_file = File.join(
    @databag_directory, "#{name}#{DATABAG_ITEM_SUFFIX}"
  )
  File.write databag_file, @configuration.merge(id: name).to_json
  _attempt_to_create_databag unless _databag_exists
  _attempt_to_create_databag_item(
    name: name,
    databag_file: databag_file
  )
end

#initObject

rubocop:disable Metrics/MethodLength



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/formatron/chef/knife.rb', line 46

def init
  File.write @knife_file, <<-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
  File.write @databag_secret_file, @databag_secret
  FileUtils.mkdir_p @databag_directory
end

#node_exists?(guid:) ⇒ Boolean

Returns:

  • (Boolean)


167
168
169
170
# File 'lib/formatron/chef/knife.rb', line 167

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