def unsuspend_aid(self, aid):
     sess = crud.get_session(self.conf)
     lbs = self.get_all_lbs(aid)
     self.logger.set_tenant_id(aid)
     self.logger.log("Unsuspending all LBS for account %d", aid)
     all_unsuspends_worked = True  # During borked loop set this to false
     for (dc, lb_list) in lbs['lbs'].iteritems():
         for lb in lb_list:
             lid = lb['lid']
             status = lb['status']
             if status != "SUSPENDED":
                 self.logger.log("Not unsuspending lb %d status %s",
                                 lid, status)
                 continue
             act = tables.Action(dc=dc, aid=aid, lid=lid,
                                 status_from=status, status_to=ACTIVE)
             sess.add(act)
             sess.commit()
             self.logger.log("Attempting to unsuspend %d_%d %s",
                             aid, lid, dc)
             self.lc.set_dc(dc)
             req = self.lc.unsuspend_lb(lid)
             time.sleep(self.delay)
             if req.status_code != 202:
                 fmt = "Error got http %d when trying to suspend lb %d: %s"
                 self.logger.log(fmt, req.status_code, lid, req.text)
                 act.success = False
                 all_unsuspends_worked = False
             else:
                 act.success = True
             sess.merge(act)
             sess.commit()
     sess.close()
     return all_unsuspends_worked
 def test_logger(self):
     l = terminator_app.TerminatorLogger()
     sess = crud.get_session()
     crud.get_curr_run(sess)
     l.set_tenant_id(354934)
     l.log("Testing if the logger works")
     l.log("Testing some more")
     l.log("hope the db didn't explode")
 def get_new_terminator_entries(self):
     self.tfc.get_token()
     feed_entries = self.tfc.get_all_entries(TERMINATOR_CHOMP_SIZE)
     n_entries = len(feed_entries)
     sess = crud.get_session()
     new_entries = crud.get_new_entries(sess, feed_entries)
     crud.save_entries(sess, new_entries)
     n_new_entries = len(new_entries)
     return {"n_entries": n_entries, "n_new_entries": n_new_entries}
 def run_iteration(self):
     try:
         sess = crud.get_session()
         self.bump_run_id(sess)
         self.run_terminator_client(sess)
         self.run_needs_push(sess)
         return True
     except:
         self.logger.set_tenant_id(None)
         self.logger.log("Error During run %d exception caught",
                         tables.curr_run_id, comment=utils.excuse(),
                         type="error")
         return False
 def __init__(self, conf=None):
     self.conf = utils.load_config(conf)
     self.endpoints = {}
     self.sess = crud.get_session(conf=self.conf)
     if self.conf.get('dryrun', False):
         self.lc = utils.DryRunLbaasClient(conf=self.conf)
     else:
         self.lc = utils.LbaasClient(conf=self.conf)
     self.tfc = utils.TerminatorFeedClient(conf=self.conf)
     self.logger = TerminatorLogger(conf=self.conf)
     self.delay = float(self.conf['clb']['delay'])
     self.run_id = None
     for (dcname, dc_dict) in self.conf['clb']['dc'].iteritems():
         self.endpoints[dcname] = dc_dict['endpoint']
     if tables.curr_run_id is None:
         self.bump_run()
 def delete_aid(self, aid):
     sess = crud.get_session(self.conf)
     lbs = self.get_all_lbs(aid)
     self.logger.set_tenant_id(aid)
     self.logger.log("Deleting all LBS for account %d", aid)
     all_deletes_success = True  # Flip this on failure
     for (dc, lb_list) in lbs['lbs'].iteritems():
         for lb in lb_list:
             lid = lb['lid']
             status = lb['status']
             if status != ACTIVE and status != SUSPENDED:
                 fmt = "Not deleting lb %d_%d in %s its status is %s"
                 msg = fmt % (aid, lid, dc, status)
                 self.logger.log(msg, tenant_id=aid)
                 continue
             self.logger.log("attempting delete of %d_%d in %s",
                             aid, lid, dc)
             self.lc.set_dc(dc)
             act = tables.Action(dc=dc, aid=aid, lid=lid,
                                 status_from=status, status_to=DELETED)
             sess.add(act)
             sess.commit()
             if status == ACTIVE:
                 req = self.lc.delete_lb(aid, lid)
             elif status == SUSPENDED:
                 req = self.lc.delete_suspended_lb(lid)
                 # These are special
             else:
                 # The if statement above should have been a fail early
                 # test so this state should be impossible to reach
                 self.logging.log("Impossible state ERROR", type="error")
                 continue  # Continue anyways.
             time.sleep(self.delay)
             if req.status_code != 202:
                 fmt = "Error deleting lb %d_%d from %s http %d %s"
                 msg = fmt % (aid, lid, dc, req.status_code, req.text)
                 self.logger.log(msg, type="error")
                 all_deletes_success = False
                 act.success = False
             else:
                 act.success = True
             sess.merge(act)
             sess.commit()
     return all_deletes_success
 def suspend_aid(self, terminator_id, aid):
     sess = crud.get_session(self.conf)
     lbs = self.get_all_lbs(aid)
     user = self.conf['clb']['user']
     self.logger.set_tenant_id(aid)
     self.logger.log("Suspending all LBS for account %d", aid)
     all_suspends_worked = True  # During borked loop set this to false
     for (dc, lb_list) in lbs['lbs'].iteritems():
         for lb in lb_list:
             self.lc.set_dc(dc)
             lid = lb['lid']
             status = lb['status']
             if status == ERROR:
                 self.logger.log("Not suspending an error loadbalancer %d",
                                 lid)
                 continue
             if status == SUSPENDED:
                 self.logger.log("Loadbalancer already suspended %d",
                                 lid)
                 continue
             if status != ACTIVE:
                 self.logger.log("not suspending  loadbalancer %d from %s ",
                                 lid, status)
                 continue
             act = tables.Action(dc=dc, aid=aid, lid=lid,
                                 status_from=status, status_to=SUSPENDED)
             sess.add(act)
             sess.commit()
             self.logger.log("Attempting to suspend %d_%d %s",
                             aid, lid, dc)
             req = self.lc.suspend_lb(terminator_id.replace('-',''), lid)
             time.sleep(self.delay)  #  Cause the api is fragile.
             if req.status_code != 202:
                 fmt = "Error got http %d when trying to suspend lb %d: %s"
                 self.logger.log(fmt, req.status_code, lid, req.text)
                 act.success = False
                 all_suspends_worked = False
             else:
                 act.success = True
             sess.merge(act)
             sess.commit()
     sess.close()
     return all_suspends_worked
 def bump_run(self):
     sess = crud.get_session(self.conf)
     run_id = crud.inc_curr_run(sess)
     return run_id
 def reset_session(self):
     self.sess = crud.get_session()