class Controller(threading.Thread): def __init__(self, config): threading.Thread.__init__(self) logger.debug('Initializing Controller RPC thread.') self.lock = threading.Lock() self.safeMode = True self.credential = None self.config = config #Disabled security until we have fix for AMBARI-157 #if(config.get('controller', 'user')!=None and config.get('controller', 'password')!=None): # self.credential = { 'user' : config.get('controller', 'user'), # 'password' : config.get('controller', 'password') # } self.url = config.get('controller', 'url') + '/agent/controller/heartbeat/' + socket.gethostname() def start(self): self.actionQueue = ActionQueue(self.config) self.actionQueue.start() self.heartbeat = Heartbeat(self.actionQueue) def __del__(self): logger.info("Controller connection disconnected.") def run(self): id='-1' if self.credential!=None: auth_handler = urllib2.HTTPBasicAuthHandler() auth_handler.add_password(realm="Controller", uri=self.url, user=self.credential['user'], passwd=self.credential['password']) opener = urllib2.build_opener(auth_handler) urllib2.install_opener(opener) retry=False firstTime=True while True: try: if retry==False: data = json.dumps(self.heartbeat.build(id)) logger.debug(data) req = urllib2.Request(self.url, data, {'Content-Type': 'application/json'}) f = urllib2.urlopen(req) response = f.read() f.close() data = json.loads(response) id=int(data['responseId']) self.actionQueue.put(data) if retry==True or firstTime==True: logger.info("Controller connection established") firstTime=False retry=False except Exception, err: retry=True if "code" in err: logger.error(err.code) else: logger.error("Unable to connect to: "+self.url,exc_info=True) if self.actionQueue.isIdle(): time.sleep(30) else: time.sleep(1)