mirror of
https://github.com/alexgo-io/stacks-puppet-node.git
synced 2026-06-17 02:24:48 +08:00
This commit adds:
1) Basic version of opennamed that uses zerorpc for efficient rpc calls
2) Basic version of openname-cli to talk to opennamed
3) Support for talking to a default bitcoind server
This commit is contained in:
0
__init__.py
Normal file
0
__init__.py
Normal file
32
config.py
Normal file
32
config.py
Normal file
@@ -0,0 +1,32 @@
|
||||
"""
|
||||
Opennamed
|
||||
~~~~~
|
||||
:copyright: (c) 2014 by Openname.org
|
||||
:license: MIT, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
DEBUG = True
|
||||
|
||||
DEFAULT_PORT = '8344'
|
||||
LISTEN_IP = '0.0.0.0'
|
||||
VERSION = 'v0.1-beta'
|
||||
|
||||
try:
|
||||
OPENNAMED_SERVER = os.environ['OPENNAMED_SERVER']
|
||||
OPENNAMED_PORT = os.environ['OPENNAMED_PORT']
|
||||
except:
|
||||
OPENNAMED_SERVER = 'localhost'
|
||||
OPENNAMED_PORT = DEFAULT_PORT
|
||||
|
||||
try:
|
||||
BITCOIND_SERVER = os.environ['BITCOIND_SERVER']
|
||||
BITCOIND_PORT = os.environ['BITCOIND_PORT']
|
||||
BITCOIND_USER = os.environ['BITCOIND_USER']
|
||||
BITCOIND_PASSWD = os.environ['BITCOIND_PASSWD']
|
||||
except:
|
||||
BITCOIND_SERVER = 'btcd.onename.com'
|
||||
BITCOIND_PORT = '8332'
|
||||
BITCOIND_USER = 'openname'
|
||||
BITCOIND_PASSWD = 'opennamesystem'
|
||||
72
openname_cli.py
Normal file
72
openname_cli.py
Normal file
@@ -0,0 +1,72 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Opennamed
|
||||
~~~~~
|
||||
:copyright: (c) 2014 by Openname.org
|
||||
:license: MIT, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
import json
|
||||
|
||||
import zerorpc
|
||||
import config
|
||||
c = zerorpc.Client(timeout=5)
|
||||
c.connect('tcp://' + config.OPENNAMED_SERVER + ':' + config.OPENNAMED_PORT)
|
||||
|
||||
import logging
|
||||
log = logging.getLogger()
|
||||
log.setLevel(logging.DEBUG if config.DEBUG else logging.INFO)
|
||||
console = logging.StreamHandler()
|
||||
console.setLevel(logging.DEBUG if config.DEBUG else logging.INFO)
|
||||
formatter = logging.Formatter('%(message)s')
|
||||
console.setFormatter(formatter)
|
||||
log.addHandler(console)
|
||||
|
||||
#-------------------------
|
||||
def pretty_dump(input):
|
||||
|
||||
return json.dumps(input, sort_keys=False, indent=4, separators=(',', ': '))
|
||||
|
||||
#-----------------------------------
|
||||
def run_cli():
|
||||
|
||||
parser = argparse.ArgumentParser(description='Openname Cli version {}'.format(config.VERSION))
|
||||
|
||||
parser.add_argument('--opennamed-server',
|
||||
help='the hostname or IP address of the opennamed RPC server (default: {})'.format(config.OPENNAMED_SERVER))
|
||||
parser.add_argument('--opennamed-port', type=int,
|
||||
help='the opennamed RPC port to connect to (default: {})'.format(config.OPENNAMED_PORT))
|
||||
|
||||
subparsers = parser.add_subparsers(dest='action', help='the action to be taken')
|
||||
|
||||
parser_cli = subparsers.add_parser('getinfo', help='get basic info from the opennamed server')
|
||||
parser_cli = subparsers.add_parser('name_show', help='<name> display value of a registered name')
|
||||
|
||||
#print default help message, if no argument is given
|
||||
if len(sys.argv)==1:
|
||||
parser.print_help()
|
||||
sys.exit(1)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.action == 'getinfo':
|
||||
|
||||
try:
|
||||
log.info(pretty_dump(c.getinfo()))
|
||||
except Exception as e:
|
||||
log.info("Couldn't connect to opennamed server")
|
||||
exit(0)
|
||||
|
||||
elif args.action == 'name_show':
|
||||
|
||||
log.info('in name_show')
|
||||
|
||||
#name_show code here
|
||||
|
||||
#-----------------------------------
|
||||
if __name__ == '__main__':
|
||||
|
||||
run_cli()
|
||||
109
opennamed.py
Normal file
109
opennamed.py
Normal file
@@ -0,0 +1,109 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Opennamed
|
||||
~~~~~
|
||||
:copyright: (c) 2014 by Openname.org
|
||||
:license: MIT, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
#-----------------------------------
|
||||
|
||||
import argparse
|
||||
import zerorpc
|
||||
import daemon
|
||||
import sys
|
||||
|
||||
import config
|
||||
|
||||
import logging
|
||||
log = logging.getLogger()
|
||||
log.setLevel(logging.DEBUG if config.DEBUG else logging.INFO)
|
||||
console = logging.StreamHandler()
|
||||
console.setLevel(logging.DEBUG if config.DEBUG else logging.INFO)
|
||||
formatter = logging.Formatter('%(message)s')
|
||||
console.setFormatter(formatter)
|
||||
log.addHandler(console)
|
||||
|
||||
from bitcoinrpc.authproxy import AuthServiceProxy
|
||||
|
||||
config_options = 'https://' + config.BITCOIND_USER + ':' + config.BITCOIND_PASSWD + '@' \
|
||||
+ config.BITCOIND_SERVER + ':' + str(config.BITCOIND_PORT)
|
||||
|
||||
bitcoind = AuthServiceProxy(config_options)
|
||||
|
||||
#-----------------------------------
|
||||
class OpennamedRPC(object):
|
||||
|
||||
#------------------
|
||||
def getinfo(self):
|
||||
|
||||
info = bitcoind.getinfo()
|
||||
|
||||
reply = {}
|
||||
reply['blocks'] = info['blocks']
|
||||
return reply
|
||||
|
||||
#-----------------------------------
|
||||
def run_server():
|
||||
|
||||
try:
|
||||
server = zerorpc.Server(OpennamedRPC())
|
||||
server.bind('tcp://' + config.LISTEN_IP + ':' + config.DEFAULT_PORT)
|
||||
server.run()
|
||||
except Exception as e:
|
||||
log.debug(e)
|
||||
log.info('Exiting opennamed server')
|
||||
|
||||
#-----------------------------------
|
||||
def run_opennamed():
|
||||
|
||||
parser = argparse.ArgumentParser(description='Openname Core Daemon version {}'.format(config.VERSION))
|
||||
|
||||
parser.add_argument('--bitcoind-server', help='the hostname or IP address of the bitcoind RPC server')
|
||||
parser.add_argument('--bitcoind-port', type=int, help='the bitcoind RPC port to connect to')
|
||||
parser.add_argument('--bitcoind-user', help='the username for bitcoind RPC server')
|
||||
parser.add_argument('--bitcoind-passwd', help='the password for bitcoind RPC server')
|
||||
|
||||
subparsers = parser.add_subparsers(dest='action', help='the action to be taken')
|
||||
|
||||
parser_server = subparsers.add_parser('start', help='start the opennamed server')
|
||||
parser_server.add_argument('--foreground', action='store_true', help='start the opennamed server in foreground')
|
||||
parser_server = subparsers.add_parser('stop', help='stop the opennamed server')
|
||||
|
||||
#print default help message, if no argument is given
|
||||
if len(sys.argv)==1:
|
||||
parser.print_help()
|
||||
sys.exit(1)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.action == 'start':
|
||||
|
||||
if args.foreground:
|
||||
log.info('Starting opennamed server in foreground')
|
||||
run_server()
|
||||
else:
|
||||
log.info('Starting opennamed server')
|
||||
with daemon.DaemonContext():
|
||||
run_server()
|
||||
|
||||
|
||||
elif args.action == 'stop':
|
||||
|
||||
log.info('Stopping opennamed server')
|
||||
#quick hack to kill a background daemon
|
||||
import subprocess, signal
|
||||
import os
|
||||
p = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE)
|
||||
out, err = p.communicate()
|
||||
|
||||
for line in out.splitlines():
|
||||
if 'opennamed' in line:
|
||||
pid = int(line.split(None, 1)[0])
|
||||
os.kill(pid, signal.SIGTERM)
|
||||
|
||||
#-----------------------------------
|
||||
if __name__ == '__main__':
|
||||
|
||||
run_opennamed()
|
||||
8
requirements.txt
Normal file
8
requirements.txt
Normal file
@@ -0,0 +1,8 @@
|
||||
gevent==1.0.1
|
||||
greenlet==0.4.5
|
||||
lockfile==0.10.2
|
||||
msgpack-python==0.4.2
|
||||
python-bitcoinrpc==0.1
|
||||
python-daemon==1.6.1
|
||||
pyzmq==13.1.0
|
||||
zerorpc==0.4.4
|
||||
Reference in New Issue
Block a user