class Formatron

manages a Formatron stack rubocop:disable Metrics/ClassLength

add logger to class

add version to class

Constants

FORMATRONFILE
LOG
VERSION
WORKING_DIRECTORY

Attributes

protected[R]
protected?[R]

Public Class Methods

new(credentials:, directory:, target:) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/formatron.rb, line 26
def initialize(credentials:, directory:, target:)
  @working_directory = File.join directory, WORKING_DIRECTORY, target
  @target = target
  @aws = AWS.new credentials: credentials
  @config = Config.target(
    directory: directory,
    target: target
  )
  @external = External.new(
    target: @target,
    config: @config,
    aws: @aws
  )
  @dsl = DSL.new(
    file: File.join(directory, FORMATRONFILE),
    config: @config,
    target: @target,
    external: @external,
    aws: @aws
  )
  _initialize
end

Public Instance Methods

_deploy_chef_server_certs() click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/formatron.rb, line 225
def _deploy_chef_server_certs
  @chef_servers.values.each do |chef_servers|
    chef_servers.values.each do |chef_server|
      S3::ChefServerCert.deploy(
        aws: @aws,
        kms_key: @kms_key,
        bucket: @bucket,
        name: @name,
        target: @target,
        guid: chef_server.guid,
        cert: chef_server.ssl_cert,
        key: chef_server.ssl_key
      )
    end
  end
end
_destroy_chef_server_cert() click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/formatron.rb, line 277
def _destroy_chef_server_cert
  @chef_servers.values.each do |chef_servers|
    chef_servers.values.each do |chef_server|
      S3::ChefServerCert.destroy(
        aws: @aws,
        bucket: @bucket,
        name: @name,
        target: @target,
        guid: chef_server.guid
      )
    end
  end
rescue => error
  LOG.warn error
end
_destroy_chef_server_keys() click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/formatron.rb, line 295
def _destroy_chef_server_keys
  @chef_servers.values.each do |chef_servers|
    chef_servers.values.each do |chef_server|
      S3::ChefServerKeys.destroy(
        aws: @aws,
        bucket: @bucket,
        name: @name,
        target: @target,
        guid: chef_server.guid
      )
    end
  end
rescue => error
  LOG.warn error
end
deploy() click to toggle source

rubocop:enable Metrics/AbcSize rubocop:enable Metrics/MethodLength

# File lib/formatron.rb, line 153
def deploy
  _deploy_configuration
  _deploy_chef_server_certs
  if @cloud_formation_template[:Resources].empty?
    _destroy_template
    _destroy_stack
  else
    _deploy_template
    _deploy_stack
  end
end
destroy() click to toggle source

rubocop:enable Metrics/ParameterLists

# File lib/formatron.rb, line 204
def destroy
  _destroy_chef_instances
  _destroy_configuration
  _destroy_chef_server_cert
  _destroy_chef_server_keys
  _destroy_template
  _destroy_stack
end
provision(guid: nil) click to toggle source
# File lib/formatron.rb, line 165
def provision(guid: nil)
  @all_instances.each do |key, instances|
    _provision_vpc key, instances, guid
  end
end

Private Instance Methods

_deploy_configuration() click to toggle source
# File lib/formatron.rb, line 213
def _deploy_configuration
  S3::Configuration.deploy(
    aws: @aws,
    kms_key: @kms_key,
    bucket: @bucket,
    name: @name,
    target: @target,
    configuration: @configuration
  )
end
_deploy_stack() click to toggle source
# File lib/formatron.rb, line 255
def _deploy_stack
  CloudFormation.deploy(
    aws: @aws,
    bucket: @bucket,
    name: @name,
    target: @target,
    parameters: @external.outputs.hash
  )
end
_deploy_template() click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/formatron.rb, line 243
def _deploy_template
  S3::CloudFormationTemplate.deploy(
    aws: @aws,
    kms_key: @kms_key,
    bucket: @bucket,
    name: @name,
    target: @target,
    cloud_formation_template:
      JSON.pretty_generate(@cloud_formation_template)
  )
end
_destroy_chef_instance(chef, guid) click to toggle source
# File lib/formatron.rb, line 351
def _destroy_chef_instance(chef, guid)
  chef.destroy(
    guid: guid
  )
rescue => error
  LOG.warn error
end
_destroy_chef_instances() click to toggle source
# File lib/formatron.rb, line 333
def _destroy_chef_instances
  @all_instances.each do |key, instances|
    _destroy_chef_vpc_instances key, instances
  end
end
_destroy_chef_vpc_instances(key, instances) click to toggle source
# File lib/formatron.rb, line 339
def _destroy_chef_vpc_instances(key, instances)
  chef_clients = @chef_clients[key]
  chef_clients.init
  instances.values.each do |instance|
    dsl_chef = instance.chef
    next if dsl_chef.cookbook.nil?
    chef = chef_clients.get dsl_chef.server
    guid = instance.guid
    _destroy_chef_instance chef, guid
  end
end
_destroy_configuration() click to toggle source
# File lib/formatron.rb, line 265
def _destroy_configuration
  S3::Configuration.destroy(
    aws: @aws,
    bucket: @bucket,
    name: @name,
    target: @target
  )
rescue => error
  LOG.warn error
end
_destroy_stack() click to toggle source
# File lib/formatron.rb, line 323
def _destroy_stack
  CloudFormation.destroy(
    aws: @aws,
    name: @name,
    target: @target
  )
rescue => error
  LOG.warn error
end
_destroy_template() click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/formatron.rb, line 312
def _destroy_template
  S3::CloudFormationTemplate.destroy(
    aws: @aws,
    bucket: @bucket,
    name: @name,
    target: @target
  )
rescue => error
  LOG.warn error
end
_initialize() click to toggle source

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity

# File lib/formatron.rb, line 54
def _initialize
  @formatron = @dsl.formatron
  @vpcs = @formatron.vpc
  @name = @formatron.name
  @bucket = @formatron.bucket
  external_formatron = @external.formatron
  @external_vpcs = external_formatron.vpc
  external_global = external_formatron.global
  global = @formatron.global || external_global
  external_ec2 = external_global.ec2
  external_windows = external_global.windows
  ec2 = global.ec2 || external_ec2
  key_pair = ec2.key_pair || external_ec2.key_pair
  @ec2_key = ec2.private_key || external_ec2.private_key
  windows = global.windows || external_windows
  @administrator_name =
    windows.administrator_name ||
    external_windows.administrator_name
  @administrator_password =
    windows.administrator_password ||
    external_windows.administrator_password
  @protected = global.protect || external_global.protect
  @kms_key = global.kms_key || external_global.kms_key
  @databag_secret = global.databag_secret || external_global.databag_secret
  hosted_zone_id = global.hosted_zone_id || external_global.hosted_zone_id
  @hosted_zone_name =
    global.hosted_zone_name || external_global.hosted_zone_name
  @configuration = @external.export formatron: @formatron
  @cloud_formation_template = CloudFormation::Template.new(
    formatron: @formatron,
    hosted_zone_name: @hosted_zone_name,
    key_pair: key_pair,
    administrator_name: @administrator_name,
    administrator_password: @administrator_password,
    kms_key: @kms_key,
    hosted_zone_id: hosted_zone_id,
    target: @target,
    external: @external
  ).hash
  _initialize_chef_clients
  _initialize_instances
end
_initialize_chef_clients() click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/formatron.rb, line 102
def _initialize_chef_clients
  @chef_clients = {}
  @vpcs.each do |key, vpc|
    @chef_clients[key] = ChefClients.new(
      directory: @working_directory,
      aws: @aws,
      bucket: @bucket,
      name: @name,
      target: @target,
      ec2_key: @ec2_key,
      administrator_name: @administrator_name,
      administrator_password: @administrator_password,
      hosted_zone_name: @hosted_zone_name,
      vpc: vpc,
      external: @external_vpcs[key],
      configuration: @configuration,
      databag_secret: @databag_secret
    )
  end
end
_initialize_instances() click to toggle source

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize

# File lib/formatron.rb, line 126
def _initialize_instances
  @chef_servers = {}
  @bastions = {}
  @nats = {}
  @instances = {}
  @all_instances = {}
  @vpcs.each do |k, v|
    chef_servers = @chef_servers[k] = {}
    bastions = @bastions[k] = {}
    nats = @nats[k] = {}
    instances = @instances[k] = {}
    all_instances = @all_instances[k] = {}
    v.subnet.values.each do |s|
      chef_servers.merge! s.chef_server
      bastions.merge! s.bastion
      nats.merge! s.nat
      instances.merge! s.instance
    end
    all_instances.merge! chef_servers
    all_instances.merge! bastions
    all_instances.merge! nats
    all_instances.merge! instances
  end
end
_provision_instance(chef, cookbook, sub_domain, guid, bastion, os) click to toggle source

rubocop:disable Metrics/ParameterLists

# File lib/formatron.rb, line 193
def _provision_instance(chef, cookbook, sub_domain, guid, bastion, os)
  chef.provision(
    os: os,
    sub_domain: sub_domain,
    guid: guid,
    cookbook: cookbook,
    bastion: bastion
  )
end
_provision_vpc(key, instances, guid_filter) click to toggle source

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize

# File lib/formatron.rb, line 173
def _provision_vpc(key, instances, guid_filter)
  chef_clients = @chef_clients[key]
  chef_clients.init
  instances.values.each do |instance|
    guid = instance.guid
    os = instance.os
    next unless guid_filter.nil? || guid_filter.eql?(guid)
    dsl_chef = instance.chef
    next if dsl_chef.cookbook.nil?
    chef = chef_clients.get dsl_chef.server
    cookbook = dsl_chef.cookbook
    bastion = dsl_chef.bastion
    sub_domain = instance.sub_domain
    _provision_instance chef, cookbook, sub_domain, guid, bastion, os
  end
end