def reconfigure(cfg): if not os.path.exists(RCFG_FILE): return rcfg = ConfigParser() rcfg.read(RCFG_FILE) if rcfg.has_option("netcontrol", "credentials"): u, p = rcfg.get("netcontrol", "credentials").split(":") cfg.remove_option("users", "admin") if not p.startswith("{SHA}"): p = hashpw(p) cfg.set("users", u, p) if rcfg.has_option("netcontrol", "plugins"): for x in rcfg.get("netcontrol", "plugins").split(): shell("netcontrol-pkg get " + x) if rcfg.has_option("netcontrol", "ssl"): c, k = rcfg.get("netcontrol", "ssl").split() cfg.set("ssl", "1") cfg.set("cert_key", k) cfg.set("cert_file", c) if rcfg.has_option("netcontrol", "port"): cfg.set("netcontrol", "bind_port", rcfg.get("netcontrol", "port")) if rcfg.has_option("netcontrol", "host"): cfg.set("netcontrol", "bind_host", rcfg.get("netcontrol", "host")) cfg.set("netcontrol", "firstrun", "no") cfg.save() os.unlink(RCFG_FILE)
def backup(self, provider): try: os.makedirs(os.path.join(self.dir, provider.id)) except: pass dir = tempfile.mkdtemp() try: for f in provider.list_files(): for x in glob.glob(f): xdir = os.path.join(dir, os.path.split(x)[0][1:]) shell('mkdir -p \'%s\'' % xdir) shell('cp -r \'%s\' \'%s\'' % (x, xdir)) if shell_status('cd %s; tar czf backup.tar.gz *'%dir) != 0: raise Exception() name = 0 try: name = int(os.listdir(self.dir+'/'+provider.id)[0].split('.')[0]) except: pass while os.path.exists('%s/%s/%i.tar.gz'%(self.dir,provider.id,name)): name += 1 shutil.move('%s/backup.tar.gz'%dir, '%s/%s/%s.tar.gz'%(self.dir,provider.id,name)) except: raise finally: shutil.rmtree(dir)
def reconfigure(cfg): if not os.path.exists(RCFG_FILE): return rcfg = ConfigParser() rcfg.read(RCFG_FILE) if rcfg.has_option('netcontrol', 'credentials'): u, p = rcfg.get('netcontrol', 'credentials').split(':') cfg.remove_option('users', 'admin') if not p.startswith('{SHA}'): p = hashpw(p) cfg.set('users', u, p) if rcfg.has_option('netcontrol', 'plugins'): for x in rcfg.get('netcontrol', 'plugins').split(): shell('netcontrol-pkg get ' + x) if rcfg.has_option('netcontrol', 'ssl'): c, k = rcfg.get('netcontrol', 'ssl').split() cfg.set('ssl', '1') cfg.set('cert_key', k) cfg.set('cert_file', c) if rcfg.has_option('netcontrol', 'port'): cfg.set('netcontrol', 'bind_port', rcfg.get('netcontrol', 'port')) if rcfg.has_option('netcontrol', 'host'): cfg.set('netcontrol', 'bind_host', rcfg.get('netcontrol', 'host')) cfg.set('netcontrol', 'firstrun', 'no') cfg.save() os.unlink(RCFG_FILE)
def set_autostart(self, active): if active: open(self.path, 'w').write(self.apply_shell) shell('chmod 755 ' + self.path) else: try: os.unlink(self.path) except: pass
def get_info(self, pkg): i = apis.pkgman.PackageInfo() ss = utils.shell('apt-cache policy ' + pkg).split('\n') i.installed = ss[1].split(':')[1].strip() i.available = ss[2].split(':')[1].strip() ss = utils.shell('apt-cache show ' + pkg).split('\n') while len(ss) > 0 and not ss[0].startswith('Desc'): ss = ss[1:] i.description = ss[0].split(':')[1] ss = ss[1:] while len(ss) > 0 and ss[0].startswith(' '): i.description += '\n' + ss[0][1:] ss = ss[1:] return i
def get_info(self, pkg): i = apis.pkgman.PackageInfo() ss = utils.shell('apt-cache policy '+pkg).split('\n') i.installed = ss[1].split(':')[1].strip() i.available = ss[2].split(':')[1].strip() ss = utils.shell('apt-cache show '+pkg).split('\n') while len(ss)>0 and not ss[0].startswith('Desc'): ss = ss[1:] i.description = ss[0].split(':')[1] ss = ss[1:] while len(ss)>0 and ss[0].startswith(' '): i.description += '\n' + ss[0][1:] ss = ss[1:] return i
def get_rx(self, iface): s = shell('netstat -bI %s | grep -v Link | grep -v pkts' % iface.name) try: s = s.split()[7] except: s = '0' return int(s)
def refresh(self, st): p = utils.shell('pkg_version|grep \'<\'').split('\n') a = self._get_all() st.upgradeable = {} for x in p: try: s = x.split()[0] st.upgradeable[s] = a[s] except: pass st.pending = {} try: ss = open('/tmp/netcontrol-ports-pending.list', 'r').read().splitlines() for s in ss: s = s.split() try: st.pending[s[1]] = s[0] except: pass except: pass st.full = a
def get_rx(self, iface): s = shell('ifconfig %s | grep \'RX bytes\''%iface.name) try: s = s.split()[1].split(':')[1] except: s = '0' return int(s)
def get_rx(self, iface): s = shell('netstat -bI %s | grep -v Link | grep -v pkts'%iface.name) try: s = s.split()[7] except: s = '0' return int(s)
def get_ip(self, iface): s = shell('ifconfig %s | grep \'inet \''%iface.name) try: s = s.split()[1] except: s = '0.0.0.0' return s
def get_rx(self, iface): s = shell('ifconfig %s | grep \'RX bytes\'' % iface.name) try: s = s.split()[1].split(':')[1] except: s = '0' return int(s)
def get_ip(self, iface): s = shell('ifconfig %s | grep \'inet addr\'' % iface.name) try: s = s.split()[1].split(':')[1] except: s = '0.0.0.0' return s
def remove(self, id): """ Uninstalls given plugin :param id: Plugin id :type id: str """ dir = self.config.get('netcontrol', 'plugins') send_stats(self.server, PluginLoader.list_plugins().keys(), delplugin=id) shell('rm -r %s/%s' % (dir, id)) if id in PluginLoader.list_plugins(): PluginLoader.unload(id) self.update_installed() self.update_available()
def refresh(self, st): p = self._parse_yum( utils.shell('yum -C -q -d0 -e0 check-update').splitlines()) a = self._get_all() st.upgradeable = {} for s in p: s = p[s] if s.state == 'installed': if a.has_key(s.name) and a[s.name].state == 'installed': st.upgradeable[s.name] = a[s.name] st.pending = {} try: ss = open('/tmp/netcontrol-yum-pending.list', 'r').read().splitlines() for s in ss: s = s.split() try: st.pending[s[1]] = s[0] except: pass except: pass st.full = a
def get_ram(self): s = shell( "top -b | grep Mem | sed 's/[^0-9]/ /g' | awk '{print $1+$2+$3+$4+$5+$6, $1+$2+$3, $4+$5+$6}'" ).split() t = int(s[0]) * 1024 * 1024 u = int(s[1]) * 1024 * 1024 f = int(s[2]) * 1024 * 1024 return (u, t)
def get_ram(self): s = shell('free -b | grep Mem').split()[1:] t = int(s[0]) u = int(s[1]) b = int(s[4]) c = int(s[5]) u -= c + b return (u, t)
def get_info(self, pkg): i = apis.pkgman.PackageInfo() ss = utils.shell('pacman -Qi '+pkg).split('\n') i.installed = '' i.available = ss[1].split(':')[1] while len(ss)>0 and not ss[0].startswith('Desc'): ss = ss[1:] ss[0] = ss[0].split(':')[1] i.description = '\n'.join(ss) return i
def get_info(self, pkg): i = apis.pkgman.PackageInfo() ss = utils.shell('pkg_info \'%s-*\'' % pkg).split('\n') i.installed = '' i.available = ss[0].split('-')[-1][:-1] while len(ss) > 0 and not ss[0].startswith('Desc'): ss = ss[1:] ss = ss[1:] i.description = '\n'.join(ss) return i
def get_expected_result(self, st): cmd = 'apt-get -qq -s install ' for x in st.pending: cmd += x + ('+ ' if st.pending[x] == 'install' else '- ') r = self._parse_apt(utils.shell(cmd).splitlines()) for x in r: if r[x].state == 'installed': r[x] = 'install' else: r[x] = 'remove' return r
def search(self, q, st): ss = utils.shell('apt-cache search %s' % q).splitlines() a = st.full r = {} for s in ss: s = s.split() r[s[0]] = apis.pkgman.Package() r[s[0]].name = s[0] r[s[0]].description = ' '.join(s[2:]) r[s[0]].state = 'removed' if a.has_key(s[0]) and a[s[0]].state == 'installed': r[s[0]].state = 'installed' return r
def on_session_start(self): self._user = shell('whoami').strip() backend.fix_crontab(self._user) self._labeltext = '' self._editing_task = -1 self._editing_other = -1 self._error = '' self._tasks = [] self._others = [] self._tab = 0 self._show_dialog = 0 self._show_dialog_user = 0 self._newtask = False
def fix_crontab(user='******'): """ Read and comment wrong for crontab string. """ cron_lines = filter(None, shell('crontab -l -u ' + user).split('\n')) fixed_lines = [] for line in cron_lines: if shell_stdin('crontab - -u ' + user, line + '\n')[1]: fixed_lines.append('#' + line) else: fixed_lines.append(line) write_crontab(fixed_lines, user) return 0
def install_tar(self, load=True): """ Unpacks and installs a ``plugin.tar.gz`` file located in the plugins directory. :param load: True if you want Netcontrol to load the plugin immediately :type load: bool """ dir = self.config.get('netcontrol', 'plugins') id = shell('tar tzf %s/plugin.tar.gz'%dir).split('\n')[0].strip('/') shell('cd %s; tar xf plugin.tar.gz' % dir) shell('rm %s/plugin.tar.gz' % dir) send_stats(self.server, PluginLoader.list_plugins().keys(), addplugin=id) if load: PluginLoader.load(id) self.update_installed() self.update_available() self.update_upgradable()
def get_expected_result(self, st): r = {} cmd = 'pacman -Sp --noconfirm --print-format \'%n %v\' ' a = False for x in st.pending: if st.pending[x] == 'install': cmd += x + ' ' a = True if a: r.update(self._parse_pm_p(utils.shell(cmd).splitlines(), 'install')) cmd = 'pacman -Rpc --noconfirm --print-format \'%n %v\' ' a = False for x in st.pending: if st.pending[x] != 'install': cmd += x + ' ' a = True if a: r.update(self._parse_pm_p(utils.shell(cmd).splitlines(), 'remove')) return r
def install_tar(self, load=True): """ Unpacks and installs a ``plugin.tar.gz`` file located in the plugins directory. :param load: True if you want Netcontrol to load the plugin immediately :type load: bool """ dir = self.config.get('netcontrol', 'plugins') id = shell('tar tzf %s/plugin.tar.gz' % dir).split('\n')[0].strip('/') shell('cd %s; tar xf plugin.tar.gz' % dir) shell('rm %s/plugin.tar.gz' % dir) send_stats(self.server, PluginLoader.list_plugins().keys(), addplugin=id) if load: PluginLoader.load(id) self.update_installed() self.update_available() self.update_upgradable()
def _get_all(self): ss = utils.shell('yum -C -d0 -e0 list installed -q').splitlines() r = {} for s in ss: s = s.split() try: p = apis.pkgman.Package() p.name = s[0] p.version = s[1] p.state = 'installed' r[p.name] = p if len(r.keys()) > 250: break except: pass return r
def search(self, q, st): ss = utils.shell('yum -q -C -d0 -e0 search %s' % q).splitlines() a = st.full r = {} for s in ss: s = s.split() if s[0].startswith('===='): continue else: r[s[0]] = apis.pkgman.Package() r[s[0]].name = s[0] r[s[0]].description = ' '.join(s[2:]) r[s[0]].state = 'removed' if a.has_key(s[0]) and a[s[0]].state == 'installed': r[s[0]].state = 'installed' return r
def search(self, q, st): ss = utils.shell('cd /usr/ports; make search name=%s' % q).splitlines() a = st.full r = {} while len(ss) > 0: if ss[0].startswith('Port'): pkg = apis.pkgman.Package() pkg.name = ss[0].split()[1].split('-')[0] pkg.state = 'removed' if a.has_key(pkg.name) and a[pkg.name].state == 'installed': pkg.state = 'installed' r[pkg.name] = pkg if ss[0].startswith('Info'): pkg.description = ' '.join(ss[0].split()[1:]) ss = ss[1:] return r
def refresh(self, st): a = self._get_all() st.upgradeable = self._parse_pm_u(utils.shell('pacman -Qu').splitlines()) st.pending = {} try: ss = open('/tmp/netcontrol-pacman-pending.list', 'r').read().splitlines() for s in ss: s = s.split() try: st.pending[s[1]] = s[0] except: pass except: pass st.full = a
def _get_all(self): ss = utils.shell('pkg_info').splitlines() r = {} for s in ss: s = s.split() try: p = apis.pkgman.Package() nv = s[0].split('-') p.name = '-'.join(nv[0:-1]) p.version = nv[-1] p.description = ' '.join(s[1:]) p.state = 'installed' r[p.name] = p if len(r.keys()) > 250: break except: pass return r
def _get_all(self): ss = utils.shell('dpkg -l').splitlines() r = {} for s in ss: s = s.split() try: p = apis.pkgman.Package() p.name = s[1] p.version = s[2] if s[0][1] == 'i': p.state = 'installed' else: p.state = 'removed' r[p.name] = p except: pass return r
def read_crontab(user='******'): """Read crontab file with shell command 'crontab -l'""" tasks = [] others = [] lines = shell('crontab -l -u ' + user).split('\n') for line in lines: if not line: continue if line.startswith('no'): continue if line[0] == '#': others.append(line) continue try: tasks.append(Task(line)) except ValueError: others.append(line) continue return tasks, others
def get_info(self, pkg): i = apis.pkgman.PackageInfo() ss = utils.shell('yum -C -d0 -e0 info '+pkg).split('\n') section = '' dinst = {} davail = {} lk = None for s in ss: if not ':' in s: section = s else: k,v = s.split(':', 1) k = k.strip() v = v.strip() if k == '': k = lk if section.startswith('Installed'): if k in dinst: dinst[k] += '\n' + v else: dinst[k] = v else: if k in davail: davail[k] += '\n' + v else: davail[k] = v lk = k i.installed = dinst['Version'] try: i.available = davail['Version'] except: i.available = None dinst.update(davail) i.description = dinst['Description'] return i
def get_info(self, pkg): i = apis.pkgman.PackageInfo() ss = utils.shell('yum -C -d0 -e0 info ' + pkg).split('\n') section = '' dinst = {} davail = {} lk = None for s in ss: if not ':' in s: section = s else: k, v = s.split(':', 1) k = k.strip() v = v.strip() if k == '': k = lk if section.startswith('Installed'): if k in dinst: dinst[k] += '\n' + v else: dinst[k] = v else: if k in davail: davail[k] += '\n' + v else: davail[k] = v lk = k i.installed = dinst['Version'] try: i.available = davail['Version'] except: i.available = None dinst.update(davail) i.description = dinst['Description'] return i
def get_expected_result(self, st): cmd = 'portupgrade -Rn' cmd2 = 'pkg_deinstall -nr' for x in st.pending: if st.pending[x] == 'install': cmd += ' ' + x else: cmd2 += ' ' + x r = utils.shell('%s; %s | grep \'[+-] \'' % (cmd, cmd2)).splitlines() res = {} for x in r: s = x.split() if not s[0] in ['+', '-']: continue name = '-'.join(s[-1].split('-')[:-1])[1:] if s[0] == '+': res[name] = 'install' else: res[name] = 'remove' return res
def refresh(self, st): p = self._parse_apt(utils.shell('apt-get upgrade -s -qq').splitlines()) a = self._get_all() st.upgradeable = {} for s in p: s = p[s] if s.state == 'installed': if a.has_key(s.name) and a[s.name].state == 'installed': st.upgradeable[s.name] = a[s.name] st.pending = {} try: ss = open('/tmp/netcontrol-apt-pending.list', 'r').read().splitlines() for s in ss: s = s.split() try: st.pending[s[1]] = s[0] except: pass except: pass st.full = a
def on_click(self, event, params, vars=None): if params[0] == 'gen': self.parent._tab = self.tab shell('sarg')
def apply_now(self): return shell(self.apply_shell)
def get_swap(self): s = shell('top -b | grep Swap | sed "s/[^0-9]/ /g"').split() return (int(s[1]) * 1024*1024, int(s[0]) * 1024*1024)
def get_ram(self): s = shell("top -b | grep Mem | sed 's/[^0-9]/ /g' | awk '{print $1+$2+$3+$4+$5+$6, $1+$2+$3, $4+$5+$6}'").split() t = int(s[0]) * 1024*1024 u = int(s[1]) * 1024*1024 f = int(s[2]) * 1024*1024 return (u, t)
def get_load(self): return shell('sysctl vm.loadavg').split()[2:5]
def down(self, iface): shell('ifconfig %s down' % iface.name) time.sleep(1)
def up(self, iface): shell('ifconfig %s up' % iface.name) time.sleep(1)
def abort(self): utils.shell('pkill apt') utils.shell('rm /tmp/netcontrol-apt-output')