module Qops::DeployHelpers

Private Instance Methods

config() click to toggle source
# File lib/qops/deployment/helpers.rb, line 22
def config
  return @_config if @_config
  Qops::Environment.notifiers
  @_config ||= Qops::Environment.new(profile: options[:profile], force_config: options[:force_config], verbose: options[:verbose])

  fail "Invalid configure deploy_type detected: #{@_config.deploy_type}" unless %w[staging production].include?(@_config.deploy_type)

  @_config
end
requested_hostname() click to toggle source
# File lib/qops/deployment/helpers.rb, line 89
def requested_hostname
  return @requested_hostname if @requested_hostname
  if options[:hostname]
    @requested_hostname = options[:hostname]
    puts "NOTE: You have specified a custom hostname of #{@requested_hostname}. Be sure to continue to use this hostname for future commands to avoid problems."

  # Alternative flow if user has not overridden the hostname
  else
    if config.deploy_type == 'staging'
      @requested_hostname = revision_used.parameterize
    elsif config.deploy_type == 'production'
      @requested_hostname = config.app_name
      existing_hostnames = retrieve_instances.map(&:hostname)
      @requested_hostname += "-#{existing_hostnames.sort.last.to_s.split('-').last.to_i + 1}"
    end
    @requested_hostname = config.hostname_prefix + @requested_hostname
  end

  @requested_hostname = @requested_hostname.gsub(/[^A-Za-z0-9\-]+/, '-').gsub(/-+/, '-')
  @requested_hostname = @requested_hostname[0..62]
  @requested_hostname = @requested_hostname.match(/^([A-Za-z0-9\-]+).*$/)[1]
  @requested_hostname
end
retrieve_instance(instance_id = nil) click to toggle source
# File lib/qops/deployment/helpers.rb, line 40
def retrieve_instance(instance_id = nil)
  # Retrieve a specific instance as necessary
  if instance_id
    instances_results = config.opsworks.describe_instances(instance_ids: [instance_id])
    return instances_results.data.instances.first
  end

  # Get instance based on hostname
  instances_results = config.opsworks.describe_instances(layer_id: config.layer_id)

  # Determine if instance exists.
  instances = instances_results.data.instances

  return unless instances.map(&:hostname).include?(requested_hostname)

  instances.find { |k| k.hostname == requested_hostname }
end
retrieve_instances(options = {}) click to toggle source
# File lib/qops/deployment/helpers.rb, line 32
def retrieve_instances(options = {})
  # Describe and create instances as necessary
  instances_results = config.opsworks.describe_instances({ layer_id: config.layer_id }.merge(options))

  # Determine if instance exists.
  instances_results.data.instances
end
revision_used() click to toggle source
# File lib/qops/deployment/helpers.rb, line 113
def revision_used
  return 'master' unless config.deploy_type == 'staging'
  if options[:branch].present?
    options[:branch]
  elsif `git --version` # rubocop:disable Lint/LiteralAsCondition
    `git symbolic-ref --short HEAD`.strip
  else
    'master'
  end
end
show_stack(options = {}) click to toggle source
# File lib/qops/deployment/helpers.rb, line 124
def show_stack(options = {})
  stack = config.stack(options)
  {
    name: stack.name,
    stack_id: stack.stack_id,
    subnet: stack.default_subnet_id,
    layers: config.layers(options).map { |layer| layer.to_h.slice(:name, :layer_id, :shortname) },
    apps: config.apps(options).map { |app| app.to_h.slice(:name, :app_id) },
    config_manager: stack.configuration_manager.to_h,
    default_os: stack.default_os
  }
end
tag_instance(instance) click to toggle source
# File lib/qops/deployment/helpers.rb, line 58
def tag_instance(instance)
  print "Tagging instance #{instance.hostname}\n"

  tags = [
    {
      key: 'environment',
      value: config.deploy_type
    },
    {
      key: 'branch',
      value: revision_used
    },
    {
      key: 'app',
      value: config.app_name
    }
  ]

  if config.deploy_type == 'staging'
    tags << {
      key: 'cleanable',
      value: 'true'
    }
  end

  config.ec2.create_tags(
    resources: [instance.ec2_instance_id],
    tags: tags
  )
end