<commit_before>#
# Command: deploy
#

require 'ridoku/base'

module Ridoku
  register :deploy

  class Deploy < Base
    attr_accessor :app

    def run
      clist = Base.config[:command]
      command = clist.shift
      sub_command = clist.shift

      case sub_command
      when 'to', nil
        deploy
      when 'rollback'
        rollback
      when 'restart'
        restart
      when 'info'
        info
      else
        print_deploy_help
      end
    end

    protected

    def rollback
      Base.fetch_instance
      Base.fetch_app

      Base.instances.select! { |inst| inst[:status] == 'online' }
      instance_ids = Base.instances.map { |inst| inst[:instance_id] }

      # :app_source => {
      #   :type => "git",
      #   :url => "<EMAIL>:Protobit/Survly.git",
      #   :ssh_key => "*****FILTERED*****",
      #   :revision => "opsworks-staging"
      # }

      $stdout.puts "Application:"
      $stdout.puts "  #{$stdout.colorize(Base.app[:name], :bold)}"
      $stdout.puts "Rolling back on #{Base.instances.length} instance(s):"
      Base.pretty_instances($stdout).each do |inst|
        $stdout.puts "  #{inst}"
      end
      $stdout.puts "Repository:"
      $stdout.puts "  #{$stdout.colorize(Base.app[:app_source][:url], :bold)} " +
        "@ #{$stdout.colorize(Base.app[:app_source][:revision], :bold)}"

      command = Base.rollback(Base.app[:app_id], instance_ids,
        Base.config[:comment], {
          opsworks_custom_cookbooks: {
            recipes: [
              "workers::rollback"
            ]
          }
        }
      )

      Base.run_command(command)
    end

    def deploy
      custom_json = {}.tap do |json|
        json[:deploy] = {
          Base.config[:app] => {
            action: 'force_deploy'
          }
        } if Base.config[:force]

        json[:migrate] = true if Base.config[:migrate]
        json[:opsworks_custom_cookbooks] = {
          recipes: [
            "workers::deploy"
          ]
        }
      end

      if Base.config[:force]
        begin
          $stdout.puts 'Hold your seats: force deploying in... (Press CTRL-C to Stop)'
          5.times { |t| $stdout.print "#{$stdout.colorize(5-t, :red)} "; sleep 1 }
          $stdout.puts "\nSilence is acceptance..."
        rescue Interrupt
          $stdout.puts $stdout.colorize("\nCommand canceled", :green)
          exit 1
        end
      end

      $stdout.puts 'Database will be migrated.' if Base.config[:migrate]
      Base.standard_deploy(:all, custom_json)
    end

    def restart
      Base.config[:layers] = 'rails-app'

      custom_json = {}.tap do |json|
        json[Base.app[:name]] = {
          deploy: {
            application_type: 'rails'
          }
        }
      end

      $stdout.puts "Restarting application: #{Base.app[:name]}."
      Ridoku::Cook.cook_recipe('unicorn::force-restart', custom_json)
    end

    def info
      Base.fetch_instance('rails-app') unless Base.instances<commit_msg>Fetch app before accessing its name.<commit_after>#
# Command: deploy
#

require 'ridoku/base'

module Ridoku
  register :deploy

  class Deploy < Base
    attr_accessor :app

    def run
      clist = Base.config[:command]
      command = clist.shift
      sub_command = clist.shift

      case sub_command
      when 'to', nil
        deploy
      when 'rollback'
        rollback
      when 'restart'
        restart
      when 'info'
        info
      else
        print_deploy_help
      end
    end

    protected

    def rollback
      Base.fetch_instance
      Base.fetch_app

      Base.instances.select! { |inst| inst[:status] == 'online' }
      instance_ids = Base.instances.map { |inst| inst[:instance_id] }

      # :app_source => {
      #   :type => "git",
      #   :url => "<EMAIL>.com:Protobit/Survly.git",
      #   :ssh_key => "*****FILTERED*****",
      #   :revision => "opsworks-staging"
      # }

      $stdout.puts "Application:"
      $stdout.puts "  #{$stdout.colorize(Base.app[:name], :bold)}"
      $stdout.puts "Rolling back on #{Base.instances.length} instance(s):"
      Base.pretty_instances($stdout).each do |inst|
        $stdout.puts "  #{inst}"
      end
      $stdout.puts "Repository:"
      $stdout.puts "  #{$stdout.colorize(Base.app[:app_source][:url], :bold)} " +
        "@ #{$stdout.colorize(Base.app[:app_source][:revision], :bold)}"

      command = Base.rollback(Base.app[:app_id], instance_ids,
        Base.config[:comment], {
          opsworks_custom_cookbooks: {
            recipes: [
              "workers::rollback"
            ]
          }
        }
      )

      Base.run_command(command)
    end

    def deploy
      custom_json = {}.tap do |json|
        json[:deploy] = {
          Base.config[:app] => {
            action: 'force_deploy'
          }
        } if Base.config[:force]

        json[:migrate] = true if Base.config[:migrate]
        json[:opsworks_custom_cookbooks] = {
          recipes: [
            "workers::deploy"
          ]
        }
      end

      if Base.config[:force]
        begin
          $stdout.puts 'Hold your seats: force deploying in... (Press CTRL-C to Stop)'
          5.times { |t| $stdout.print "#{$stdout.colorize(5-t, :red)} "; sleep 1 }
          $stdout.puts "\nSilence is acceptance..."
        rescue Interrupt
          $stdout.puts $stdout.colorize("\nCommand canceled", :green)
          exit 1
        end
      end

      $stdout.puts 'Database will be migrated.' if Base.config[:migrate]
      Base.standard_deploy(:all, custom_json)
    end

    def restart
      Base.fetch_app

      Base.config[:layers] = 'rails-app'
      custom_json = {}.tap do |json|
        json[Base.app[:name]] = {
          deploy: {
            application_type: 'rails'
          }
        }
      end

      $stdout.puts "Restarting application: #{Base.app[:name]}."
      Ridoku::Cook.cook_recipe('unicorn::force-restart', custom_json)
    end

    def info
      Base.fetch_instance('rails-app') unless Base.instances
{'fraction_non_alphanumeric': 0.13578807064311896, 'fraction_numerical': 0.0013328890369876708, 'mean_word_length': 1.9629812438302072, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7618698', 'n_tokens_mistral': 1948, 'n_tokens_neox': 1882, 'n_words': 377}