constancy 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3119c8dc521f42f974a1f708273026eb49f59c19e25867408aae150dc83261ac
4
- data.tar.gz: f6c729bfd5bbf17e29ecd21570076dcf212ae5af999b5f5d89f629216d1c33b0
3
+ metadata.gz: d19ad8be143bf52f84efca8bcbec9b3883bba3b840ea77583179a41c03de514c
4
+ data.tar.gz: 6b403727f5ef73b943b09b33e127b701bf52b95fcfb287900a992198ae0cbbca
5
5
  SHA512:
6
- metadata.gz: 1928aead12f4a255cc9e8595d3c024b115a30b950fdcefe5df3ab50d35b122ba8340ed7a95f4db15a36b04f8b8f124d10e00467000088d78a1e6e88e72d0c3e0
7
- data.tar.gz: f1d991706a3e0883bf2b22f9e53588a1c6472a55376fb424c5ead455a5c704d99bc6966d378deda7d07c648c606941fb5499fa23affd837104e4e1e513362265
6
+ metadata.gz: 865d4c79c77a83070381ecc61e7cad153bc4c8d540907d15d6798437615171cd061b36816ef0e2327cc7419f6a1f24cb61388e225ffb1646943058025befef5d
7
+ data.tar.gz: f07b781b03b708fe48c3ff010efcfc2e2df652848561af8bd332651b21368c6454602d52d7058c2600e16ead78f50a9d305e932c724b0f9c2404b05454c9a005
data/README.md CHANGED
@@ -222,33 +222,92 @@ If the file `yourapp.yml` has the following content:
222
222
  prod/dbname: yourapp
223
223
  prod/message: |
224
224
  Hello, world. This is a multiline message.
225
- I hope you like it.
226
- Thanks,
227
- YourApp
225
+ Thanks.
228
226
  prod/app/config.json: |
229
227
  {
230
228
  "port": 8080,
231
- "listen": "0.0.0.0",
232
229
  "enabled": true
233
230
  }
234
231
 
235
232
  Then `constancy push` will attempt to create and/or update the following keys
236
233
  with the corresponding content from `yourapp.yml`:
237
234
 
238
- config/yourapp/prod/dbname
239
- config/yourapp/prod/message
240
- config/yourapp/prod/app/config.json
235
+ | `config/yourapp/prod/dbname` | `yourapp` |
236
+ | `config/yourapp/prod/message` | `Hello, world. This is a multiline message.\nThanks.` |
237
+ | `config/yourapp/prod/app/config.json` | `{\n "port": 8080,\n "enabled": true\n}` |
241
238
 
242
- Likewise, a `constancy pull` operation will work in reverse, and pull values
243
- from any keys under `config/yourapp/` into the file `yourapp.yml`, overwriting
244
- whatever values are there.
239
+ In addition to specifying the entire relative path in each key, you may also
240
+ reference paths via your file's YAML structure directly. For example:
245
241
 
246
- Note that JSON is also supported for this file for `push` operations, given that
247
- YAML parsers will correctly parse JSON. However, `constancy pull` will only
248
- write out YAML in the current version.
242
+ ---
243
+ prod:
244
+ redis:
245
+ port: 6380
246
+ host: redis.example.com
247
+
248
+ When pushed, this document will create and/or update the following keys:
249
+
250
+ | `config/yourapp/prod/redis/port` | `6380` |
251
+ | `config/yourapp/prod/redis/host` | `redis.example.com` |
252
+
253
+ You may mix and match relative paths and document hierarchy to build paths as
254
+ you would like. And you may also use the special key `_` to embed a value for
255
+ a particular prefix while also nesting values underneath it. For example, given
256
+ this local file target content:
257
+
258
+ ---
259
+ prod/postgres:
260
+ host: db.myproject.example.com
261
+ port: 10001
262
+
263
+ prod:
264
+ redis:
265
+ _: Embedded Value
266
+ port: 6380
267
+
268
+ prod/redis/host: cache.myproject.example.com
269
+
270
+ This file target content would correspond to the following values, when pushed:
271
+
272
+ | `config/yourapp/prod/postgres/host` | `db.myproject.example.com` |
273
+ | `config/yourapp/prod/postgres/port` | `10001` |
274
+ | `config/yourapp/prod/redis` | `Embedded Value` |
275
+ | `config/yourapp/prod/redis/port` | `6380` |
276
+ | `config/yourapp/prod/redis/host` | `cache.myproject.example.com` |
277
+
278
+ A `constancy pull` operation against a file type target will work in reverse,
279
+ and pull values from any keys under `config/yourapp/` into the file
280
+ `yourapp.yml`, overwriting whatever values are there.
281
+
282
+ **NOTE**: Values in local file targets are converted to strings before comparing
283
+ with or uploading to the remote Consul server. However, because YAML parsing
284
+ converts some values (such as `yes` or `no`) to boolean types, the effective
285
+ value of a key with a value of a bare `yes` will be `true` when converted to a
286
+ string. If you need the actual values `yes` or `no`, use quotes around the value
287
+ to force the YAML parser to interpret it as a string.
288
+
289
+
290
+ #### IMPORTANT NOTES ABOUT PULL MODE WITH FILE TARGETS
291
+
292
+ Against a file target, the structure of the local file can vary in a number
293
+ of ways while still producing the same remote structure. Thus, in pull mode,
294
+ Constancy must necessarily choose one particular rendering format, and will not
295
+ be able to retain the exact structure of the local file if you alternate push
296
+ and pull operations.
297
+
298
+ Specifically, the following caveats are important to note, when pulling a target
299
+ to a local file:
300
+
301
+ * The local file will be written out as YAML, even if it was originally
302
+ provided locally as a JSON file, and even if the extension is `.json`.
303
+
304
+ * Any existing comments in the local file will be lost.
305
+
306
+ * The document structure will be that of a flat hash will fully-specified
307
+ relative paths as the keys.
249
308
 
250
- Also important to note that any comments in the YAML file will be lost on a
251
- `pull` operation that updates a file sync target.
309
+ Future versions of Constancy may provide options to modify the behavior for pull
310
+ operations on a per-target basis. Pull requests are always welcome.
252
311
 
253
312
 
254
313
  ### Dynamic configuration
@@ -291,6 +350,7 @@ Constancy is relatively new software. There's more to be done. Some ideas, which
291
350
  may or may not ever be implemented:
292
351
 
293
352
  * Using CAS to verify the key has not changed in the interim before updating/deleting
353
+ * Options for file target pull-mode rendering
294
354
  * Automation support for running non-interactively
295
355
  * Pattern- and prefix-based exclusions
296
356
  * Logging of changes to files, syslog, other services
@@ -6,6 +6,7 @@ require 'constancy/cli/check_command'
6
6
  require 'constancy/cli/push_command'
7
7
  require 'constancy/cli/pull_command'
8
8
  require 'constancy/cli/config_command'
9
+ require 'constancy/cli/targets_command'
9
10
 
10
11
  class Constancy
11
12
  class CLI
@@ -56,6 +57,7 @@ Commands:
56
57
  push Push changes from filesystem to Consul
57
58
  pull Pull changes from Consul to filesystem
58
59
  config Print a summary of the active configuration
60
+ targets List target names
59
61
 
60
62
  General options:
61
63
  --help Print help for the given command
@@ -125,6 +127,7 @@ USAGE
125
127
  when 'push' then Constancy::CLI::PushCommand.run
126
128
  when 'pull' then Constancy::CLI::PullCommand.run
127
129
  when 'config' then Constancy::CLI::ConfigCommand.run
130
+ when 'targets' then Constancy::CLI::TargetsCommand.run
128
131
  when nil then self.print_usage
129
132
 
130
133
  else
@@ -0,0 +1,21 @@
1
+ # This software is public domain. No rights are reserved. See LICENSE for more information.
2
+
3
+ class Constancy
4
+ class CLI
5
+ class TargetsCommand
6
+ class << self
7
+ def run
8
+ Constancy::CLI.configure(call_external_apis: false)
9
+
10
+ Constancy.config.sync_targets.each do |target|
11
+ if target.name
12
+ puts target.name
13
+ else
14
+ puts "[unnamed target] #{target.datacenter}:#{target.prefix}"
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -98,7 +98,7 @@ class Constancy
98
98
 
99
99
  when :file
100
100
  if File.exist?(self.base_path)
101
- @local_items = YAML.load_file(self.base_path)
101
+ @local_items = flatten_hash(nil, YAML.load_file(self.base_path))
102
102
  end
103
103
  end
104
104
 
@@ -122,5 +122,26 @@ class Constancy
122
122
  def diff(mode)
123
123
  Constancy::Diff.new(target: self, local: self.local_items, remote: self.remote_items, mode: mode)
124
124
  end
125
+
126
+ private def flatten_hash(prefix, hash)
127
+ new_hash = {}
128
+
129
+ hash.each do |k, v|
130
+ if k == '_' && !prefix.nil?
131
+ new_key = prefix
132
+ else
133
+ new_key = [prefix, k].compact.join('/')
134
+ end
135
+
136
+ case v
137
+ when Hash
138
+ new_hash.merge!(flatten_hash(new_key, v))
139
+ else
140
+ new_hash[new_key] = v.to_s
141
+ end
142
+ end
143
+
144
+ new_hash
145
+ end
125
146
  end
126
147
  end
@@ -1,5 +1,5 @@
1
1
  # This software is public domain. No rights are reserved. See LICENSE for more information.
2
2
 
3
3
  class Constancy
4
- VERSION = "0.3.0"
4
+ VERSION = "0.3.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: constancy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Adams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-07 00:00:00.000000000 Z
11
+ date: 2019-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: imperium
@@ -83,6 +83,7 @@ files:
83
83
  - lib/constancy/cli/config_command.rb
84
84
  - lib/constancy/cli/pull_command.rb
85
85
  - lib/constancy/cli/push_command.rb
86
+ - lib/constancy/cli/targets_command.rb
86
87
  - lib/constancy/config.rb
87
88
  - lib/constancy/diff.rb
88
89
  - lib/constancy/sync_target.rb