bsdci / libioc

A Python library to manage jails with ioc{age,ell}
https://bsd.ci/libioc
Other
38 stars 11 forks source link

can't create jail system via ioc.Jail.create #599

Closed himrock922 closed 5 years ago

himrock922 commented 5 years ago

Hi all, I developing of jail hosting system via Django. the ioc.Jail.create function couldn't create jail system.

Reference: https://bsdci.github.io/handbook/library-essentials/jail/

Jail Creation
When creating a Jail the new argument disables the internal checks for existence of the jail data structure. Instead it is created in the next step from a fetched Release.

release = iocage.Release("11.2-RELEASE")
jail = iocage.Jail(data(id="myjail"), new=True)
jail.create(release)

Sample Code:

def create(request):
    try:
        response = json.loads(request.body)
        release = ioc.Release(response['release'])
        jail = ioc.Jail(data=dict(id=response['jail_name']), new=True)
        jail.create(release)
    except(ioc.errors.JailAlreadyExists):
        return HttpResponse('%s is already exists' % response['jail_name'], status=500)
    return HttpResponse('OK')
packages/django/core/handlers/base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/home/himrock922/Development/django/jail_hosting_system/jails/views.py", line 66, in create
    jail = ioc.Jail(data=dict(id=response['jail_name']), new=True)
  File "/usr/home/himrock922/Development/jail_hosting_system/lib/python3.7/site-packages/ioc/__init__.py", line 36, in __call__
    return self.main_module(*args, **kwargs)
  File "/usr/home/himrock922/Development/jail_hosting_system/lib/python3.7/site-packages/ioc/Jail.py", line 329, in __init__
    data["id"] = self._resolve_name(data["id"])
  File "/usr/home/himrock922/Development/jail_hosting_system/lib/python3.7/site-packages/ioc/Jail.py", line 2106, in _resolve_name
    raise ioc.errors.JailNotFound(text, logger=self.logger)
ioc.errors.JailNotFound: test

After I did read above log, I think the below code happened problem.

https://github.com/bsdci/libioc/blob/master/ioc/Jail.py#L329

        if "id" in data.keys():
            data["id"] = self._resolve_name(data["id"])

Before new jail system create, the code resolves jail_name from file system of existing jail...?

Make sure to follow and check these boxes before submitting an issue! Thank you.

gronke commented 5 years ago

A jail identified by id always assumes it to be existing. When creating it from a name, it is attempted to claim that id during creation.

import ioc
release = ioc.Release("11.2-RELEASE")
jail = ioc.Jail(dict(name="myjail"), new=True)
jail.create(release) 
- jail = ioc.Jail(data=dict(id="myjail"), new=True)
+ jail = ioc.Jail(data=dict(name="myjail"), new=True)
gronke commented 5 years ago

The Handbook is wrong, I will update it accordingly. Thanks for reporting the mistake!