2022-11-24 18:06:41 +00:00
|
|
|
import time
|
2022-11-23 18:55:06 +00:00
|
|
|
import pylxd
|
2022-11-24 18:06:41 +00:00
|
|
|
import ipaddress
|
2022-11-23 18:55:06 +00:00
|
|
|
|
|
|
|
lxd_client = pylxd.client.Client()
|
|
|
|
|
|
|
|
|
|
|
|
def create_instance(container_name: str):
|
|
|
|
config = {'name': container_name, 'source':
|
|
|
|
{'type': 'image', "mode": "pull", "server": "https://cloud-images.ubuntu.com/daily", "protocol": "simplestreams",
|
2022-11-24 18:06:41 +00:00
|
|
|
'alias': 'lts/amd64'}, 'config': {'security.nesting': 'true'}}
|
2022-11-23 18:55:06 +00:00
|
|
|
|
|
|
|
instance = lxd_client.instances.create(config, wait=True)
|
2022-11-24 18:06:41 +00:00
|
|
|
instance.start(wait=True)
|
2022-11-23 18:55:06 +00:00
|
|
|
|
2022-11-24 18:06:41 +00:00
|
|
|
while type(ipaddress.ip_address(instance.state().network['eth0']['addresses'][0]['address'])) != ipaddress.IPv4Address:
|
|
|
|
time.sleep(0.1)
|
|
|
|
return instance.state().network['eth0']['addresses'][0]
|
2022-11-23 18:55:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
def destroy_instance(container_name: str):
|
|
|
|
instance = lxd_client.instances.get(container_name)
|
|
|
|
instance.stop(wait=True)
|
|
|
|
instance.delete(wait=True)
|
|
|
|
|
|
|
|
return True
|