canonical / pylxd

Python module for LXD
https://pylxd.readthedocs.io/en/latest/
Apache License 2.0
251 stars 133 forks source link

migration: "instances" entry already exists #513

Open nihr43 opened 2 years ago

nihr43 commented 2 years ago

Hi; unsure if this is expected behavior or something that slipped through the integration tests.

Im trying to migrate instances within a cluster, precisely akin to lxc mv "$instance" --target="$target_node". Im getting:

...
  File "/home/nhensel/.local/lib/python3.10/site-packages/pylxd/models/instance.py", line 601, in migrate
    res = getattr(new_client, self._endpoint).create(
  File "/home/nhensel/.local/lib/python3.10/site-packages/pylxd/models/instance.py", line 323, in create
    response = client.api[cls._endpoint].post(json=config, target=target)
  File "/home/nhensel/.local/lib/python3.10/site-packages/pylxd/client.py", line 216, in post
    self._assert_response(response, allowed_status_codes=(200, 201, 202))
  File "/home/nhensel/.local/lib/python3.10/site-packages/pylxd/client.py", line 145, in _assert_response
    raise exceptions.LXDAPIException(response)
pylxd.exceptions.LXDAPIException: Failed creating instance record: Add instance info to the database: This "instances" entry already exists

I'm reminded of how the lxc client creates a temporary instance like 'mv-uuid...' during a migration; this behavior must have already been worked around over there.

I see how this could have been missed; i don't see any sort of ad-hoc cluster construction ( like in lxd proper ) going on in the integration tests here. I see migration/run_migration_integration_tests-18-04 but it doesnt appear to be consumed by anything.

So my questions are.. Is this expected behavior? Am I the consumer expected to work around this in my own code? Or, if this is not expected behavior, what input would you have on an integration test and fix for this? Im interested in taking a stab at it.. I figure migration/run_migration_integration_tests-18-04 needs taught to build a cluster of two, and migrate() needs taught to juggle instance names like lxc does.

Or perhaps im missing something? Any better ideas? Thanks.

nihr43 commented 2 years ago

oh, looks like lxd itself does know how to handle intra-cluster migrations correctly, pylxd just needs to know how to do it:

heres what lxc does: https://github.com/lxc/lxd/blob/master/lxc/move.go#L258

heres what lxd knows: https://github.com/lxc/lxd/blob/master/lxd/instance_post.go#L102

meanwhile pylxd is just doing a naive create(): https://github.com/lxc/pylxd/blob/master/pylxd/models/instance.py#L592