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



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 139

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 {
      |arg|
      if arg.start_with?("--#{argv_name}=")
        value = arg.split("--#{argv_name}=").last
      end
    }
  end

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

  value
end

Instance Method Details

#check_instances(stack_id_name_or_object, via_proxy = false) ⇒ Object



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

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['OpsDeploy'].trigger('check_instances', {
      stack: stack.stack_id
    })
  else
    @main.instances_check_callback = Proc.new {
      |instance, response, error|

      puts

      if (error.nil? and 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_check_serverObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/ops_deploy/cli.rb', line 118

def start_check_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.kind_of?(String)
      stack_id = info["stack"] || info[:stack]

      check_instances(stack_id)
    rescue StandardError => e
      puts e
    end
  end

  info_msg("Started OpsDeploy server")
  socket.connect
end

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



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

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

#wait_for_deployments(stack_id_name_or_object) ⇒ Object



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

def wait_for_deployments(stack_id_name_or_object)
  stack = find_stack(stack_id_name_or_object)

  step_msg("Checking deployments...")
  @main.deployments_callback = Proc.new {
    |deployment|

    puts

    if (deployment.status == "successful")
      success_msg("Deployment", "OK".green.bold, deployment.duration ? "(#{deployment.duration.to_s}s)" : "")
    else
      failure_msg("Deployment", "Failed".red.bold, deployment.duration ? "(#{deployment.duration.to_s}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