Class: OpsDeploy::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/ops_deploy/cli.rb

Defined Under Namespace

Classes: Notifier

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ops_deploy/cli.rb', line 3

def initialize
  config = {
    region: OpsDeploy::CLI.argument('aws-region', 'AWS_REGION', true)
  }

  profile = OpsDeploy::CLI.argument('aws-profile', 'AWS_PROFILE')
  config[:credentials] = Aws::SharedCredentials.new(profile_name: profile) if profile

  aws_access_key_id = OpsDeploy::CLI.argument('aws-access-key-id', 'AWS_ACCESS_KEY_ID')
  aws_secret_access_key = OpsDeploy::CLI.argument('aws-secret-access-key', 'AWS_SECRET_ACCESS_KEY')
  if aws_access_key_id && aws_secret_access_key
    config[:access_key_id] = aws_access_key_id
    config[:secret_access_key] = aws_secret_access_key
  end

  @main = OpsDeploy.new(config)
  @stacks = {}
  @notifier = OpsDeploy::CLI::Notifier.new(slack: {
    webhook_url: OpsDeploy::CLI.argument('slack-webhook-url', 'SLACK_WEBHOOK_URL'),
    username: OpsDeploy::CLI.argument('slack-username', 'SLACK_USERNAME'),
    channel: OpsDeploy::CLI.argument('slack-channel', 'SLACK_CHANNEL'),
    notify_user: OpsDeploy::CLI.argument('slack-notify-user', 'SLACK_NOTIFY_USER')
  })

  @notification_messages = {
    info: [],
    success: [],
    failure: []
  }
  @notification_success = false
  @notification_failure = false
end

Class Method Details

.argument(argv_name, env_name = nil, required = false) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/ops_deploy/cli.rb', line 175

def self.argument(argv_name, env_name = nil, required = false)
  value = nil
  value = ENV[env_name] if env_name

  if value.nil?
    value = ARGV.include?(argv_name) ? true : nil
    return value if value

    ARGV.each do |arg|
      if arg.start_with?("--#{argv_name}=")
        value = arg.split("--#{argv_name}=").last
      end
    end
  end

  if required && value.nil?
    env_name_message = env_name ? "the environment variable #{env_name} or " : ''
    error = "Argument '#{argv_name}' unspecified."
    suggestion = "Please set #{env_name_message}the argument --#{argv_name}=<value>"
    fail StandardError.new("#{error} #{suggestion}")
  end

  value
end

Instance Method Details

#check_instances(stack_id_name_or_object, via_proxy = false) ⇒ Object



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
# File 'lib/ops_deploy/cli.rb', line 95

def check_instances(stack_id_name_or_object, via_proxy = false)
  stack = find_stack(stack_id_name_or_object)

  if via_proxy
    Pusher.url = OpsDeploy::CLI.argument('pusher-url', 'PUSHER_URL', true)
    Pusher.trigger('OpsDeploy', 'check_instances', stack: stack.stack_id,
                                                   slack: @notifier.options[:slack])
  else
    @main.instances_check_callback = proc { |instance, response, error|
      puts
      if error.nil? && response.code == 200
        success_msg('Response from', "#{instance.hostname.green}:", '200 OK'.green)
      elsif error.nil?
        failure_msg('Response from', "#{instance.hostname.red}:", "#{response.code}".red)
      else
        failure_msg('Error checking', "#{instance.hostname.red}:", error.to_s)
      end
    }

    waiter = @main.check_instances(stack)
    if waiter
      step_msg("Checking instances' HTTP response...")

      print '.'.blue
      print '.'.blue until waiter.join(1)

      info_msg('Response check finished')
    else
      info_msg('No online instances on stack', "'#{stack_id_name_or_object.blue}'")
    end

    send_notification(stack)
  end
end

#start_deployment(stack_id_name_or_object, application_id = nil, migrate = false) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ops_deploy/cli.rb', line 36

def start_deployment(stack_id_name_or_object, application_id = nil, migrate = false)
  stack = find_stack(stack_id_name_or_object)

  step_msg('Starting deployment on stack', "'#{stack.name.blue}'...")

  if @main.start_deployment(stack, application_id, migrate)
    success_msg('Deployment started on stack', "'#{stack.name.green}'")
    send_notification(stack)

    true
  else
    failure_msg("Couldn't start deployment on stack", "'#{stack.name.red}'")
    send_notification(stack)

    false
  end
end

#start_serverObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/ops_deploy/cli.rb', line 130

def start_server
  pusher_comp = URI.parse(OpsDeploy::CLI.argument('pusher-url', 'PUSHER_URL', true))
  PusherClient.logger.level = Logger::ERROR
  socket = PusherClient::Socket.new(pusher_comp.user, secure: true)
  socket.subscribe('OpsDeploy')

  socket['OpsDeploy'].bind('check_instances') do |data|
    begin
      info = data
      info = symbolize_keys(JSON.parse(data)) if data.is_a?(String)

      with_slack(info[:slack]) do
        check_instances(info[:stack])
      end
    rescue StandardError => e
      puts e
    end
  end

  socket['OpsDeploy'].bind('wait_for_deployments') do |data|
    begin
      info = data
      info = symbolize_keys(JSON.parse(data)) if data.is_a?(String)

      with_slack(info[:slack]) do
        wait_for_deployments(info[:stack])
      end
    rescue StandardError => e
      puts e
    end
  end

  info_msg("Started OpsDeploy server #{OpsDeploy::VERSION}")
  socket.connect
end

#wait_for_deployments(stack_id_name_or_object, via_proxy = false) ⇒ Object



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
# File 'lib/ops_deploy/cli.rb', line 54

def wait_for_deployments(stack_id_name_or_object, via_proxy = false)
  stack = find_stack(stack_id_name_or_object)

  if via_proxy
    Pusher.url = OpsDeploy::CLI.argument('pusher-url', 'PUSHER_URL', true)
    Pusher.trigger('OpsDeploy', 'wait_for_deployments', stack: stack.stack_id,
                                                        slack: @notifier.options[:slack])
  else
    step_msg('Checking deployments...')
    notify_user = @notifier.notify_user

    @main.deployments_callback = proc { |deployment|
      puts
      if (deployment.status == 'successful')
        success_msg('Deployment', 'OK'.green.bold,
                    deployment.duration ? "(#{deployment.duration}s)" : '',
                    notify_user ? "@#{notify_user}" : '')
      else
        failure_msg('Deployment', 'Failed'.red.bold,
                    deployment.duration ? "(#{deployment.duration}s)" : '',
                    notify_user ? "@#{notify_user}" : '')
      end
    }

    waiter = @main.wait_for_deployments(stack)
    if waiter
      step_msg('Waiting for deployments to finish...')

      print '.'.blue
      print '.'.blue until waiter.join(1)

      info_msg('Deployments finished')
    else
      info_msg('No running deployments on stack', "'#{stack_id_name_or_object.blue}'",
               notify_user ? "@#{notify_user}" : '')
    end

    send_notification(stack)
  end
end

#with_slack(slack_options = nil) ⇒ Object



166
167
168
169
170
171
172
173
# File 'lib/ops_deploy/cli.rb', line 166

def with_slack(slack_options = nil)
  old_notifier = @notifier
  @notifier = OpsDeploy::CLI::Notifier.new(slack: slack_options) if slack_options

  yield

  @notifier = old_notifier
end