Pylxd, create a container

At the moment I’m doing

def launch( image, hostname, four, six, fourmask, sixmask, fourgate, sixgate , other):
success = False
cmdstr = f’lxc launch {image} ’
f’-c environment.IPv4={four} ’
f’-c environment.IPv6={six} ’
f’-c environment.IPv4netmast={fourmask} ’
f’-c environment.IPv6netmask={sixmask} ’
f’-c environment.IPv4gateway={fourgate} ’
f’-c environment.IPv6gateway={sixgate}’
cmdstr = ’ ‘.join([cmdstr, *other, hostname.split(’.’)[0]])
lxp = subprocess.run(cmdstr.split(’ ') , stdout=subprocess.PIPE,stderr=subprocess.PIPE)

which I’m thoroughly ashamed of. What would the equivalent look like if I wanted to do the same with pylxd, containers = pylxd.Client().create()? The statement “The config itself is beyond the scope of this documentation. Please refer to the LXD documentation for more information.” doesn’t really help me understand how to put the config together.

It’s worthwhile noting that *other contains arguments that my python script doesn’t know/care about, and passes through to lxc, e.g. names of profiles I’d like to utilise (we have profiles for networks, disk-types/-sizes, memory-size and CPU count).

Cheers,
Tink

Hello
code says

response = client.api.containers.post(json=config, target=target)

so param is a json formatted string :slight_smile:

lxc --debug is your friend

so getting all together

>>> import pylxd
>>> cli=pylxd.client.Client()
>>> config="""
...     {
...             "architecture": "",
...             "config": {},
...             "devices": {},
...             "ephemeral": false,
...             "profiles": null,
...             "stateful": false,
...             "description": "",
...             "name": "test1",
...             "source": {
...                     "type": "image",
...                     "certificate": "",
...                     "fingerprint": "35f6bff57c250208c6dc73445eefe8c1020853a1bc8571ebd8bfb1fe3a239504"
...             },
...             "instance_type": ""
...     }
... """
>>> import json
>>> jconfig=json.loads(config)
>>> cli.containers.create(jconfig, wait=True)
<pylxd.models.container.Container object at 0x7ff8698fcee8>
>>> 

please don’t comment about the python-icity of it, I have no involvement in pylxd :slight_smile: If you are unhappy about it, I’m sure they are waiting for your patches.

1 Like

Hi gpatel-fr :slight_smile:
Thank you for responding!

Heh. Yeah … I was hoping on some guidance as to what the bare necessities of this were :slight_smile:

I’ll bare that in mind - thanks for the pointer!

GRIN. I’m not a python guru, I’m a sysadmin hoping to make my life (and that of my colleagues) a little easier by a bit of scripting; in questions of style and pythonicity I always rely on our inhouse development team to beat my stuff into shape with comments. Thanks again - will play with this on Monday.

Cheers,
Tink