cgminer_api_client 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/.whitesource +8 -0
- data/Gemfile +10 -0
- data/LICENSE.txt +22 -0
- data/README.md +188 -0
- data/Rakefile +7 -0
- data/bin/cgminer_api_client +22 -0
- data/cgminer_api_client.gemspec +20 -0
- data/config/miners.yml.example +2 -0
- data/lib/cgminer_api_client.rb +40 -0
- data/lib/cgminer_api_client/miner.rb +103 -0
- data/lib/cgminer_api_client/miner/commands.rb +201 -0
- data/lib/cgminer_api_client/miner_pool.rb +68 -0
- data/lib/cgminer_api_client/socket_with_timeout.rb +30 -0
- data/lib/cgminer_api_client/version.rb +3 -0
- data/spec/cgminer_api_client/miner/commands_spec.rb +501 -0
- data/spec/cgminer_api_client/miner_pool_spec.rb +134 -0
- data/spec/cgminer_api_client/miner_spec.rb +296 -0
- data/spec/cgminer_api_client_spec.rb +41 -0
- data/spec/spec_helper.rb +25 -0
- metadata +71 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8df86a4b12bb2eef41633534532290139749a0491fb27d89a5c7ce55b596d4a5
|
4
|
+
data.tar.gz: adce4406c1bcec642a7b6698473a355a211b5c467765aaba466ef34efd563d64
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0305613f25ab54883339bb54f7155ff08b09af899d42a57dabd8dd051b6176d4aed3f698584606dd6081045756cb6ffe16035ec8abaca382271289f30859e86c
|
7
|
+
data.tar.gz: e093f302848547c460865383a7fd65eb48103c1dab9d2991109376ab5bc9c3c3e1d32c66140dcb031da5e65ee634f840d51dfff6a689364426b360f6099cdf91
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/.whitesource
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
##########################################################
|
2
|
+
#### WhiteSource "Bolt for Github" configuration file ####
|
3
|
+
##########################################################
|
4
|
+
|
5
|
+
# Configuration #
|
6
|
+
#---------------#
|
7
|
+
ws.repo.scan=true
|
8
|
+
vulnerable.check.run.conclusion.level=failure
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Justin Ramos
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,188 @@
|
|
1
|
+
# CgminerApiClient [![Codacy Badge](https://api.codacy.com/project/badge/Grade/b59cb27aeae64f7c992f7072c5909788)](https://app.codacy.com/app/justin_28/cgminer_api_client?utm_source=github.com&utm_medium=referral&utm_content=jramos/cgminer_api_client&utm_campaign=Badge_Grade_Dashboard) [![Build Status](https://travis-ci.org/jramos/cgminer_api_client.png?branch=master)](https://travis-ci.org/jramos/cgminer_api_client) [![DepShield Badge](https://depshield.sonatype.org/badges/jramos/cgminer_api_client/depshield.svg)](https://depshield.github.io)
|
2
|
+
|
3
|
+
A gem that allows sending API commands to a pool of cgminer instances.
|
4
|
+
|
5
|
+
## Requirements
|
6
|
+
|
7
|
+
- Ruby (~> 2.0.0, ~> 2.1.0)
|
8
|
+
- YAML
|
9
|
+
- JSON
|
10
|
+
- Socket
|
11
|
+
- Thread
|
12
|
+
- cgminer (~> 3.12.0)
|
13
|
+
|
14
|
+
## GUI
|
15
|
+
|
16
|
+
- <https://github.com/jramos/cgminer_manager>
|
17
|
+
|
18
|
+
## Installation Options
|
19
|
+
|
20
|
+
### Bundler
|
21
|
+
|
22
|
+
Add the following to your `Gemfile`:
|
23
|
+
|
24
|
+
gem 'cgminer_api_client', '~> 0.2.6'
|
25
|
+
|
26
|
+
### RubyGems
|
27
|
+
|
28
|
+
$ gem install cgminer_api_client
|
29
|
+
|
30
|
+
### Manually
|
31
|
+
|
32
|
+
$ git clone git@github.com:jramos/cgminer_api_client.git
|
33
|
+
$ cd cgminer_api_client
|
34
|
+
$ gem build cgminer_api_client.gemspec
|
35
|
+
$ gem install cgminer_api_client-0.2.6.gem
|
36
|
+
|
37
|
+
## Configuration
|
38
|
+
|
39
|
+
Copy [`config/miners.yml.example`](https://github.com/jramos/cgminer_api_client/blob/master/config/miners.yml.example) to `config/miners.yml` and update with the IP addresses (and optional ports and timeouts) of your cgminer instances. E.g.:
|
40
|
+
|
41
|
+
# connect to localhost on the default port (4028) with the default timeout (5 seconds)
|
42
|
+
- host: 127.0.0.1
|
43
|
+
# connect to 192.168.1.1 on a non-standard port (1234) with a custom timeout (1 second)
|
44
|
+
- host: 192.168.1.1
|
45
|
+
port: 1234
|
46
|
+
timeout: 1
|
47
|
+
|
48
|
+
### Remote API Access
|
49
|
+
|
50
|
+
Your cgminer instances must be configured to allow remote API access if connecting from anywhere but localhost (127.0.0.1). See the [cgminer API-README](https://github.com/ckolivas/cgminer/blob/master/API-README) for more information.
|
51
|
+
|
52
|
+
#### Linux Instructions
|
53
|
+
|
54
|
+
Under Linux, you would do the following to allow access from any computer on your local network (192.168.1.x):
|
55
|
+
|
56
|
+
$ vi /etc/config/cgminer
|
57
|
+
|
58
|
+
Make the following change:
|
59
|
+
|
60
|
+
# option api_allow 'W:127.0.0.1'
|
61
|
+
option api_allow 'W:127.0.0.1,W:192.168.1.0/24'
|
62
|
+
|
63
|
+
You also need to updated the init.d script to pass the `--api_allow` option:
|
64
|
+
|
65
|
+
$ vi /etc/init.d/cgminer
|
66
|
+
|
67
|
+
Make the following change:
|
68
|
+
|
69
|
+
#PARAMS="$AOPTIONS $POOL1 $POOL2 $POOL3 $_pb --api-listen --api-network"
|
70
|
+
PARAMS="$AOPTIONS $POOL1 $POOL2 $POOL3 $_pb --api-listen --api-network --api-allow $_aa"
|
71
|
+
|
72
|
+
Restart cgminer:
|
73
|
+
|
74
|
+
$ /etc/init.d/cgminer restart
|
75
|
+
|
76
|
+
## Gem Usage
|
77
|
+
|
78
|
+
require 'cgminer_api_client'
|
79
|
+
|
80
|
+
# change the default timeout and port for the miners
|
81
|
+
CgminerApiClient.config do |config|
|
82
|
+
config.default_port = 4023
|
83
|
+
config.default_timeout = 3
|
84
|
+
end
|
85
|
+
|
86
|
+
pool = CgminerApiClient::MinerPool.new
|
87
|
+
|
88
|
+
# run 'devs' on each miner in the pool; returns an array of response hashes
|
89
|
+
devices = pool.devs
|
90
|
+
|
91
|
+
# run 'summary' on each miner in the pool; returns an array of response hashes
|
92
|
+
summaries = pool.summary
|
93
|
+
|
94
|
+
# run 'devs' on available miners; returns an array of response hashes
|
95
|
+
pool.available_miners.collect do |miner|
|
96
|
+
miner.devs
|
97
|
+
end
|
98
|
+
|
99
|
+
# restart the pool
|
100
|
+
pool.restart
|
101
|
+
|
102
|
+
## CLI Usage
|
103
|
+
|
104
|
+
API commands can be sent to your miner pool from the command line.
|
105
|
+
|
106
|
+
$ cgminer_api_client <command> (<arguments>)
|
107
|
+
|
108
|
+
### Commands & Arguments
|
109
|
+
|
110
|
+
#### Read-Only
|
111
|
+
|
112
|
+
The following read-only miner and pool commands are currently available:
|
113
|
+
|
114
|
+
- asc(number)
|
115
|
+
- asccount
|
116
|
+
- check(command)
|
117
|
+
- coin
|
118
|
+
- config
|
119
|
+
- devdetails
|
120
|
+
- devs
|
121
|
+
- pga(number)
|
122
|
+
- pgacount
|
123
|
+
- pools
|
124
|
+
- privileged
|
125
|
+
- notify
|
126
|
+
- stats
|
127
|
+
- summary
|
128
|
+
- usbstats
|
129
|
+
- version
|
130
|
+
|
131
|
+
#### Privileged
|
132
|
+
|
133
|
+
The following privileged miner and pool commands are currently available:
|
134
|
+
|
135
|
+
##### Asc
|
136
|
+
|
137
|
+
- ascdisable(number)
|
138
|
+
- ascenable(number)
|
139
|
+
- ascidentify(number)
|
140
|
+
- ascset(number, option, value = nil)
|
141
|
+
|
142
|
+
##### Pga
|
143
|
+
|
144
|
+
- pgadisable(number)
|
145
|
+
- pgaenable(number)
|
146
|
+
- pgaidentify(number)
|
147
|
+
- pgaset(number, option, value = nil)
|
148
|
+
|
149
|
+
##### Pool
|
150
|
+
|
151
|
+
- addpool(url, user, pass)
|
152
|
+
- disablepool(number)
|
153
|
+
- enablepool(number)
|
154
|
+
- poolpriority(\*id_order)
|
155
|
+
- poolquota(number, value)
|
156
|
+
- removepool(number)
|
157
|
+
- switchpool(number)
|
158
|
+
|
159
|
+
##### System
|
160
|
+
|
161
|
+
- debug(setting = 'D')
|
162
|
+
- failover_only(value)
|
163
|
+
- hotplug(seconds)
|
164
|
+
- quit
|
165
|
+
- restart
|
166
|
+
- save(filename = nil)
|
167
|
+
- setconfig(name, value)
|
168
|
+
- zero(which = 'All', full_summary = false)
|
169
|
+
|
170
|
+
Any cgminer API commands not explictly defined above are implemented using `method_missing`. A complete list of available API commands and options can be found in the [cgminer API-README](https://github.com/ckolivas/cgminer/blob/master/API-README).
|
171
|
+
|
172
|
+
## Contributing
|
173
|
+
|
174
|
+
1. Fork it ( <https://github.com/jramos/cgminer_api_client/fork> )
|
175
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
176
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
177
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
178
|
+
5. Create a new Pull Request
|
179
|
+
|
180
|
+
## Donating
|
181
|
+
|
182
|
+
If you find this gem useful, please consider donating.
|
183
|
+
|
184
|
+
BTC: `18HFFqZv2KJMHPNwPes839PJd5GZc4cT3U`
|
185
|
+
|
186
|
+
## License
|
187
|
+
|
188
|
+
Code released under [the MIT license](LICENSE.txt).
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib/")
|
4
|
+
|
5
|
+
require 'cgminer_api_client'
|
6
|
+
require 'pp'
|
7
|
+
|
8
|
+
command = ARGV.shift&.to_sym
|
9
|
+
commands = CgminerApiClient::Miner::Commands.instance_methods
|
10
|
+
|
11
|
+
unless command and commands.include?(command)
|
12
|
+
puts "USAGE: cgminer_api_client command (arguments)"
|
13
|
+
puts "commands: " + commands.sort.collect(&:to_s).join(', ')
|
14
|
+
exit(0)
|
15
|
+
end
|
16
|
+
|
17
|
+
begin
|
18
|
+
miner_pool = CgminerApiClient::MinerPool.new
|
19
|
+
pp(ARGV.empty? ? miner_pool.query(command) : miner_pool.query(command, *ARGV))
|
20
|
+
rescue => e
|
21
|
+
puts e.message
|
22
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'cgminer_api_client/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "cgminer_api_client"
|
8
|
+
spec.version = CgminerApiClient::VERSION
|
9
|
+
spec.authors = ["Justin Ramos"]
|
10
|
+
spec.email = ["justin.ramos@gmail.com"]
|
11
|
+
spec.summary = %q{A gem that allows sending API commands to a pool of cgminer instances}
|
12
|
+
spec.description = %q{}
|
13
|
+
spec.homepage = "https://github.com/jramos/cgminer_api_client"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'socket'
|
3
|
+
require 'thread'
|
4
|
+
require 'yaml'
|
5
|
+
|
6
|
+
require "cgminer_api_client/miner"
|
7
|
+
require "cgminer_api_client/miner/commands"
|
8
|
+
require "cgminer_api_client/miner_pool"
|
9
|
+
require "cgminer_api_client/socket_with_timeout"
|
10
|
+
require "cgminer_api_client/version"
|
11
|
+
|
12
|
+
module CgminerApiClient
|
13
|
+
def self.default_host
|
14
|
+
defined?(@default_host) ? @default_host : '127.0.0.1'
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.default_host=(value)
|
18
|
+
@default_host = value
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.default_port
|
22
|
+
defined?(@default_port) ? @default_port : 4028
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.default_port=(value)
|
26
|
+
@default_port = value
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.default_timeout
|
30
|
+
defined?(@default_timeout) ? @default_timeout : 5
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.default_timeout=(value)
|
34
|
+
@default_timeout = value
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.config
|
38
|
+
yield self if block_given?
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'cgminer_api_client/socket_with_timeout'
|
2
|
+
require 'cgminer_api_client/miner/commands'
|
3
|
+
|
4
|
+
module CgminerApiClient
|
5
|
+
class Miner
|
6
|
+
include SocketWithTimeout
|
7
|
+
include Miner::Commands
|
8
|
+
|
9
|
+
attr_accessor :host, :port, :timeout
|
10
|
+
|
11
|
+
def initialize(host = nil, port = nil, timeout = nil)
|
12
|
+
@host = host ? host : CgminerApiClient.default_host
|
13
|
+
@port = port ? port : CgminerApiClient.default_port
|
14
|
+
@timeout = timeout ? timeout : CgminerApiClient.default_timeout
|
15
|
+
end
|
16
|
+
|
17
|
+
def query(method, *params)
|
18
|
+
if available?
|
19
|
+
request = {command: method}
|
20
|
+
|
21
|
+
unless params.length == 0
|
22
|
+
params = params.map { |p| p.to_s.gsub('\\', '\\\\').gsub(',', '\,') }
|
23
|
+
request[:parameter] = params.join(',')
|
24
|
+
end
|
25
|
+
|
26
|
+
response = perform_request(request)
|
27
|
+
data = sanitized(response)
|
28
|
+
method.to_s.match('\+') ? data : data[method.to_sym]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def available?(force_reload = false)
|
33
|
+
@available = nil if force_reload
|
34
|
+
|
35
|
+
@available ||= begin
|
36
|
+
open_socket(@host, @port, @timeout).close
|
37
|
+
true
|
38
|
+
rescue
|
39
|
+
false
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def method_missing(name, *args)
|
44
|
+
query(name, *args)
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def perform_request(request)
|
50
|
+
begin
|
51
|
+
s = open_socket(@host, @port, @timeout)
|
52
|
+
rescue
|
53
|
+
raise "Connection to #{@host}:#{@port} failed"
|
54
|
+
end
|
55
|
+
|
56
|
+
s.write(request.to_json)
|
57
|
+
response = s.read.strip.chars.map { |c| c.ord >= 32 ? c : "\\u#{'%04x' % c.ord}" }.join
|
58
|
+
s.close
|
59
|
+
|
60
|
+
response.gsub! '}{', '}, {'
|
61
|
+
response.gsub! '[,{', '[ {'
|
62
|
+
|
63
|
+
data = JSON.parse(response)
|
64
|
+
|
65
|
+
if request[:command].to_s.match('\+')
|
66
|
+
data.each_pair do |command, response|
|
67
|
+
check_status(response.first) if response.respond_to?(:first)
|
68
|
+
end
|
69
|
+
else
|
70
|
+
check_status(data)
|
71
|
+
end
|
72
|
+
|
73
|
+
return data
|
74
|
+
end
|
75
|
+
|
76
|
+
def check_status(data)
|
77
|
+
status = data['STATUS'][0]
|
78
|
+
sc = status['STATUS']
|
79
|
+
c = status['Code']
|
80
|
+
msg = status['Msg']
|
81
|
+
|
82
|
+
case sc
|
83
|
+
when 'S'
|
84
|
+
when 'I'
|
85
|
+
puts "Info from API [#{c}]: #{msg}"
|
86
|
+
when 'W'
|
87
|
+
puts "Warning from API [#{c}]: #{msg}"
|
88
|
+
else
|
89
|
+
raise "#{c}: #{msg}"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def sanitized(data)
|
94
|
+
if data.is_a?(Hash)
|
95
|
+
data.inject({}) { |n, (k, v)| n[k.to_s.downcase.tr(' ', '_').to_sym] = sanitized(v); n }
|
96
|
+
elsif data.is_a?(Array)
|
97
|
+
data.map { |v| sanitized(v) }
|
98
|
+
else
|
99
|
+
data
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|