Module: Aptible::CLI::Subcommands::LogDrain

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

Class Method Summary collapse

Class Method Details

.included(thor) ⇒ Object



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
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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/aptible/cli/subcommands/log_drain.rb', line 5

def self.included(thor)
  thor.class_eval do
    include Helpers::Token
    include Helpers::Database
    include Helpers::LogDrain
    include Helpers::Telemetry

    drain_flags = '--environment ENVIRONMENT ' \
                  '[--drain-apps|--no-drain-apps] ' \
                  '[--drain-databases|--no-drain-databases] ' \
                  '[--drain-ephemeral-sessions|' \
                  +'--no-drain-ephemeral-sessions] ' \
                  '[--drain_proxies|--no-drain-proxies]'

    def self.drain_options
      option :drain_apps, default: true, type: :boolean
      option :drain_databases, default: true, type: :boolean
      option :drain_ephemeral_sessions, default: true, type: :boolean
      option :drain_proxies, default: true, type: :boolean
      option :environment, aliases: '--env'
    end

    desc 'log_drain:list', 'List all Log Drains'
    option :environment, aliases: '--env'
    define_method 'log_drain:list' do
      telemetry(__method__, options)

      Formatter.render(Renderer.current) do |root|
        root.grouped_keyed_list(
          { 'environment' => 'handle' },
          'handle'
        ) do |node|
          accounts = scoped_environments(options)
          acc_map = environment_map(accounts)

          Aptible::Api::LogDrain.all(
            token: fetch_token,
            href: '/log_drains?per_page=5000'
          ).each do |drain|
             = acc_map[drain.links..href]
            next if .nil?

            node.object do |n|
              ResourceFormatter.inject_log_drain(n, drain, )
            end
          end
        end
      end
    end

    desc 'log_drain:create:elasticsearch HANDLE '\
         '--db DATABASE_HANDLE ' \
         + drain_flags,
         'Create an Elasticsearch Log Drain. By default, App, ' \
         +'Database, Ephemeral Session, and Proxy logs will be sent ' \
         +'to your chosen destination.'
    drain_options
    option :db, type: :string
    option :pipeline, type: :string
    define_method 'log_drain:create:elasticsearch' do |handle|
      telemetry(__method__, options.merge(handle: handle))

       = ensure_environment(options)
      database = ensure_database(options)

      opts = {
        handle: handle,
        database_id: database.id,
        logging_token: options[:pipeline],
        drain_apps: options[:drain_apps],
        drain_databases: options[:drain_databases],
        drain_ephemeral_sessions: options[:drain_ephemeral_sessions],
        drain_proxies: options[:drain_proxies],
        drain_type: :elasticsearch_database
      }

      create_log_drain(, opts)
    end

    desc 'log_drain:create:datadog HANDLE ' \
         '--url DATADOG_URL ' \
         + drain_flags,
         'Create a Datadog Log Drain. By default, App, Database, ' \
         + 'Ephemeral Session, and Proxy logs will be sent ' \
         + 'to your chosen destination.'
    drain_options
    option :url, type: :string
    define_method 'log_drain:create:datadog' do |handle|
      telemetry(__method__, options.merge(handle: handle))

      msg = 'Must be in the format of ' \
            '"https://http-intake.logs.datadoghq.com' \
            '/v1/input/<DD_API_KEY>".'
      create_https_based_log_drain(handle, options, url_format_msg: msg)
    end

    desc 'log_drain:create:https HANDLE ' \
         '--url URL ' \
         + drain_flags,
         'Create a HTTPS Drain'
    option :url, type: :string
    drain_options
    define_method 'log_drain:create:https' do |handle|
      telemetry(__method__, options.merge(handle: handle))
      create_https_based_log_drain(handle, options)
    end

    desc 'log_drain:create:sumologic HANDLE ' \
         '--url SUMOLOGIC_URL ' \
         + drain_flags,
         'Create a Sumologic Drain. By default, App, Database, ' \
         + 'Ephemeral Session, and Proxy logs will be sent ' \
         + 'to your chosen destination.'
    option :url, type: :string
    drain_options
    define_method 'log_drain:create:sumologic' do |handle|
      telemetry(__method__, options.merge(handle: handle))
      create_https_based_log_drain(handle, options)
    end

    desc 'log_drain:create:logdna HANDLE ' \
         '--url LOGDNA_URL ' \
         + drain_flags,
         'Create a LogDNA/Mezmo Log Drain. By default, App, ' \
         + 'Database, Ephemeral Session, and Proxy logs ' \
         + 'will be sent to your chosen destination.'
    option :url, type: :string
    drain_options
    define_method 'log_drain:create:logdna' do |handle|
      telemetry(__method__, options.merge(handle: handle))

      msg = 'Must be in the format of ' \
            '"https://logs.logdna.com/aptible/ingest/<INGESTION KEY>".'
      create_https_based_log_drain(handle, options, url_format_msg: msg)
    end

    desc 'log_drain:create:papertrail HANDLE ' \
         '--host PAPERTRAIL_HOST --port PAPERTRAIL_PORT ' \
         + drain_flags,
         'Create a Papertrail Log Drain. By default, App, Database, ' \
         + 'Ephemeral Session, and Proxy logs will be sent ' \
         + 'to your chosen destination.'
    option :host, type: :string
    option :port, type: :string
    drain_options
    define_method 'log_drain:create:papertrail' do |handle|
      telemetry(__method__, options.merge(handle: handle))
      create_syslog_based_log_drain(handle, options)
    end

    desc 'log_drain:create:syslog HANDLE ' \
         '--host SYSLOG_HOST --port SYSLOG_PORT ' \
         '[--token TOKEN] ' \
         + drain_flags,
         'Create a Syslog Log Drain. By default, App, Database, ' \
         + 'Ephemeral Session, and Proxy logs will be sent ' \
         + 'to your chosen destination.'
    option :host, type: :string
    option :port, type: :string
    option :token, type: :string
    drain_options
    define_method 'log_drain:create:syslog' do |handle|
      telemetry(__method__, options.merge(handle: handle))
      create_syslog_based_log_drain(handle, options)
    end

    desc 'log_drain:deprovision HANDLE --environment ENVIRONMENT',
         'Deprovisions a log drain'
    option :environment, aliases: '--env'
    define_method 'log_drain:deprovision' do |handle|
      telemetry(__method__, options.merge(handle: handle))
       = ensure_environment(options)
      drain = ensure_log_drain(, handle)
      op = drain.create_operation(type: :deprovision)
      begin
        attach_to_operation_logs(op)
      rescue HyperResource::ClientError => e
        # A 404 here means that the operation completed successfully,
        # and was removed faster than attach_to_operation_logs
        # could attach to the logs.
        raise if e.response.status != 404
      end
    end
  end
end