class Formatron::Chef::Knife

Wrapper for the knife cli rubocop:disable Metrics/ClassLength

Constants

CONFIG_FILE
DATABAG_DIRECTORY
DATABAG_ITEM_SUFFIX
DATABAG_SECRET_FILE

Public Class Methods

new( directory:, keys:, administrator_name:, administrator_password:, chef_server_url:, username:, organization:, ssl_verify:, databag_secret:, configuration: ) click to toggle source

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

# 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

Public Instance Methods

bootstrap( os:, guid:, bastion_hostname:, cookbook:, hostname: ) click to toggle source

rubocop:disable Metrics/MethodLength

# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source

rubocop:enable Metrics/MethodLength

# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source

rubocop:enable Metrics/MethodLength

# 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
init() click to toggle source

rubocop:disable Metrics/MethodLength

# 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:) click to toggle source
# 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

Private Instance Methods

_attempt_to_create_databag() click to toggle source
# File lib/formatron/chef/knife.rb, line 77
def _attempt_to_create_databag
  fail 'failed to create data bag: formatron' unless _create_databag
end
_attempt_to_create_databag_item(name:, databag_file:) click to toggle source
# File lib/formatron/chef/knife.rb, line 85
def _attempt_to_create_databag_item(name:, databag_file:)
  # rubocop:disable Metrics/LineLength
  fail "failed to create data bag item: #{name}" unless _create_databag_item databag_file: databag_file
  # rubocop:enable Metrics/LineLength
end
_attempt_to_create_environment(environment) click to toggle source
# File lib/formatron/chef/knife.rb, line 109
def _attempt_to_create_environment(environment)
  # rubocop:disable Metrics/LineLength
  fail "failed to create opscode environment: #{environment}" unless _create_environment environment
  # rubocop:enable Metrics/LineLength
end
_create_databag() click to toggle source
# File lib/formatron/chef/knife.rb, line 81
def _create_databag
  Util::Shell.exec "knife data bag create formatron -c #{@knife_file}"
end
_create_databag_item(databag_file:) click to toggle source
# File lib/formatron/chef/knife.rb, line 91
def _create_databag_item(databag_file:)
  # rubocop:disable Metrics/LineLength
  Util::Shell.exec "knife data bag from file formatron #{databag_file} --secret-file #{@databag_secret_file} -c #{@knife_file}"
  # rubocop:enable Metrics/LineLength
end
_create_environment(environment) click to toggle source
# File lib/formatron/chef/knife.rb, line 115
def _create_environment(environment)
  # rubocop:disable Metrics/LineLength
  Util::Shell.exec "knife environment create #{environment} -c #{@knife_file} -d '#{environment} environment created by formatron'"
  # rubocop:enable Metrics/LineLength
end
_databag_exists() click to toggle source
# File lib/formatron/chef/knife.rb, line 73
def _databag_exists
  Util::Shell.exec "knife data bag show formatron -c #{@knife_file}"
end
_environment_exists(environment) click to toggle source
# File lib/formatron/chef/knife.rb, line 103
def _environment_exists(environment)
  # rubocop:disable Metrics/LineLength
  Util::Shell.exec "knife environment show #{environment} -c #{@knife_file}"
  # rubocop:enable Metrics/LineLength
end