How to track operations via API

Hi,

I want to restore a snapshot to a container every day at the same time. Therefore I wrote a shell script which uses curl and the API on LXD 2.16. So far so good . . . the restore process is running with the following output

curl -s --unix-socket /var/lib/lxd/unix.socket -X PUT -d '{"restore": "snap2"}' a/1.0/containers/apache-12345 | jq .
{
  "type": "async",
  "status": "Operation created",
  "status_code": 100,
  "operation": "/1.0/operations/d6e9dc2b-9c00-47e3-8b6b-67a9839a83a5",
  "error_code": 0,
  "error": "",
  "metadata": {
    "id": "d6e9dc2b-9c00-47e3-8b6b-67a9839a83a5",
    "class": "task",
    "created_at": "2017-10-08T11:35:09.705302724+02:00",
    "updated_at": "2017-10-08T11:35:09.705302724+02:00",
    "status": "Running",
    "status_code": 103,
    "resources": {
      "containers": [
        "/1.0/containers/apache-12345"
      ]
    },
    "metadata": null,
    "may_cancel": false,
    "err": ""
  }
}

But when I want to access the information of the operation I get a “not found” error like this

curl -s --unix-socket /var/lib/lxd/unix.socket -X GET a/1.0/operations/d6e9dc2b-9c00-47e3-8b6b-67a9839a83a5 | jq .
{
  "error": "not found",
  "error_code": 404,
  "type": "error"
}

I was hoping to get a status of the process to proceed with further steps in my script. What did I wrong?

Best regards!

Andy

Operations expire after 5 seconds, so you need to be pretty quick to hit it.

If you upgrade to LXD 2.17 or higher (2.18 would be recommended), then we have a new “lxc query” tool which will make that much easier for you.

stgraber@castiana:~$ lxc launch ubuntu:16.04 test
Creating test
Starting test                               
stgraber@castiana:~$ lxc snapshot test snap2

stgraber@castiana:~$ lxc query -X PUT -d '{"restore": "snap2"}' /1.0/containers/test --wait
{
	"class": "task",
	"created_at": "2017-10-08T13:47:00.050106943-04:00",
	"err": "",
	"id": "255d0af7-5561-48e8-b0f8-376dbe31fc72",
	"may_cancel": false,
	"metadata": null,
	"resources": {
		"containers": [
			"/1.0/containers/test"
		]
	},
	"status": "Success",
	"status_code": 200,
	"updated_at": "2017-10-08T13:47:00.050106943-04:00"
}
stgraber@castiana:~$ 

lxc query behaves like curl, but abstract the communication mechanism so you can run it against any remote that lxc is aware of. It’s also aware of LXD operations so when you pass --wait it will automatically track the operation for you and only return once the operation hits a final state.

Hi Stefane,

thanks for the reply! I will check to upgrade to 2.18.

Andy