Esempio n. 1
0
def Ubuntu(host, force=False):
    unix.isvalid(host)

    root = host.__dict__.get('root', None)

    instances = unix.instances(host)
    if len(instances) >= 1:
        host = Linux(getattr(unix, instances[0]).clone(host))
    if root:
        host = Chroot(host, root)
    host = Debian(host)

    if host.distrib[0] != 'Ubuntu' and not force:
        raise LinuxError('invalid distrib')

    class UbuntuHost(host.__class__):
        def __init__(self):
            #            kwargs = {'root': root} if root else {}
            host.__class__.__init__(self)
            self.__dict__.update(host.__dict__)

        @property
        def services(self):
            version = float(self.distrib[1])
            if version <= 9.04:
                service_handler = Initd
            elif 9.10 <= version <= 14.10:
                service_handler = Upstart
            elif version >= 15.04:
                servivce_handler = Systemd
            return service_handler(weakref.ref(self)())

    return UbuntuHost()
Esempio n. 2
0
def Arch(host, force=False):
    unix.isvalid(host)

    root = host.__dict__.get('root', None)

    instances = unix.instances(host)
    if len(instances) >= 1:
        host = Linux(getattr(unix, instances[0]).clone(host))
    if root:
        host = Chroot(host, root)

    if host.distrib[0] != 'Arch' and not force:
        raise LinuxError('invalid distrib')

    class ArchHost(host.__class__):
        def __init__(self):
            kwargs = {'root': root} if root else {}
            host.__class__.__init__(self, **kwargs)
            self.__dict__.update(host.__dict__)

        @property
        def hostname(self):
            with self.open(_HOSTNAMEFILE) as fhandler:
                return fhandler.read().decode().strip()

        @hostname.setter
        def hostname(self, value):
            with self.open(_HOSTNAMEFILE, 'w') as fhandler:
                fhandler.write(value)

        @property
        def services(self):
            return Systemd(weakref.ref(self)())

    return ArchHost()
Esempio n. 3
0
def Ubuntu(host, force=False):
    unix.isvalid(host)

    root = host.__dict__.get('root', None)

    instances = unix.instances(host)
    if len(instances) >= 1:
        host = Linux(getattr(unix, instances[0]).clone(host))
    if root:
        host = Chroot(host, root)
    host = Debian(host)

    if host.distrib[0] != 'Ubuntu' and not force:
        raise LinuxError('invalid distrib')

    class UbuntuHost(host.__class__):
        def __init__(self):
#            kwargs = {'root': root} if root else {}
            host.__class__.__init__(self)
            self.__dict__.update(host.__dict__)

        @property
        def services(self):
            version = float(self.distrib[1])
            if version <= 9.04:
                service_handler = Initd
            elif 9.10 <= version <= 14.10:
                service_handler = Upstart
            elif version >= 15.04:
                servivce_handler = Systemd
            return service_handler(weakref.ref(self)())

    return UbuntuHost()
Esempio n. 4
0
def Debian(host, force=False):
    unix.isvalid(host)

    root = host.__dict__.get('root', None)

    instances = unix.instances(host)
    if len(instances) >= 1:
        host = Linux(getattr(unix, instances[0]).clone(host))
    if root:
        host = Chroot(host, root)

    if host.distrib[0] not in DISTRIBS and not force:
        raise LinuxError('invalid distrib')

    class DebianHost(host.__class__):
        def __init__(self):
            kwargs = {'root': root} if root else {}
            host.__class__.__init__(self, **kwargs)
            self.__dict__.update(host.__dict__)

        def list_packages(self):
            return self.execute('dpkg -l')

        @property
        def hostname(self):
            with self.open(_HOSTNAMEFILE) as fhandler:
                return fhandler.read().decode().strip()

        @hostname.setter
        def hostname(self, value):
            with self.open(_HOSTNAMEFILE, 'w') as fhandler:
                fhandler.write(value)

        @property
        def network(self):
            return _Network(weakref.ref(self)())

        @property
        def apt(self):
            return _APT(weakref.ref(self)())

        @property
        def services(self):
            major_version = int(self.distrib[1][0])
            if major_version <= 5:
                service_handler = Initd
            elif 6 <= major_version <= 7:
                service_handler = Upstart
            elif major_version >= 8:
                service_handler = Systemd
            return service_handler(weakref.ref(self)())

    return DebianHost()
Esempio n. 5
0
def Debian(host, force=False):
    unix.isvalid(host)

    root = host.__dict__.get('root', None)

    instances = unix.instances(host)
    if len(instances) >= 1:
        host = Linux(getattr(unix, instances[0]).clone(host))
    if root:
        host = Chroot(host, root)

    if host.distrib[0] not in DISTRIBS and not force:
        raise LinuxError('invalid distrib')

    class DebianHost(host.__class__):
        def __init__(self):
            kwargs = {'root': root} if root else {}
            host.__class__.__init__(self, **kwargs)
            self.__dict__.update(host.__dict__)

        def list_packages(self):
            return self.execute('dpkg -l')

        @property
        def hostname(self):
            with self.open(_HOSTNAMEFILE) as fhandler:
                return fhandler.read().decode().strip()

        @hostname.setter
        def hostname(self, value):
            with self.open(_HOSTNAMEFILE, 'w') as fhandler:
                fhandler.write(value)

        @property
        def network(self):
            return _Network(weakref.ref(self)())

        @property
        def apt(self):
            return _APT(weakref.ref(self)())

        @property
        def services(self):
            major_version = int(self.distrib[1][0])
            if major_version <= 5:
                service_handler = Initd
            elif 6 <= major_version <= 7:
                service_handler = Upstart
            elif major_version >= 8:
                service_handler = Systemd
            return service_handler(weakref.ref(self)())

    return DebianHost()
Esempio n. 6
0
def RedHat(host, force=False):
    unix.isvalid(host)

    root = host.__dict__.get('root', None)

    instances = unix.instances(host)
    if len(instances) >= 1:
        host = Linux(getattr(unix, instances[0]).clone(host))
    if root:
        host = Chroot(host, root)

    if host.distrib[0] not in DISTRIBS and not force:
        raise LinuxError('invalid distrib')

    class RedHatHost(host.__class__):
        def __init__(self):
            kwargs = {'root': root} if root else {}
            host.__class__.__init__(self, **kwargs)
            self.__dict__.update(host.__dict__)

        def list_packages(self):
            return self.execute('dpkg -l')

        @property
        def hostname(self):
            with self.open(_NETFILE) as fhandler:
                for line in fhandler.read().splitlines():
                    attr, value = line.split('=')
                    if attr == 'HOSTNAME':
                        return value

        @hostname.setter
        def hostname(self, value):
            contnet = ''
            with self.open(_NETFILE) as fhandler:
                content = re.sub('HOSTNAME=[^\n]*',
                                 'HOSTNAME=%s\n' % value,
                                 fhandler.read())
            with self.open(_NETFILE, 'w') as fhandler:
                fhandler.write(content)

        @property
        def services(self):
            major_version = int(self.distrib[1][0])
            if major_version <= 5:
                service_handler = Initd
            elif major_version == 6:
                service_handler = Upstart
            elif major_version >= 7:
                service_handler = Systemd
            return service_handler(weakref.ref(self)())

    return RedHatHost()
Esempio n. 7
0
def RedHat(host, force=False):
    unix.isvalid(host)

    root = host.__dict__.get('root', None)

    instances = unix.instances(host)
    if len(instances) >= 1:
        host = Linux(getattr(unix, instances[0]).clone(host))
    if root:
        host = Chroot(host, root)

    if host.distrib[0] not in DISTRIBS and not force:
        raise LinuxError('invalid distrib')

    class RedHatHost(host.__class__):
        def __init__(self):
            kwargs = {'root': root} if root else {}
            host.__class__.__init__(self, **kwargs)
            self.__dict__.update(host.__dict__)

        def list_packages(self):
            return self.execute('dpkg -l')

        @property
        def hostname(self):
            with self.open(_NETFILE) as fhandler:
                for line in fhandler.read().splitlines():
                    attr, value = line.split('=')
                    if attr == 'HOSTNAME':
                        return value

        @hostname.setter
        def hostname(self, value):
            contnet = ''
            with self.open(_NETFILE) as fhandler:
                content = re.sub('HOSTNAME=[^\n]*', 'HOSTNAME=%s\n' % value,
                                 fhandler.read())
            with self.open(_NETFILE, 'w') as fhandler:
                fhandler.write(content)

        @property
        def services(self):
            major_version = int(self.distrib[1][0])
            if major_version <= 5:
                service_handler = Initd
            elif major_version == 6:
                service_handler = Upstart
            elif major_version >= 7:
                service_handler = Systemd
            return service_handler(weakref.ref(self)())

    return RedHatHost()
Esempio n. 8
0
def Linux(host):
    unix.isvalid(host)
    host.is_connected()

    instances = unix.instances(host)
    if len(instances) > 1:
        host = getattr(unix, instances[0]).clone(host)

    host_type = host.type
    if host_type != 'linux':
        raise LinuxError('this is not a Linux host (%s)' % host_type)

    class LinuxHost(host.__class__):
        def __init__(self):
            host.__class__.__init__(self)
            self.__dict__.update(host.__dict__)

        @property
        def distrib(self):
            return distribution(self)

        @property
        def chrooted(self):
            return False

        @property
        def conf(self):
            return _Conf(weakref.ref(self)())

        @property
        def memory(self):
            return _Memory(weakref.ref(self)())

        def stat(self, filepath):
            return _Stat(weakref.ref(self)(), filepath)

        @property
        def modules(self):
            return _Modules(weakref.ref(self)())

        @property
        def sysctl(self):
            return _Sysctl(weakref.ref(self)())

        @property
        def fstab(self):
            return _Fstab(weakref.ref(self)())

    return LinuxHost()
Esempio n. 9
0
def Linux(host):
    unix.isvalid(host)
    host.is_connected()

    instances = unix.instances(host)
    if len(instances) > 1:
        host = getattr(unix, instances[0]).clone(host)

    host_type = host.type
    if host_type != 'linux':
        raise LinuxError('this is not a Linux host (%s)' % host_type)

    class LinuxHost(host.__class__):
        def __init__(self):
            host.__class__.__init__(self)
            self.__dict__.update(host.__dict__)

        @property
        def distrib(self):
            return distribution(self)

        @property
        def chrooted(self):
            return False

        @property
        def conf(self):
            return _Conf(weakref.ref(self)())

        @property
        def memory(self):
            return _Memory(weakref.ref(self)())

        def stat(self, filepath):
            return _Stat(weakref.ref(self)(), filepath)

        @property
        def modules(self):
            return _Modules(weakref.ref(self)())

        @property
        def sysctl(self):
            return _Sysctl(weakref.ref(self)())

        @property
        def fstab(self):
            return _Fstab(weakref.ref(self)())

    return LinuxHost()
Esempio n. 10
0
def CentOS(host, root='', force=False):
    unix.isvalid(host)

    root = host.__dict__.get('root', None)

    instances = unix.instances(host)
    if len(instances) >= 1:
        host = Linux(getattr(unix, instances[0]).clone(host))
    if root:
        host = Chroot(host, root)
    host = RedHat(host)

    if host.distrib[0] != 'CentOS' and not force:
        raise LinuxError('invalid distrib')

    class CentOSHost(host.__class__):
        def __init__(self):
            host.__class__.__init__(self)
            self.__dict__.update(host.__dict__)

    return CentOSHost()
Esempio n. 11
0
def Chroot(host, root):
    unix.isvalid(host)
    host.is_connected()

    if root and host.username != 'root':
        raise ChrootError('you need to be root for chroot')

    instances = unix.instances(host)
    if len(instances) > 1:
        host = getattr(unix, instances[0]).clone(host)
    host = Linux(host)

    class ChrootHost(host.__class__):
        def __init__(self, root):
            host.__class__.__init__(self)
            self.__dict__.update(host.__dict__)
            self.root = root

        @property
        def chrooted(self):
            return True

        def execute(self, cmd, *args, **kwargs):
            if self.root:
                cmd = 'chroot %s %s' % (self.root, cmd)
            result = host.execute(cmd, *args, **kwargs)
            # Set return code of the parent. If not set some functions (like
            # Path) does not work correctly on chrooted objects.
            self.return_code = host.return_code
            return result

        def open(self, filepath, mode='r'):
            if self.root:
                filepath = filepath[1:] if filepath.startswith('/') else filepath
                filepath = os.path.join(self.root, filepath)
            return host.open(filepath, mode)

        @contextmanager
        def set_controls(self, **controls):
            cur_controls = dict(host.controls)

            try:
                for control, value in controls.items():
                    host.set_control(control, value)
                yield None
            finally:
                for control, value in cur_controls.items():
                    host.set_control(control, value)

        def chroot(self):
            for (fs, opts) in _FILESYSTEMS:
                mount_point = os.path.join(root, fs[1:] if fs.startswith('/') else fs)
                status, _, stderr = host.mount(fs, mount_point, **opts)
                if not status:
                    raise ChrootError("unable to mount '%s': %s" % (fs, stderr))

        def unchroot(self):
            for (fs, _) in _FILESYSTEMS:
                mount_point = os.path.join(root, fs[1:] if fs.startswith('/') else fs)
                status, _, stderr = host.umount(mount_point)
                if not status:
                    raise ChrootError("unable to umount '%s': %s" % (fs, stderr))


    return ChrootHost(root)
Esempio n. 12
0
def Chroot(host, root):
    unix.isvalid(host)
    host.is_connected()

    if root and host.username != 'root':
        raise ChrootError('you need to be root for chroot')

    instances = unix.instances(host)
    if len(instances) > 1:
        host = getattr(unix, instances[0]).clone(host)
    host = Linux(host)

    class ChrootHost(host.__class__):
        def __init__(self, root):
            host.__class__.__init__(self)
            self.__dict__.update(host.__dict__)
            self.root = root

        @property
        def chrooted(self):
            return True

        def execute(self, cmd, *args, **kwargs):
            if self.root:
                cmd = 'chroot %s %s' % (self.root, cmd)
            result = host.execute(cmd, *args, **kwargs)
            # Set return code of the parent. If not set some functions (like
            # Path) does not work correctly on chrooted objects.
            self.return_code = host.return_code
            return result

        def open(self, filepath, mode='r'):
            if self.root:
                filepath = filepath[1:] if filepath.startswith('/') else filepath
                filepath = os.path.join(self.root, filepath)
            return host.open(filepath, mode)

        @contextmanager
        def set_controls(self, **controls):
            cur_controls = dict(host.controls)

            try:
                for control, value in controls.items():
                    host.set_control(control, value)
                yield None
            finally:
                for control, value in cur_controls.items():
                    host.set_control(control, value)

        def chroot(self):
            for (fs, opts) in _FILESYSTEMS:
                mount_point = os.path.join(root, fs[1:] if fs.startswith('/') else fs)
                status, _, stderr = host.mount(fs, mount_point, **opts)
                if not status:
                    raise ChrootError("unable to mount '%s': %s" % (fs, stderr))

        def unchroot(self):
            for (fs, _) in _FILESYSTEMS:
                mount_point = os.path.join(root, fs[1:] if fs.startswith('/') else fs)
                status, _, stderr = host.umount(mount_point)
                if not status:
                    raise ChrootError("unable to umount '%s': %s" % (fs, stderr))


    return ChrootHost(root)