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
# 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

  @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')
                                           })

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

Class Method Details

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



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/ops_deploy/cli.rb', line 170

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



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

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

  if via_proxy
    slack_webhook_url = OpsDeploy::CLI.argument('slack-webhook-url', 'SLACK_WEBHOOK_URL')
    Pusher.url = OpsDeploy::CLI.argument('pusher-url', 'PUSHER_URL', true)
    Pusher.trigger('OpsDeploy', 'check_instances', stack: stack.stack_id,
                                                   slack: slack_webhook_url)
  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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ops_deploy/cli.rb', line 28

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



117
118
119
120
121
122
123
124
125
126
127
128
129
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
# File 'lib/ops_deploy/cli.rb', line 117

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 = JSON.parse(data) if data.is_a?(String)
      stack_id = info['stack'] || info[:stack]
      slack_webhook_url = info['slack'] || info[:slack]

      with_slack_webhook(slack_webhook_url) do
        check_instances(stack_id)
      end
    rescue StandardError => e
      puts e
    end
  end

  socket['OpsDeploy'].bind('wait_for_deployments') do |data|
    begin
      info = data
      info = JSON.parse(data) if data.is_a?(String)
      stack_id = info['stack'] || info[:stack]
      slack_webhook_url = info['slack'] || info[:slack]

      with_slack_webhook(slack_webhook_url) do
        wait_for_deployments(stack_id)
      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



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

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

  if via_proxy
    slack_webhook_url = OpsDeploy::CLI.argument('slack-webhook-url', 'SLACK_WEBHOOK_URL')
    Pusher.url = OpsDeploy::CLI.argument('pusher-url', 'PUSHER_URL', true)
    Pusher.trigger('OpsDeploy', 'wait_for_deployments', stack: stack.stack_id,
                                                        slack: slack_webhook_url)
  else
    step_msg('Checking deployments...')
    @main.deployments_callback = proc { |deployment|
      puts
      if (deployment.status == 'successful')
        success_msg('Deployment', 'OK'.green.bold, deployment.duration ? "(#{deployment.duration}s)" : '')
      else
        failure_msg('Deployment', 'Failed'.red.bold, deployment.duration ? "(#{deployment.duration}s)" : '')
      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}'")
    end

    send_notification(stack)
  end
end

#with_slack_webhook(slack_webhook_url = nil) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/ops_deploy/cli.rb', line 157

def with_slack_webhook(slack_webhook_url = nil)
  old_notifier = @notifier
  if slack_webhook_url && !slack_webhook_url.strip.empty?
    @notifier = OpsDeploy::CLI::Notifier.new(slack: {
                                             webhook_url: slack_webhook_url
                                             })
  end

  yield

  @notifier = old_notifier
end