def job(self, run: "Run", 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_connection = run.napalm_connection(device) run.log("info", f"Fetching configuration on {device.name} (Napalm)") config = controller.str_dict(napalm_connection.get_config()) 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[str( now)] = device.current_configuration = config with open(path_device_config / device.name, "w") as file: file.write(config) device.last_update = str(now) self.generate_yaml_file(path_device_config, device) except Exception as e: device.last_status = "Failure" device.last_failure = str(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, run: "Run", payload: dict) -> dict: slack_client = SlackClient(controller.slack_token) result = slack_client.api_call( "chat.postMessage", channel=controller.slack_channel, text=controller.str_dict(payload["content"]), ) return {"success": True, "result": str(result)}
def start(name: str, devices: str, payload: str) -> None: devices_list = devices.split(",") if devices else [] devices_list = [fetch("Device", name=name).id for name in devices_list] payload_dict = loads(payload) if payload else {} payload_dict["devices"] = devices_list job = fetch("Job", name=name) results = run_job(job.id, **payload_dict) Session.commit() echo(controller.str_dict(results))
def slack_feedback_notification(self, payload: dict, *args) -> dict: slack_client = SlackClient(controller.slack_token) self.logs.append( f"Sending Slack notification for {payload['job']['name']}") result = slack_client.api_call( "chat.postMessage", channel=controller.slack_channel, text=controller.str_dict(payload["content"]), ) return {"success": True, "result": str(result)}
def git_push(self, results: dict) -> None: path_git_folder = Path.cwd() / "git" / "automation" with open(path_git_folder / self.name, "w") as file: file.write(controller.str_dict(results)) repo = Repo(str(path_git_folder)) try: repo.git.add(A=True) repo.git.commit(m=f"Automatic commit ({self.name})") except GitCommandError: pass repo.remotes.origin.push()
def mail_feedback_notification(self, run: "Run", payload: dict) -> dict: name = payload["job"]["name"] recipients = payload["run"]["mail_recipient"] runtime = run.runtime.replace(".", "").replace(":", "") filename = f"results-{runtime}.txt" controller.send_email( f"{name} ({'PASS' if payload['results']['success'] else 'FAILED'})", payload["content"], recipients=recipients, filename=filename, file_content=controller.str_dict(payload["results"]), ) return {"success": True}
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) results = fetch("Job", name=name).run(targets=targets, payload=payload)[0] Session.commit() echo(controller.str_dict(results))
def mail_feedback_notification(self, payload: dict, *args) -> dict: name = f"{payload['job']['name']}" recipients = payload["job"]["mail_recipient"] runtime = payload["runtime"].replace(".", "").replace(":", "") filename = f"results-{runtime}.txt" self.logs.append(f"Sending mail notification for {name}") controller.send_email( f"{name} ({'PASS' if payload['result'] else 'FAILED'})", payload["content"], recipients=recipients, filename=filename, file_content=controller.str_dict( payload["results"][payload["runtime"]]), ) return {"success": True}
def cli_delete(table: str, name: str) -> None: device = delete(table, name=name) Session.commit() echo(controller.str_dict(device))
def update(table: str, properties: str) -> None: result = factory(table, **loads(properties)).get_properties() Session.commit() echo(controller.str_dict(result))
def cli_fetch(table: str, name: str) -> None: echo(controller.str_dict(fetch(table, name=name).get_properties()))