diff --git a/helpers/etcd.py b/helpers/etcd.py index c9deff1c..5201dde2 100644 --- a/helpers/etcd.py +++ b/helpers/etcd.py @@ -12,9 +12,8 @@ logger = logging.getLogger(__name__) class Etcd: def __init__(self, config): - self.scope = config["scope"] - self.host = config["host"] - self.ttl = config["ttl"] + self.ttl = config['ttl'] + self.base_client_url = 'http://{host}/v2/keys/service/{scope}'.format(**config) def get_client_path(self, path, max_attempts=1): attempts = 0 @@ -27,7 +26,7 @@ class Etcd: except (urllib2.HTTPError, urllib2.URLError) as e: attempts += 1 if attempts < max_attempts: - logger.info("Failed to return %s, trying again. (%s of %s)" % (path, attempts, max_attempts)) + logger.info('Failed to return %s, trying again. (%s of %s)', path, attempts, max_attempts) time.sleep(3) else: raise e @@ -43,14 +42,14 @@ class Etcd: opener.open(request) def client_url(self, path): - return "http://%s/v2/keys/service/%s%s" % (self.host, self.scope, path) + return self.base_client_url + path def current_leader(self): try: - hostname = self.get_client_path("/leader")["node"]["value"] - address = self.get_client_path("/members/%s" % hostname)["node"]["value"] + hostname = self.get_client_path('/leader')['node']['value'] + address = self.get_client_path('/members/' + hostname)['node']['value'] - return {"hostname": hostname, "address": address} + return {'hostname': hostname, 'address': address} except urllib2.HTTPError as e: if e.code == 404: return None @@ -71,7 +70,7 @@ class Etcd: raise CurrentLeaderError("Etcd is not responding properly") def touch_member(self, member, connection_string): - self.put_client_path("/members/%s" % member, {"value": connection_string}) + self.put_client_path('/members/' + member, {"value": connection_string}) def take_leader(self, value): return self.put_client_path("/leader", {"value": value, "ttl": self.ttl}) is None @@ -81,7 +80,7 @@ class Etcd: return self.put_client_path("/leader", {"value": value, "ttl": self.ttl, "prevExist": False}) is None except urllib2.HTTPError as e: if e.code == 412: - logger.info("Could not take out TTL lock: %s" % e) + logger.info('Could not take out TTL lock: %s', e) return False def update_leader(self, value): @@ -105,7 +104,7 @@ class Etcd: def am_i_leader(self, value): # try: reponse = self.get_client_path("/leader") - logger.info("Lock owner: %s; I am %s" % (reponse["node"]["value"], value)) + logger.info('Lock owner: %s; I am %s', reponse["node"]["value"], value) return reponse["node"]["value"] == value # except Exception as e: # return False