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



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

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



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

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



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
156
157
158
159
160
# File 'lib/ops_deploy/cli.rb', line 126

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
# 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...')

    @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(slack_options = nil) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/ops_deploy/cli.rb', line 162

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