def listen_to_octoprint(settings, q, on_heartbeat): def on_error(ws, error): print("!!! Error: {}".format(error)) def on_message(ws, message_type, message_payload): def __deplete_queue__(q): while q.qsize() > 10: q.get_nowait() if type(message_payload) is not dict: return __deplete_queue__(q) message_payload.update({"origin": "octoprint"}) q.put(json.dumps(message_payload)) if "init_client" in dir(octoprint_client): # OctoPrint <= 1.3.2 octoprint_client.init_client(settings) socket = octoprint_client.connect_socket(on_error=on_error, on_heartbeat=on_heartbeat, on_message=on_message) else: host = settings.get(["server", "host"]) host = host if host != "0.0.0.0" else "127.0.0.1" port = settings.getInt(["server", "port"]) apikey = settings.get(["api", "key"]) baseurl = octoprint_client.build_base_url(host=host, port=port) client = octoprint_client.Client(baseurl, apikey) client.create_socket(on_error=on_error, on_heartbeat=on_heartbeat, on_message=on_message)
def client(ctx, host, port, httpuser, httppass, https, prefix): """Basic API client.""" try: ctx.obj.settings = init_settings(get_ctx_obj_option(ctx, "basedir", None), get_ctx_obj_option(ctx, "configfile", None)) octoprint_client.init_client(ctx.obj.settings, https=https, httpuser=httpuser, httppass=httppass, host=host, port=port, prefix=prefix) except FatalStartupError as e: click.echo(e.message, err=True) click.echo("There was a fatal error initializing the client.", err=True) ctx.exit(-1)
def client(ctx, obj, host, port, httpuser, httppass, https, prefix): """Basic API client.""" try: obj.settings = init_settings(obj.basedir, obj.configfile) octoprint_client.init_client(obj.settings, https=https, httpuser=httpuser, httppass=httppass, host=host, port=port, prefix=prefix) except FatalStartupError as e: click.echo(e.message, err=True) click.echo("There was a fatal error initializing the client.", err=True) ctx.exit(-1)
def listen_to_octoprint(settings, q): def on_connect(ws): print(">>> Connected!") def on_close(ws): print(">>> Oh No! Connection closed! What happened?") def on_error(ws, error): print("!!! Error: {}".format(error)) def on_heartbeat(ws): q.put(json.dumps({'hb': {'ipAddrs': ip_addr()}})) def on_message(ws, message_type, message_payload): def __deplete_queue__(q): while q.qsize() > 10: q.get_nowait() if type(message_payload) is not dict: return __deplete_queue__(q) q.put(json.dumps(message_payload)) if "init_client" in dir(octoprint_client): # OctoPrint <= 1.3.2 octoprint_client.init_client(settings) socket = octoprint_client.connect_socket(on_connect=on_connect, on_close=on_close, on_error=on_error, on_heartbeat=on_heartbeat, on_message=on_message) else: host = settings.get(["server", "host"]) host = host if host != "0.0.0.0" else "127.0.0.1" port = settings.getInt(["server", "port"]) apikey = settings.get(["api", "key"]) baseurl = octoprint_client.build_base_url(host=host, port=port) client = octoprint_client.Client(baseurl, apikey) client.create_socket(on_connect=on_connect, on_close=on_close, on_error=on_error, on_heartbeat=on_heartbeat, on_message=on_message)
def update_command(force, targets): """ Apply updates. If any TARGETs are provided, only those components will be updated. \b Examples: - octoprint plugins softwareupdate:update This will update all components with a pending update that can be updated. - octoprint plugins softwareupdate:update --force This will force an update of all registered components that can be updated, even if they don't have an updated pending. - octoprint plugins softwareupdate:update octoprint This will only update OctoPrint and leave any further components with pending updates at their current versions. """ data = dict(force=force) if targets: data["check"] = targets client.init_client(cli_group.settings) flags = dict(waiting_for_restart=False, seen_close=False) def on_message(ws, msg_type, msg): if msg_type != "plugin" or msg["plugin"] != "softwareupdate": return plugin_message = msg["data"] if not "type" in plugin_message: return plugin_message_type = plugin_message["type"] plugin_message_data = plugin_message["data"] if plugin_message_type == "updating": click.echo("Updating {} to {}...".format( plugin_message_data["name"], plugin_message_data["target"])) elif plugin_message_type == "update_failed": click.echo("\t... failed :(") elif plugin_message_type == "loglines" and "loglines" in plugin_message_data: for entry in plugin_message_data["loglines"]: prefix = ">>> " if entry["stream"] == "call" else "" error = entry["stream"] == "stderr" click.echo("\t{}{}".format( prefix, entry["line"].replace("\n", "\n\t")), err=error) elif plugin_message_type == "success" or plugin_message_type == "restart_manually": results = plugin_message_data[ "results"] if "results" in plugin_message_data else dict() if results: click.echo("The update finished successfully.") if plugin_message_type == "restart_manually": click.echo("Please restart the OctoPrint server.") else: click.echo("No update necessary") ws.close() elif plugin_message_type == "restarting": flags["waiting_for_restart"] = True click.echo("Restarting to apply changes...") elif plugin_message_type == "failure": click.echo("Error") ws.close() def on_open(ws): if flags["waiting_for_restart"] and flags["seen_close"]: click.echo(" Reconnected!") else: click.echo("Connected to server...") def on_close(ws): if flags["waiting_for_restart"] and flags["seen_close"]: click.echo(".", nl=False) else: flags["seen_close"] = True click.echo("Disconnected from server...") socket = client.connect_socket(on_message=on_message, on_open=on_open, on_close=on_close) r = client.post_json("plugin/softwareupdate/update", data=data) try: r.raise_for_status() except requests.exceptions.HTTPError as e: click.echo( "Could not get update information from server, got {}".format( e)) sys.exit(1) data = r.json() to_be_updated = data["order"] checks = data["checks"] click.echo("Update in progress, updating:") for name in to_be_updated: click.echo( "\t{}".format(name if not name in checks else checks[name])) socket.wait() if flags["waiting_for_restart"]: if socket.reconnect(timeout=60): click.echo("The update finished successfully.") else: click.echo( "The update finished successfully but the server apparently didn't restart as expected." ) click.echo("Please restart the OctoPrint server.")
def check_command(force, targets): """ Check for updates. If any TARGETs are provided, only those components will be checked. \b Examples: - octoprint plugins softwareupdate:check This will check all components for available updates, utilizing cached version information. - octoprint plugins softwareupdate:check --force This will check all components for available updates, ignoring any cached version information even if it's still valid. - octoprint plugins softwareupdate:check octoprint This will only check OctoPrint itself for available updates. """ params = dict(force=force) if targets: params["check"] = ",".join(targets) client.init_client(cli_group.settings) r = client.get("plugin/softwareupdate/check", params=params) try: r.raise_for_status() except requests.exceptions.HTTPError as e: click.echo( "Could not get update information from server, got {}".format( e)) sys.exit(1) data = r.json() status = data["status"] information = data["information"] lines = [] octoprint_line = None for key, info in information.items(): status_text = "Up to date" if info["updateAvailable"]: if info["updatePossible"]: status_text = "Update available" else: status_text = "Update available (manual)" line = "{}\n\tInstalled: {}\n\tAvailable: {}\n\t=> {}".format( info["displayName"], info["information"]["local"]["name"], info["information"]["remote"]["name"], status_text) if key == "octoprint": octoprint_line = line else: lines.append(line) lines.sort() if octoprint_line: lines = [octoprint_line] + lines for line in lines: click.echo(line) click.echo() if status == "current": click.echo("Everything is up to date") else: click.echo("There are updates available!")
def update_command(force, targets): """ Apply updates. If any TARGETs are provided, only those components will be updated. \b Examples: - octoprint plugins softwareupdate:update This will update all components with a pending update that can be updated. - octoprint plugins softwareupdate:update --force This will force an update of all registered components that can be updated, even if they don't have an updated pending. - octoprint plugins softwareupdate:update octoprint This will only update OctoPrint and leave any further components with pending updates at their current versions. """ data = dict(force=force) if targets: data["check"] = targets client.init_client(cli_group.settings) flags = dict( waiting_for_restart=False, seen_close=False ) def on_message(ws, msg_type, msg): if msg_type != "plugin" or msg["plugin"] != "softwareupdate": return plugin_message = msg["data"] if not "type" in plugin_message: return plugin_message_type = plugin_message["type"] plugin_message_data = plugin_message["data"] if plugin_message_type == "updating": click.echo("Updating {} to {}...".format(plugin_message_data["name"], plugin_message_data["target"])) elif plugin_message_type == "update_failed": click.echo("\t... failed :(") elif plugin_message_type == "loglines" and "loglines" in plugin_message_data: for entry in plugin_message_data["loglines"]: prefix = ">>> " if entry["stream"] == "call" else "" error = entry["stream"] == "stderr" click.echo("\t{}{}".format(prefix, entry["line"].replace("\n", "\n\t")), err=error) elif plugin_message_type == "success" or plugin_message_type == "restart_manually": results = plugin_message_data["results"] if "results" in plugin_message_data else dict() if results: click.echo("The update finished successfully.") if plugin_message_type == "restart_manually": click.echo("Please restart the OctoPrint server.") else: click.echo("No update necessary") ws.close() elif plugin_message_type == "restarting": flags["waiting_for_restart"] = True click.echo("Restarting to apply changes...") elif plugin_message_type == "failure": click.echo("Error") ws.close() def on_open(ws): if flags["waiting_for_restart"] and flags["seen_close"]: click.echo(" Reconnected!") else: click.echo("Connected to server...") def on_close(ws): if flags["waiting_for_restart"] and flags["seen_close"]: click.echo(".", nl=False) else: flags["seen_close"] = True click.echo("Disconnected from server...") socket = client.connect_socket(on_message=on_message, on_open=on_open, on_close=on_close) r = client.post_json("plugin/softwareupdate/update", data=data) try: r.raise_for_status() except requests.exceptions.HTTPError as e: click.echo("Could not get update information from server, got {}".format(e)) sys.exit(1) data = r.json() to_be_updated = data["order"] checks = data["checks"] click.echo("Update in progress, updating:") for name in to_be_updated: click.echo("\t{}".format(name if not name in checks else checks[name])) socket.wait() if flags["waiting_for_restart"]: if socket.reconnect(timeout=60): click.echo("The update finished successfully.") else: click.echo("The update finished successfully but the server apparently didn't restart as expected.") click.echo("Please restart the OctoPrint server.")
def check_command(force, targets): """ Check for updates. If any TARGETs are provided, only those components will be checked. \b Examples: - octoprint plugins softwareupdate:check This will check all components for available updates, utilizing cached version information. - octoprint plugins softwareupdate:check --force This will check all components for available updates, ignoring any cached version information even if it's still valid. - octoprint plugins softwareupdate:check octoprint This will only check OctoPrint itself for available updates. """ params = dict(force=force) if targets: params["check"] = ",".join(targets) client.init_client(cli_group.settings) r = client.get("plugin/softwareupdate/check", params=params) try: r.raise_for_status() except requests.exceptions.HTTPError as e: click.echo("Could not get update information from server, got {}".format(e)) sys.exit(1) data = r.json() status = data["status"] information = data["information"] lines = [] octoprint_line = None for key, info in information.items(): status_text = "Up to date" if info["updateAvailable"]: if info["updatePossible"]: status_text = "Update available" else: status_text = "Update available (manual)" line = "{}\n\tInstalled: {}\n\tAvailable: {}\n\t=> {}".format(info["displayName"], info["information"]["local"]["name"], info["information"]["remote"]["name"], status_text) if key == "octoprint": octoprint_line = line else: lines.append(line) lines.sort() if octoprint_line: lines = [octoprint_line] + lines for line in lines: click.echo(line) click.echo() if status == "current": click.echo("Everything is up to date") else: click.echo("There are updates available!")