def do_snapd_request(self, request): """ Forward the API request to snapd. """ if os.path.exists(settings.GOVERNOR_INTERFACE): interface = settings.GOVERNOR_INTERFACE path = '/snapd/' + '/'.join(request.postpath) else: interface = settings.SNAPD_INTERFACE path = '/' + '/'.join(request.postpath) body = None headers = {} # Try to read Content-Type of original request, and set up the body of # the request to snapd. We should expect None for GET requests and # JSON for POST requests. ctype = request.requestHeaders.getRawHeaders('Content-Type', default=[None])[0] if ctype is not None: headers['Content-Type'] = ctype body = request.content.read() # Make the request to snapd using a Unix socket. conn = UHTTPConnection(interface) conn.request(request.method, path, body, headers) res = conn.getresponse() data = res.read() # Return the response to the original request. request.setHeader('Content-Type', 'application/json') request.write(data) request.finish()
def connect(self, plug_snap=SNAP_NAME, plug=None, slot_snap="core", slot=None): """ Connect an interface. """ conn = UHTTPConnection(settings.GOVERNOR_INTERFACE) path = "/snapd/v2/interfaces" body = json.dumps({ 'action': 'connect', 'slots': [{ 'snap': slot_snap, 'slot': slot }], 'plugs': [{ 'snap': plug_snap, 'plug': plug }] }) headers = {"Content-type": "application/json"} conn.request("POST", path, body, headers) res = conn.getresponse() data = json.loads(res.read()) return self._maybeWait(data)
def listAuthorizedKeys(self, user="******"): """ Get a list of authorized keys. """ path = "/users/{}/authorized_keys".format(user) conn = UHTTPConnection(settings.GOVERNOR_INTERFACE) conn.request("GET", path) res = conn.getresponse() return json.loads(res.read().decode('utf-8'))
def listSnaps(self): """ Get a list of installed snaps. """ conn = UHTTPConnection(settings.GOVERNOR_INTERFACE) conn.request("GET", "/snapd/v2/snaps") res = conn.getresponse() data = json.loads(res.read()) return self._maybeWait(data)
def listSnaps(self): """ Get a list of installed snaps. """ conn = UHTTPConnection(settings.SNAPD_INTERFACE) conn.request("GET", "/v2/snaps") res = conn.getresponse() data = json.loads(res.read()) return self._maybeWait(data)
def get_change(self, change_id): """ Get the current status of a change. """ conn = UHTTPConnection(settings.GOVERNOR_INTERFACE) path = "/snapd/v2/changes/{}".format(change_id) conn.request("GET", path) res = conn.getresponse() data = json.loads(res.read()) return self._maybeWait(data)
def get_change(self, change_id): """ Get the current status of a change. """ conn = UHTTPConnection(settings.SNAPD_INTERFACE) path = "/v2/changes/{}".format(change_id) conn.request("GET", path) res = conn.getresponse() data = json.loads(res.read()) return self._maybeWait(data)
def addAuthorizedKey(self, key, user="******"): """ Add an authorized key. """ conn = UHTTPConnection(settings.GOVERNOR_INTERFACE) path = "/users/{}/authorized_keys".format(user) body = json.dumps({'key': key}) headers = {"Content-type": "application/json"} conn.request("POST", path, body, headers) res = conn.getresponse() return json.loads(res.read().decode('utf-8'))
def installSnap(self, snapName): """ Install a snap from the store. """ conn = UHTTPConnection(settings.GOVERNOR_INTERFACE) path = "/snapd/v2/snaps" body = json.dumps({'action': 'install', 'snaps': [snapName]}) headers = {"Content-type": "application/json"} conn.request("POST", path, body, headers) res = conn.getresponse() data = json.loads(res.read()) return self._maybeWait(data)
def installSnap(self, snapName): """ Install a snap from the store. """ conn = UHTTPConnection(settings.SNAPD_INTERFACE) path = "/v2/snaps" body = json.dumps({ 'action': 'install', 'snaps': [snapName] }) headers = {"Content-type": "application/json"} conn.request("POST", path, body, headers) res = conn.getresponse() data = json.loads(res.read()) return self._maybeWait(data)
def connect(self, plug_snap=SNAP_NAME, plug=None, slot_snap="core", slot=None): """ Connect an interface. """ conn = UHTTPConnection(settings.SNAPD_INTERFACE) path = "/v2/interfaces" body = json.dumps({ 'action': 'connect', 'slots': [{'snap': slot_snap, 'slot': slot}], 'plugs': [{'snap': plug_snap, 'plug': plug}] }) headers = {"Content-type": "application/json"} conn.request("POST", path, body, headers) res = conn.getresponse() data = json.loads(res.read()) return self._maybeWait(data)
def updateSnap(self, snapName, data): """ Post an update to a snap. Valid actions are: install, refresh, remove, revert, enable, disable. Example: updateSnap("paradrop-daemon", {"action": "refresh"} """ conn = UHTTPConnection(settings.GOVERNOR_INTERFACE) path = "/snapd/v2/snaps/{}".format(snapName) body = json.dumps(data) headers = {"Content-type": "application/json"} conn.request("POST", path, body, headers) res = conn.getresponse() data = json.loads(res.read()) return self._maybeWait(data)
def updateSnap(self, snapName, data): """ Post an update to a snap. Valid actions are: install, refresh, remove, revert, enable, disable. Example: updateSnap("paradrop-daemon", {"action": "refresh"} """ conn = UHTTPConnection(settings.SNAPD_INTERFACE) path = "/v2/snaps/{}".format(snapName) body = json.dumps(data) headers = {"Content-type": "application/json"} conn.request("POST", path, body, headers) res = conn.getresponse() data = json.loads(res.read()) return self._maybeWait(data)