Pylxd equivalent of '--target'

When using LXD clustering we can specify which host to create the instance with the --target option. I have been reading the documentation for pylxd regarding the same option but have been unable to find any relevant information.

I am using pylxd version 2.2.11.

@ack pylxd question for you :slight_smile:

hi @yang, you should be able to pass target=name to the create() call for containers/VM to specify a target node in the cluster.

Hi again, that solution did indeed work when creating a new instance.

I have been looking into the lxc move instance --target node3 command to change the location of an instance and the closest I have found on the wiki was the migrate option. However it seems to not allow moving from one host to another if the instance name does not change as it would already exist.

Following is the small script I used for testing:

from pylxd import Client

source_client = Client(endpoint='https://192.168.1.110:8443',verify=False)
destination_client = Client(endpoint='https://192.168.1.161:8443',verify=False)

container = source_client.instances.get('test')
container.migrate(destination_client, wait=True)

And the error output:

/usr/local/lib/python3.8/dist-packages/pylxd-2.2.11-py3.8.egg/pylxd/models/operation.py:76: UserWarning: Attempted to set unknown attribute "location" on instance of "Operation"
  warnings.warn(
Traceback (most recent call last):
  File "lxd-test.py", line 7, in <module>
    container.migrate(destination_client, wait=True)
  File "/usr/local/lib/python3.8/dist-packages/pylxd-2.2.11-py3.8.egg/pylxd/models/instance.py", line 531, in migrate
    res = getattr(new_client, self._endpoint).create(
  File "/usr/local/lib/python3.8/dist-packages/pylxd-2.2.11-py3.8.egg/pylxd/models/instance.py", line 276, in create
    response = client.api[cls._endpoint].post(
  File "/usr/local/lib/python3.8/dist-packages/pylxd-2.2.11-py3.8.egg/pylxd/client.py", line 177, in post
    self._assert_response(response, allowed_status_codes=(200, 201, 202))
  File "/usr/local/lib/python3.8/dist-packages/pylxd-2.2.11-py3.8.egg/pylxd/client.py", line 117, in _assert_response
    raise exceptions.LXDAPIException(response)
pylxd.exceptions.LXDAPIException: Failed creating instance record: Add instance info to the database: This instance already exists

The cluster and instance information is as follows:

$ lxc cluster list
+-------+----------------------------+----------+--------+-------------------+--------------+
| NAME  |            URL             | DATABASE | STATE  |      MESSAGE      | ARCHITECTURE |
+-------+----------------------------+----------+--------+-------------------+--------------+
| node1 | https://192.168.1.110:8443 | YES      | ONLINE | fully operational | x86_64       |
+-------+----------------------------+----------+--------+-------------------+--------------+
| node2 | https://192.168.1.161:8443 | NO       | ONLINE | fully operational | x86_64       |
+-------+----------------------------+----------+--------+-------------------+--------------+

$ lxc list
+-----------------+---------+----------------------+------+-----------+-----------+----------+
|      NAME       |  STATE  |         IPV4         | IPV6 |   TYPE    | SNAPSHOTS | LOCATION |
+-----------------+---------+----------------------+------+-----------+-----------+----------+
| test            | STOPPED |                      |      | CONTAINER | 0         | node1    |
+-----------------+---------+----------------------+------+-----------+-----------+----------+

I am guessing this is not the intended use case for the migrate function. Could you point me in the right direction to mimic the behavior of lxc move instance --target using pylxd?

I suspect the issue there is that you’re trying to migrate the instance as if source and dest were two different LXD servers, but they’re part of the same cluster, so the instance is “seen” with that name on both sides.

I’m not familiar with how migration works within a cluster, @stgraber could you help there?