def post_system_test(_cloud): foreman = Foreman(conf["foreman_api_url"], _cloud.name, _cloud.ticket) quads = Api(API_URL) try: build_hosts = foreman.get_build_hosts() except RequestException: logger.error("Unable to query Foreman for cloud: %s" % _cloud.name) logger.error("Verify Foreman password is correct: %s" % _cloud.ticket) return False pending = [] schedules = quads.get_current_schedule(cloud=_cloud.name) if "result" not in schedules: for schedule in schedules: host = quads.get_hosts(id=schedule["host"]["$oid"]) if host and host['name'] in build_hosts: pending.append(host["name"]) if pending: logger.info("The following hosts are marked for build:") for host in pending: logger.info(host) return False return True
def post_system_test(self): foreman = Foreman( conf["foreman_api_url"], self.cloud.name, self.cloud.ticket ) quads = Api(API_URL) if not foreman.verify_credentials(): logger.error("Unable to query Foreman for cloud: %s" % self.cloud.name) logger.error("Verify Foreman password is correct: %s" % self.cloud.ticket) self.report = self.report + "Unable to query Foreman for cloud: %s\n" % self.cloud.name self.report = self.report + "Verify Foreman password is correct: %s\n" % self.cloud.ticket return False build_hosts = foreman.get_build_hosts() pending = [] schedules = quads.get_current_schedule(cloud=self.cloud.name) if "result" not in schedules: for schedule in schedules: host = quads.get_hosts(id=schedule["host"]["$oid"]) if host and host['name'] in build_hosts: pending.append(host["name"]) if pending: logger.info("The following hosts are marked for build:") self.report = self.report + "The following hosts are marked for build:\n" for host in pending: logger.info(host) self.report = self.report + "%s\n" % host return False return True
def verify(_cloud_name, change=False): _cloud_obj = Cloud.objects(name=_cloud_name).first() quads = Api(API_URL) hosts = quads.get_cloud_hosts(_cloud_name) for _host in hosts: _host_obj = Host.objects(name=_host["name"]).first() if _host_obj.interfaces: _public_vlan_obj = Vlan.objects(cloud=_cloud_obj).first() for i, interface in enumerate(_host_obj.interfaces): ssh_helper = SSHHelper(interface.ip_address, conf["junos_username"]) vlan = get_vlan(_cloud_obj, i) try: old_vlan_out = ssh_helper.run_cmd( "show configuration interfaces %s" % interface.switch_port) old_vlan = old_vlan_out[0].split(";")[0].split()[1][7:] except IndexError: old_vlan = None try: vlan_member_out = ssh_helper.run_cmd( "show vlans interface %s.0" % interface.switch_port) vlan_member = vlan_member_out[1].split()[2][4:].strip(",") except IndexError: vlan_member = None ssh_helper.disconnect() if not old_vlan: logger.warning( "Could not determine the previous VLAN for %s on %s, switch %s, switchport %s" % (_host["name"], interface.name, interface.ip_address, interface.switch_port)) old_vlan = get_vlan(_cloud_obj, i) if not vlan_member: logger.warning( "Could not determine the previous VLAN member for %s on %s, switch %s, switchport %s" % (_host["name"], interface.name, interface.ip_address, interface.switch_port)) vlan_member = old_vlan if int(old_vlan) != int(vlan): logger.warning("interface %s not using QinQ_vl%s", interface.switch_port, vlan) if _public_vlan_obj and i == len(_host_obj.interfaces) - 1: vlan = _public_vlan_obj.vlan_id if int(vlan_member) != int(vlan): logger.warning( "interface %s appears to be a member of VLAN %s, should be %s", interface.switch_port, vlan_member, vlan) if change: logger.info('=== INFO: change requested') if _public_vlan_obj and i == len( _host_obj.interfaces) - 1: logger.info( "Setting last interface to public vlan %s." % _public_vlan_obj.vlan_id) success = juniper_convert_port_public( interface.ip_address, interface.switch_port, str(old_vlan), str(vlan)) else: success = juniper_set_port(interface.ip_address, interface.switch_port, str(vlan_member), str(vlan)) if success: logger.info( "Successfully updated switch settings.") else: logger.error( "There was something wrong updating switch for %s:%s" % (_host.name, interface.name))
def main(): days = [1, 3, 5, 7] future_days = 7 quads = QuadsApi(API_URL) _clouds = quads.get_summary() _active_clouds = [_cloud for _cloud in _clouds if int(_cloud["count"]) > 0] if not os.path.exists(os.path.join(conf["data_dir"], "report")): Path(os.path.join(conf["data_dir"], "report")).mkdir(parents=True, exist_ok=True) for cloud in _active_clouds: cloud_info = "%s: %s (%s)" % (cloud["name"], cloud["count"], cloud["description"]) logger.info('=============== Initial Message') if not cloud["notified"]: create_initial_message(cloud["owner"], cloud["name"], cloud_info, cloud["ticket"], cloud["ccuser"], cloud["validated"]) for day in days: future = datetime.now() + timedelta(days=day) future_date = "%4d-%.2d-%.2d 22:00" % (future.year, future.month, future.day) current_hosts = quads.get_current_schedule(cloud=cloud["name"]) future_hosts = quads.get_current_schedule(cloud=cloud["name"], date=future_date) if "result" in current_hosts: current_hosts = [] else: current_host_ids = [host["host"] for host in current_hosts] current_hosts = [ quads.get_hosts(**{"id": host["$oid"]})["name"] for host in current_host_ids ] if "result" in future_hosts: future_hosts = [] else: future_host_ids = [host["host"] for host in future_hosts] future_hosts = [ quads.get_hosts(**{"id": host["$oid"]})["name"] for host in future_host_ids ] diff = set(current_hosts) - set(future_hosts) if diff: logger.info('=============== Additional Message') create_message(cloud["owner"], day, cloud["name"], cloud_info, cloud["ticket"], cloud["ccuser"], cloud["validated"], current_hosts, future_hosts) break for cloud in _clouds: if cloud["name"] != "cloud01" and cloud["owner"] not in [ "quads", None ]: report_file = "%s-%s-pre-initial-%s" % ( cloud["name"], cloud["owner"], cloud["ticket"]) if not os.path.exists( os.path.join(conf["data_dir"], "report", report_file)): cloud_info = "%s: %s (%s)" % (cloud["name"], cloud["count"], cloud["description"]) logger.info('=============== Future Initial Message') create_future_initial_message(cloud["owner"], cloud["name"], cloud_info, cloud["ticket"], cloud["ccuser"]) future = datetime.now() + timedelta(days=future_days) future_date = "%4d-%.2d-%.2d 22:00" % ( future.year, future.month, future.day) current_hosts = quads.get_current_schedule(cloud=cloud["name"]) future_hosts = quads.get_current_schedule(cloud=cloud["name"], date=future_date) if "result" in current_hosts: current_hosts = [] else: current_host_ids = [host["host"] for host in current_hosts] current_hosts = [ quads.get_hosts(**{"id": host["$oid"]})["name"] for host in current_host_ids ] if "result" in future_hosts: future_hosts = [] else: future_host_ids = [host["host"] for host in future_hosts] future_hosts = [ quads.get_hosts(**{"id": host["$oid"]})["name"] for host in future_host_ids ] diff = set(current_hosts) - set(future_hosts) if diff: logger.info('=============== Additional Message') create_future_message( cloud["owner"], future_days, cloud["name"], cloud_info, cloud["ticket"], cloud["ccuser"], cloud["validated"], current_hosts, future_hosts, ) continue
def generator(_host_file, _days, _month, _year, _gentime): color_array = [] quads = Api(API_URL) def get_spaced_colors(visual_colors): result = [] for k in sorted(visual_colors.keys()): result.append(visual_colors[k].split()) return result # covert palette to hex def get_cell_color(a, b, c): return "#" + \ ('0' if len(hex(int(a))) < 4 else '') + hex(int(a))[2:] + \ ('0' if len(hex(int(b))) < 4 else '') + hex(int(b))[2:] + \ ('0' if len(hex(int(c))) < 4 else '') + hex(int(c))[2:] colors = get_spaced_colors(quads_config["visual_colors"]) for i in colors: if int(i[0]) >= 0: color_array.append(get_cell_color(i[0], i[1], i[2])) else: color_array.append(i[1]) def print_simple_table(data, data_colors, _days, _gentime): lines = [] for item in range(len(data)): line = {"hostname": str(data[item][0])} __days = [] for j in range(1, _days): chosen_color = data_colors[item][j - 1] cell_date = "%s-%s-%.2d 00:00" % (_year, _month, int(j)) _day = { "day": j, "chosen_color": chosen_color, "color": color_array[int(chosen_color) - 1], "cell_date": cell_date, "cell_time": datetime.strptime(cell_date, '%Y-%m-%d %H:%M') } # TODO: this # history = quads.get_history() # for cloud in sorted(history["cloud" + str(_day["chosen_color"])]): # if datetime.fromtimestamp(cloud) <= _day["cell_time"]: # _day["display_description"] = \ # history["cloud" + str(_day["chosen_color"])][cloud]["description"] # _day["display_owner"] = history["cloud" + str(_day["chosen_color"])][cloud]["owner"] # _day["display_ticket"] = history["cloud" + str(_day["chosen_color"])][cloud]["ticket"] # break __days.append(_day) line["days"] = __days lines.append(line) with open(os.path.join(TEMPLATES_PATH, "simple_table")) as _file: template = Template(_file.read()) content = template.render( gentime=_gentime, _days=_days, lines=lines, ) return content if _host_file: with open(_host_file, 'r') as f: reader = csv.reader(f) your_list = list(reader) else: your_list = [] for h in sorted(quads.get_hosts()): your_list.append([h]) your_list_colors = [] for h in your_list: one_host = [] for d in range(0, _days): day = d + 1 if day < 10: day_string = "0" + str(day) else: day_string = str(day) current = quads.get_current_schedule(host=h[0], date="{}-{}-{} 00:00".format( _year, _month, day_string)) if current: one_host.append(current["cloud"].lstrip("cloud")) your_list_colors.append(one_host) return print_simple_table(your_list, your_list_colors, _days, _gentime)