metadata-v2 0.8.5.pre

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ae51ffa35b0e4db9971328dda85b2c551e6595e48e4086fdeca07d2358f967dd
4
+ data.tar.gz: f1f3e0db7bdc0be7da9a7b50cac9cc1d0a940897eab83fc044f684adb0471f24
5
+ SHA512:
6
+ metadata.gz: '0086c81a9f53ae2baa2f8b5bbb74faf2c14e8fa4e21a775139e383ef3e877149defc120937879a19d5c4db3ee98c768648c2f0424f3c6c65e1752775c13fd642'
7
+ data.tar.gz: e2761a48c6d61e0369ecc3471f792dd63d3804f648dc9954aef71cfba93e62f2a79315adbec7a452d1a2280b8ce0937aa55b8b3ccc77c328bed4be3161398eea
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+
2
+ The MIT License (MIT)
3
+ Copyright © 2018 Chris Olstrom <chris@olstrom.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the “Software”), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.org ADDED
@@ -0,0 +1,29 @@
1
+ #+TITLE: metadata-v2 - A Ruby Interface to the Joyent Metadata Protocol
2
+ #+LATEX: \pagebreak
3
+
4
+ * Overview
5
+
6
+ ~metadata-v2~ provides protocol and socket abstractions to simplify
7
+ interactions with any metadata service that conforms to Version 2 of the
8
+ [[https://eng.joyent.com/mdata/protocol.html][Joyent Metadata Protocol Specification]].
9
+
10
+ Included are drop-in replacements for the [[https://github.com/joyent/mdata-client/][mdata-client]] tools:
11
+ - mdata-list
12
+ - mdata-get
13
+ - mdata-put
14
+ - mdata-delete
15
+
16
+ * Why does this exist?
17
+
18
+ The [[https://github.com/joyent/mdata-client/][mdata-client]] tools are both portable and well-documented. You should use
19
+ them when available! ~metadata-v2~ only really becomes useful once you want to
20
+ do non-trivial things with your metadata service.
21
+
22
+ * License
23
+
24
+ ~metadata-v2~ is available under the [[https://tldrlegal.com/license/mit-license][MIT License]]. See ~LICENSE.txt~ for the
25
+ full text.
26
+
27
+ * Contributors
28
+
29
+ - [[https://colstrom.github.io/][Chris Olstrom]] | [[mailto:chris@olstrom.com][e-mail]] | [[https://twitter.com/ChrisOlstrom][Twitter]]
data/bin/mdata-delete ADDED
@@ -0,0 +1,20 @@
1
+ #! /usr/bin/env ruby
2
+ # -*- ruby -*-
3
+
4
+ require_relative '../lib/metadata/v2'
5
+
6
+ unless key = ARGV.first
7
+ STDERR.puts 'mdata-delete: Usage: mdata-delete <keyname>'
8
+ exit 3
9
+ end
10
+
11
+ metadata = Metadata::V2::Client.new
12
+ response = metadata.delete key
13
+
14
+ case response.code
15
+ when 'SUCCESS'
16
+ exit 0
17
+ else
18
+ STDERR.puts "Error deleting metadata key '#{key}': #{response.decode}"
19
+ exit 2
20
+ end
data/bin/mdata-get ADDED
@@ -0,0 +1,24 @@
1
+ #! /usr/bin/env ruby
2
+ # -*- ruby -*-
3
+
4
+ require_relative '../lib/metadata/v2'
5
+
6
+ unless key = ARGV.first
7
+ STDERR.puts 'mdata-get: Usage: mdata-get <keyname>'
8
+ exit 3
9
+ end
10
+
11
+ metadata = Metadata::V2::Client.new
12
+ response = metadata.get key
13
+
14
+ case response.code
15
+ when 'SUCCESS'
16
+ STDOUT.puts response.decode
17
+ exit 0
18
+ when 'NOTFOUND'
19
+ STDERR.puts "No metadata for '#{ARGV.first}'"
20
+ exit 1
21
+ else
22
+ STDERR.puts "Error getting metadata for key '#{key}': #{response.decode}"
23
+ exit 2
24
+ end
data/bin/mdata-list ADDED
@@ -0,0 +1,15 @@
1
+ #! /usr/bin/env ruby
2
+ # -*- ruby -*-
3
+
4
+ require_relative '../lib/metadata/v2'
5
+
6
+ metadata = Metadata::V2::Client.new
7
+ response = metadata.keys
8
+
9
+ case response.code
10
+ when 'SUCCESS'
11
+ STDOUT.puts response.decode
12
+ exit 0
13
+ else
14
+ exit 2
15
+ end
data/bin/mdata-put ADDED
@@ -0,0 +1,25 @@
1
+ #! /usr/bin/env ruby
2
+ # -*- ruby -*-
3
+
4
+ require_relative '../lib/metadata/v2'
5
+
6
+ unless key = ARGV.shift
7
+ STDERR.puts 'mdata-put: Usage: mdata-put <keyname> [ <value> ]'
8
+ exit 3
9
+ end
10
+
11
+ unless value = ARGV.shift || (STDIN.read unless STDIN.tty?)
12
+ STDERR.puts 'ERROR: either specify the metadata value as the second command-line argument, or pipe content to stdin.'
13
+ exit 2
14
+ end
15
+
16
+ metadata = Metadata::V2::Client.new
17
+ response = metadata.put key, value
18
+
19
+ case response.code
20
+ when 'SUCCESS'
21
+ exit 0
22
+ else
23
+ STDERR.puts "Error putting metadata for key '#{key}': #{response.decode}"
24
+ exit 2
25
+ end
@@ -0,0 +1,4 @@
1
+
2
+ # -*- ruby -*-
3
+
4
+ require_relative 'metadata/v2'
@@ -0,0 +1,7 @@
1
+
2
+ # -*- ruby -*-
3
+
4
+ require_relative 'v2/client'
5
+ require_relative 'v2/request'
6
+ require_relative 'v2/response'
7
+ require_relative 'v2/socket_paths'
@@ -0,0 +1,71 @@
1
+
2
+ # -*- ruby -*-
3
+
4
+ require 'socket' # Ruby Standard Library
5
+
6
+ require_relative 'request'
7
+ require_relative 'response'
8
+ require_relative 'socket_paths'
9
+
10
+ module Metadata
11
+ module V2
12
+ class Client
13
+ def initialize(path = nil)
14
+ @socket = UNIXSocket.new path || ENV.fetch('METADATA_SOCKET') { discover }
15
+ self
16
+ end
17
+
18
+ def keys
19
+ request! 'KEYS'
20
+ block_given? ? yield(response!) : response!
21
+ end
22
+
23
+ def get(key)
24
+ request! 'GET', key.to_s
25
+ block_given? ? yield(response!) : response!
26
+ end
27
+
28
+ def put(key, value)
29
+ request! 'PUT', key.to_s, value.to_s
30
+ block_given? ? yield(response!) : response!
31
+ end
32
+
33
+ def delete(key)
34
+ request! 'DELETE', key.to_s
35
+ block_given? ? yield(response!) : response!
36
+ end
37
+
38
+ private
39
+
40
+ def discover
41
+ SOCKET_PATHS.find { |path| File.socket? path } or raise Errno::ENOENT
42
+ end
43
+
44
+ def raw_request!(message)
45
+ @socket.sendmsg message.to_s
46
+ end
47
+
48
+ def raw_response!
49
+ @socket.readline
50
+ end
51
+
52
+ def flush!
53
+ raw_request! "\n"
54
+ "invalid command\n" == raw_response!
55
+ end
56
+
57
+ def negotiate!
58
+ raw_request! "NEGOTIATE V2\n"
59
+ "V2_OK\n" == raw_response!
60
+ end
61
+
62
+ def request!(*args)
63
+ raw_request! Request.new(*args)
64
+ end
65
+
66
+ def response!
67
+ Response.new raw_response!
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,44 @@
1
+
2
+ # -*- ruby -*-
3
+
4
+ require 'base64' # Ruby Standard Library
5
+ require 'zlib' # Ruby Standard Library
6
+
7
+ module Metadata
8
+ module V2
9
+ class Request
10
+ def initialize(operation, *args)
11
+ @operation = operation
12
+ @args = args
13
+ self
14
+ end
15
+
16
+ attr_reader :operation, :args
17
+
18
+ def id
19
+ @id ||= 8.times.map { rand (0..15) }.map { |n| n.to_s(16) }.reduce(:+)
20
+ end
21
+
22
+ def payload
23
+ case args.size
24
+ when 1
25
+ Base64.encode64(*args).chomp
26
+ when 2
27
+ Base64.encode64(args.map { |arg| Base64.encode64(arg).chomp }.join(' ')).chomp
28
+ end
29
+ end
30
+
31
+ def body
32
+ @body ||= [id, operation, payload].compact.reject(&:empty?).join(' ')
33
+ end
34
+
35
+ def checksum
36
+ @checksum ||= Zlib.crc32(body).to_s(16)
37
+ end
38
+
39
+ def to_s
40
+ ['V2', body.length, checksum, body].join(' ') + "\n"
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,53 @@
1
+
2
+ # -*- ruby -*-
3
+
4
+ require 'base64' # Ruby Standard Library
5
+
6
+ module Metadata
7
+ module V2
8
+ class Response
9
+ def initialize(frame)
10
+ @frame = frame
11
+ self
12
+ end
13
+
14
+ attr_reader :frame
15
+
16
+ def to_s
17
+ frame + "\n"
18
+ end
19
+
20
+ def protocol
21
+ @protocol ||= frame.split(' ')[0]
22
+ end
23
+
24
+ def length
25
+ @length ||= frame.split(' ')[1].to_i
26
+ end
27
+
28
+ def checksum
29
+ @checksum ||= frame.split(' ')[2]
30
+ end
31
+
32
+ def body
33
+ @body ||= frame.split(' ').drop(3).join(' ')
34
+ end
35
+
36
+ def id
37
+ @id ||= body.split(' ')[0]
38
+ end
39
+
40
+ def code
41
+ @code ||= body.split(' ')[1]
42
+ end
43
+
44
+ def payload
45
+ @payload ||= body.split(' ')[2]
46
+ end
47
+
48
+ def decode
49
+ Base64.decode64 payload if payload
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,12 @@
1
+
2
+ # -*- ruby -*-
3
+
4
+ module Metadata
5
+ module V2
6
+ SOCKET_PATHS = [
7
+ "/.zonecontrol/metadata.sock",
8
+ "/native/.zonecontrol/metadata.sock",
9
+ "/var/run/smartdc/metadata.sock"
10
+ ].freeze
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.name = 'metadata-v2'
3
+ gem.version = `git describe --tags --abbrev=0`.chomp + '.pre'
4
+ gem.licenses = 'MIT'
5
+ gem.authors = ['Chris Olstrom']
6
+ gem.email = 'chris@olstrom.com'
7
+ gem.homepage = 'https://github.com/colstrom/metadata-v2'
8
+ gem.summary = 'A Ruby Interface to the Joyent Metadata Protocol (Version 2)'
9
+
10
+ gem.files = `git ls-files`.split("\n")
11
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
12
+ gem.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
13
+ gem.require_paths = ['lib']
14
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: metadata-v2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.8.5.pre
5
+ platform: ruby
6
+ authors:
7
+ - Chris Olstrom
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: chris@olstrom.com
15
+ executables:
16
+ - mdata-delete
17
+ - mdata-get
18
+ - mdata-list
19
+ - mdata-put
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - ".gitignore"
24
+ - Gemfile
25
+ - LICENSE.txt
26
+ - README.org
27
+ - bin/mdata-delete
28
+ - bin/mdata-get
29
+ - bin/mdata-list
30
+ - bin/mdata-put
31
+ - lib/metadata-v2.rb
32
+ - lib/metadata/v2.rb
33
+ - lib/metadata/v2/client.rb
34
+ - lib/metadata/v2/request.rb
35
+ - lib/metadata/v2/response.rb
36
+ - lib/metadata/v2/socket_paths.rb
37
+ - metadata-v2.gemspec
38
+ homepage: https://github.com/colstrom/metadata-v2
39
+ licenses:
40
+ - MIT
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">"
54
+ - !ruby/object:Gem::Version
55
+ version: 1.3.1
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 2.7.6
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: A Ruby Interface to the Joyent Metadata Protocol (Version 2)
62
+ test_files: []