def reconfigure(cfg): if not os.path.exists(RCFG_FILE): return rcfg = ConfigParser() rcfg.read(RCFG_FILE) if rcfg.has_option('ajenti', 'credentials'): u,p = rcfg.get('ajenti', 'credentials').split(':') cfg.remove_option('users', 'admin') if not p.startswith('{SHA}'): p = hashpw(p) cfg.set('users', u, p) if rcfg.has_option('ajenti', 'plugins'): for x in rcfg.get('ajenti', 'plugins').split(): shell('ajenti-pkg get ' + x) if rcfg.has_option('ajenti', 'ssl'): c,k = rcfg.get('ajenti', 'ssl').split() cfg.set('ssl', '1') cfg.set('cert_key', k) cfg.set('cert_file', c) if rcfg.has_option('ajenti', 'port'): cfg.set('ajenti', 'bind_port', rcfg.get('ajenti', 'port')) if rcfg.has_option('ajenti', 'host'): cfg.set('ajenti', 'bind_host', rcfg.get('ajenti', 'host')) cfg.set('ajenti', 'firstrun', 'no') cfg.save() os.unlink(RCFG_FILE)
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 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 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('ajenti', 'credentials'): u, p = rcfg.get('ajenti', 'credentials').split(':') cfg.remove_option('users', 'admin') if not p.startswith('{SHA}'): p = hashpw(p) cfg.set('users', u, p) if rcfg.has_option('ajenti', 'plugins'): for x in rcfg.get('ajenti', 'plugins').split(): shell('ajenti-pkg get ' + x) if rcfg.has_option('ajenti', 'ssl'): c, k = rcfg.get('ajenti', 'ssl').split() cfg.set('ssl', '1') cfg.set('cert_key', k) cfg.set('cert_file', c) if rcfg.has_option('ajenti', 'port'): cfg.set('ajenti', 'bind_port', rcfg.get('ajenti', 'port')) if rcfg.has_option('ajenti', 'host'): cfg.set('ajenti', 'bind_host', rcfg.get('ajenti', 'host')) cfg.set('ajenti', '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_rx(self, iface): try: return int(shell('ifconfig %s | grep \'RX bytes\''%iface.name).split()[1].split(':')[1]) except: pass try: return int(shell('ifconfig %s | grep -E \'RX .+ bytes\''%iface.name).split()[4]) except: pass return 0
def get_ip(self, iface): try: return shell('ifconfig %s | grep \'inet addr\''%iface.name).split()[1].split(':')[1] except: pass try: return shell('ifconfig %s | grep \'inet\''%iface.name).split()[1] except: pass return '0.0.0.0'
def get_ip(self, iface): try: return shell("ifconfig %s | grep 'inet addr'" % iface.name).split()[1].split(":")[1] except: pass try: return shell("ifconfig %s | grep 'inet'" % iface.name).split()[1] except: pass return "0.0.0.0"
def get_rx(self, iface): try: return int(shell("ifconfig %s | grep 'RX bytes'" % iface.name).split()[1].split(":")[1]) except: pass try: return int(shell("ifconfig %s | grep -E 'RX .+ bytes'" % iface.name).split()[4]) except: pass return 0
def get_ip(self, iface): try: return shell('ifconfig %s | grep \'inet addr\'' % iface.name).split()[1].split(':')[1] except: pass try: return shell('ifconfig %s | grep \'inet\'' % iface.name).split()[1] except: pass return '0.0.0.0'
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_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_param(self, param): try: s = shell('grep \'^%s=\' /etc/rc.conf' % param).split('=')[1].strip() except: s = '' return s.strip('"')
def remove(self, id): """ Uninstalls given plugin :param id: Plugin id :type id: str """ dir = self.config.get('ajenti', '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 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 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/ajenti-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_usage(self, dev): u = shell('df --total | grep -w %s' % dev) if dev == 'total': u = int(u.split().pop().strip('%')) else: u = int(u.split().pop(-2).strip('%')) return u
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 get_rx(self, iface): try: return int( shell('ifconfig %s | grep \'RX bytes\'' % iface.name).split()[1].split(':')[1]) except: pass try: return int( shell('ifconfig %s | grep -E \'RX .+ bytes\'' % iface.name).split()[4]) except: pass return 0
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_ip(self, iface): s = shell('ip addr list %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('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 addr'" % iface.name) try: s = s.split()[1].split(":")[1] except: s = "0.0.0.0" return s
def get_rx(self, iface): s = shell("ifconfig %s | grep 'RX bytes'" % iface.name) print s 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) print s try: s = s.split()[7] except: s = '0' return int(s)
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_rx(self, iface): s = shell("netstat -bI %s | grep -v Link | grep -v pkts" % iface.name) print s try: s = s.split()[7] except: s = "0" return int(s)
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_rx(self, iface): s = shell('ip -s link ls %s' % iface.name) s = s.split('\n')[3] try: s = s.split()[0] except: s = '0' return int(s)
def get_rx(self, iface): s = shell('ifconfig %s | grep \'RX bytes\'' % iface.name) print s try: s = s.split()[1].split(':')[1] except: s = '0' return int(s)
def get_ram(self): s = shell('free -m | 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_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_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_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 get_expected_result(self, st): cmd = "apt-get -qq -s install " for x in st.pending: cmd += x + ("+ " if st.pending[x] == "install" else "- ") print cmd 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 deploy_config(self, cfg, node): tmp = tempfile.mkdtemp() for f,m in cfg.files: path = tmp + f shell('mkdir -p ' + path) shell('cp -r %s%s/%s %s' % (self.config_dir,f,m,path)) for root, dirs, files in os.walk(path): for fl in files: try: self._insert_vars(os.path.join(root, fl), node) except: pass self._ssh_run(node, 'rm -r %s/%s'%(f,m)) self._ssh_copy('%s/*'%tmp, node, '/') shutil.rmtree(tmp) _rt = self.config.get('uzuri-root') self.config.set('uzuri-root', self.config_dir) for cmd in cfg.run_after: self._ssh_run(node, self._insert_vars_str(cmd, node)) self.config.set('uzuri-root', _rt)
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 Ajenti to load the plugin immediately :type load: bool """ dir = self.config.get('ajenti', '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 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 on_session_start(self): self._user = shell('whoami').strip() backend.fix_crontab(self._user) self._log = '' self._labeltext = '' self._editing_task = -1 self._editing_other = -1 self._error = '' self._tasks = [] self._others = [] #self._tab = 0 self._show_dialog = 0 self._newtask = False
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 fix_crontab(): """ Read and comment wrong for crontab string. """ cron_lines = filter(None, shell('crontab -l').split('\n')) fixed_lines = [] for line in cron_lines: if shell_stdin('crontab -', line + '\n')[1]: fixed_lines.append('#' + line) else: fixed_lines.append(line) write_crontab(fixed_lines) return 0
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 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 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 _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 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 refresh(self, st): a = self._get_all() st.upgradeable = self._parse_zypp_lu( utils.shell('zypper -An list-updates').splitlines()) st.pending = {} try: ss = open('/tmp/ajenti-zypper-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