<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def gatherkeys(args):
 """ Gather keys from any mon and store in current working directory. Backs up keys from previous installs and stores new keys. """

    oldmask = os.umask(0o77)
    try:
        try:
            tmpd = tempfile.mkdtemp()
            LOG.info("Storing keys in temp directory %s", tmpd)
            sucess = False
            for host in args.mon:
                sucess = gatherkeys_with_mon(args, host, tmpd)
                if sucess:
                    break
            if not sucess:
                LOG.error("Failed to connect to host:%s" ,', '.join(args.mon))
                raise RuntimeError('Failed to connect any mon')
            had_error = False
            date_string = time.strftime("%Y%m%d%H%M%S")
            for keytype in ["admin", "mds", "mgr", "mon", "osd", "rgw"]:
                filename = keytype_path_to(args, keytype)
                tmp_path = os.path.join(tmpd, filename)
                if not os.path.exists(tmp_path):
                    LOG.error("No key retrived for '%s'" , keytype)
                    had_error = True
                    continue
                if not os.path.exists(filename):
                    LOG.info("Storing %s" % (filename))
                    shutil.move(tmp_path, filename)
                    continue
                if _keyring_equivalent(tmp_path, filename):
                    LOG.info("keyring '%s' already exists" , filename)
                    continue
                backup_keyring = "%s-%s" % (filename, date_string)
                LOG.info("Replacing '%s' and backing up old key as '%s'", filename, backup_keyring)
                shutil.copy(filename, backup_keyring)
                shutil.move(tmp_path, filename)
            if had_error:
                raise RuntimeError('Failed to get all key types')
        finally:
            LOG.info("Destroy temp directory %s" %(tmpd))
            shutil.rmtree(tmpd)
    finally:
        os.umask(oldmask)