Sleds/buildvm/scat/scatregistrar.py

273 lines
6.7 KiB
Python
Raw Normal View History

2025-03-13 21:28:38 +00:00
#!/usr/bin/python3
#Version: 15Jul2016
###############################################################################
# This program sits on an open service port and listens for client
# registration requests. When a request is received the input string must be
# a single line that includes four colon seperated values:
# 1) IP address of the client
# 2) A random key (string) provided by the client. reregistrations requires
# the idenical key or else the registration data will be wipped out in mongo
# 3) HostType where is "Mongo" or "Mongo-Primary" or TBD-Future
# 4) tbd
#
# Once the input line has been received by the client this program will then
# add or update the record in mongo with the IP address as the key.
# Then this program will look in mongo for ENVIORNMENT=xx records (i.e. xx=
# QA, QA2, DEV, PROD1, PROD2, etc.) and it will then send a block of
# environment records to the client and it will end the registration processes.
#
###############################################################################
import pymongo
import fileinput
import random
import string
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 *
#-------------------------------- STEP 1 --------------------------------------#
# Accept the input from the client system on standard in
#
### Accept just one one and process it ###
line=sys.stdin.readline()
line=line.rstrip()
list=line.split(':')
mongoprimary=0
mongo=0
port="27017"
rkey="-"
hosttype=""
x=len(list)
print("X:",x)
ipaddr=list[0]
if (len(list) > 1):
rkey=list[1]
if (len(list) > 2):
### Check to see if this has a valid hosttype attribute
hosttype=list[2]
e = re.search('mongo-primary\s*', hosttype)
if (e):
mongoprimary=1
e = re.search('mongo\s*', hosttype)
if (e):
mongo=1
myip=myipaddr()
print("HOSTTYPE:",hosttype,mongo)
filename="/var/tmp/sleds-client."+line
f = open(filename,"w")
regkey=ipaddr+":"+rkey+"\n"
f.write(regkey)
print("REGISTRATION ACCEPTED: ",regkey)
#-------------------------------- STEP 2 --------------------------------------#
### MONGO STUFF - first we read in the mongo data because we need some of the
### data contained in mongo.
mongohost="10.130.13.21"
mongohost="127.0.0.1"
##-------------------------##
## 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
##
collection = db.scat_registry
result=db.collection.find()
print("RESULT:",result)
###############################################################################
### Here we accept the info comming in from the client.
### We write into mongo here.
## UPDATE MONGO
# first we need to know the mongo master we can get that from the envornment
#########################################################
#########################################################
# First search mongo for an existing record.
# Then if the records regkey matches the incomming regkey
# we will only update the record, else we create a new one
registry = db.scat_registry
facts = db.sleds_facts
a=registry.find_one({"regkey":rkey})
if (a):
ipaddress=a['ip']
currregkey=a['regkey']
else:
ipaddress=""
currregkey=""
##
## Add (or 'post') a record to the collection
##
#if $max_policy
when=utctimenow()
status="online"
max_bot=2
max_total=6
state="OK"
### NEED TO FIX env !
env="QA"
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,
"max_policybot" : max_bot,
"max_jobstatus" : max_bot,
"max_ingest" : max_bot,
"max_outflow" : max_bot,
"max_reportbot" : max_bot,
"max_libindex" : max_bot,
"max_flowbot" : max_bot,
"max_tomcat" : max_bot,
"max_classification" : max_bot,
"max_ocr" : max_bot,
"max_monitor" : max_bot,
"max_scheduler" : max_bot,
"max_k2daemon" : max_bot,
"max_total" : max_total,
}
if (mongo):
p="["+str(port)+"]"
post.update({"mongo":p})
print(post)
registry = db.scat_registry
if (currregkey):
post_id = registry.update_one(
{'ip':ipaddr},
{"$set": post},
upsert=True )
else:
registry.remove({"ip":ipaddr})
post_id = registry.insert_one(post).inserted_id
print("-------")
find = db.registry.find()
print("=======--------------------------=======")
#a = registry.find({"status":"online"})
#a = registry.find({"ip":ipaddr,"status":1})
if (ipaddr):
slscatlog('I',"scat-registrar Registration accepted for: "+ipaddr+" "+regkey)
print("-------DONE---------")
###############################################################################
### Then we want to read in all of the environment information from mongo
### and we spit it out to the client who in turn will put it in the
### /etc/sleds/facts directory for consumption by programs and facter.
#########################
### READ IN ENVIRONMENT FROM MONGO ###
for a in facts.find( { "scat-registrar": myip} ):
env=a.get("environment")
environment=""
#a = registry.find({"ip":ipaddr,"status":1})
for a in facts.find({"environment":env, "key": { "$exists": "true" } }):
fenv=a.get("environment")
print("FENV=",fenv)
fkey=a.get("key")
fvalue=a.get("value")
if ( fenv == env):
environment =environment+fkey+"="+fvalue+"\n"
# This is where the environment is passed to the remote system, the remote system
# then takes this data and creates facts from it.
f.write("\n----- BEGIN ENVIRONMENT -----\n")
print("\n----- BEGIN ENVIRONMENT -----\n")
f.write(environment)
print(environment)
f.write("\n----- END ENVIRONMENT -----\n")
print("\n----- END ENVIRONMENT -----\n")
f.close()
##########################
#"max_policybot" : 1,
#"max_jobstatus" : 1,
#"max_ingest" : 1,
#"max_outflow" : 1,
#"max_reportbot" : 1,
#"max_libindex" : 1,
#"max_flowbot" : 1,
#"max_tomcat" : 1,
#"max_classification" : 1,
#"max_ocr" : 1,
#"max_monitor" : 1,
#"max_scheduler" : 1,
#"max_k2daemon" : 1,
#"max_total" : 6,
#"status" : "online",
#"classification0001" : "dry-run",
#"updated" : "2016-07-12T21:10:42.840Z",
#"outflow0001" : "dry-run",
#"tomcat0001" : true,
#"k2daemon0001" : true,
#"ocr0001" : "dry-run",
#"scheduler0001" : "dry-run",
#"scatd" : false
#"regdate" : "2016-07-12T21:09:32Z",
#"state" : "OK",