Skip to content

Instantly share code, notes, and snippets.

@emre
Last active August 29, 2015 14:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save emre/b9b84f51426be1e4910f to your computer and use it in GitHub Desktop.
Save emre/b9b84f51426be1e4910f to your computer and use it in GitHub Desktop.
NHZ hallmark installer script

this script made/tested on ubuntu/debian based systems.

usage

$ sudo python installer.py
import os
import sys
import urllib2
import urllib
import json
import time
import re
import getpass
INSTALL_PATH = '/srv/'
CONF_FILE = os.path.join(INSTALL_PATH, "hz/conf/nhz-default.properties")
NHZ_PATH = os.path.join(INSTALL_PATH, "hz")
SUPERVISOR_CONF = "/etc/supervisor/conf.d/hz.conf"
def make_get(url):
response = urllib2.urlopen(url)
html = response.read()
return html
def make_post(url, data):
data = urllib.urlencode(data)
req = urllib2.Request(url=url,data=data)
content = urllib2.urlopen(req).read()
return content
def installed():
return os.path.exists(CONF_FILE)
def edited(ip):
return ip in open(CONF_FILE).read()
def get_ip():
return json.loads(make_get("http://httpbin.org/ip"))["origin"]
def create_hallmark(ip, secret):
endpoint = 'http://127.0.0.1:7776/nhz'
content = make_post(endpoint, {
"requestType": "markHost",
"host": ip,
"weight": 100,
"date": "2013-12-10",
"secretPhrase": secret,
})
return json.loads(content)["hallmark"]
def update_config(ip, hallmark):
conf_content = open(CONF_FILE).read()
conf_content = re.sub("nhz.myHallmark=([^\n]*)\n", "nhz.myHallmark={}".format(hallmark), conf_content)
conf_content = re.sub("nhz.myAddress=([^\n]*)\n", "nhz.myAddress={}".format(ip), conf_content)
f = open(CONF_FILE, 'w+')
f.write(conf_content)
f.close()
print "config updated with addr: {}, hallmark: {}".format(ip, hallmark)
def write_supervisor_config():
if not os.path.exists(os.path.dirname(SUPERVISOR_CONF)):
os.makedirs(os.path.dirname(SUPERVISOR_CONF))
file_content = """[program:quark]
directory={}
command=/bin/sh run.sh
stdout_logfile=/var/log/nhz.log
stderr_logfile=/var/log/nhz.error.log
autorestart=true""".format(NHZ_PATH)
f = open(SUPERVISOR_CONF, "w+")
f.write(file_content)
f.close()
def main():
try:
f = open("/root/sec-test", "w+")
f.close()
except IOError:
print >> sys.stderr, "You need root permissions to run this script."
sys.exit(1)
if not installed():
print "installing dependencies..."
os.system("apt-get update -y")
os.system("apt-get install -y screen")
os.system("apt-get install -y openjdk-7-jre")
os.system("apt-get install -y unzip")
os.system("apt-get install -y supervisor")
os.chdir(INSTALL_PATH)
os.system("wget http://downloads.horizonplatform.io/binaries/hz_v3.8.zip")
os.system("unzip hz_v3.8.zip")
# open port
os.system("iptables -A INPUT -p tcp -d 0/0 -s 0/0 --dport 7774 -j ACCEPT")
os.chdir(NHZ_PATH)
ip = get_ip()
if not edited(ip):
os.system("screen -d -m -S hallmark ./run.sh")
print "waiting 30 seconds for node to set itself up..."
time.sleep(30)
try:
hallmark = create_hallmark(ip, getpass.getpass("enter your secret phrase:\n"))
update_config(ip, hallmark)
finally:
os.system("screen -S hallmark -X quit")
write_supervisor_config()
print "starting supervisord"
os.system("service supervisor stop")
time.sleep(3)
os.system("service supervisor start")
print "-" * 42
print "success! your hallmark node set on ip: {}".format(ip)
print "NHZ address for donations: NHZ-MGUU-WTUR-JC6K-DREBV"
print "thanks!"
print "-" * 42
if __name__ == '__main__':
main()
import requests
from clint.textui import puts, indent, colored
NODE_LIST = [
"ip",
"ip2",
"ipN",
]
for node in NODE_LIST:
response = requests.get("http://explorer.nhzcrypto.org/api.php?page=nodecheck&nodeAddress={0}".format(node))
result = response.json()
next_payout = ""
if result.get("error"):
state = colored.red("down")
else:
if result["sbs"]["state"] == 'ONLINE':
state = colored.green("up")
next_payout = str(result["sbs"]["next_payout"]) + "m"
else:
state = colored.magenta("not detected")
with indent(2):
puts("{0} {1} {2}".format(node, state, next_payout))
@Zronek
Copy link

Zronek commented Apr 3, 2015

Hello,

Great script but I have a error with nodechecker.py :

File "nodechecker.py", line 13, in
response = requests.get("http://explorer.nhzcrypto.org/api.php?page=nodecheck&nodeAddress={0}".format(node))
File "/usr/local/lib/python2.7/dist-packages/requests-2.6.0-py2.7.egg/requests/api.py", line 68, in get
return request('get', url, *_kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests-2.6.0-py2.7.egg/requests/api.py", line 50, in request
response = session.request(method=method, url=url, *_kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests-2.6.0-py2.7.egg/requests/sessions.py", line 464, in request
resp = self.send(prep, *_send_kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests-2.6.0-py2.7.egg/requests/sessions.py", line 602, in send
history = [resp for resp in gen] if allow_redirects else []
File "/usr/local/lib/python2.7/dist-packages/requests-2.6.0-py2.7.egg/requests/sessions.py", line 195, in resolve_redirects
allow_redirects=False,
File "/usr/local/lib/python2.7/dist-packages/requests-2.6.0-py2.7.egg/requests/sessions.py", line 576, in send
r = adapter.send(request, *_kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests-2.6.0-py2.7.egg/requests/adapters.py", line 431, in send

It's normal ? or not ? :-)

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment