train-pgsql 1.0.0
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 +7 -0
- data/Gemfile +10 -0
- data/README.md +38 -0
- data/lib/train-pgsql.rb +6 -0
- data/lib/train-pgsql/connection.rb +67 -0
- data/lib/train-pgsql/transport.rb +22 -0
- data/lib/train-pgsql/version.rb +5 -0
- data/train-pgsql.gemspec +24 -0
- metadata +78 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3a5a7f95a8bbb5fa60e7582b24b6bd7ba271dd659ff66e524c13aa6f4790b86d
|
4
|
+
data.tar.gz: b9350bb9dcad6c1ed426b0a58ff663d4f676e3058494a047f3ba754c0d439368
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 707700dcf6d3067f7082d2973a5c1f14235a78a4b026f3c042af70c1c9e63c20822b52fe0553731a05cb618f53e58833159e5f090d0a38068b17aaac5344552d
|
7
|
+
data.tar.gz: 2e7a11fa24fff87cb75d7effa39646276b6ab7e589d97de2dd176b33af3d9d04c2c6ba9541c10f0fc6178bc222221ea8f6a01f8a76b6a6edf67b84d8ebaccea6
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# train-pgsql - Train Plugin for connecting via pgsql
|
2
|
+
|
3
|
+
This plugin allows applications that rely on Train to communicate via postgres SQL.
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
You will have to build this gem yourself to install it as it is not yet on
|
9
|
+
Rubygems.Org. For this there is a rake task which makes this a one-liner:
|
10
|
+
|
11
|
+
```bash
|
12
|
+
rake install:local
|
13
|
+
```
|
14
|
+
|
15
|
+
## Transport parameters
|
16
|
+
|
17
|
+
| Option | Explanation | Default |
|
18
|
+
| ---------- | ------------------- | ---------- |
|
19
|
+
| `host` | Hostname | (required) |
|
20
|
+
| `user` | Username to connect | (required) |
|
21
|
+
| `password` | Password to connect | (required) |
|
22
|
+
| `database` | Database to connect | `postgres` |
|
23
|
+
| `port` | Remote port | `5432` |
|
24
|
+
|
25
|
+
## Example use
|
26
|
+
|
27
|
+
This will work for a Cisco IOS XE device with Telnet enabled:
|
28
|
+
```ruby
|
29
|
+
require "train"
|
30
|
+
train = Train.create("pgsql", {
|
31
|
+
host: "localhost",
|
32
|
+
user: "username",
|
33
|
+
password: "password",
|
34
|
+
})
|
35
|
+
conn = train.connection
|
36
|
+
result = conn.run_command("show version\n")
|
37
|
+
conn.close
|
38
|
+
```
|
data/lib/train-pgsql.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'train'
|
2
|
+
require 'pg'
|
3
|
+
|
4
|
+
module TrainPlugins
|
5
|
+
module Pgsql
|
6
|
+
class Connection < Train::Plugins::Transport::BaseConnection
|
7
|
+
def initialize(options)
|
8
|
+
super(options)
|
9
|
+
end
|
10
|
+
|
11
|
+
def platform
|
12
|
+
Train::Platforms.name('pgsql').in_family('sql')
|
13
|
+
force_platform!('pgsql',
|
14
|
+
release: TrainPlugins::Pgsql::VERSION)
|
15
|
+
end
|
16
|
+
|
17
|
+
def close
|
18
|
+
return if @connection.nil?
|
19
|
+
|
20
|
+
logger.info format('[Pgsql] Closed connection to %s:%d', @options[:host], @options[:port])
|
21
|
+
@connection.finish
|
22
|
+
ensure
|
23
|
+
@connection = nil
|
24
|
+
end
|
25
|
+
|
26
|
+
def uri
|
27
|
+
"pgsql://#{options[:user]}@#{@options[:host]}:#{@options[:port]}/"
|
28
|
+
end
|
29
|
+
|
30
|
+
def run_command_via_connection(query)
|
31
|
+
stdout = ''
|
32
|
+
stderr = ''
|
33
|
+
exit_status = 0
|
34
|
+
|
35
|
+
logger.debug format('[Pgsql] Sending command (%s) to %s:%d', query, @options[:host], @options[:port])
|
36
|
+
connection.exec(query) do |result|
|
37
|
+
result.each_row do |values|
|
38
|
+
stdout += values.join(', ') + "\n"
|
39
|
+
end
|
40
|
+
stdout = stdout.strip
|
41
|
+
end
|
42
|
+
|
43
|
+
CommandResult.new(stdout, stderr, exit_status)
|
44
|
+
rescue PG::Error => e
|
45
|
+
raise Train::TransportError, "Pgsql exec failed (#{e.message})"
|
46
|
+
end
|
47
|
+
|
48
|
+
def connection
|
49
|
+
@connection ||= connect
|
50
|
+
end
|
51
|
+
|
52
|
+
def connect
|
53
|
+
logger.info format('[Pgsql] Opening connection to %s:%d', @options[:host], @options[:port])
|
54
|
+
|
55
|
+
@connection = PG::Connection.new(
|
56
|
+
host: @options[:host],
|
57
|
+
port: @options[:port],
|
58
|
+
user: @options[:user],
|
59
|
+
password: @options[:password],
|
60
|
+
dbname: @options[:database]
|
61
|
+
)
|
62
|
+
|
63
|
+
@connection
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require "train-pgsql/connection"
|
2
|
+
|
3
|
+
module TrainPlugins
|
4
|
+
module Pgsql
|
5
|
+
class Transport < Train.plugin(1)
|
6
|
+
name "pgsql"
|
7
|
+
|
8
|
+
option :host, required: true
|
9
|
+
option :user, required: true
|
10
|
+
option :password, required: true
|
11
|
+
option :database, default: "postgres"
|
12
|
+
option :port, default: 5432
|
13
|
+
|
14
|
+
# Non documented options for development
|
15
|
+
option :debug_pgsql, default: false
|
16
|
+
|
17
|
+
def connection(_instance_opts = nil)
|
18
|
+
@connection ||= TrainPlugins::Pgsql::Connection.new(@options)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/train-pgsql.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'train-pgsql/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'train-pgsql'
|
7
|
+
spec.version = TrainPlugins::Pgsql::VERSION
|
8
|
+
spec.authors = ['Cris Barbero']
|
9
|
+
spec.email = ['cris.barbero@stridehealth.com']
|
10
|
+
spec.summary = 'Train Transport for PostgreSQL connections'
|
11
|
+
spec.description = 'Allows applications using Train connect via pg sql'
|
12
|
+
spec.homepage = 'https://github.com/coveredinc/train-pgsql'
|
13
|
+
spec.license = 'Apache-2.0'
|
14
|
+
|
15
|
+
spec.files = %w[
|
16
|
+
README.md train-pgsql.gemspec Gemfile
|
17
|
+
] + Dir.glob(
|
18
|
+
'lib/**/*', File::FNM_DOTMATCH
|
19
|
+
).reject { |f| File.directory?(f) }
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_dependency 'train', '~> 3.0'
|
23
|
+
spec.add_dependency 'pg', '~> 1.0'
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: train-pgsql
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Cris Barbero
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-10-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: train
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pg
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.0'
|
41
|
+
description: Allows applications using Train connect via pg sql
|
42
|
+
email:
|
43
|
+
- cris.barbero@stridehealth.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- Gemfile
|
49
|
+
- README.md
|
50
|
+
- lib/train-pgsql.rb
|
51
|
+
- lib/train-pgsql/connection.rb
|
52
|
+
- lib/train-pgsql/transport.rb
|
53
|
+
- lib/train-pgsql/version.rb
|
54
|
+
- train-pgsql.gemspec
|
55
|
+
homepage: https://github.com/coveredinc/train-pgsql
|
56
|
+
licenses:
|
57
|
+
- Apache-2.0
|
58
|
+
metadata: {}
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
requirements: []
|
74
|
+
rubygems_version: 3.1.4
|
75
|
+
signing_key:
|
76
|
+
specification_version: 4
|
77
|
+
summary: Train Transport for PostgreSQL connections
|
78
|
+
test_files: []
|