| Server IP : 103.161.17.216 / Your IP : 216.73.216.1 Web Server : nginx/1.18.0 System : Linux tipsysaigoncharming 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 User : www-data ( 33) PHP Version : 7.4.3-4ubuntu2.29 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /snap/lxd/37560/bin/ |
Upload File : |
#!/usr/bin/python3
"""Query the status of the lxd snap."""
# Python implementation of:
# $ curl -s --unix /run/snapd.socket 'http://unix.socket/v2/changes?select=in-progress&for=lxd' | jq -r '.result[0].kind'
# auto-refresh
# The full JSON output when querying the in-progress changes for the lxd snap:
# $ curl -s --unix /run/snapd.socket 'http://snapd/v2/changes?select=in-progress&for=lxd' | jq -r .
# {
# "type": "sync",
# "status-code": 200,
# "status": "OK",
# "result": [
# {
# "id": "1907",
# "kind": "auto-refresh",
# "summary": "Auto-refresh snap \"lxd\"",
# "status": "Hold",
# "ready": true,
# "spawn-time": "2024-02-19T10:21:34.429396408-05:00",
# "data": {
# "snap-names": [
# "lxd"
# ]
# }
# }
# ]
# }
# Heavily inspired from snapd's api-client.py:
# https://github.com/snapcore/snapd/blob/master/tests/main/theme-install/api-client/bin/api-client.py
import http.client
import json
import socket
import sys
# This class is a subclass of http.client.HTTPConnection that connects to a Unix socket instead of a TCP socket.
class UnixSocketHTTPConnection(http.client.HTTPConnection):
def __init__(self, socket_path):
super().__init__("snapd")
self._socket_path = socket_path
def connect(self):
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect(self._socket_path)
self.sock = s
# This function connects to the Unix socket and requests the in-progress changes for the lxd snap.
# Returns the kind of the first result, or an empty string if an error occurs.
def main():
# Try to connect to the Unix socket and request the in-progress changes for the lxd snap.
# If an exception occurs, print an error message and return an empty string.
try:
conn = UnixSocketHTTPConnection("/run/snapd.socket")
conn.request("GET", "/v2/changes?select=in-progress&for=lxd")
response = conn.getresponse()
body = response.read().decode()
except FileNotFoundError:
print("missing socket", file=sys.stderr)
return ""
except http.client.HTTPException as e:
print("HTTP exception:", e, file=sys.stderr)
return ""
finally:
conn.close()
# If the response status is not 200, print an error message and return an empty string.
if response.status != 200:
print("HTTP error:", response.status, file=sys.stderr)
return ""
# If the response body is missing or empty, print an error message and return an empty string.
if not body:
print("Missing/empty body", file=sys.stderr)
return ""
# Try to parse the response body as JSON, and extract the "kind" field from the first result.
try:
data = json.loads(body)
except json.JSONDecodeError as e:
print("JSON decode exception:", e, file=sys.stderr)
return ""
if "result" in data and isinstance(data["result"], list) and len(data["result"]):
return data["result"][0].get("kind", "")
return ""
if __name__ == "__main__":
print(main())