Module: Aptible::CLI::Subcommands::Deploy

Included in:
Agent
Defined in:
lib/aptible/cli/subcommands/deploy.rb

Constant Summary collapse

DEPRECATED_ENV =
Hash[%w(
APTIBLE_DOCKER_IMAGE
APTIBLE_PRIVATE_REGISTRY_USERNAME
APTIBLE_PRIVATE_REGISTRY_PASSWORD
        ).map do |var|
opt = var.gsub(/^APTIBLE_/, '').downcase.to_sym
[opt, var]
NULL_SHA1 =
'0000000000000000000000000000000000000000'.freeze

Class Method Summary collapse

Class Method Details

.included(thor) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/aptible/cli/subcommands/deploy.rb', line 16

def self.included(thor)
  thor.class_eval do
    include Helpers::Operation
    include Helpers::App
    include Helpers::Telemetry

    desc 'deploy [--app APP] [OPTIONS] [VAR1=VAL1] [VAR2=VAL2] [...]',
         'Deploy an app'
    option :git_commitish,
           desc: 'Deploy a specific git commit or branch: the ' \
                 'commitish must have been pushed to Aptible beforehand'
    option :git_detach,
           type: :boolean, default: false,
           desc: 'Detach this app from its git repository: ' \
                 'its Procfile, Dockerfile, and .aptible.yml will be ' \
                 'ignored until you deploy again with git'
    option :container_count, type: :numeric,
                             desc: 'This option only affects new ' \
                                   'services, not existing ones.'
    option :container_size, type: :numeric,
                            desc: 'This option only affects new ' \
                                   'services, not existing ones.'
    option :container_profile, type: :string,
                               desc: 'This option only affects new ' \
                                     'services, not existing ones. ' \
                                     'Examples: m c r'

    option :docker_image,
           type: :string,
           desc: 'The docker image to deploy. If none specified, ' \
                 'the currently deployed image will be pulled again'
    option :private_registry_username,
           type: :string,
           desc: 'Username for Docker images located in a private ' \
                'repository'
    option :private_registry_password,
           type: :string,
           desc: 'Password for Docker images located in a private ' \
                 'repository'
    option :private_registry_email,
           type: :string,
           desc: 'This parameter is deprecated'

    app_options
    def deploy(*args)
      telemetry(__method__, options)

      app = ensure_app(options)

      git_ref = options[:git_commitish]
      if options[:git_detach]
        if git_ref
          raise Thor::Error, 'The options --git-commitish and ' \
                             '--git-detach are incompatible'
        end
        git_ref = NULL_SHA1
      end

      env = extract_env(args)

      DEPRECATED_ENV.each_pair do |opt, var|
        val = options[opt]
        dasherized = "--#{opt.to_s.tr('_', '-')}"
        if env[var]
          m = "WARNING: The environment variable #{var} " \
              'will be deprecated. Use the option ' \
              "#{dasherized}, instead."
          CLI.logger.warn m
        end
        next unless val
        if env[var] && env[var] != val
          raise Thor::Error, "The options #{dasherized} and #{var} " \
                             'cannot be set to different values'
        end
      end

      settings = {}
      sensitive_settings = {}

      if options[:docker_image]
        settings['APTIBLE_DOCKER_IMAGE'] = options[:docker_image]
      end

      if options[:private_registry_username]
        sensitive_settings['APTIBLE_PRIVATE_REGISTRY_USERNAME'] =
          options[:private_registry_username]
      end
      if options[:private_registry_password]
        sensitive_settings['APTIBLE_PRIVATE_REGISTRY_PASSWORD'] =
          options[:private_registry_password]
      end

      opts = {
        type: 'deploy',
        env: env,
        settings: settings,
        sensitive_settings: sensitive_settings,
        git_ref: git_ref,
        container_count: options[:container_count],
        container_size: options[:container_size],
        instance_profile: options[:container_profile]
      }.delete_if { |_, v| v.nil? || v.try(:empty?) }

      allow_it = [
        opts[:git_ref],
        opts[:settings].try(:[], 'APTIBLE_DOCKER_IMAGE'),
        app.status == 'provisioned'
      ].any? { |x| x }

      unless allow_it
        m = 'You need to deploy either from git or a Docker image'
        raise Thor::Error, m
      end

      operation = app.create_operation!(opts)

      CLI.logger.info 'Deploying app...'
      attach_to_operation_logs(operation)
    end
  end
end