Sleds/buildvm/scat/sledslighthouse.py

172 lines
5.1 KiB
Python

#! /usr/bin/python3
#Version: 12Jul2016
### SCAT LIGHTHOUSE ###
###########################################################################
#### This program will run as a deamon on one or more mongo servers. It
#### will read mongo env info and it will then send out a broadcast
#### packet to the network for potential new VMs to see. New VMs listening
#### for this broadcast packet will receive information that will tell
#### them SCAT infomation about their enviornment. Most notably the
#### enviornment name and the address of the SCAT "registrar" which they
#### can then contact to register themselves into the environment.
###########################################################################
import time
import os
import pymongo
from socket import *
import sys
sys.path.insert(0, '/sl/lib')
from sleds_utilities import *
import zlib, base64
import pymongo
# This is the file we will read if we can't reach mongo.
scatdatafile="/sl/config/scatdata"
myip=myipaddr()
print("MY ADDR: ",myip)
#f=os.popen("facter ipaddress")
#myip=f.read()
myip=myip.rstrip()
##########################################################################
# First, we read in data from mongo
def readEnvData():
port="27017"
host="127.0.0.1" # We always want localhost
collection="sleds_facts"
try:
#conn=pymongo.MongoClient()
conn=pymongo.MongoClient(host)
print("Connected successfully!!!")
except pymongo.errors.ConnectionFailure:
print("Could not connect to MongoDB: %s" % e )
print("Connection:",conn)
print(">> DBASE NAMES:",conn.database_names() )
# print(">> COLLECTION NAMES:",db.collection_names())
print("-1-")
db = conn.sequencelogic
#facts = db.collection
facts = db.sleds_facts
#result=db.facts.find_one()
#result=facts.find_one({ "scat-registrar": { "$exists": "true" } })
result=facts.find_one({ "scat-registrar": myip })
print("RESULT:",result)
scatRegServer=result["scat-registrar"]
env=result["environment"]
return(env,scatRegServer)
print("-2-")
#for a in facts.find_one({"environment":1}):
for a in facts.find( { "status": { "$exists": "true" } } ):
print("A2:",a)
print("-3-")
#for a in facts.find({"scat_registrar":1}):
for a in facts.find({"scat-registrar": { "$exists": "true" }}):
print("A3:",a, "--------->>", a["scat-registrar"])
scatRegServer=a["scat-registrar"]
a=""
envi,regip=readEnvData()
#print("DONE", envi, regip)
#f = open(scatdatafile, 'r')
scatdata = ""
#scatdata= f.read()
#SLSync.secret=AJR7GKIILJ2JWC25QMSDDQIQ4RHP5Z2H5
#storageGuard.cloud-guard-ip=https://sledsqa.sequencelogic.net
#storageGuard.cloud-guard-port=443
#storageGuard.storageGuardLoadBalancer=https://sledsqa.sequencelogic.net
#storageGuard.sgdm.locationDefaults[2].bucket_id=com.sequencelogic.sgdm.SOMES3BUCKET
#cloudGuard.webapp-host-absolute=https://sledsqa.sequencelogic.net
#dataManager.mongoServers[0].host=ip-or-hostname-mongo-instance-0
#dataManager.mongoServers[1].host=ip-or-hostname-mongo-instance-1
#dataManager.mongoServers[2].host=ip-or-hostname-mongo-instance-2
#K2Daemon.hostAndPort=k2hostname:32000
#K2Daemon.mongoReplicaSet=rs0::ip-or-hostname-mongo-instance-0:27017,ip-or-hostname-mongo-instance-1:27017,ip-or-hostname-mongo-instance-2:27017
#K2Proxy.daemonHost=k2hostname
#K2Proxy.clientUrl=http://k2proxy-hostname:8888
#shared.cloudGuardIP=http://sledsqa.sequencelogic.net:8080
#########################################################################
masterurl="http://sledsqa.sequencelogic.net"
bucketlist="com.sequencelogic.sgdm.SOMES3BUCKET"
k2list="xxx.xxx.xxx.xxx:32000"
scatstring=scatdata
scatstring+="SCATREGISTRAR=" + myip + "\n"
scatstring+="SCATSERVER=" + myip + ",10.130.0.0,10.110.1.1\n"
scatstring+="MONGOSERVER=" + myip + ",10.130.9.0,10.110.9.1\n"
scatstring+="LB-URL=" + masterurl + "\n"
scatstring+="ENV=QA\n"
scatstring+="VERSION=10.1\n"
scatstring+="LIGHTHOUSE=" + myip + "\n"
scatstring+="BUCKETLIST=" + bucketlist + "\n"
scatstring+="K2=" + k2list + "\n"
print(" MY IP ADDRESS: ",myip,"---",scatstring," LEN:" + str(len(scatstring)) )
#compressed = base64.b64encode(zlib.compress(scatstring,9))
s=scatstring.encode('utf-8')
compressed = zlib.compress(s)
print(" MY IP ADDRESS: ",myip,"---",compressed," LEN:" + str(len(compressed)) )
# create an UDP socket
s = socket(AF_INET, SOCK_DGRAM)
# bind the socket to localhost Src Port 54321
s.bind(('',54321))
# allow you to reuse the socket whici is stuck in TIME_WAIT
s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
# set broadcast
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
# send UDP broadcast packets. Dst Port 12345
while 1:
#s.sendto(bytes(scatstring,'UTF-8'),('255.255.255.255',54321))
#s.sendto(bytes(compressed,'UTF-8'),('255.255.255.255',54321))
s.sendto((compressed),('255.255.255.255',54321))
time.sleep(5);
print("BEACON...\n")
#def mysend(self, msg):
# totalsent = 0
# while totalsent < MSGLEN:
# sent = self.sock.send(msg[totalsent:])
# if sent == 0:
# raise RuntimeError("socket connection broken")
### totalsent = totalsent + sent