Sha256: 49918fa1f6943a44d5f961f1419e46f91431f77e304503c7799ca040b95f02a3

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

module Phase
  module Deploy

    class Deployment
      include ::Phase::Util::Console
      include ::Phase::Util::Shell

      attr_reader :role_name, :version_tag, :options

      def initialize(role_name, version_tag, options = {})
        @role_name, @version_tag, @options = role_name, version_tag, options
      end

      def execute!
        hosts = ::Phase::Adapters::AWS::Server.where(role: role_name).map do |host|
          "deploy@#{host.resource.dns_name}"
        end

        fail "No instance with role `role_name' found" if hosts.blank?

        deploy_image(hosts)
      end

      private

        def deploy_image(hosts, options = {})
          image_name   = docker_image_name
          server_count = 2  # FIXME: make this a config var
          run_flags    = docker_run_flags

          ::SSHKit::Coordinator.new(hosts).each(options) do
            execute(:docker, :pull, image_name)

            # TODO: fail if pending migrations
            execute(:docker, :run,  run_flags, "--rm -it", image_name,
              "bundle exec rails runner '::ActiveRecord::Migrator.needs_migration? ? exit(1) : exit(0)'")

            server_count.times do |i|
              server_name = "server-#{i}"
              port_number = 3000 + i  # FIXME: make this a config var

              execute(:docker, :stop, server_name, raise_on_non_zero_exit: false)
              execute(:docker, :rm,   server_name, raise_on_non_zero_exit: false)

              execute(:docker, :run,  run_flags, "-p #{port_number}:3000 -d", image_name)
            end
          end
        end

        def docker_image_name
          @docker_image ||= "#{::Phase.config.deploy.docker_repository}:#{version_tag}"
        end

        def docker_run_flags
          @docker_run_flags ||= ::Phase.config.deploy.docker_run_flags
        end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
phase-1.0.0.rc2 lib/phase/kit/deploy/deployment.rb