Migration using Python and pylxd

Hello to everyone. I am trying to migrate my container from my HP laptop to my lenovo laptop.

Here is the code :

try to migrate a container ## (myport = 8443)

from pylxd import Client

lxd = Client()

ret_ex = lxd.containers.exists(‘container’)
if(ret_ex == True):
print(“Container exists”)
container = lxd.containers.get(‘container’)
#endif
“”"

migrate(new_client, live=False, wait=False)
Client(endpoint=None, version=‘1.0’, cert=None, verify=True, timeout=None)

“”"
print(“Attempt for migration starts…”)
lenovoLXD = Client(‘http://10.100.11.1:53’)

try:
ret_migrate=container.migrate(lenovoLXD,live=False,wait=True)
except ValueError:
print(“Exception raised…!!!”)
#end try-Exception

print(“After migration”)
print(ret_migrate).
I guess i an doing something wrong with the lenovoLXD object.
I tried also lenovoLXD = Client(‘http://10.100.11.1:8443’)

you should know (well, this forum intro should include) how to paste code on Discourse; use three (3) backquotes before and after pasting it. If you don’t do that, it’s not possible to test your code without painful reformatting.
Anyway posting code without posting any error messages don’t help your case either.

Be aware that Python Lxd bindings don’t seem to be so actively maintained since there are pending easy issues.

Sorry for any mistakes . I am kinda new here .

Traceback (most recent call last):
  File "/home/tkasidakis/.local/lib/python3.6/site-packages/urllib3/connection.py", line 160, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File "/home/tkasidakis/.local/lib/python3.6/site-packages/urllib3/util/connection.py", line 84, in create_connection
    raise err
  File "/home/tkasidakis/.local/lib/python3.6/site-packages/urllib3/util/connection.py", line 74, in create_connection
    sock.connect(sa)
TimeoutError: [Errno 110] Connection timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/tkasidakis/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 677, in urlopen
    chunked=chunked,
  File "/home/tkasidakis/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 392, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib/python3.6/http/client.py", line 1264, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.6/http/client.py", line 1310, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.6/http/client.py", line 1259, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.6/http/client.py", line 1038, in _send_output
    self.send(msg)
  File "/usr/lib/python3.6/http/client.py", line 976, in send
    self.connect()
  File "/home/tkasidakis/.local/lib/python3.6/site-packages/urllib3/connection.py", line 187, in connect
    conn = self._new_conn()
  File "/home/tkasidakis/.local/lib/python3.6/site-packages/urllib3/connection.py", line 172, in _new_conn
    self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7ff40c79a358>: Failed to establish a new connection: [Errno 110] Connection timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/tkasidakis/.local/lib/python3.6/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/home/tkasidakis/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 725, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "/home/tkasidakis/.local/lib/python3.6/site-packages/urllib3/util/retry.py", line 439, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='10.100.11.1', port=8443): Max retries exceeded with url: /1.0 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7ff40c79a358>: Failed to establish a new connection: [Errno 110] Connection timed out',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/tkasidakis/.local/lib/python3.6/site-packages/pylxd/client.py", line 308, in __init__
    response = self.api.get()
  File "/home/tkasidakis/.local/lib/python3.6/site-packages/pylxd/client.py", line 158, in get
    response = self.session.get(self._api_endpoint, *args, **kwargs)
  File "/home/tkasidakis/.local/lib/python3.6/site-packages/requests/sessions.py", line 543, in get
    return self.request('GET', url, **kwargs)
  File "/home/tkasidakis/.local/lib/python3.6/site-packages/requests/sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/tkasidakis/.local/lib/python3.6/site-packages/requests/sessions.py", line 643, in send
    r = adapter.send(request, **kwargs)
  File "/home/tkasidakis/.local/lib/python3.6/site-packages/requests/adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='10.100.11.1', port=8443): Max retries exceeded with url: /1.0 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7ff40c79a358>: Failed to establish a new connection: [Errno 110] Connection timed out',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "mig.py", line 19, in <module>
    lenovoLXD = Client('http://10.100.11.1:8443')
  File "/home/tkasidakis/.local/lib/python3.6/site-packages/pylxd/client.py", line 315, in __init__
    raise exceptions.ClientConnectionFailed()
pylxd.exceptions.ClientConnectionFailed

This is the error message

Bad formatting on this again use ``` ```` arround code I.E

Ive escape the backticks so you can see them this wont happen when you post

```
test
```

Never used the python library but it would appear LXD cant reach the other instance of LXD is either machine running a firewall and can both hosts access each other on port 8443 ?

10.100.11.1 also looks like a “LXD IP address” is this routable over your network ? (I.E can you ping it from your laptop)

When creating the lenovoLXD object i pass as the first argument the ip address of the lxd deamon which is running in my lenovo laptop . I am running the python script to my hp laptop. The 10.100.11.1 is what appears at lxdbr0 when typing ifconfig at my lenovo terminal .
Is this the correct ip ?
Also when i was executing lxd init and answered yes to the question if i want lxd to be available over the network , i put both lxd at lenovo and lxd at hp the default port 8443. Is this a problem ?
Thanks in advance for the answers and again my apologies for any bad formating.

Dont think so, you want to use your host IP address, probably something like 192.168.X.X

I did it and i received the same error message immediately. I guess i am ok with the port. Should i be worried for any other arguments in the Client constructor ?

Blockquote
migrate - Migrate the container. The first argument is a client connection to the destination server. This call is asynchronous, so wait=True is optional. The container on the new client is returned. If live=True is passed to the function call, then the container is live migrated (see the LXD documentation for further details).
API documentation — pylxd documentation

I am confused when using move command from the command line.
Let’s say we have a container with name cont1 in the host with ip 192.168.2.5 and we want to migrate it to 192.168.2.10 ?
How can do this ?

Read the docs (there is an example for migrate)

https://pylxd.readthedocs.io/en/latest/containers.html#container-methods

Literally straight from the docs :roll_eyes:

from pylxd import Client

client_source=Client(endpoint='https://192.168.1.104:8443',cert=('lxd.crt','lxd.key'),verify=False)
client_destination=Client(endpoint='https://192.168.1.106:8443',cert=('lxd.crt','lxd.key'),verify=False)
cont = client_source.containers.get('testm')
cont.migrate(client_destination,wait=True)
1 Like

Ok. Thanks a lot for the help . Searching around and having testst for hours had as result missing the obvious example. Im sure it has happened to all of us . Thanks again

Yeah sorry - I dont know why is was being moody there (I hadn’t eaten), literally happened to me last week :laughing:

Migration starts but i got the follownig exception

ret_migrate=container.migrate(lenovoLXD,live=False,wait=True)
  File "/home/tkasidakis/.local/lib/python3.6/site-packages/pylxd/models/container.py", line 528, in migrate
    self.generate_migration_data(live), wait=wait)
  File "/home/tkasidakis/.local/lib/python3.6/site-packages/pylxd/models/container.py", line 276, in create
    response = client.api.containers.post(json=config, target=target)
  File "/home/tkasidakis/.local/lib/python3.6/site-packages/pylxd/client.py", line 177, in post
    self._assert_response(response, allowed_status_codes=(200, 201, 202))
  File "/home/tkasidakis/.local/lib/python3.6/site-packages/pylxd/client.py", line 117, in _assert_response
    raise exceptions.LXDAPIException(response)
pylxd.exceptions.LXDAPIException: Can't find a storage pool for the container to use

Any help ?

This looks like the issue. One of the two LXD server has not been configured. You can either do this interactively with sudo lxd init, or programmatically (i.e. API) by doing the tasks that lxd init does for you. The part that is missing, is the storage pool. One of the LXDs does not show any output at lxc storage list.

I have edited your earlier posts with those triples of backticks. Click on Edit on them to how it is done.
Also, see this guide on post formatting, How to best ask questions on this discussion forum

Thanks a lot simos for the help and the guide for making appropriate questions to this forum . I know that should be the first thing to do before posting. The problem is that i did sudo lxd inti and set the storage pool. It seems that default storage pool is used by any container and my container uses the storage pool with name pool which i created during lxd init . The default already exists . I guess i have making problems myself during installing and reinstalling lxd , deleting network bridges etc. I am also confused with snap and the folder where lxd is installed. Anyway , thanks for the help . I will try to start from the beginning and if i still have problems , i will post here ( with the appropriate syntax :slight_smile:)

I finally managed to do the migration :slight_smile:
One problem was that i had more than one storage pool.
The other thing was the difference between the OSs of my 2 machines (32bit and 64bit).
I installed also criu.
Also it is necessary when you write your python script to authenticate with the right password, both your local lxd deamon and the remote one.
Anyone who has/had the same problem, feel free to write here or message me.:slight_smile:

Many thanks to simos for his patience and guidance towards me :slight_smile: