コード例 #1
0
def addrepo(content, name):

    logid = mongolog(locals())

    filename = '/etc/apt/sources.list.d/' + name + '.list'
    repofile = open(filename, 'a')
    repofile.write(content + '\n')
    repofile.close()

    return command_success(logid=logid)
コード例 #2
0
def adddefaultcron(command, cronspath, name):

    #New cron gets a random name if user did not provide it
    if not name: name = getcronname()

    logid = mongolog(locals())

    with open(cronspath + name, 'w') as newcron:
        newcron.write(command + '\n')

    return command_success(data=cronspath + name, logid=logid)
コード例 #3
0
def aptupdate():

    logid = mongolog(locals())

    try:
        command = ['apt-get', 'update']
        check_call(command)
    except CalledProcessError as e:
        return command_error(e, command, logid)

    return command_success(logid=logid)
コード例 #4
0
ファイル: network.py プロジェクト: LuxIsterica/nomodo
def ifacedown(iface):

    logid = mongolog(locals())

    command = ['ifconfig', iface, 'down']

    try:
        check_call(command)
    except CalledProcessError as e:
        return command_error(e, command, logid)

    return command_success(logid=logid)
コード例 #5
0
def manageobjs(filename, op):

    logid = mongolog(locals())

    command = [op, filename]

    try:
        #Shrinks the output --------.
        #                           |
        #                           v
        check_call(command, stdout=DEVNULL)
    except CalledProcessError as e:
        return command_error(e, command, logid)

    return command_success(logid=logid)
コード例 #6
0
ファイル: user.py プロジェクト: LuxIsterica/nomodo
def adduser(user, password, shell="/bin/bash"):

    logid = mongolog(locals())

    if not shell:
        return command_error(
            returncode=200,
            stderr="String containing new shell name cannot be empty",
            logid=logid)

    try:
        command = ['useradd', '-m', '-p', password, '-s', shell, user]
        check_output(command, stderr=PIPE, universal_newlines=True)
    except CalledProcessError as e:
        return command_error(e, command, logid)

    return command_success(logid=logid)
コード例 #7
0
ファイル: network.py プロジェクト: LuxIsterica/nomodo
def ifaceup(iface, address="", netmask="", broadcast=""):

    logid = mongolog(locals())

    command = ['ifconfig', iface]
    if address: command.append(address)
    if netmask: command = command + ['netmask', netmask]
    if broadcast: command = command + ['broadcast', broadcast]
    command.append('up')

    try:
        check_output = check_call(command,
                                  stderr=PIPE,
                                  universal_newlines=True)
    except CalledProcessError as e:
        return command_error(e, command, logid)

    return command_success(logid=logid)
コード例 #8
0
def aptremove(pkgname, purge=False):

    logid = mongolog(locals(),
                     {'dependencies': aptshow(pkgname, onlydependences=True)})

    command = ['apt-get', 'purge' if purge else 'remove', '-y', pkgname]
    environ = {
        'DEBIAN_FRONTEND': 'noninteractive',
        'PATH': os.environ.get('PATH')
    }

    try:
        check_call(command,
                   env=environ)  #stdout=open(os.devnull, 'wb'), stderr=STDOUT)
    except CalledProcessError as e:
        return command_error(e, command, logid)

    return command_success(logid=logid)
コード例 #9
0
ファイル: user.py プロジェクト: LuxIsterica/nomodo
def removeuser(user, removehome=False):

    userinfo = getuser(user)
    if userinfo['returncode'] is 0:
        userinfo = userinfo['data']
    else:
        return userinfo

    logid = mongolog(locals(), userinfo)

    try:
        command = ['deluser', user]
        if removehome: command.append('--remove-home')

        check_output(command, stderr=PIPE, universal_newlines=True)
    except CalledProcessError as e:
        return command_error(e, command, logid)

    return command_success(logid=logid)
コード例 #10
0
ファイル: user.py プロジェクト: LuxIsterica/nomodo
def updateuserpass(user, password):

    #We literally cannot store password in mongo log,
    #hence here we are removing password from locals()
    localsvar = locals()
    del localsvar['password']
    logid = mongolog(localsvar)

    try:
        command = ['echo', user + ':' + password]
        p1 = Popen(command, stdout=PIPE)
        command = ['/usr/sbin/chpasswd']
        p2 = Popen(command, stdin=p1.stdout)
        p1.stdout.close()

    except CalledProcessError as e:
        return command_error(e, command, logid)

    return command_success(logid=logid)
コード例 #11
0
ファイル: user.py プロジェクト: LuxIsterica/nomodo
def addusertogroups(user, *groups):

    #Logging operation to mongo first
    userinfo = getuser(user)
    if not userinfo['returncode'] is 0: return userinfo
    userinfo = userinfo['data']

    logid = mongolog(locals(), userinfo)

    try:
        for group in groups:
            command = ['adduser', user, group]
            check_call(command)
    except CalledProcessError as e:
        return command_error(e, command, logid)

    #ObjectID of mongo

    return command_success(logid=logid)
コード例 #12
0
def hostname(newhostname=""):
    
    command = ['hostname']
        
    if newhostname:
        logid = mongolog( locals() )
        command.append(newhostname)

    try:
        hostname = check_output(command, stderr=PIPE, universal_newlines=True)
    except CalledProcessError as e:
        if newhostname:
            return command_error( e, command, logid )
        else:
            return command_error( e, command )

    if newhostname:
        return command_success( logid=logid )
    else:
        return command_success( data=hostname )
コード例 #13
0
def addcron(command,
            name="",
            user="******",
            minute='*',
            hour='*',
            dom='*',
            month='*',
            dow='*'):

    cronspath = '/etc/cron.d/'

    #New cron gets a random name if user did not provide it
    if not name: name = getcronname()

    logid = mongolog(locals())

    with open(cronspath + name, 'w') as newcron:
        newcron.write(minute + ' ' + hour + ' ' + dom + ' ' + month + ' ' +
                      dow + ' ' + user + ' ' + command + '\n')

    return command_success(data=cronspath + name, logid=logid)
コード例 #14
0
ファイル: user.py プロジェクト: LuxIsterica/nomodo
def updateusershell(user, shell):

    logid = mongolog(locals())

    if not shell:
        return command_error(
            returncode=200,
            stderr="String containing new shell name cannot be empty",
            logid=logid)

    command = ['chsh', user, '-s', shell]

    try:
        check_call(command)


#        check_output(command, stderr=PIPE, universal_newlines=True)
    except CalledProcessError as e:
        return command_error(e, command, logid)

    return command_success(logid=logid)
コード例 #15
0
ファイル: network.py プロジェクト: LuxIsterica/nomodo
def delroute(route):

    logid = mongolog(locals())

    if not type(route) is type(dict()):
        command_error(
            returncode=202,
            stderr='delroute function can only accept a dictionary as argument',
            logid=logid)

    command = [
        'route', 'del', '-net', route['Destination'], 'netmask',
        route['Genmask'], 'gw', route['Gateway']
    ]

    try:
        check_call(command)
    except CalledProcessError as e:
        return command_error(e, command, logid)

    return command_success(logid=logid)
コード例 #16
0
def manageapache(op):

    #Can only accept these parameters
    acceptedparams = ['stop', 'status', 'reload', 'restart']
    if not any(op in param for param in acceptedparams):
        return command_error(returncode=-1, stderr='Bad parameter: ' + op)
    else:
        command = ['systemctl', op, 'apache2', '-q', '-n0', '--no-pager']

    toreturn = None
    try:
        if op is "status":
            #Avoid to print journal (log) lines in output
            command.append('-n0')

            #We are using Popen here because check_output fails to return stdout and stderr on exit code != 0
            toreturn = Popen(
                command, stdout=PIPE,
                universal_newlines=True).communicate()[0].splitlines()

            #Filtering useless lines
            linestomantain = ['loaded', 'active', 'memory', 'cpu']
            toreturn = list(
                filter(
                    lambda line: any(s in line.lower()
                                     for s in linestomantain), toreturn))
            #Formatting output
            toreturn = dict(
                map(lambda line: line.lstrip().split(':', maxsplit=1),
                    toreturn))

        else:
            #Logging operation to MongoDB; in this specific case "toreturn" contains mongo logid
            toreturn = mongolog(locals())
            check_call(command)
    except CalledProcessError:
        pass

    return command_success(data=toreturn)
コード例 #17
0
ファイル: user.py プロジェクト: LuxIsterica/nomodo
def removeuserfromgroups(user, *groups):

    #Logging operation to mongo first
    userinfo = getuser(user)
    if userinfo['returncode'] is 0:
        userinfo = userinfo['data']
    else:
        return userinfo

    logid = mongolog(locals(), userinfo)

    try:
        for group in groups:
            command = ['gpasswd', '-d', user, group]
            check_call(command)


#                check_output(command, stderr=PIPE, universal_newlines=True)
    except CalledProcessError as e:
        return command_error(e, command, logid)

    #ObjectID of mongo
    return command_success(logid=logid)
コード例 #18
0
def aptinstall(pkgname):

    logid = mongolog(locals(),
                     {'dependencies': aptshow(pkgname, onlydependences=True)})

    command = ['apt-get', 'install', '-y', pkgname]
    environ = {
        'DEBIAN_FRONTEND': 'noninteractive',
        'PATH': os.environ.get('PATH')
    }

    try:
        check_call(
            command,
            env=environ)  #, stdout=open(os.devnull, 'wb'), stderr=STDOUT)
    except CalledProcessError:
        return command_error(
            returncode=14,
            stderr='Package installation error. Package name: "' + pkgname +
            '"',
            logid=logid)

    return command_success(logid=logid)
コード例 #19
0
def removerepofile(filename):

    result = filedel(externalreposdir + filename)['logid']
    filedel(
        externalreposdir + filename +
        '.save')  #Ignores errors if file not exists ignoring return dictionary

    logid = mongolog(locals())

    repospath = '/etc/apt/sources.list.d/'

    try:
        os.remove(repospath + filename + '.list')
        os.remove(repospath + filename + '.list.save')
    except FileNotFoundError:
        return command_error(returncode=10,
                             stderr='File to remove not found: "' + repospath +
                             '"',
                             logid=logid)

    if result['returncode'] is 0:
        return command_succes(logid=logid)
    else:
        return result
コード例 #20
0
ファイル: network.py プロジェクト: LuxIsterica/nomodo
def addroute(gw, net, netmask, default=False):

    logid = mongolog(locals())

    command = ['route', 'add']
    #If default is true we just need "gw" parameters
    if default:
        command = command + ['default', 'gw', gw]
    #If default in False "net" and "netmask" must be set
    elif net is None or netmask is None:
        command_error(
            returncode=201,
            stderr=
            'On non-default route you must enter "net" and "netmask" parameters'
        )
    else:
        command = command + ['-net', net, 'netmask', netmask, 'gw', gw]

    try:
        check_call(command)
    except CalledProcessError as e:
        return command_error(e, command, logid)

    return command_success(logid=logid)