Browse Source

check existing servers when configuring client

raylu 11 years ago
parent
commit
4036361a29
3 changed files with 58 additions and 5 deletions
  1. 19 0
      api/db.py
  2. 9 0
      api/server.py
  3. 30 5
      client/sysvitals_client

+ 19 - 0
api/db.py

@@ -46,6 +46,18 @@ def cursor(): # https://code.google.com/p/gevent/source/browse/examples/psycopg2
 			raise Exception('cursor context manager got back closed connection')
 		pool.put_nowait(conn)
 
+def query(cur, sql, *args):
+	cur.execute(sql, args)
+	return cur.fetchall()
+
+def query_iter(cur, sql, *args):
+	cur.execute(sql, args)
+	while True:
+		r = cur.fetchone()
+		if r is None:
+			break
+		yield r
+
 def query_one(cur, sql, *args):
 	cur.execute(sql, args)
 	rval = cur.fetchone()
@@ -58,3 +70,10 @@ def create_server(group_id, hostname):
 		server_id = query_one(cur, 'INSERT INTO servers (group_id, hostname) VALUES(%s, %s) RETURNING id',
 				group_id, hostname)[0]
 	return server_id
+
+def get_servers(group_id):
+	servers = []
+	with cursor() as cur:
+		for server in query(cur, 'SELECT id, hostname FROM servers WHERE group_id = %s', group_id):
+			servers.append(server.copy())
+	return servers

+ 9 - 0
api/server.py

@@ -32,6 +32,7 @@ def main():
 		'datum': post_datum,
 		'raw': get_raw,
 		'stats': get_stats,
+		'servers': get_servers,
 		'register_server': register_server,
 	}
 	server = gevent.pywsgi.WSGIServer(('0.0.0.0', config.api_port), application)
@@ -230,6 +231,14 @@ def post_datum(split, query, environ):
 		fileio.write_datum(f, data)
 	return {'status': 'ok'}
 
+def get_servers(split, query, environ):
+	try:
+		group = int(split[1])
+	except (IndexError, ValueError):
+		raise HTTPException(400, '')
+
+	return db.get_servers(group)
+
 def register_server(split, query, environ):
 	try:
 		group = int(split[1])

+ 30 - 5
client/sysvitals_client

@@ -72,13 +72,38 @@ def configure():
 			print 'group_id should be numeric'
 	config.set('client', 'group_id', group_id)
 
-	print 'getting a brand new server id'
+	print 'checking existing servers'
 	try:
-		url = '%s/v1/%s/register_server' % (default_api_server, group_id)
-		r = urllib2.urlopen(url, json.dumps({'hostname': socket.getfqdn()}))
-		server_id = json.load(r)['server_id']
+		url = '%s/v1/%s/servers' % (default_api_server, group_id)
+		r = urllib2.urlopen(url)
+		servers = json.load(r)
 	except Exception as e:
-		sys.exit('an error occurred while getting the new server id:\n%r' % e)
+		sys.exit('an error occurred while fetching existing servers:\n%r' % e)
+	our_hostname = socket.getfqdn()
+	server_id = None
+	matches = 0
+	for server in servers:
+		if server['hostname'] == our_hostname:
+			server_id = server['id']
+			matches += 1
+	if matches == 0:
+		print 'no hostname matches found'
+	elif matches == 1:
+		print '1 hostname match found (%s, server id %d)' % (our_hostname, server_id)
+		if raw_input('take over this server id? (only do this if the old host is dead) [y/N] ') != 'y':
+			server_id = None
+	else:
+		print 'found %d servers with the same hostname... hopefully this is ok...' % matches
+		server_id = None
+
+	if server_id is None:
+		print 'getting a brand new server id'
+		try:
+			url = '%s/v1/%s/register_server' % (default_api_server, group_id)
+			r = urllib2.urlopen(url, json.dumps({'hostname': our_hostname}))
+			server_id = json.load(r)['server_id']
+		except Exception as e:
+			sys.exit('an error occurred while getting the new server id:\n%r' % e)
 	config.set('client', 'server_id', server_id)
 
 	print 'writing config to', cfg_path