def move_masters_from(self, node, master=None, no_wait=False, timeout=DEFAULT_TIMEOUT, ask_user=True): move_apps = [] for key, value in self.consul.kv.find('app/').items(): app = util.json2obj(value) if app.master == node: mstr = app.slave if not mstr: if not master: raise RuntimeError( "You must define a default master (--master) as " "there are some services (at least {}) without " "replicate (slave)".format(key)) if master not in self.nodes: raise RuntimeError( "The given default master hostname: {} is " "unknown. Available nodes: {}".format( master, self.nodes)) if master == node: raise RuntimeError( "You must provide a different default master: {} " "it must be different to the node that you want" "clear: {}".format(master, node)) mstr = master move_apps.append(( key, app, mstr, app.master if app.slave else None, )) if ask_user: print("You are going to move following apps:") for key, app, mstr, _ in move_apps: print(" - from {} to {}, project: {}".format( app.master, mstr, key)) answer = util.get_input("Please confim by entering 'yes': ") if answer.strip().lower() != 'yes': print("Not confirmed, Aborting") logger.warning("Not confirmed. Aborting") return for key, app, mstr, slave in move_apps: self._deploy(key, app.repo_url, app.branch, mstr, slave=slave, no_wait=no_wait, timeout=timeout)
def get_kv_application(self, repo_name, branch): apps = self.consul.kv.find('app/{repo}_{branch}'.format(repo=repo_name, branch=branch)) if not apps: return None, None if len(apps) > 1: raise RuntimeError( "Repo / branch are ambiguous, multiple keys ({}) found for" "given repo: {}, branch: {}".format(apps.keys(), repo_name, branch)) key, data = apps.popitem() return key, util.json2obj(data)
def _fire_event(self, kv_key, event_name, payload, no_wait, event_consumed, timeout): app_before = util.json2obj(self.consul.kv.get(kv_key)) logger.info("Emit %s event for kv key: %s with following payload: %r", event_name, kv_key, payload) event_id = self.consul.event.fire(event_name, payload) start_date = datetime.now() while not no_wait and not event_consumed( app_before, util.json2obj(self.consul.kv.get(kv_key)), maintenance=self.consul.kv.get_record( kv_key.replace("app/", "maintenance/")), self=self): time.sleep(1) if (datetime.now() - start_date).seconds > timeout: raise TimeoutError( "Event (id: {}) was not processed in the expected time" " ({}s),".format(event_id, timeout)) logger.info("Event %s takes %ss to consume", event_name, (datetime.now() - start_date).seconds) return event_id
def inspect_node( self, node, ): master_apps = [] for key, value in self.consul.kv.find('app/').items(): app = util.json2obj(value) if app.master == node: master_apps.append(key) print("Master apps of node {node}:".format(node=node)) print("\n".join(sorted(master_apps)))
def get_kv_application(self, repo_name, branch): apps = self.consul.kv.find(APP_KV_FIND_PATTERN.format( repo=repo_name, branch=branch, separator=APP_KEY_SEPARATOR ) ) if not apps: return None, None if len(apps) > 1: raise RuntimeError( "Repo / branch are ambiguous, multiple keys ({}) found for" "given repo: {}, branch: {}".format( sorted(apps.keys()), repo_name, branch ) ) key, data = apps.popitem() return key, util.json2obj(data)
def test_json2object(self): obj = util.json2obj("""{ "dict_var": { "dict-var2": { "string.var": "a string", "int_var": 123, "string-list": [ "el1", "el2", "el3" ] } }, "int-array": [ 1, 2, 3 ] }""") self.assertEqual(obj.dict_var.dict_var2.string_var, "a string") self.assertEqual(obj.dict_var.dict_var2.int_var, 123) self.assertEqual(obj.dict_var.dict_var2.string_list, ["el1", "el2", "el3"]) self.assertEqual(obj.int_array, [1, 2, 3])
def test_json2object_no_data(self): self.assertFalse(util.json2obj(None))