def restore_db(): settings = get_settings() service = MontaguService(settings) notifier = Notifier(settings['notify_channel']) try: ok = service.status == 'running' and service.db_volume_present if not ok: raise Exception('montagu not in a state we can restore') bb8_backup.restore(service) database.setup(service) if settings["add_test_user"] is True: add_test_users() notifier.post("*Restored* data from backup on `{}` :recycle:".format( settings['instance_name'])) except Exception as e: print(e) try: notifier.post("*Failed* to restore data on `{}` :bomb:", settings['instance_name']) except: raise
class App: def __init__(self, id): alpr = Alpr("eu", "/etc/openalpr/openalpr.conf", "../runtime_data") if not alpr.is_loaded(): print("Error loading OpenALPR") exit() alpr.set_top_n(20) self.alpr = alpr self.notifier = Notifier() self.id = id self.source = DataSource(self) def on_image_input(self, image_path): result = self.alpr.recognize_file(image_path) self.send_data(result) # self.print_file_result(image_path, result) def print_file_result(self, file_name, data): print("=====\nFile {}:\n---".format(file_name)) results = data["results"] if len(results) == 0: print("No results found") else: print(results[0]["plate"]) def send_data(self, data): results = data["results"] if len(results) == 0: return json = { "id": self.id, "plate": results[0]["plate"], "isEntering": True } self.notifier.post(json) def exit(self): self.alpr.unload()
def _deploy(): print_ascii_art() print("Beginning Montagu deploy") settings = get_settings() service = MontaguService(settings) status = service.status volume_present = service.db_volume_present is_first_time = (status is None) and (not volume_present) if is_first_time: print("Montagu not detected: Beginning new deployment") else: print("Montagu status: {}. " "Data volume present: {}".format(status, volume_present)) notifier = Notifier(settings['notify_channel']) # Check that the deployment environment is clean enough version = git_check(settings) deploy_str = "montagu {} (`{}`) on `{}`".format( version['tag'] or "(untagged)", version['sha'][:7], settings['instance_name']) notifier.post("*Starting* deploy of " + deploy_str) # Pull images service.pull() # If Montagu is running, back it up before tampering with it if status == "running": if settings["bb8_backup"]: bb8_backup.backup() # Stop Montagu if it is running # (and delete data volume if persist_data is False) if not is_first_time: notifier.post("*Stopping* previous montagu " "on `{}` :hand:".format(settings['instance_name'])) service.stop() # Schedule backups if settings["bb8_backup"]: bb8_backup.schedule() # BB8 restore data_exists = (not is_first_time) and service.settings["persist_data"] if settings["initial_data_source"] == "bb8_restore": data_update = service.settings["update_on_deploy"] and \ not service.settings["bb8_backup"] if data_exists and not data_update: print("Skipping bb8 restore: 'persist_data' is set, " "and this is not a first-time deployment") else: print("Running bb8 restore (while service is stopped)") bb8_backup.restore() # Start Montagu again service.start() try: print("Configuring Montagu") configure_montagu(service, data_exists) print("Starting Montagu metrics") service.start_metrics() print("Montagu metrics started") except Exception as e: print("An error occurred before deployment could be completed:") print(e) print("\nYou may need to call ./stop.py before redeploying.") try: notifier.post("*Failed* deploy of " + deploy_str + " :bomb:") except: pass raise if settings["add_test_user"] is True: print("Adding tests users") add_test_users() last_deploy_update(version) notifier.post("*Completed* deploy of " + deploy_str + " :shipit:") print("Finished deploying Montagu") if settings["open_browser"]: sleep(1) webbrowser.open("https://localhost:{}/".format(settings["port"]))