kennel 1.66.2 → 1.70.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d5c7df715aecb3223069ef066f070a0b78dd699c46feae0052e90facc2ef0f5
4
- data.tar.gz: d88d4f7b8ff2ec7458490fd79cf269bc319abf589c17ace29a4d368a29626b2d
3
+ metadata.gz: 857c5d57edd43c806560c9a97b579398f440ed311dd7b8541e1262af03de51bd
4
+ data.tar.gz: b75bf7667bcdd473732d1e69231a52a37bdd90366462890a85d39d6325004593
5
5
  SHA512:
6
- metadata.gz: f7a603b340355a6be34c57d7843729dbdf9d27f910d94511ab4dc6c5a71c32a477160ebfe188fc03675bc28261dc9beb82f27faaceb4be355aceba343bbf5c54
7
- data.tar.gz: e320fdfb09fc4f2400cdcad2f16297e6715dbb9094959e8fd50784bc7b1a4b880e65de806caaaa1b3e016ce90a4d8217e1c3e174b757cf6d83895273e5652b80
6
+ metadata.gz: 51662e560022e916c3b2e7254cb9dcc4ec331468f06cc29c47f45ea857c1dc49ded3496b9313a06073848abc782576c46d0bfcd410099ababdc5571a19cccc28
7
+ data.tar.gz: de5dd05943415b655efc290399417853a68477a81a88c098b4812d908e83a19ff06594dbb143f6b3120a41caca2cbbe8d86ef27a8eee449e2d34bc05b1da5140
data/Readme.md CHANGED
@@ -72,7 +72,7 @@ end
72
72
  ### Updating an existing monitor
73
73
  - use [datadog monitor UI](https://app.datadoghq.com/monitors/manage) to find a monitor
74
74
  - get the `id` from the url
75
- - run `RESOURCE=monitor ID=12345 bundle exec rake kennel:import` and copy the output
75
+ - run `URL='https://app.datadoghq.com/monitors/123' bundle exec rake kennel:import` and copy the output
76
76
  - find or create a project in `projects/`
77
77
  - add the monitor to `parts: [` list, for example:
78
78
  ```Ruby
@@ -118,7 +118,7 @@ end
118
118
  ### Updating an existing dashboard
119
119
  - go to [datadog dashboard UI](https://app.datadoghq.com/dashboard/lists) and click on _New Dashboard_ to find a dashboard
120
120
  - get the `id` from the url
121
- - run `RESOURCE=dashboard ID=abc-def-ghi bundle exec rake kennel:import` and copy the output
121
+ - run `URL='https://app.datadoghq.com/dashboard/bet-foo-bar' bundle exec rake kennel:import` and copy the output
122
122
  - find or create a project in `projects/`
123
123
  - add a dashboard to `parts: [` list, for example:
124
124
  ```Ruby
@@ -34,14 +34,6 @@ module Kennel
34
34
  super
35
35
 
36
36
  widgets_pairs(expected, actual).each do |pair|
37
- # datadog always adds 2 to slo widget height
38
- # need to check fir layout since some monitors have height/width in their definition
39
- pair[1].each do |widget|
40
- if widget.dig(:definition, :type) == "slo" && widget.dig(:layout, :height)
41
- widget[:layout][:height] -= 2
42
- end
43
- end
44
-
45
37
  # conditional_formats ordering is randomly changed by datadog, compare a stable ordering
46
38
  pair.each do |widgets|
47
39
  widgets.each do |widget|
@@ -124,6 +116,10 @@ module Kennel
124
116
  Utils.path_to_url "/dashboard/#{id}"
125
117
  end
126
118
 
119
+ def self.parse_url(url)
120
+ url[/\/dashboard\/([a-z\d-]+)/, 1]
121
+ end
122
+
127
123
  def resolve_linked_tracking_ids(id_map)
128
124
  widgets = as_json[:widgets].flat_map { |w| [w, *w.dig(:definition, :widgets) || []] }
129
125
  widgets.each do |widget|
@@ -120,6 +120,12 @@ module Kennel
120
120
  Utils.path_to_url "/monitors##{id}/edit"
121
121
  end
122
122
 
123
+ # datadog uses both / and # as separator in it's links
124
+ def self.parse_url(url)
125
+ return unless id = url[/\/monitors[\/#](\d+)/, 1]
126
+ Integer(id)
127
+ end
128
+
123
129
  def self.normalize(expected, actual)
124
130
  super
125
131
  options = actual.fetch(:options)
@@ -199,7 +205,7 @@ module Kennel
199
205
  allowed = data.fetch(:query)[/by\s*{([^\}]+)}/, 1].to_s.split(/\s*,\s*/)
200
206
  unsupported = used - allowed
201
207
  if unsupported.any?
202
- invalid! "is_match used with unsupported dimensions #{unsupported}, allowed dimensions are #{allowed}"
208
+ invalid! "is_match used with #{unsupported}, but metric is only grouped by #{allowed}"
203
209
  end
204
210
  end
205
211
  end
@@ -11,6 +11,14 @@ module Kennel
11
11
  settings :id, :kennel_id
12
12
 
13
13
  class << self
14
+ def parse_any_url(url)
15
+ subclasses.detect do |s|
16
+ if id = s.parse_url(url)
17
+ break s.api_resource, id
18
+ end
19
+ end
20
+ end
21
+
14
22
  private
15
23
 
16
24
  def normalize(_expected, actual)
@@ -62,6 +62,10 @@ module Kennel
62
62
  Utils.path_to_url "/slo?slo_id=#{id}"
63
63
  end
64
64
 
65
+ def self.parse_url(url)
66
+ url[/\/slo\?slo_id=([a-z\d]+)/, 1]
67
+ end
68
+
65
69
  def resolve_linked_tracking_ids(id_map)
66
70
  as_json[:monitor_ids] = as_json[:monitor_ids].map do |id|
67
71
  id.is_a?(String) ? resolve_link(id, :monitor, id_map) : id
@@ -25,8 +25,7 @@ module Kennel
25
25
  names.each do |name|
26
26
  next if method_defined?(name)
27
27
  define_method name do
28
- message = "Trying to call #{name} for #{self.class} but it was never set or passed as option"
29
- raise_with_location ArgumentError, message
28
+ raise_with_location ArgumentError, "'#{name}' on #{self.class} was not set or passed as option"
30
29
  end
31
30
  end
32
31
  end
@@ -19,6 +19,7 @@ module Kennel
19
19
  end
20
20
  @expected.each { |e| add_tracking_id e }
21
21
  calculate_diff
22
+ prevent_irreversible_partial_updates
22
23
  end
23
24
 
24
25
  def plan
@@ -42,7 +43,6 @@ module Kennel
42
43
  Kennel.out.puts "Created #{e.class.api_resource} #{tracking_id(e.as_json)} #{e.url(reply.fetch(:id))}"
43
44
  end
44
45
 
45
- block_irreversible_partial_updates
46
46
  @update.each do |id, e|
47
47
  @api.update e.class.api_resource, id, e.as_json
48
48
  Kennel.out.puts "Updated #{e.class.api_resource} #{tracking_id(e.as_json)} #{e.url(id)}"
@@ -151,7 +151,7 @@ module Kennel
151
151
  return if list.empty?
152
152
  list.each do |_, e, a, diff|
153
153
  api_resource = (e ? e.class.api_resource : a.fetch(:api_resource))
154
- Kennel.out.puts Utils.color(color, "#{step} #{api_resource} #{tracking_id(e&.as_json || a)}")
154
+ Kennel.out.puts Utils.color(color, "#{step} #{api_resource} #{e&.tracking_id || tracking_id(a)}")
155
155
  print_diff(diff) if diff # only for update
156
156
  end
157
157
  end
@@ -159,12 +159,12 @@ module Kennel
159
159
  def print_diff(diff)
160
160
  diff.each do |type, field, old, new|
161
161
  if type == "+"
162
- temp = new.inspect
163
- new = old.inspect
162
+ temp = Utils.pretty_inspect(new)
163
+ new = Utils.pretty_inspect(old)
164
164
  old = temp
165
165
  else # ~ and -
166
- old = old.inspect
167
- new = new.inspect
166
+ old = Utils.pretty_inspect(old)
167
+ new = Utils.pretty_inspect(new)
168
168
  end
169
169
 
170
170
  if (old + new).size > 100
@@ -177,18 +177,30 @@ module Kennel
177
177
  end
178
178
  end
179
179
 
180
- def block_irreversible_partial_updates
180
+ # Do not add tracking-id when working with existing ids on a branch,
181
+ # so resource do not get deleted from merges to master.
182
+ # Also make sure the diff still makes sense, by kicking out the now noop-update.
183
+ #
184
+ # Note: ideally we'd never add tracking in the first place, but at that point we do not know the diff yet
185
+ def prevent_irreversible_partial_updates
181
186
  return unless @project_filter
182
- return if @update.none? do |_, e, _, diff|
183
- e.id && diff.any? do |_, field, old, new = nil|
184
- TRACKING_FIELDS.include?(field.to_sym) && tracking_value(old) != tracking_value(new)
187
+ @update.select! do |_, e, _, diff|
188
+ next true unless e.id # short circuit for performance
189
+
190
+ diff.select! do |field_diff|
191
+ (_, field, old, new) = field_diff
192
+ next true unless tracking_field?(field)
193
+
194
+ if (old_tracking = tracking_value(old))
195
+ old_tracking == tracking_value(new) || raise("do not update! (atm unreachable)")
196
+ else
197
+ field_diff[3] = remove_tracking_id(e) # make plan output match update
198
+ old != field_diff[3]
199
+ end
185
200
  end
201
+
202
+ !diff.empty?
186
203
  end
187
- raise <<~TEXT
188
- Updates with PROJECT= filter should not update tracking id in #{TRACKING_FIELDS.join("/")} of resources with a set `id:`,
189
- since this makes them get deleted by a full update.
190
- Remove the `id:` to test them out, which will result in a copy being created and later deleted.
191
- TEXT
192
204
  end
193
205
 
194
206
  def resolve_linked_tracking_ids(actual)
@@ -212,6 +224,13 @@ module Kennel
212
224
  json[field] = "#{json[field]}\n-- Managed by kennel #{e.tracking_id} in #{e.project.class.file_location}, do not modify manually".lstrip
213
225
  end
214
226
 
227
+ def remove_tracking_id(e)
228
+ json = e.as_json
229
+ field = tracking_field(json)
230
+ value = json[field]
231
+ json[field] = value.dup.sub!(/\n-- Managed by kennel .*/, "") || raise("did not find tracking id in #{value}")
232
+ end
233
+
215
234
  def tracking_id(a)
216
235
  tracking_value a[tracking_field(a)]
217
236
  end
@@ -223,5 +242,9 @@ module Kennel
223
242
  def tracking_field(a)
224
243
  TRACKING_FIELDS.detect { |f| a.key?(f) }
225
244
  end
245
+
246
+ def tracking_field?(field)
247
+ TRACKING_FIELDS.include?(field.to_sym)
248
+ end
226
249
  end
227
250
  end
@@ -111,12 +111,18 @@ namespace :kennel do
111
111
  end
112
112
  end
113
113
 
114
- desc "Convert existing resources to copy-pastable definitions to import existing resources RESOURCE=[dashboard,monitor,slo] ID=1234"
114
+ desc "Convert existing resources to copy-pasteable definitions to import existing resources (call with URL= or call with RESOURCE= and ID=)"
115
115
  task import: :environment do
116
- resource = ENV["RESOURCE"] || Kennel::Tasks.abort("Call with RESOURCE=dashboard or monitor or slo")
117
- id = ENV["ID"] || Kennel::Tasks.abort("Call with ID=1234")
118
- id = Integer(id) if id =~ /^\d+$/ # dashboards can have alphanumeric ids
119
- puts Kennel::Importer.new(Kennel.send(:api)).import(resource, id)
116
+ if (id = ENV["ID"]) && (resource = ENV["RESOURCE"])
117
+ id = Integer(id) if id =~ /^\d+$/ # dashboards can have alphanumeric ids
118
+ elsif (url = ENV["URL"])
119
+ resource, id = Kennel::Models::Record.parse_any_url(url) || Kennel::Tasks.abort("Unable to parse url")
120
+ else
121
+ possible_resources = Kennel::Models::Record.subclasses.map(&:api_resource)
122
+ Kennel::Tasks.abort("Call with URL= or call with RESOURCE=#{possible_resources.join(" or ")} and ID=")
123
+ end
124
+
125
+ Kennel.out.puts Kennel::Importer.new(Kennel.send(:api)).import(resource, id)
120
126
  end
121
127
 
122
128
  desc "Dump ALL of datadog config as raw json ... useful for grep/search TYPE=slo|monitor|dashboard"
@@ -130,6 +136,14 @@ namespace :kennel do
130
136
  require "kennel"
131
137
  gem "dotenv"
132
138
  require "dotenv"
133
- Dotenv.load
139
+ source = ".env"
140
+
141
+ # warn when users have things like DATADOG_TOKEN already set and it will not be loaded from .env
142
+ unless ENV["KENNEL_SILENCE_UPDATED_ENV"]
143
+ updated = Dotenv.parse(source).select { |k, v| ENV[k] && ENV[k] != v }
144
+ warn "Environment variables #{updated.keys.join(", ")} need to be unset to be sourced from #{source}" if updated.any?
145
+ end
146
+
147
+ Dotenv.load(source)
134
148
  end
135
149
  end
@@ -142,6 +142,17 @@ module Kennel
142
142
  else []
143
143
  end
144
144
  end
145
+
146
+ # TODO: use awesome-print or similar, but it has too many monkey-patches
147
+ # https://github.com/amazing-print/amazing_print/issues/36
148
+ def pretty_inspect(object)
149
+ string = object.inspect
150
+ string.gsub!(/:([a-z_]+)=>/, "\\1: ")
151
+ 10.times do
152
+ string.gsub!(/{(\S.*?\S)}/, "{ \\1 }") || break
153
+ end
154
+ string
155
+ end
145
156
  end
146
157
  end
147
158
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Kennel
3
- VERSION = "1.66.2"
3
+ VERSION = "1.70.0"
4
4
  end
@@ -54,7 +54,7 @@ end
54
54
  ### Updating an existing monitor
55
55
  - use [datadog monitor UI](https://app.datadoghq.com/monitors/manage) to find a monitor
56
56
  - get the `id` from the url
57
- - run `RESOURCE=monitor ID=12345 bundle exec rake kennel:import` and copy the output
57
+ - run `URL='https://app.datadoghq.com/monitors/123' bundle exec rake kennel:import` and copy the output
58
58
  - find or create a project in `projects/`
59
59
  - add the monitor to `parts: [` list, for example:
60
60
  ```Ruby
@@ -100,7 +100,7 @@ end
100
100
  ### Updating an existing dashboard
101
101
  - go to [datadog dashboard UI](https://app.datadoghq.com/dashboard/lists) and click on _New Dashboard_ to find a dashboard
102
102
  - get the `id` from the url
103
- - run `RESOURCE=dashboard ID=abc-def-ghi bundle exec rake kennel:import` and copy the output
103
+ - run `URL='https://app.datadoghq.com/dashboard/bet-foo-bar' bundle exec rake kennel:import` and copy the output
104
104
  - find or create a project in `projects/`
105
105
  - add a dashboard to `parts: [` list, for example:
106
106
  ```Ruby
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kennel
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.66.2
4
+ version: 1.70.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Grosser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-26 00:00:00.000000000 Z
11
+ date: 2020-05-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -39,19 +39,19 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: net-http-persistent-retry
42
+ name: net-http-persistent
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: '4.0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: '4.0'
55
55
  description:
56
56
  email: michael@grosser.it
57
57
  executables: []
@@ -101,8 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
101
101
  - !ruby/object:Gem::Version
102
102
  version: '0'
103
103
  requirements: []
104
- rubyforge_project:
105
- rubygems_version: 2.7.6.2
104
+ rubygems_version: 3.1.3
106
105
  signing_key:
107
106
  specification_version: 4
108
107
  summary: Keep datadog monitors/dashboards/etc in version control, avoid chaotic management