consul-migrate 0.1.1 → 0.1.2

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,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MzA5ODIyY2UzNmNmNjk1M2EzMTZhMzI1ZmFiNDVjYWM5YzEzMDYwMQ==
4
+ MWY0MGEzNDA0ZjFkYjQwNDI5ZmNhYTE5MzNkYzZjZmNhYjQ2NWJkOQ==
5
5
  data.tar.gz: !binary |-
6
- MWU4YTM4YmZiMjkzNWQ0MDI4MmJiMDM4M2UwNWQyY2U2MTRlZGVhZA==
6
+ OWNjYTE0ZGZjNWFiZDczOTdjMTI2MWQyNTg4ZDIwYjA4NjRlNTEyNA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NDQ1ZjcyOTA5MTk0MjAzNDE2YzYyMDhhZDFlNTk1YTBkOWRhMWVjYmYyZWIx
10
- M2ZmZDE1OTI2YzgzYzU4NDM1N2E4ZTczY2UwZDIyMDk3NzM2ODk2MTdiY2Vi
11
- ZTE2YzYwODc2OTYxNTU0NzZmNDEzOWZjYzZjMzdmMGY5NjdmODE=
9
+ OGFiN2VjM2VlNTA4OTZhN2E4ODkzMzgxZDlkYzQ4YzljYjc2ZTI4ZWY4ZjZh
10
+ NzdlZTUxMDA1NjUxOWJmZWI4YWNlYTRhNGNhYTZlM2E0OGMxMWQ3OWM1YmNj
11
+ NzIwNDJmNWVmMTcwMTI4YjYxNWZkZWM1MGIwMzMxZTZjYTc0MTI=
12
12
  data.tar.gz: !binary |-
13
- ZWI3MjNhMTBhOTUzNmQ4MzA4OWRkMGRiN2NiNGY0YTIyZjZlMzFjOWE3ZDU3
14
- OGMzMTVmYjgxODQxYTNhMzE1MmQ1YmY3MGIzMTM3NzE3MTA0OWU3ODdmMmUz
15
- ZmY3N2MxODZhZDljZDZiMWRlMzRhZDI4MGY1ZjhlNGYxODk0ZmI=
13
+ ODIzNWMyNjVmMjgyNzExZWY0MmQ2ZWVmNjk5ZDcwMTUyMTdjNDk2MjJjMDhl
14
+ NmZhMzliNTNiYWNhMDNiYWE5MGJhYjViZWE3MWI0MzM2MDkyNGM4MzI0Njk4
15
+ OTcwOGM5Y2NhYWViNGEyMmQ2Y2U2YWIyZTViOWU0YTllYmU2NDY=
@@ -1,5 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - 1.9.3
3
4
  - 2.0.0
4
5
  deploy:
5
6
  provider: rubygems
data/README.md CHANGED
@@ -22,16 +22,16 @@ Or install it yourself as:
22
22
  From the current authoritative ACL datacenter:
23
23
 
24
24
  ```ruby
25
- require consul-migrate
25
+ require 'consul/migrate'
26
26
 
27
- client = Consul::Migrate::Client.new(acl_token: 'you-acl-master-token')
27
+ client = Consul::Migrate::Client.new(acl_token: 'your-acl-master-token')
28
28
  client.export_acls('/path/to/file')
29
29
  ```
30
30
 
31
31
  From the desired new ACL datacenter:
32
32
  ```ruby
33
- require consul-migrate
34
- client = Consul::Migrate::Client.new(acl_token: 'you-acl-master-token')
33
+ require 'consul/migrate'
34
+ client = Consul::Migrate::Client.new(acl_token: 'your-acl-master-token')
35
35
  client.import_acls('/path/to/file')
36
36
  ```
37
37
 
@@ -0,0 +1,4 @@
1
+ #! ruby
2
+ require 'consul/migrate/cli'
3
+
4
+ Consul::Migrate::Cli.start(ARGV)
@@ -15,14 +15,16 @@ Gem::Specification.new do |spec|
15
15
  spec.license = 'Apache 2.0'
16
16
 
17
17
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
- spec.bindir = 'exe'
19
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.bindir = 'bin'
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
20
  spec.require_paths = ['lib']
21
21
 
22
22
  spec.add_development_dependency 'bundler', '~> 1.7'
23
23
  spec.add_development_dependency 'rake', '~> 10.0'
24
- spec.add_development_dependency 'webmock', '~> 1.21.0'
25
- spec.add_development_dependency 'rspec', '~> 3.1.0'
24
+ spec.add_development_dependency 'webmock', '~> 1.21'
25
+ spec.add_development_dependency 'rspec', '~> 3.1'
26
26
  spec.add_development_dependency 'sinatra', '~> 1.4'
27
27
  spec.add_development_dependency 'fakefs', '~> 0.6'
28
+
29
+ spec.add_runtime_dependency 'thor', '~> 0.19'
28
30
  end
@@ -1,4 +1,5 @@
1
1
  require 'consul/migrate/client'
2
+ require 'consul/migrate/cli'
2
3
  require 'consul/migrate/version'
3
4
 
4
5
  module Consul
@@ -0,0 +1,71 @@
1
+ require 'consul/migrate/version'
2
+ require 'consul/migrate/client'
3
+ require 'consul/migrate/defaults'
4
+ require 'thor'
5
+ require 'yaml'
6
+ require 'fileutils'
7
+
8
+ module Consul
9
+ module Migrate
10
+ class Cli < Thor
11
+
12
+ attr_accessor :client
13
+
14
+ def initialize(*args)
15
+ super
16
+ read_config
17
+ end
18
+
19
+ desc 'version', 'Display consul-migrate version'
20
+ def version
21
+ say VERSION
22
+ end
23
+
24
+ desc 'init', 'Initialize consul-migrate instance'
25
+ method_option :bind_client, type: :string, aliases: :c,
26
+ desc: 'HTTPS client to bind to'
27
+ method_option :port, type: :numeric, aliases: :p,
28
+ desc: 'Port to bind to'
29
+ method_option :acl_token, type: :string, aliases: :t,
30
+ desc: 'ACL token that is used to access API'
31
+ def init
32
+ write_config(options)
33
+ end
34
+
35
+ desc 'export', 'Export ACLs from Consul via agent running in this system'
36
+ method_option :file, type: :string, default: 'output.json', aliases: :f,
37
+ desc: 'Target file to write to'
38
+ def export
39
+ @client.export_acls(options[:file])
40
+ end
41
+
42
+ desc 'import', 'Import ACLs into Consul cluster via agent running in this system'
43
+ method_option :file, type: :string, default: 'output.json', aliases: :f,
44
+ desc: 'Target file to read from'
45
+ def import
46
+ @client.import_acls(options[:file])
47
+ end
48
+
49
+ private
50
+
51
+ def write_config(options)
52
+ parent_dir = File.dirname(CONFIG_FILE)
53
+ FileUtils.mkdir_p parent_dir
54
+
55
+ client = Consul::Migrate::Client.new(options)
56
+
57
+ File.open(CONFIG_FILE, 'w') do |f|
58
+ # Convert keys from symbols to string and turn hash into YAML
59
+ f.write client.options.to_yaml
60
+ end
61
+ end
62
+
63
+ def read_config
64
+ return unless File.exists?(CONFIG_FILE)
65
+
66
+ config = YAML.load_file(CONFIG_FILE)
67
+ @client = Consul::Migrate::Client.new(config)
68
+ end
69
+ end
70
+ end
71
+ end
@@ -1,47 +1,59 @@
1
1
  require 'json'
2
2
  require 'net/http'
3
+ require 'consul/migrate/defaults'
4
+ require 'consul/migrate/error'
3
5
 
4
6
  module Consul
5
7
  module Migrate
6
8
  class Client
9
+ # Make options readable to CLI
7
10
  attr_reader :options
8
11
 
9
- def bind_client; options[:bind_client]; end
10
- def port; options[:port]; end
11
- def acl_token; options[:acl_token]; end
12
- def base_url; "http://#{bind_client}:#{port}"; end
12
+ def bind_client; @options[:bind_client]; end
13
+ def port; @options[:port]; end
14
+
15
+ def base_url
16
+ "http://#{bind_client}:#{port}"
17
+ end
18
+
19
+ def http_params
20
+ { :token => @options[:acl_token] }
21
+ end
13
22
 
14
23
  def initialize(options = {})
15
- @options = {
16
- :bind_client => 'localhost',
17
- :port => 8500
18
- }.merge(options)
24
+ @options = CLIENT_DEFAULTS.merge(symbolize_keys(options))
19
25
  end
20
26
 
21
- # Get all ACLs
27
+ # GET /v1/acl/list
22
28
  def get_acl_list
23
- url = "#{base_url}/v1/acl/list?token=#{acl_token}"
24
- response = Net::HTTP.get_response(URI.parse(url))
29
+ uri = URI("#{base_url}/v1/acl/list")
30
+ uri.query = URI.encode_www_form(http_params)
31
+ response = Net::HTTP.get_response(uri)
25
32
 
26
- return response
33
+ fail(Error, response.body) unless response.kind_of? Net::HTTPSuccess
34
+
35
+ response.body
27
36
  end
28
37
 
29
- # PUT single ACL
38
+ # PUT /v1/acl/create
30
39
  def put_acl(acl_hash)
31
- uri = URI("#{base_url}/v1/acl/create?token=#{acl_token}")
32
- req = Net::HTTP::Put.new(uri)
40
+ uri = URI("#{base_url}/v1/acl/create")
41
+ uri.query = URI.encode_www_form(http_params)
42
+ req = Net::HTTP::Put.new(uri.request_uri)
33
43
  req.body = acl_hash.to_json
34
44
 
35
45
  response = Net::HTTP.start(uri.hostname, uri.port) do |http|
36
46
  http.request(req)
37
47
  end
38
48
 
39
- return response
49
+ fail(Error, response.body) unless response.kind_of? Net::HTTPSuccess
50
+
51
+ response.body
40
52
  end
41
53
 
42
54
  # Export ACLs into a file
43
55
  def export_acls(dest)
44
- json = get_acl_list.body
56
+ json = get_acl_list
45
57
 
46
58
  File.open(dest, 'w') { |file|
47
59
  file.write(json)
@@ -58,13 +70,18 @@ module Consul
58
70
 
59
71
  result = []
60
72
  data_hash.each do |k, v|
61
- h = JSON.parse(put_acl(k).body)
73
+ h = JSON.parse(put_acl(k))
62
74
  result.push(h)
63
75
  end
64
76
 
65
77
  return result
66
78
  end
67
79
 
80
+ private
81
+
82
+ def symbolize_keys(hash)
83
+ Hash[hash.map{|k,v| v.is_a?(Hash) ? [k.to_sym, symbolize_keys(v)] : [k.to_sym, v] }]
84
+ end
68
85
  end
69
86
  end
70
87
  end
@@ -0,0 +1,10 @@
1
+ module Consul
2
+ module Migrate
3
+ CLIENT_DEFAULTS = {
4
+ :bind_client => 'localhost',
5
+ :port => 8500
6
+ }
7
+ end
8
+
9
+ CONFIG_FILE = File.join(Dir.home, '.cmigrate/init.conf')
10
+ end
@@ -0,0 +1,8 @@
1
+ module Consul
2
+ module Migrate
3
+ # A standard error with a different name
4
+ # for identifying errors internal to this gem
5
+ class Error < StandardError
6
+ end
7
+ end
8
+ end
@@ -1,5 +1,5 @@
1
1
  module Consul
2
2
  module Migrate
3
- VERSION = "0.1.1"
3
+ VERSION = '0.1.2'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: consul-migrate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Calvin Leung Huang
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-08 00:00:00.000000000 Z
11
+ date: 2015-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -44,28 +44,28 @@ dependencies:
44
44
  requirements:
45
45
  - - ~>
46
46
  - !ruby/object:Gem::Version
47
- version: 1.21.0
47
+ version: '1.21'
48
48
  type: :development
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: 1.21.0
54
+ version: '1.21'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: 3.1.0
61
+ version: '3.1'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - ~>
67
67
  - !ruby/object:Gem::Version
68
- version: 3.1.0
68
+ version: '3.1'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: sinatra
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -94,10 +94,25 @@ dependencies:
94
94
  - - ~>
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0.6'
97
+ - !ruby/object:Gem::Dependency
98
+ name: thor
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ version: '0.19'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: '0.19'
97
111
  description: Gem that can be used to import and export Consul ACL tokens
98
112
  email:
99
113
  - cleung2010@gmail.com
100
- executables: []
114
+ executables:
115
+ - consul-migrate
101
116
  extensions: []
102
117
  extra_rdoc_files: []
103
118
  files:
@@ -108,11 +123,13 @@ files:
108
123
  - LICENSE.txt
109
124
  - README.md
110
125
  - Rakefile
111
- - bin/console
112
- - bin/setup
126
+ - bin/consul-migrate
113
127
  - consul-migrate.gemspec
114
128
  - lib/consul/migrate.rb
129
+ - lib/consul/migrate/cli.rb
115
130
  - lib/consul/migrate/client.rb
131
+ - lib/consul/migrate/defaults.rb
132
+ - lib/consul/migrate/error.rb
116
133
  - lib/consul/migrate/version.rb
117
134
  homepage: https://github.com/Cimpress-MCP/consul-migrate
118
135
  licenses:
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "consul/migrate"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start
data/bin/setup DELETED
@@ -1,7 +0,0 @@
1
- #!/bin/bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
-
5
- bundle install
6
-
7
- # Do any other automated setup that you need to do here