Esempio n. 1
0
def update(name):
    if name:
        ret = exec_cmd(
            "/usr/bin/apt-get -quy install {0} --force-yes".format(name))
        if ret['returncode'] != 0:
            raise ResourceException(ret['stderr'])
    else:
        ret = exec_cmd("/usr/bin/apt-get -qy update")
        ret = exec_cmd("/usr/bin/apt-get -qy upgrade --force-yes")
        if ret['returncode'] != 0:
            raise ResourceException(ret['stderr'])
Esempio n. 2
0
def update(name):
    if name:
        ret = exec_cmd(
            "/usr/bin/apt-get -quy install {0} --force-yes".format(name))
        if ret['returncode'] != 0:
            raise ResourceException(ret['stderr'])
    else:
        ret = exec_cmd("/usr/bin/apt-get -qy update")
        ret = exec_cmd("/usr/bin/apt-get -qy upgrade --force-yes")
        if ret['returncode'] != 0:
            raise ResourceException(ret['stderr'])
Esempio n. 3
0
def user_add(name, password, login_group, groups):
    cmd = []
    cmd.append("/usr/sbin/useradd")
    if login_group:
        cmd.append("-g")
        cmd.append(login_group)
    if groups:
        groups_no_ws = re.sub(r'\s', '', groups)
        try:
            group_list = groups_no_ws.split(',')
            for group in group_list:
                groups.read(group)
            cmd.append("-G")
            cmd.append(groups_no_ws)
        except ResourceException:
            raise ResourceException("Group does not exist")

    cmd.append(name)

    ret = exec_cmd(' '.join(cmd))

    if ret['returncode'] != 0:
        raise ResourceException(ret['stderr'])

    if password:
        set_password(name, password)
Esempio n. 4
0
def user_add(name, password, login_group, groups):
    cmd = []
    cmd.append("/usr/sbin/useradd")
    if login_group:
        cmd.append("-g")
        cmd.append(login_group)
    if groups:
        groups_no_ws = re.sub(r'\s', '', groups)
        try:
            group_list = groups_no_ws.split(',')
            for group in group_list:
                groups.read(group)
            cmd.append("-G")
            cmd.append(groups_no_ws)
        except ResourceException:
            raise ResourceException("Group does not exist")

    cmd.append(name)

    ret = exec_cmd(' '.join(cmd))

    if ret['returncode'] != 0:
        raise ResourceException(ret['stderr'])

    if password:
        set_password(name, password)
Esempio n. 5
0
def user_add(name, password, login_group, groups, homedir, comment, uid, gid,
             shell):

    cmd = ['/usr/sbin/useradd']

    if login_group:
        cmd += ['-g', '%s' % login_group]
    if len(groups):
        cmd += ['-G', ','.join(groups)]
    if homedir:
        cmd += ['--home', '%s' % homedir]
    if comment:
        cmd += ['--comment', '%s' % comment]
    if uid:
        cmd += ['--uid', '%s' % uid]
    if gid:
        cmd += ['--gid', '%s' % gid]
    if shell:
        cmd += ['--shell', '%s' % shell]

    cmd.append(name)

    ret = exec_cmd(' '.join(cmd))

    # retcode 9 is group already exists. That's what we want.
    if ret['returncode'] != 9 and ret['returncode'] != 0:
        raise ResourceException(ret['stderr'])

    if password:
        set_password(name, password)
Esempio n. 6
0
def user_mod(name, password, login_group, groups, homedir, move_home,
             comment, uid, gid, shell):

    try:
        if password:
            set_password(name, password)

        cmd = ["/usr/sbin/usermod"]
        if login_group:
            cmd += ['-g', login_group]
        if len(groups):
            cmd += ['-G', ','.join(groups)]
        if homedir:
            cmd += ['--home', homedir]
        if homedir and move_home:
            cmd += ['--move-home']
        if comment:
            cmd += ['--comment', comment]
        if uid:
            cmd += ['--uid', uid]
        if gid:
            cmd += ['--gid', gid]
        if shell:
            cmd += ['--shell', shell]

        cmd.append(name)

        if len(cmd) > 2:
            ret = exec_cmd(' '.join(cmd))
            if ret['returncode'] != 0:
                raise ResourceException(ret['stderr'])

    except ResourceException:
        raise
Esempio n. 7
0
def user_add(name, password, login_group, groups,
             homedir, comment, uid, gid, shell):

    cmd = ['/usr/sbin/useradd']

    if login_group:
        cmd += ['-g', login_group]
    if len(groups):
        cmd += ['-G', ','.join(groups)]
    if homedir:
        cmd += ['--home', homedir]
    if comment:
        cmd += ['--comment', comment]
    if uid:
        cmd += ['--uid', uid]
    if gid:
        cmd += ['--gid', gid]
    if shell:
        cmd += ['--shell', shell]

    cmd.append(name)

    ret = exec_cmd(' '.join(cmd))

    # retcode 9 is group already exists. That's what we want.
    if ret['returncode'] != 9 and ret['returncode'] != 0:
        raise ResourceException(ret['stderr'])

    if password:
        set_password(name, password)
Esempio n. 8
0
def user_mod(name, password, login_group, groups, homedir, move_home, comment,
             uid, gid, shell):

    try:
        if password:
            set_password(name, password)

        cmd = ["/usr/sbin/usermod"]
        if login_group:
            cmd += ['-g', '%s' % login_group]
        if len(groups):
            cmd += ['-G', ','.join(groups)]
        if homedir:
            cmd += ['--home', '%s' % homedir]
        if homedir and move_home:
            cmd += ['--move-home']
        if comment:
            cmd += ['--comment', '%s' % comment]
        if uid:
            cmd += ['--uid', '%s' % uid]
        if gid:
            cmd += ['--gid', '%s' % gid]
        if shell:
            cmd += ['--shell', '%s' % shell]

        cmd.append(name)

        if len(cmd) > 2:
            ret = exec_cmd(' '.join(cmd))
            if ret['returncode'] != 0:
                raise ResourceException(ret['stderr'])

    except ResourceException:
        raise
Esempio n. 9
0
 def _execute(self, name, cmd):
     result = exec_cmd(cmd)
     if result['returncode'] != 0:
         result['name'] = name
         msg = OutgoingMessage(collection=self.__resource__, status=result,
                               msg_type='alert')
         self.publish(msg)
Esempio n. 10
0
 def _execute(self, name, cmd):
     result = exec_cmd(cmd)
     if result['returncode'] != 0:
         result['name'] = name
         msg = OutgoingMessage(collection=self.__resource__,
                               status=result,
                               msg_type='alert')
         self.publish(msg)
Esempio n. 11
0
    def read(self, res_id=None, attributes=None):
        sensors = attributes.keys()
        status = {}
        for sensor in sensors:
            if sensor in self.plugins.keys():
                status[sensor] = exec_cmd(self.plugins[sensor]['command'])

        return status
Esempio n. 12
0
    def read(self, res_id=None, attributes=None):
        sensors = attributes.keys()
        status = {}
        for sensor in sensors:
            if sensor in self.plugins.keys():
                status[sensor] = exec_cmd(self.plugins[sensor]['command'])

        return status
Esempio n. 13
0
def update(name):
    # We need to check first if the package is installed. yum update of a
    # non-existing package has a returncode of 0. We need to raise an exception
    # if the package is not installed !
    inst = is_installed(name)
    ret = exec_cmd("/usr/bin/yum -q -y update %s" % name)

    if ret['returncode'] != 0 or not inst:
        raise ResourceException(ret['stderr'])
Esempio n. 14
0
def get_groups(name):
    cmd = ["/usr/bin/groups"]
    cmd.append(name)

    ret = exec_cmd(' '.join(cmd))
    if ret['returncode'] != 0:
        raise ResourceException(ret['stderr'])

    # Return a list of groups
    return ret['stdout'].split(':')[1].lstrip().split()
Esempio n. 15
0
def get_groups(name):
    cmd = ["/usr/bin/groups"]
    cmd.append(name)

    ret = exec_cmd(' '.join(cmd))
    if ret['returncode'] != 0:
        raise ResourceException(ret['stderr'])

    # Return a list of groups
    return ret['stdout'].split(':')[1].lstrip().split()
Esempio n. 16
0
    def update(self, res_id=None, attributes=None):
        if not res_id:
            raise ResourceException('Please provide a command')

        #exec_status = self.module.exec_threaded_cmd(res_id)
        self.logger.info("Executing: %s" % res_id)
        exec_status = exec_cmd(res_id)
        self.response = self.set_response(exec_status)
        self.logger.info("Done executing '%s'" % res_id)

        return self.response
Esempio n. 17
0
def group_add(name, gid):
    cmd = ["/usr/sbin/groupadd"]

    if gid:
        cmd += ['--gid', "%s" % gid]

    cmd.append(name)

    ret = exec_cmd(' '.join(cmd))

    # retcode 9 is group already exists. That's what we want.
    if ret['returncode'] != 9 and ret['returncode'] != 0:
        raise ResourceException(ret['stderr'])
Esempio n. 18
0
    def update(self, res_id=None, attributes=None):
        try:
            if not res_id:
                raise ResourceException('Please provide a command')

            #exec_status = self.module.exec_threaded_cmd(res_id)
            self.logger.info("Executing: %s" % res_id)
            exec_status = exec_cmd(res_id)
            response = self.set_response(exec_status)
            self.logger.info("Done executing '%s'" % res_id)

        except ResourceException, err:
            response = self.set_response('Exec error', error='%s' % err)
Esempio n. 19
0
    def update(self, res_id=None, attributes=None):
        if not res_id:
            raise ResourceException("Please provide a command")

        # status = self.module.exec_threaded_cmd(res_id)
        self.logger.info("Executing: %s" % res_id)
        status = exec_cmd(res_id)
        if status["returncode"] != 0:
            error = "Status code %s: [%s]" % (status["returncode"], status["stderr"])
            raise ResourceException(error)
        self.logger.info("Done executing '%s'" % res_id)

        return status
Esempio n. 20
0
def is_enabled(name):
    ret = exec_cmd("/sbin/runlevel")
    if ret['returncode'] != 0:
        raise ResourceException(ret['stderr'])

    match = False
    try:
        runlevel = ret['stdout'].split()[1]
        for filename in os.listdir('/etc/rc%s.d' % runlevel):
            if name in filename and filename.startswith('S'):
                match = True

    except ValueError, err:
        raise ResourceException(err)
Esempio n. 21
0
    def update(self, res_id=None, attributes=None):
        if not res_id:
            raise ResourceException('Please provide a command')

        #status = self.module.exec_threaded_cmd(res_id)
        self.logger.info("Executing: %s" % res_id)
        status = exec_cmd(res_id)
        if status['returncode'] != 0:
            error = "Status code %s: [%s]" % (status["returncode"],
                                              status["stderr"])
            raise ResourceException(error)
        self.logger.info("Done executing '%s'" % res_id)

        return status
Esempio n. 22
0
def user_mod(name,
             password=None,
             login_group=None,
             add_to_groups=None,
             remove_from_groups=None,
             set_groups=None
             ):

    try:
        if password:
            set_password(name, password)

        cmd = []
        cmd.append("/usr/sbin/usermod")

        if login_group:
            cmd.append("-g")
            cmd.append(login_group)

        elif add_to_groups:
            groups = filter_existing_groups(add_to_groups)
            if len(groups):
                cmd.append("-G")
                cmd.append(','.join(groups))
                cmd.append("-a")

        elif remove_from_groups:
            groups = filter_existing_groups(remove_from_groups)
            current_groups = get_groups(name)

            if len(groups):
                groups_to_set = filter(lambda x: x not in groups,
                                       current_groups)
                cmd.append("-G")
                cmd.append(','.join(groups_to_set))

        elif set_groups:
            groups = filter_existing_groups(set_groups)
            if len(groups):
                cmd.append("-G")
                cmd.append(','.join(groups))

        cmd.append(name)
        if len(cmd) > 2:
            ret = exec_cmd(' '.join(cmd))
            if ret['returncode'] != 0:
                raise ResourceException(ret['stderr'])

    except ResourceException:
        raise
Esempio n. 23
0
def user_mod(name,
             password=None,
             login_group=None,
             add_to_groups=None,
             remove_from_groups=None,
             set_groups=None):

    try:
        if password:
            set_password(name, password)

        cmd = []
        cmd.append("/usr/sbin/usermod")

        if login_group:
            cmd.append("-g")
            cmd.append(login_group)

        elif add_to_groups:
            groups = filter_existing_groups(add_to_groups)
            if len(groups):
                cmd.append("-G")
                cmd.append(','.join(groups))
                cmd.append("-a")

        elif remove_from_groups:
            groups = filter_existing_groups(remove_from_groups)
            current_groups = get_groups(name)

            if len(groups):
                groups_to_set = filter(lambda x: x not in groups,
                                       current_groups)
                cmd.append("-G")
                cmd.append(','.join(groups_to_set))

        elif set_groups:
            groups = filter_existing_groups(set_groups)
            if len(groups):
                cmd.append("-G")
                cmd.append(','.join(groups))

        cmd.append(name)
        if len(cmd) > 2:
            ret = exec_cmd(' '.join(cmd))
            if ret['returncode'] != 0:
                raise ResourceException(ret['stderr'])

    except ResourceException:
        raise
Esempio n. 24
0
    def update(self, res_id=None, attributes=None):
        if not res_id:
            raise ResourceException('Please provide a command')

        #exec_status = self.module.exec_threaded_cmd(res_id)
        self.logger.info("Executing: %s" % res_id)
        exec_status = exec_cmd(res_id)
        if exec_status['returncode'] != 0:
            error = "Status code %s: [%s]" %(exec_status["returncode"],
                                             exec_status["stderr"])
            raise ResourceException(error)
        self.response = self.set_response(exec_status)
        self.logger.info("Done executing '%s'" % res_id)

        return self.response
Esempio n. 25
0
def group_mod(name, new_name, gid):
    cmd = ["/usr/sbin/groupmod"]

    if new_name:
        cmd += ['--new-name', "%s" % new_name]

    if gid:
        cmd += ['--gid', "%s" % gid]

    cmd.append(name)

    ret = exec_cmd(' '.join(cmd))

    if ret['returncode'] != 0:
        raise ResourceException(ret['stderr'])
Esempio n. 26
0
def is_installed(name):
    ret = exec_cmd("/usr/bin/dpkg-query -l '{0}'".format(name))
    if ret['returncode'] != 0:
        return False

    # There's no way to use return code of any of the dpkg-query options.
    # Instead we use the "state" column of dpkg-query -l
    # So programmaticaly here:
    # 1. Get stdout
    # 2. Split on new line
    # 3. Get the last but one line (last is blank, in any case?)
    # 4. Get first character (i=installed)
    try:
        return ret['stdout'].split('\n')[-2][0] == 'i'
    except IndexError:
        raise ResourceException(ret['stderr'])
Esempio n. 27
0
def is_installed(name):
    ret = exec_cmd("/usr/bin/dpkg-query -l '{0}'".format(name))
    if ret['returncode'] != 0:
        return False

    # There's no way to use return code of any of the dpkg-query options.
    # Instead we use the "state" column of dpkg-query -l
    # So programmaticaly here:
    # 1. Get stdout
    # 2. Split on new line
    # 3. Get the last but one line (last is blank, in any case?)
    # 4. Get first character (i=installed)
    try:
        return ret['stdout'].split('\n')[-2][0] == 'i'
    except IndexError:
        raise ResourceException(ret['stderr'])
Esempio n. 28
0
def get_installed_packages():
    ret = exec_cmd("/usr/bin/dpkg-query -l")
    return ret['stdout'].split('\n')
Esempio n. 29
0
def remove(name):
    ret = exec_cmd("/usr/bin/apt-get -qy remove {0} --force-yes".format(name))
    if ret['returncode'] != 0:
        raise ResourceException(ret['stderr'])
Esempio n. 30
0
def user_del(name):
    ret = exec_cmd("/usr/sbin/userdel {0} -f".format(name))

    # retcode 6 is group doesn't exist. That's what we want.
    if ret['returncode'] != 6 and ret['returncode'] != 0:
        raise ResourceException(ret['stderr'])
Esempio n. 31
0
def set_password(name, password):
    ret = exec_cmd("echo {0}:{1} | chpasswd".format(name, password))
    if ret['returncode'] != 0:
        raise ResourceException(ret['stderr'])
Esempio n. 32
0
def remove(name):
    ret = exec_cmd("/usr/bin/yum -q -y remove %s" % name)
    if ret['returncode'] != 0:
        raise ResourceException(ret['stderr'])
Esempio n. 33
0
def remove(name):
    ret = exec_cmd("/usr/bin/apt-get -qy remove {0} --force-yes".format(name))
    if ret['returncode'] != 0:
        raise ResourceException(ret['stderr'])
Esempio n. 34
0
def user_del(name):
    ret = exec_cmd("/usr/sbin/userdel {0} -f".format(name))
    if ret['returncode'] != 0:
        raise ResourceException(ret['stderr'])
Esempio n. 35
0
def get_installed_packages():
    ret = exec_cmd("/usr/bin/dpkg-query -l")
    return ret['stdout'].split('\n')
Esempio n. 36
0
def set_password(name, password):
    ret = exec_cmd("echo -n {0} | passwd --stdin {1}".format(password, name))
    if ret['returncode'] != 0:
        raise ResourceException(ret['stderr'])
Esempio n. 37
0
def group_del(name):
    ret = exec_cmd("/usr/sbin/groupdel {0}".format(name))
    if ret['returncode'] != 0:
        raise ResourceException(ret['stderr'])
Esempio n. 38
0
def group_mod(name, new_name):
    ret = exec_cmd("/usr/sbin/groupmod -n {0} {1}".format(new_name, name))
    if ret['returncode'] != 0:
        raise ResourceException(ret['stderr'])
Esempio n. 39
0
def install(name):
    ret = exec_cmd("/usr/bin/yum -q -y install %s" % name)
    if ret['returncode'] != 0:
        raise ResourceException(ret['stderr'])
Esempio n. 40
0
def is_installed(name):
    if name:
        ret = exec_cmd("/bin/rpm -q %s" % name)
        return ret['returncode'] == 0
    else:
        return get_installed_packages()
Esempio n. 41
0
def user_del(name):
    ret = exec_cmd("/usr/sbin/userdel {0} -f".format(name))
    if ret['returncode'] != 0:
        raise ResourceException(ret['stderr'])
Esempio n. 42
0
def user_del(name):
    ret = exec_cmd("/usr/sbin/userdel {0} -f".format(name))

    # retcode 6 is group doesn't exist. That's what we want.
    if ret['returncode'] != 6 and ret['returncode'] != 0:
        raise ResourceException(ret['stderr'])
Esempio n. 43
0
def set_password(name, password):
    ret = exec_cmd("echo -n {0} | passwd --stdin {1}".format(password, name))
    if ret['returncode'] != 0:
        raise ResourceException(ret['stderr'])
Esempio n. 44
0
def group_del(name):
    ret = exec_cmd("/usr/sbin/groupdel %s" % name)

    # retcode 6 is group doesn't exist. That's what we want.
    if ret['returncode'] != 6 and ret['returncode'] != 0:
        raise ResourceException(ret['stderr'])
Esempio n. 45
0
def set_password(name, password):
    ret = exec_cmd("echo {0}:{1} | chpasswd".format(name, password))
    if ret['returncode'] != 0:
        raise ResourceException(ret['stderr'])
Esempio n. 46
0
def is_installed(name):
    ret = exec_cmd("/bin/rpm -q {0}".format(name))
    return ret['returncode'] == 0
Esempio n. 47
0
def install(name):
    ret = exec_cmd("/usr/bin/apt-get -qy update")
    ret = exec_cmd("/usr/bin/apt-get -qy install {0} --force-yes".format(name))
    if not is_installed(name):
        raise ResourceException(ret['stderr'])
Esempio n. 48
0
def get_installed_packages():
    ret = exec_cmd("/bin/rpm -qa")
    return ret['stdout'].split('\n')