Beispiel #1
0
Datei: sc.py Projekt: oh8/inpanel
 def _read_fstab(self, line, **params):
     if not line or line.startswith('#'):
         return
     fields = line.split()
     dev = fields[0]
     config = {
         'dev':    fields[0],
         'mount':  fields[1],
         'fstype': fields[2],
     }
     if dev.startswith('/dev/'):
         try:
             devlink = os.readlink(dev)
             dev = os.path.abspath(os.path.join(
                 os.path.dirname(devlink), dev))
         except:
             pass
         dev = dev.replace('/dev/', '')
         if dev == params['devname']:
             return config
     elif dev.startswith('UUID='):
         uuid = dev.replace('UUID=', '')
         partinfo = ServerInfo.partinfo(devname=params['devname'])
         # partinfo = ServerInfo.partinfo(uuid=uuid, devname=params['devname'])
         if partinfo['uuid'] == uuid:
             return config
Beispiel #2
0
Datei: sc.py Projekt: oh8/inpanel
    def timezone(self, inifile, timezone=None):
        """Get or set system timezone.

        Pass None to parameter config (as default) to get timezone,
        or pass timezone full name like 'Asia/Shanghai' to set timezone.
        """
        tzpath = '/etc/localtime'
        zonepath = '/usr/share/zoneinfo'

        config = Config(inifile)
        if not config.has_section('time'):
            config.add_section('time')

        if timezone == None:
            # firstly read from config file
            timezone = ''
            if config.has_option('time', 'timezone'):
                timezone = config.get('time', 'timezone')
            if timezone:
                return timezone

            # or else check the system config file
            dist = ServerInfo.dist()
            if dist['name'] in ('centos', 'redhat'):
                clockinfo = raw_loadconfig('/etc/sysconfig/clock')
                if clockinfo and 'ZONE' in clockinfo:
                    timezone = clockinfo['ZONE']
                    return timezone
            else:
                pass

            # or else find the file match /etc/localtime
            with open(tzpath) as f:
                tzdata = f.read()
            regions = ServerSet.timezone_regions()
            for region in regions:
                regionpath = os.path.join(zonepath, region)
                for zonefile in os.listdir(regionpath):
                    if not os.path.isfile(os.path.join(regionpath, zonefile)):
                        continue
                    with open(os.path.join(regionpath, zonefile)) as f:
                        if f.read() == tzdata:  # got it!
                            return '%s/%s' % (region, zonefile)
        else:
            # check and set the timezone
            timezonefile = os.path.join(zonepath, timezone)
            if not os.path.exists(timezonefile):
                return False
            try:
                shutil.copyfile(timezonefile, tzpath)
            except:
                return False

            # write timezone setting to config file
            return config.set('time', 'timezone', timezone)
Beispiel #3
0
Datei: sc.py Projekt: oh8/inpanel
 def ifconfigs(self):
     """Read config of all interfaces.
     """
     configs = {}
     ifaces = ServerInfo.netifaces()
     for iface in ifaces:
         ifname = iface['name']
         config = ServerSet.ifconfig(ifname)
         if config:
             configs[ifname] = config
     return configs
Beispiel #4
0
Datei: sc.py Projekt: oh8/inpanel
    def ifconfig(self, ifname, config=None):
        """Read or write single interface's config.

        Pass None to parameter config (as default) to read config,
        or pass a dict type to config to write config.
        """
        dist = ServerInfo.dist()
        if dist['name'] in ('centos', 'redhat'):
            cfile = '/etc/sysconfig/network-scripts/ifcfg-%s' % ifname
            cmap = {
                'DEVICE': 'name',
                'HWADDR': 'mac',
                'IPADDR': 'ip',
                'NETMASK': 'mask',
                'GATEWAY': 'gw',
            }
            if config == None:
                return loadconfig(cfile, cmap)
            else:
                cmap_reverse = dict((v, k) for k, v in cmap.items())
                return saveconfig(cfile, config, cmap_reverse)
        else:
            return None
Beispiel #5
0
def _getmounts():
    mounts = ServerInfo.mounts()
    mounts = [mount['path'] for mount in mounts]
    # let the longest path at the first
    mounts.sort(lambda x, y: cmp(len(y), len(x)))
    return mounts