module Qops::Helpers

Private Instance Methods

been_a_minute?(i) click to toggle source
# File lib/qops/helpers.rb, line 21
def been_a_minute?(i)
  i > 1 && (i % 60).zero?
end
initialize_run() click to toggle source
# File lib/qops/helpers.rb, line 12
def initialize_run
  return if @_run_initialized

  @_run_initialized = true
  verify_opsworks_config
  verify_environment_selection
  Qops::Environment.notifiers
end
iterator(options) { |i| ... } click to toggle source
# File lib/qops/helpers.rb, line 25
def iterator(options)
  config.wait_iterations.times.each do |i|
    result = yield(i)
    break if result

    if i + 1 == config.wait_iterations
      puts " failed to complete within #{config.wait_iterations} seconds"
      ping_slack('Quandl::Slack::Release', 'Command timeout', 'failure',
                 options.merge(failed_at: Time.now))
      exit(-1)
    elsif been_a_minute?(i)
      print " #{i / 60} minute(s) "
    end

    sleep(1)
  end
end
ping_slack(notifier, message, status, manifest) click to toggle source
# File lib/qops/helpers.rb, line 43
def ping_slack(notifier, message, status, manifest)
  fields = manifest.keys
  fields.map! { |field| { title: field, value: manifest[field], short: true } }

  if Object.const_defined?(notifier)
    notifier_class = notifier.constantize

    notifier_class.ping("#{config.app_name}: #{message}",
                        attachments: [{ color: status, mrkdwn_in: ['text'], fallback: 'Details', fields: fields }])
  else
    puts "#{config.app_name}: #{message}"
    pp fields
  end
end
read_failure_log(opsworks_options, options = {}) click to toggle source
# File lib/qops/helpers.rb, line 90
def read_failure_log(opsworks_options, options = {})
  results = config.opsworks.describe_commands(opsworks_options)
  results.commands.each do |command|
    if command.log_url
      puts "\nReading last 100 lines from #{command.log_url}\n"
      lines = open(command.log_url).read.split("\n")
      num_lines = lines.count < config.command_log_lines ? lines.count : config.command_log_lines
      puts open(command.log_url).read.split("\n")[-1 * num_lines..-1].join("\n")
      puts "\nLog file at: #{command.log_url}"
    end

    ping_slack(
      'Quandl::Slack::Release',
      'Deployment failure',
      'failure',
      (options[:manifest] || {}).merge(
        command: command.type,
        status: command.status
      )
    )

    exit(-1) if options[:last_only]
  end

  exit(-1)
end
run_opsworks_command(deployment_params, instance_ids = []) click to toggle source
# File lib/qops/helpers.rb, line 58
def run_opsworks_command(deployment_params, instance_ids = [])
  deployment_params[:instance_ids] = instance_ids unless instance_ids.empty?

  # Create the deployment
  deployment_results = config.opsworks.create_deployment(deployment_params)
  deployment_id = deployment_results.data.deployment_id

  iterator(deployment_params) do |i|
    deployment_results = config.opsworks.describe_deployments(deployment_ids: [deployment_id])
    deployment = deployment_results.data.deployments.first

    if deployment.completed_at
      puts ' ' + deployment.status

      if deployment.status != 'successful'
        read_failure_log(deployment_id: deployment.deployment_id)
        exit(-1)
      end

      true
    else
      print '.'
      if been_a_minute?(i)
        print " #{retrieve_instance.status} :" if config.deploy_type == :staging && instance_ids.any?
        print " #{deployment.status} :" if config.deploy_type == :production
      end
    end
  end

  print "\n"
end
verify_environment_selection() click to toggle source
# File lib/qops/helpers.rb, line 122
def verify_environment_selection
  return if Quandl::Config.environment

  project_root = Pathname.new(Quandl::ProjectRoot.root)
  file_path = project_root.join('config', "#{Qops::Environment.file_name}.yml")

  if File.exist?(file_path) # rubocop:disable Style/GuardClause
    raw_config = File.read(file_path)
    erb_config = ERB.new(raw_config).result
    configs = YAML.load(erb_config)

    env = options[:environment]

    msg = 'Run command using config environment:'
    msg = "Invalid config environment '#{env}'. Switch to:" if env && !configs.keys.include?(env)

    unless env && configs.keys.include?(env)
      env = Thor::Shell::Color.new.ask(
        msg,
        :yellow,
        limited_to: configs.keys.reject { |g| g.start_with?('_') },
        echo: false
      )
    end

    Quandl::Config.environment = env
    puts "\nRunning commands with config environment: #{env}"
  else
    raise "Not a qops compatible project. Please be sure to add a config/opsworks.yml file as described in the readme. Path: #{file_path}"
  end
end
verify_opsworks_config() click to toggle source
# File lib/qops/helpers.rb, line 117
def verify_opsworks_config
  return if File.exist?("config/#{Qops::Environment.file_name}.yml")
  raise "Could not find configuration file: config/#{Qops::Environment.file_name}.yml"
end