Ejemplo n.º 1
0
    def _expand_globs(self):
        """
        Returns the hosts that expand out from globs.

        This is kind of a dirty hack around how Func returns minions
        in an arbitrary order.
        """

        import func.overlord.client as fc

        if not self._hosts:
            return []
        if isinstance(self._hosts, basestring):
            glob = self._hosts
            c = fc.Client(glob)
            return c.list_minions()
        else:
            # Iterate over each given item, expand it, and then push
            # it onto our list. But only if it doesn't exist already!
            found_hosts = []
            for h in self._hosts:
                c = fc.Client(h)
                new_hosts = filter(lambda h: h not in found_hosts,
                                   c.list_minions())
                found_hosts.extend(new_hosts)
                # for found_host in c.list_minions():
                #     h = filter
                #     if not found_host in found_hosts:
                #         found_hosts.append(found_host)
            return found_hosts
Ejemplo n.º 2
0
def Upd_Root_Passwd(hostname, passwd):
    import func.overlord.client as func
    """
    #--- test only ---
    dic_tmp = {}
    dic_tmp[hostname] = [0,'updated successfully']
    lst_res = []
    
    if dic_tmp[hostname][0] == 0 and re.search(r'updated successfully', dic_tmp[hostname][1]):
        str_res = 'True'
    else:
        str_res = 'False_CMD'
        
    return str_res
        
    #--- test only ---
    """
    cmd_upd_passwd = "echo '%s'|passwd --stdin root" % passwd

    client = func.Client(hostname)
    try:
        dic_tmp = client.command.run(cmd_upd_passwd)
        if dic_tmp[hostname][0] == 0 and re.search(r'updated successfully', dic_tmp[hostname][1]):
            str_res = 'True'

        else:
            str_res = 'False_CMD'

    except:
        str_res = 'False_FUNC'

    return str_res
Ejemplo n.º 3
0
    def __init__(self, project, camp_id):
        self.func = fc.Client(settings.FUNC_WEB_HOST)
        self.camp_id = int(camp_id)
        self.campname = settings.CAMPS_BASENAME + str(self.camp_id)
        self.project = project

        if project in settings.EXTERNAL_HOOKS:
            self.hook = settings.EXTERNAL_HOOKS[project]
        else:
            self.hook = None
Ejemplo n.º 4
0
def get_config_files(source):
    LOGGER.info("Starting Nagios deployment")

    dest = "CHISP" + environment() + "SRV01" + "." + domain()
    sourcefolder = source.split(".")[0].upper()
    server = fc.Client(dest)
    client = fc.Client(source)

    LOGGER.debug("Gathering nagios config files from %s" % source)
    client.command.run("tar zcvf /tmp/nagios.tgz /etc/nagios/cells/*")

    remotesource = '/tmp/nagios.tgz'
    localfolder = '/tmp/'

    LOGGER.debug("Pulling nagios files from  management server")
    client.local.getfile.get(remotesource, localfolder)

    LOGGER.debug("Sending nagios tarball to " + dest)
    LOGGER.info("sourcefolder=/tmp/" + sourcefolder + "." + domain() +
                "/nagios.tgz")
    print server.local.copyfile.send(
        "/tmp/" + sourcefolder + "." + domain() + "/nagios.tgz",
        "/tmp/nagios.tgz")

    LOGGER.debug("Extracting nagios config files to " + dest)
    server.command.run("tar zxvf /tmp/nagios.tgz -C / --exclude .svn "
                       "--exclude servicegroups.cfg")
    server.command.run("sed -i 's/.*check_command.*/\tcheck_command\t\t\t"
                       "service-is-stale/g' /etc/nagios/cells/" +
                       sourcefolder + "/services.cfg")
    server.command.run("rm /etc/nagios/cells/" + sourcefolder +
                       "/hostgroups.cfg")
    server.command.run("rm /etc/nagios/cells/" + sourcefolder +
                       "/services.cfg")

    LOGGER.debug("Restarting Nagios agent on " + dest)
    server.command.run("/etc/init.d/nagios restart")
    LOGGER.info("Finished Nagios deployment")
Ejemplo n.º 5
0
    def _expand_globs(self):
        """
        Returns the hosts that expand out from globs.
        """

        import func.overlord.client as fc

        if not self._hosts:
            return []
        if isinstance(self._hosts, basestring):
            glob = self._hosts
        else:
            glob = ';'.join(self._hosts)
        c = fc.Client(glob)
        return c.list_minions()
Ejemplo n.º 6
0
def distribute(client, modules):
    minions = client.list_minions()
    for i in minions:
        minion = fc.Client(i)
        pyver = python_version(minion)
        if not pyver:
            continue
        for mod in modules:
            if not minion.local.copyfile.send(
                    mod, '/usr/lib/python' + pyver +
                    '/site-packages/func/minion/modules/' + mod):
                print('Failed to distribute ' + mod + ' to ' +
                      minion.list_minions())
        if not restart_func(minion):
            print('Failed to restart funcd on ' + minion.list_minions())
Ejemplo n.º 7
0
def remove_config_files(source):
    """ Delete current config files """

    dest = "CHISP" + environment() + "SRV01" + "." + domain()
    sourcefolder = source.split(".")[0].upper()
    LOGGER.info("sourcefolder=/tmp/" + sourcefolder + "." + domain() +
                "/nagios.tgz")

    server = fc.Client(dest)
    LOGGER.info("Removing Nagios Configs for cell")

    # Delete Directory
    server.command.run("rm -rf /etc/nagios/cells/" + sourcefolder + "/")
    print "/etc/nagios/cells/" + sourcefolder + "/"

    # Restart Nagios
    server.command.run("/etc/init.d/nagios reload")
Ejemplo n.º 8
0
def chroottest():
    envdict = {}
    print 'Get the env. variable AUGEAS_ROOT'
    for host, envlist in c.confmgt_augeas.getenv('AUGEAS_ROOT').iteritems():
        print "Host: " + host
        augroot = envlist.get('AUGEAS_ROOT')
        print 'AUGEAS_ROOT: ' + augroot
        envdict[host] = augroot
        if augroot == '/' or augroot == '(none)':
            print "The node " + host + " is not chrooted with AUGEAS_ROOT"
            print "You should consider setting this variable"
            print "before launching this test."
            sys.exit()
    print

    print 'Prepare the test environment'
    for host in envdict:
        augroot = envdict[host]
        client = fc.Client(host)
        print client.command.run('mkdir -p ' + augroot + '/etc/ssh')
        print client.command.run('cp /etc/hosts ' + augroot + '/etc')
        print client.command.run('cp -r /etc/ssh/* ' + augroot + '/etc/ssh')
    print
Ejemplo n.º 9
0
    def run(self, args):

        p = optparse.OptionParser()
        p.add_option("-v",
                     "--verbose",
                     dest="verbose",
                     action="store_true",
                     help="provide extra output")
        p.add_option("-s",
                     "--server-spec",
                     dest="server_spec",
                     default="*",
                     help="run against specific servers, default: '*'")
        p.add_option(
            "-m",
            "--memory",
            dest="memory",
            default="512",
            help="the memory requirements in megabytes, default: '512'")
        p.add_option("-a",
                     "--arch",
                     dest="arch",
                     default="i386",
                     help="the architecture requirements, default: 'i386'")

        (options, args) = p.parse_args(args)
        self.options = options

        # convert the memory and storage to integers for later comparisons
        memory = int(options.memory)
        arch = options.arch

        # see what hosts have enough RAM
        avail_hosts = {}
        host_freemem = func_client.Client(options.server_spec).virt.freemem()
        for (host, freemem) in host_freemem.iteritems():
            if utils.is_error(freemem):
                print "-- connection refused: %s" % host
                continue

            # Take an additional 256M off the freemem to keep
            # Domain-0 stable (shrinking it to 256M can cause
            # it to crash under load)
            if (freemem - 256) >= memory:
                avail_hosts[host] = {'memory': freemem}

        # Default the dest_host to nothing
        dest_host = None

        # see what hosts have the right architecture
        arch_hosts = {}
        host_arch = func_client.Client(
            options.server_spec).command.run('uname -i')
        for (host, output) in host_arch.iteritems():
            if utils.is_error(output):
                print "-- connection refused: %s" % host
                continue

            host_arch = output[1].rstrip()

            # If the host_arch is 64 bit, allow 32 bit machines on it
            if host_arch == arch or (host_arch == "x86_64" and arch == "i386"):
                arch_hosts[host] = host

        if len(avail_hosts) > 0:
            # Find the host that is the closest memory match
            # and matching architecture
            for (host, attrs) in avail_hosts.iteritems():
                if arch_hosts.has_key(host):
                    if not dest_host:
                        dest_host = [host, attrs['memory']]
                    else:
                        if attrs['memory'] < dest_host[1]:
                            # Use the better match
                            dest_host = [host, attrs['memory']]

        return dest_host
Ejemplo n.º 10
0
    def connect(self, port=None):
        if not HAVE_FUNC:
            raise errors.AnsibleError("func is not installed")

        self.client = fc.Client(self.host)
        return self
Ejemplo n.º 11
0
def basictest():
    #print 'Backup sshd_config'
    #print c.confmgt_augeas.backup('/etc/ssh/sshd_config')
    #print

    print 'Delete the Parameter PermitRootLogin in sshd_config'
    print c.confmgt_augeas.rm('/etc/ssh/sshd_config', 'PermitRootLogin')
    print

    print 'Delete the Parameter Port in sshd_config with an Augeas-style path'
    print c.confmgt_augeas.rm('/etc/ssh/sshd_config/Port')
    print

    print 'Get sshd_config Port value.'
    print c.confmgt_augeas.get('/etc/ssh/sshd_config', 'Port')
    print

    print 'Set Port to 22 in sshd_config'
    print c.confmgt_augeas.set('/etc/ssh/sshd_config', 'Port', '22')
    print

    print 'Get sshd_config Port value.'
    print c.confmgt_augeas.get('/etc/ssh/sshd_config', 'Port')
    print

    print 'Try to delete a non existant parameter in sshd_config'
    print c.confmgt_augeas.rm('/etc/ssh/sshd_config', 'Nawak')
    print

    print 'Try to delete a parameter in a non existant file.'
    print c.confmgt_augeas.rm('/etc/ssh/nimp', 'Nawak')
    print

    print 'Get sshd_config PermitRootLogin value.'
    print c.confmgt_augeas.get('/etc/ssh/sshd_config', 'PermitRootLogin')
    print

    print 'Set PermitRootLogin to yes in sshd_config'
    print c.confmgt_augeas.set('/etc/ssh/sshd_config', 'PermitRootLogin',
                               'yes')
    print

    print 'Set PermitRootLogin to no in sshd_config with an Augeas-style path.'
    print c.confmgt_augeas.set('/etc/ssh/sshd_config/PermitRootLogin', '',
                               'no')
    print

    print 'Set PermitRootLogin to yes in sshd_config with an Augeas-style path.'
    print c.confmgt_augeas.set('/etc/ssh/sshd_config/PermitRootLogin', '',
                               'yes')
    print

    print 'Get sshd_config PermitRootLogin value.'
    print c.confmgt_augeas.get('/etc/ssh/sshd_config', 'PermitRootLogin')
    print

    print 'Get sshd_config PermitRootLogin value with an Augeas-style path.'
    print c.confmgt_augeas.get('/etc/ssh/sshd_config/PermitRootLogin')
    print

    print 'Attempt to get a value for a non existant param in sshd_config'
    print c.confmgt_augeas.get('/etc/ssh/sshd_config', 'Nawak')
    print

    print 'Attempt to get a value for an empty param in sshd_config'
    print c.confmgt_augeas.get('/etc/ssh/sshd_config', 'Subsystem')
    print

    print 'Search for conf. entry in hosts file with canonical hostname = localhost'
    print c.confmgt_augeas.match('/etc/hosts', 'canonical', 'localhost')
    print

    #print 'List all direct children of hosts (not very useful)'
    #print c.confmgt_augeas.ls('/etc/hosts/*')
    #print

    print 'List all direct children parameters of 1st hosts entry.'
    for host, paramlist in c.confmgt_augeas.ls('/etc/hosts/1').iteritems():
        print "Host: " + host
        if type(paramlist) == type({}):
            for node in paramlist['nodes']:
                print node[0] + " = " + node[1]
        else:
            print paramlist
    print

    print 'List all children nodes of 1st hosts entry.'
    for host, paramlist in c.confmgt_augeas.printconf(
            '/etc/hosts/1').iteritems():
        print "Host: " + host
        if type(paramlist) == type({}):
            for node in paramlist['nodes']:
                print node[0] + " = " + node[1]
        else:
            print paramlist
    print

    print 'Get values of 1st host entry.'
    print c.confmgt_augeas.get('/etc/hosts/', '1')
    print

    print 'List all values for parameter of 1st fstab entry.'
    minionDict = c.confmgt_augeas.ls('/etc/fstab/1')
    for host, entry in minionDict.iteritems():
        print "Host: " + host
        if type(entry) == type({}):
            print "Entry path: " + entry['path']
            for node in entry['nodes']:
                print node[0] + " = " + node[1]
        else:
            print entry
    print

    print 'Get ipaddr of /etc/hosts 1st entry.'
    print c.confmgt_augeas.get('/etc/hosts/1', 'ipaddr')
    print
    #
    #print 'List all direct children parameters of sshd_config'
    #for host,paramlist in c.confmgt_augeas.ls('/etc/ssh/sshd_config').iteritems():
    #   print "Host: "+host
    #   for node in paramlist['nodes']:
    #       print node[0]+" = "+node[1]
    #print
    #
    print 'List all children nodes of sshd_config'
    for host, paramlist in c.confmgt_augeas.printconf(
            '/etc/ssh/sshd_config').iteritems():
        print "Host: " + host
        for node in paramlist['nodes']:
            print node[0] + " = " + node[1]
    print

    print 'List all direct children of AcceptEnv entries in sshd_config'
    for host, paramlist in c.confmgt_augeas.ls(
            '/etc/ssh/sshd_config/AcceptEnv').iteritems():
        print "Host: " + host
        if type(paramlist) == type({}):
            for node in paramlist['nodes']:
                print node[0] + " = " + node[1]
        else:
            print paramlist
    print

    print 'See all AcceptEnv entries in sshd_config'
    for host, paramlist in c.confmgt_augeas.printconf(
            '/etc/ssh/sshd_config/AcceptEnv').iteritems():
        print "Host: " + host
        if type(paramlist) == type({}):
            for node in paramlist['nodes']:
                print node[0] + " = " + node[1]
        else:
            print paramlist
    print

    print 'Try to match PermitRootLogin yes in sshd_config'
    print c.confmgt_augeas.match('/etc/ssh/sshd_config', 'PermitRootLogin',
                                 'yes')
    print

    print 'Try to match PermitRootLogin yes in sshd_config with an Augeas-style path'
    print c.confmgt_augeas.match('/etc/ssh/sshd_config/PermitRootLogin', '',
                                 'yes')
    print

    print 'Try to match PermitRootLogin yes in some config. files.'
    print c.confmgt_augeas.match('/etc/*/*', 'PermitRootLogin', 'yes')
    print

    print 'Try to match AcceptEnv in sshd_config'
    print c.confmgt_augeas.match('/etc/ssh/sshd_config', 'AcceptEnv')
    print

    print 'Try to match PermitRootLogin in sshd_config'
    print c.confmgt_augeas.match('/etc/ssh/sshd_config', 'PermitRootLogin')
    print

    print 'Try to match PermitRootLogin in sshd_config with an Augeas-style path.'
    print c.confmgt_augeas.match('/etc/ssh/sshd_config/PermitRootLogin')
    print

    print 'Try to match canonical entries in hosts file.'
    print c.confmgt_augeas.match('/etc/hosts', 'canonical')
    print

    print 'Try to match canonical entries in hosts file with an Augeas-style path.'
    print c.confmgt_augeas.match('/etc/hosts/*/canonical')
    print

    print 'Augeas metainformation.'
    print c.confmgt_augeas.ls('/', '/augeas')
    print c.confmgt_augeas.get('/', 'save', '/augeas')

    #Not supposed to work:
    print c.confmgt_augeas.set('/', 'save', 'backup', '/augeas')
    print c.confmgt_augeas.set('/save', '', 'backup', '/augeas')

    print c.confmgt_augeas.get('/save', '', '/augeas')
    print c.confmgt_augeas.get('/files/etc/hosts/lens', 'info', '/augeas')

    print 'Add a new variable FOO at the end of the last AcceptEnv line of sshd_config'
    print "And we don't want to do this twice."
    foomatch = c.confmgt_augeas.match('/etc/ssh/sshd_config', 'AcceptEnv/*',
                                      'FOO')
    for host, matchlist in foomatch.iteritems():
        if not matchlist:
            client = fc.Client(host)
            print client.confmgt_augeas.set(
                '/etc/ssh/sshd_config/AcceptEnv[last()]', '10000', 'FOO')
    print

    print 'Change the (canonical) hostname associated to a specific IP in hosts file.'
    hostfile = '/etc/hosts'
    ip = '1.2.3.4'
    newCanonical = 'fozzie'
    #newCanonical='piggy'
    # We search which entry in /etc/hosts refers to the IP
    ipmatch = c.confmgt_augeas.match(hostfile, 'ipaddr', ip)
    # for each minion concerned
    for host, entry in ipmatch.iteritems():
        # The first and unique entry in the list, entry[0], is what we searched for
        # We check that the target canonical hostname is not already set
        if (type(entry) == type([]) and entry):
            oldCanonical = c.confmgt_augeas.get(entry[0],
                                                'canonical')[host]['value']
            if oldCanonical != newCanonical:
                print c.confmgt_augeas.set(entry[0], 'canonical', newCanonical)
            else:
                print 'Nothing to do'
        else:
            print repr(entry) + ' - no match'
    print
Ejemplo n.º 12
0
#!/usr/bin/env python

import sys
import func.overlord.client as fc
c = fc.Client("*")

print c.configmgt_augeas.getenv('AUGEAS_ROOT')


def chroottest():
    envdict = {}
    print 'Get the env. variable AUGEAS_ROOT'
    for host, envlist in c.confmgt_augeas.getenv('AUGEAS_ROOT').iteritems():
        print "Host: " + host
        augroot = envlist.get('AUGEAS_ROOT')
        print 'AUGEAS_ROOT: ' + augroot
        envdict[host] = augroot
        if augroot == '/' or augroot == '(none)':
            print "The node " + host + " is not chrooted with AUGEAS_ROOT"
            print "You should consider setting this variable"
            print "before launching this test."
            sys.exit()
    print

    print 'Prepare the test environment'
    for host in envdict:
        augroot = envdict[host]
        client = fc.Client(host)
        print client.command.run('mkdir -p ' + augroot + '/etc/ssh')
        print client.command.run('cp /etc/hosts ' + augroot + '/etc')
        print client.command.run('cp -r /etc/ssh/* ' + augroot + '/etc/ssh')
Ejemplo n.º 13
0
#!/usr/bin/python

import sys
import func.overlord.client as fc
import xmlrpclib

module = sys.argv[1]
pythonmodulepath = "/usr/lib/python2.6/site-packages/func/minion/modules/"
client = fc.Client("*")

fb = file(pythonmodulepath + module, "r").read()
data = xmlrpclib.Binary(fb)

print client.copyfile.copyfile(pythonmodulepath + module, data)
print client.command.run("/etc/init.d/funcd restart")
Ejemplo n.º 14
0
        help=
        "select group(s) separated by semicolons (e.g. -g 'directors;mysql')")
    parser.add_option(
        "-n",
        "--no-check",
        action="store_true",
        dest="no_check",
        default=False,
        help="do not run module through pyflakes before distributing")

    (options, args) = parser.parse_args()
    if options.minions:
        clients = options.minions
    if options.groups:
        if not options.minions:
            clients = ''
        groups = ['@' + i for i in options.groups.split(';')]
        for i in groups:
            clients = clients + ';' + i
        clients = clients.strip(';')

    if len(args) < 1:
        parser.error("need at least one module to distribute")
        sys.exit(1)
    if not options.no_check:
        for mod in args:
            check_module(mod)

    client = fc.Client(clients)
    distribute(client, args)