def mail_feedback_notification(self, payload: dict) -> dict: parameters = get_one("Parameters") name = f"{payload['job']['name']}" recipients = payload["job"]["mail_recipient"].split( ",") or parameters.mail_recipients.split(",") self.logs.append(f"Sending mail notification for {name}") message = Message( f"{name} ({'PASS' if payload['result'] else 'FAILED'})", sender=parameters.mail_sender, recipients=recipients, body=payload["content"], ) runtime = payload["runtime"].replace(".", "").replace(":", "") filename = f"results-{runtime}.txt" with open(filename, "w") as file: file.write(str_dict(payload["results"][payload["runtime"]])) with open(filename, "r") as file: message.attach( filename, "text/plain", file.read(), disposition=f"attachment; filename={filename}", ) remove(filename) mail_client.send(message) return {"success": True}
def scheduler_job( job_id: int, aps_job_id: Optional[str] = None, targets: Optional[Set[Device]] = None, payload: Optional[dict] = None, ) -> None: with controller.app.app_context(): task = fetch("Task", creation_time=aps_job_id) job = fetch("Job", id=job_id) if targets: targets = {fetch("Device", id=device_id) for device_id in targets} results, now = job.try_run(targets=targets, payload=payload) parameters = get_one("Parameters") if job.push_to_git and parameters.git_automation: path_git_folder = Path.cwd() / "git" / "automation" with open(path_git_folder / job.name, "w") as file: file.write(str_dict(results)) repo = Repo(str(path_git_folder)) try: repo.git.add(A=True) repo.git.commit(m=f"Automatic commit ({job.name})") except GitCommandError: pass repo.remotes.origin.push() if task and not task.frequency: task.is_active = False db.session.commit()
def job(self, payload: dict, device: Device) -> dict: try: now = datetime.now() path_configurations = Path.cwd() / "git" / "configurations" path_device_config = path_configurations / device.name path_device_config.mkdir(parents=True, exist_ok=True) napalm_driver = self.napalm_connection(device) napalm_driver.open() self.logs.append( f"Fetching configuration on {device.name} (Napalm)") config = str_dict(napalm_driver.get_config()) napalm_driver.close() device.last_status = "Success" device.last_runtime = (datetime.now() - now).total_seconds() if device.configurations: last_config = device.configurations[max(device.configurations)] if config == last_config: return {"success": True, "result": "no change"} device.configurations[now] = device.current_configuration = config with open(path_device_config / device.name, "w") as file: file.write(config) device.last_update = now self.generate_yaml_file(path_device_config, device) except Exception as e: device.last_status = "Failure" device.last_failure = now self.generate_yaml_file(path_device_config, device) return {"success": False, "result": str(e)} if len(device.configurations) > self.number_of_configuration: device.configurations.pop(min(device.configurations)) return {"success": True, "result": "Get Config via Napalm"}
def slack_feedback_notification(self, payload: dict) -> dict: parameters = get_one("Parameters") slack_client = SlackClient(parameters.slack_token) self.logs.append(f"Sending Slack notification for {payload['job']['name']}") result = slack_client.api_call( "chat.postMessage", channel=parameters.slack_channel, text=str_dict(payload["content"]), ) return {"success": True, "result": str(result)}
def start(name: str, devices: str, payload: str) -> None: if devices: targets = { fetch("Device", name=name) for name in devices.split(",") } else: targets = set() if payload: payload = loads(payload) job = fetch("Job", name=name) echo(str_dict(job.try_run(targets=targets, payload=payload)[0]))
def get_diff(job_id: int, v1: str, v2: str) -> dict: job = fetch("Job", id=job_id) first = str_dict(dict(reversed(sorted(job.results[v1].items())))).splitlines() second = str_dict(dict(reversed(sorted(job.results[v2].items())))).splitlines() opcodes = SequenceMatcher(None, first, second).get_opcodes() return {"first": first, "second": second, "opcodes": opcodes}
def cli_delete(table: str, name: str) -> None: echo(str_dict(delete(table, name=name)))
def update(table: str, properties: str) -> None: echo(str_dict(factory(table, **loads(properties)).get_properties()))
def cli_fetch(table: str, name: str) -> None: echo(str_dict(fetch(table, name=name).get_properties()))