248 lines
5.0 KiB
Python
248 lines
5.0 KiB
Python
#!/usr/bin/python3
|
|
|
|
|
|
## Version: 13Jul2016a
|
|
import pymongo
|
|
import os
|
|
import re
|
|
from datetime import datetime, date, time
|
|
import iso8601
|
|
from calendar import timegm
|
|
import time
|
|
import json
|
|
import sys
|
|
sys.path.insert(0,'/sl/lib')
|
|
from sleds_utilities import *
|
|
|
|
factsdir="/etc/sleds/facts"
|
|
|
|
############ Routines ##############
|
|
def writeFact( string, filename ):
|
|
filename=factsdir+"/"+filename
|
|
f = open(filename,"w")
|
|
print("Writing to filename: "+filename)
|
|
f.write(string)
|
|
f.write("\n")
|
|
f.close()
|
|
#
|
|
############# go issue a sub command / shell ###########
|
|
#def pipe (cmd):
|
|
# proc = subprocess.Popen( cmd ,
|
|
# stdin=subprocess.PIPE,
|
|
# stdout=subprocess.PIPE,
|
|
# )
|
|
#
|
|
# data=proc.communicate()[0]
|
|
# data=data.decode()
|
|
# return(data)
|
|
#
|
|
def myipaddr():
|
|
### Second, lets collect our current IP address
|
|
cmd="facter ipaddress"
|
|
cmd=cmd.split()
|
|
|
|
#print(cmd)
|
|
|
|
data=pipe(cmd)
|
|
data=data.rstrip()
|
|
ip=data
|
|
#print("My IP: "+ip)
|
|
return(ip)
|
|
|
|
#
|
|
#def myregkey():
|
|
# ### Second, lets collect our current IP address
|
|
# cmd="facter sleds-regkey"
|
|
# cmd=cmd.split()
|
|
#
|
|
# #print(cmd)
|
|
#
|
|
# data=pipe(cmd)
|
|
# data=data.rstrip()
|
|
# rkey=data
|
|
# return(rkey)
|
|
|
|
|
|
|
|
|
|
|
|
port="27017"
|
|
ipaddr="127.0.0.1"
|
|
ipaddr=myipaddr()
|
|
print("MY IP: ", ipaddr)
|
|
mongohost="127.0.0.1"
|
|
arg1=sys.argv[1]
|
|
print("ARG: ",arg1)
|
|
if (arg1):
|
|
env=arg1
|
|
else:
|
|
env="QA1"
|
|
|
|
print("ARG: ",arg1,env)
|
|
|
|
### Clear the current enviornment values for 'env'?
|
|
clear=1
|
|
rkey="MONO"
|
|
|
|
|
|
##-------------------------##
|
|
## Connection to the Mongo Service
|
|
##
|
|
try:
|
|
conn=pymongo.MongoClient(mongohost)
|
|
print("Connected successfully!!!")
|
|
except pymongo.errors.ConnectionFailure:
|
|
print("Could not connect to MongoDB: %s" % e )
|
|
|
|
print("Connection:",conn)
|
|
|
|
|
|
##-------------------------##
|
|
## Connect to the DataBase
|
|
##
|
|
db = conn.sequencelogic
|
|
|
|
##-------------------------##
|
|
## Connect to a specific collection
|
|
##
|
|
|
|
## The other collection is scat_control, be we don't need that here.
|
|
print(">> DBASE NAMES:",conn.database_names() )
|
|
print(">> COLLECTION NAMES:",db.collection_names())
|
|
|
|
|
|
registry = db.scat_registry
|
|
facts = db.sleds_facts
|
|
when=utctimenow()
|
|
|
|
|
|
print("-------")
|
|
if (clear):
|
|
registry.remove({"environment":env})
|
|
|
|
############
|
|
## First add the 'sleds-env' as a key and value pair ###
|
|
key="sleds-env"
|
|
environ = {"environment": env,
|
|
"key": key,
|
|
"value": env}
|
|
facts.remove({"key":key})
|
|
post_id = facts.insert_one(environ).inserted_id
|
|
print("-++--++-",post_id)
|
|
|
|
############
|
|
## Second, we add our IP in as a scat_registrar servere ###
|
|
environ = {"environment": env,
|
|
"scat-registrar": ipaddr,
|
|
"regdate": when,
|
|
"status": "online"}
|
|
facts.remove({"key":key})
|
|
post_id = facts.insert_one(environ).inserted_id
|
|
print("-++--++-",post_id)
|
|
|
|
############
|
|
## Now search through the 'environment' file and add addtional data ##
|
|
filename="/sl/config/environment"
|
|
envdata=""
|
|
with open(filename,"r") as data:
|
|
for line in data:
|
|
print("DATA:",line)
|
|
#--------------------------#
|
|
e = re.search('(\S+)\s*=\s*(\S+)', line)
|
|
if e:
|
|
key = e.group(1)
|
|
value = e.group(2)
|
|
print("key:",key," - value:",value)
|
|
environ = {"environment": env,
|
|
"key": key,
|
|
"value": value}
|
|
facts.remove({"key":key})
|
|
#post_id = registry.insert_one(environ).inserted_id
|
|
post_id = facts.insert_one(environ).inserted_id
|
|
print("-++++++-",post_id)
|
|
|
|
envdata+=key+"="+value+"\n"
|
|
|
|
|
|
f="sleds-env="+env
|
|
writeFact(envdata,"SLEDS-MONOENV")
|
|
writeFact(f,"SLEDS-ENV")
|
|
|
|
|
|
#---------------------------------------------------#
|
|
mongo=1
|
|
status="online"
|
|
state="OK"
|
|
regkey=rkey
|
|
uuid="-"
|
|
print("\n\n\n======= POST =======")
|
|
post = {"_id": ipaddr,
|
|
"ip": ipaddr,
|
|
"uuid": uuid,
|
|
"status": status,
|
|
"regdate": when,
|
|
"lastcontact": when,
|
|
"regkey": regkey,
|
|
"env": env,
|
|
"state": state}
|
|
|
|
if (mongo):
|
|
p=[str(port)]
|
|
post.update({"mongo":p})
|
|
|
|
|
|
registry = db.scat_registry
|
|
|
|
print("\nRECORD REMOVE...\n")
|
|
registry.remove({"ip":ipaddr})
|
|
print("\nRECORD INSERT...\n")
|
|
post_id = registry.insert_one(post).inserted_id
|
|
print("#-------------------- DONE ---------------------#\n\n")
|
|
|
|
##########################################################################
|
|
|
|
|
|
#for line in split(string,'\n'):
|
|
|
|
|
|
print("-------")
|
|
print("-++++++-",post_id)
|
|
post_id
|
|
|
|
find = db.registry.find()
|
|
|
|
print("FIND:",find)
|
|
|
|
print("-------")
|
|
find
|
|
|
|
print("=======--------------------------=======")
|
|
for d in registry.find({"status":"online"})[:98]:
|
|
print(d)
|
|
print("--")
|
|
|
|
#posts.find_one()
|
|
print("----------------")
|
|
#a = registry.find_one({"status":"online"})
|
|
a = registry.find({"status":"online"})
|
|
for a in registry.find({"status":"online"})[:400]:
|
|
print(a)
|
|
print("------XXXX------")
|
|
print("A",a)
|
|
a = registry.find({"ip":1,"status":1})
|
|
for a in registry.find({"status":"online"})[:400]:
|
|
print(a)
|
|
print(">> CRAP:", a['status'], " --- ", a['ip'])
|
|
if "uuid" in a:
|
|
print(">> JUNK:", a['uuid'], " --- ", a['ip'])
|
|
|
|
#registry.remove({"ip":ipaddr})
|
|
|
|
print("-------DONE---------")
|
|
for x in os.environ:
|
|
print("ENV: ",x,"=",os.environ[x])
|
|
|
|
|
|
|
|
|