context.py 3.34 KB
Newer Older
Guba Sándor committed
1 2 3
""" This is the defautl context file. It replaces the Context class
    to the platform specific one.
"""
Guba Sándor committed
4
import platform
Guba Sándor committed
5 6


Guba Sándor committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
def _get_virtio_device():
    path = None
    GUID = '{6FDE7521-1B65-48ae-B628-80BE62016026}'
    from infi.devicemanager import DeviceManager
    dm = DeviceManager()
    dm.root.rescan()
    # Search Virtio-Serial by name TODO: search by class_guid
    for i in dm.all_devices:
        if i.has_property("description"):
            if "virtio-serial".upper() in i.description.upper():
                path = ("\\\\?\\" +
                        i.children[0].instance_id.lower().replace('\\', '#') +
                        "#" + GUID.lower()
                        )
    return path
Guba Sándor committed
22 23


Guba Sándor committed
24 25 26
def get_context():
    system = platform.system()
    if system == "Windows":
Guba Sándor committed
27
        from windows._win32context import Context
Guba Sándor committed
28 29
    elif system == "Linux":
        from linux._linuxcontext import Context
30
    elif system == "FreeBSD":
Bach Dániel committed
31
        from freebsd._freebsdcontext import Context
Guba Sándor committed
32 33 34
    else:
        raise NotImplementedError("Platform %s is not supported.", system)
    return Context
Guba Sándor committed
35 36


Guba Sándor committed
37 38 39 40 41
def get_serial():
    system = platform.system()
    port = None
    if system == 'Windows':
        port = _get_virtio_device()
42 43
        import pythoncom
        pythoncom.CoInitialize()
Guba Sándor committed
44
        if port:
Guba Sándor committed
45
            from windows.win32virtio import SerialPort
Guba Sándor committed
46
        else:
Bach Dániel committed
47
            from twisted.internet.serialport import SerialPort
Guba Sándor committed
48 49 50
            port = r'\\.\COM1'
    elif system == "Linux":
        port = "/dev/virtio-ports/agent"
Bach Dániel committed
51 52 53
        try:
            open(port, 'rw').close()
        except (OSError, IOError):
Bach Dániel committed
54
            from twisted.internet.serialport import SerialPort
Guba Sándor committed
55
            port = '/dev/ttyS0'
Bach Dániel committed
56 57
        else:
            from linux.posixvirtio import SerialPort
58 59 60 61 62 63 64 65 66
    elif system == "FreeBSD":
        port = "/dev/ttyV0.1"
        try:
            open(port, 'rw').close()
        except (OSError, IOError):
            from twisted.internet.serialport import SerialPort
            port = '/dev/ttyu0'
        else:
            from freebsd.posixvirtio import SerialPort
Guba Sándor committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    else:
        raise NotImplementedError("Platform %s is not supported.", system)
    return (SerialPort, port)


class BaseContext(object):
    @staticmethod
    def change_password(password):
        pass

    @staticmethod
    def restart_networking():
        pass

    @staticmethod
    def change_ip(interfaces, dns):
        pass

    @staticmethod
    def set_time(time):
        pass

    @staticmethod
    def set_hostname(hostname):
        pass

    @staticmethod
    def mount_store(host, username, password):
        pass

    @staticmethod
    def get_keys():
        pass

    @staticmethod
    def add_keys(keys):
        pass

    @staticmethod
    def del_keys(keys):
        pass

    @staticmethod
    def cleanup():
        pass

    @staticmethod
    def start_access_server():
        pass

    @staticmethod
    def append(data, filename, chunk_number, uuid):
        pass

    @staticmethod
    def update(filename, executable, checksum, uuid):
        pass

    @staticmethod
    def ipaddresses():
        pass

    @staticmethod
    def get_agent_version():
        try:
            with open('version.txt') as f:
                return f.readline()
        except IOError:
            return None

    @staticmethod
    def send_expiration(url):
        import notify
        notify.notify(url)