astdb 0.0.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.
Binary file
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in astdb.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,53 @@
1
+ # AstDB
2
+
3
+ Interact with an Asterisk Database (AstDB) instance via ssh.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'astdb'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install astdb
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ require 'astdb'
23
+
24
+ db = AstDB::Database.new host: 'ssh.server.com',
25
+ username: 'not-root',
26
+ password: 'Secret!', # Omit this line if using key-based auth.
27
+ auto_reload: true # Optionally disable the automatic initial load
28
+ # and updating of '.hash', instead opting to run
29
+ # reload_database' manually. Useful in high
30
+ # latency or low bandwidth scenarios.
31
+ # Default: true
32
+
33
+ # view a hash of the database
34
+ p db.hash
35
+
36
+ # add/update some entries
37
+ errors = db.set({'MyFamily/MyKey' => 'MyValue', 'this/is/a/longer/key' => 'another value'})
38
+
39
+ if errors
40
+ errors.each { |key, error_message| p "The following error occurred when trying to set #{key}: #{error}"
41
+ end
42
+
43
+ # see the updated database
44
+ p db.hash
45
+ ```
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'astdb/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "astdb"
8
+ gem.version = Astdb::VERSION
9
+ gem.authors = ["Taylor Boyko"]
10
+ gem.email = ["taylor@wrprojects.com"]
11
+ gem.description = %q{Interact with an Asterisk Database (AstDB) instance via ssh.}
12
+ gem.summary = %q{}
13
+ gem.homepage = "https://github.com/tboyko/astdb"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'net-ssh'
21
+ end
@@ -0,0 +1,4 @@
1
+ require 'astdb/models/connection'
2
+ require 'astdb/models/database'
3
+ require 'astdb/models/entry'
4
+ require 'astdb/version'
@@ -0,0 +1,63 @@
1
+ module AstDB
2
+ class Connection
3
+ require 'net/ssh'
4
+
5
+ attr_accessor :host, :username, :password
6
+
7
+ def initialize(opts = {})
8
+ @host = opts[:host]
9
+ @username = opts[:username]
10
+ @password = opts[:password]
11
+ end
12
+
13
+ def fetch_database
14
+
15
+ # fetch database dump from asterisk server
16
+
17
+ output = nil
18
+
19
+ Net::SSH.start(@host, @username, password: @password) do |ssh|
20
+ output = ssh.exec!("asterisk -rx 'database show'")
21
+ end
22
+
23
+ # parse dump to hash
24
+
25
+ database = {}
26
+
27
+ lines = output.split("\n").collect { |l| l.rstrip }
28
+ lines.each do |line|
29
+ m = line.match /^\/(?<key>[^:]+):(?<value>.+)$/
30
+ next if m.nil?
31
+ key = m[:key].rstrip
32
+ value = m[:value].lstrip
33
+
34
+ database[key] = value
35
+ end
36
+
37
+ database
38
+ end
39
+
40
+ def set_database_values(value_hash)
41
+
42
+ # prepare command
43
+
44
+ commands = value_hash.collect { |k, v| Entry.new(k,v).put_command }
45
+
46
+ # send to server
47
+
48
+ errors = {}
49
+
50
+ Net::SSH.start(@host, @username, password: @password) do |ssh|
51
+ value_hash.each do |key, value|
52
+ command = Entry.new(key,value).put_command
53
+
54
+ response = ssh.exec!("asterisk -rx '#{command}'")
55
+ errors[key] = response unless response.match /Updated database successfully/
56
+ end
57
+ end
58
+
59
+ return errors.count > 0 ? errors : nil
60
+ end
61
+
62
+ end
63
+ end
@@ -0,0 +1,29 @@
1
+ module AstDB
2
+ class Database
3
+
4
+ attr_reader :connection, :hash
5
+
6
+ def initialize(opts = {})
7
+ @connection = Connection.new host: opts[:host],
8
+ username: opts[:username],
9
+ password: opts[:password]
10
+
11
+ @auto_reload = opts[:auto_reload].nil? ? true : opts[:auto_reload]
12
+ @database = {}
13
+
14
+ reload_database if @auto_reload
15
+ end
16
+
17
+ def set(value_hash)
18
+ errors = @connection.set_database_values(value_hash)
19
+ reload_database if @auto_reload
20
+
21
+ errors
22
+ end
23
+
24
+ def reload_database
25
+ @hash = @connection.fetch_database
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,20 @@
1
+ module AstDB
2
+ class Entry
3
+
4
+ attr_accessor :family, :key, :value
5
+
6
+ def initialize(key, value)
7
+ m = key.match /^(?<family>.+)\/(?<key>[^\/]+)$/
8
+ raise ArgumentError, 'key must at least have a single "/" delimiting the asterisk family and key values' if m.nil?
9
+
10
+ @family = m[:family]
11
+ @key = m[:key]
12
+ @value = value
13
+ end
14
+
15
+ def put_command
16
+ "database put #{family} #{key} #{value}"
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module Astdb
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: astdb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Taylor Boyko
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: net-ssh
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Interact with an Asterisk Database (AstDB) instance via ssh.
31
+ email:
32
+ - taylor@wrprojects.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .DS_Store
38
+ - .gitignore
39
+ - Gemfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - astdb.gemspec
44
+ - lib/astdb.rb
45
+ - lib/astdb/models/connection.rb
46
+ - lib/astdb/models/database.rb
47
+ - lib/astdb/models/entry.rb
48
+ - lib/astdb/version.rb
49
+ homepage: https://github.com/tboyko/astdb
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 1.8.24
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: ''
73
+ test_files: []
74
+ has_rdoc: