Beispiel #1
0
 def __init__(self, unit=None, group_rules=None):
     state_path = os.path.join(os.environ.get("CHARM_DIR", ""), "rds.state")
     self._state = KVFile(state_path)
     self.unit = unit or Unit()
     self._group_rules = group_rules or ()
Beispiel #2
0
class Controller(object):

    def __init__(self, unit=None, group_rules=None):
        state_path = os.path.join(os.environ.get("CHARM_DIR", ""), "rds.state")
        self._state = KVFile(state_path)
        self.unit = unit or Unit()
        self._group_rules = group_rules or ()

    def get_region(self):
        return self.unit.ec2metadata['availability-zone'][:-1]

    def get_rds(self, config):
        return rds.connect_to_region(self.get_region())

    def get_ec2(self, config):
        return rds.connect_to_region(self.get_region())

    def get_db(self, config, instance):
        if 'mysql' in config['engine'].lower():
            db = MySQL(config)
            if not self._state.get('mysql.client'):
                db.install_driver()
                self._state.set('mysql.client', True)
            return db

    def get_svc_dbname(self):
        return self.get_db_identifier()[:16].replace('-', '_')

    def get_db_identifier(self):
        service = os.environ["JUJU_REMOTE_UNIT"].split("/")[0]
        rel_id = os.environ["JUJU_RELATION_ID"].split(":")[-1]
        return "-".join([
            service,
            rel_id,
            os.environ["JUJU_ENV_UUID"]])

    def wait_for_db_instance(self, rds, instance_id):
        # Don't wait more than 10m
        print "test: waiting for db instance", instance_id,
        t = time.time()
        seen = set()
        while True:
            time.sleep(3)
            sys.stdout.write(".")
            try:
                dbs = rds.get_all_dbinstances(instance_id)
            except boto.exception.EC2ResponseError, e:
                if e.error_code == "InvalidInstanceId.NotFound":
                    continue
            db = dbs.pop()
            if db.status == "available":
                print "\ntest: db %s available in %s" % (
                    instance_id, time.time() - t)
                return db
            elif db.status not in seen:
                print "\n  db status", db.status, (time.time() - t),
                seen.add(db.status)
            if time.time() - t > 15 * 60:
                raise RuntimeError(
                    "Database not provisioned within threshold %s" % (
                        instance_id))