예제 #1
0
파일: agent.py 프로젝트: tipabu/swiftagent
def main(args):
    '''Start or stop a swift-agent server.

    With no arguments, starts a server in the background.
    '''
    parser = argparse.ArgumentParser(description=main.__doc__)
    group = parser.add_mutually_exclusive_group()
    group.add_argument(
        '--daemon',
        dest='socket_addr',
        help='start the server in the foreground, listening on SOCKET_ADDR')
    group.add_argument('--stop',
                       action='store_true',
                       help='stop the server and clean up the socket')
    group.add_argument(
        '--debug',
        action='store_true',
        help='log debugging information to ${HOME}/.swift-agent.log')
    args = parser.parse_args(args[1:])

    if args.socket_addr:
        logging.basicConfig(level=logging.DEBUG)
        server.SwiftAgentServer(args.socket_addr).run()
        return

    cleanup()
    if args.stop:
        io.export({
            client.SOCKET_ENV_VAR: None,
            client.PROCESS_ID_ENV_VAR: None
        })
        return

    if args.debug:
        agent_out = os.open(os.path.expanduser('~/.swift-agent.log'),
                            os.O_WRONLY | os.O_APPEND | os.O_CREAT,
                            stat.S_IRUSR | stat.S_IWUSR)
    else:
        agent_out = open('/dev/null', 'w')

    socket_addr = os.path.join(tempfile.mkdtemp(), 'socket')
    pid = subprocess.Popen([sys.argv[0], '--daemon', socket_addr],
                           preexec_fn=os.setpgrp,
                           stdout=agent_out,
                           stderr=agent_out,
                           stdin=open('/dev/null', 'r')).pid
    io.export({
        client.SOCKET_ENV_VAR: socket_addr,
        client.PROCESS_ID_ENV_VAR: pid
    })
예제 #2
0
파일: agent.py 프로젝트: tipabu/swiftagent
def main(args):
    """Start or stop a swift-agent server.

    With no arguments, starts a server in the background.
    """
    parser = argparse.ArgumentParser(description=main.__doc__)
    group = parser.add_mutually_exclusive_group()
    group.add_argument(
        "--daemon", dest="socket_addr", help="start the server in the foreground, listening on SOCKET_ADDR"
    )
    group.add_argument("--stop", action="store_true", help="stop the server and clean up the socket")
    group.add_argument("--debug", action="store_true", help="log debugging information to ${HOME}/.swift-agent.log")
    args = parser.parse_args(args[1:])

    if args.socket_addr:
        logging.basicConfig(level=logging.DEBUG)
        server.SwiftAgentServer(args.socket_addr).run()
        return

    cleanup()
    if args.stop:
        io.export({client.SOCKET_ENV_VAR: None, client.PROCESS_ID_ENV_VAR: None})
        return

    if args.debug:
        agent_out = os.open(
            os.path.expanduser("~/.swift-agent.log"),
            os.O_WRONLY | os.O_APPEND | os.O_CREAT,
            stat.S_IRUSR | stat.S_IWUSR,
        )
    else:
        agent_out = open("/dev/null", "w")

    socket_addr = os.path.join(tempfile.mkdtemp(), "socket")
    pid = subprocess.Popen(
        [sys.argv[0], "--daemon", socket_addr],
        preexec_fn=os.setpgrp,
        stdout=agent_out,
        stderr=agent_out,
        stdin=open("/dev/null", "r"),
    ).pid
    io.export({client.SOCKET_ENV_VAR: socket_addr, client.PROCESS_ID_ENV_VAR: pid})
예제 #3
0
파일: auth.py 프로젝트: tipabu/swiftagent
def main(args):
    '''Get a storage URL and auth token for a Swift cluster.

    If a swift-agent server seems to be running, that will be used
    to authenticate.
    '''
    parser = argparse.ArgumentParser(description=main.__doc__)
    parser.add_argument(
        '--debug', action='store_true',
        help='include debugging information')
    parser.add_argument(
        'auth', default=None, nargs='?',
        help='the auth endpoint to use')
    parser.add_argument(
        '--verify', action='store_true', default=None,
        help='verify the token is still valid if received from '
             'a swift-agent server')
    parser.add_argument(
        '--no-verify', action='store_false', dest='verify',
        help='skip token verification')
    args = parser.parse_args(args[1:])

    if args.debug:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.WARNING)

    conf = config.SwiftConfig()

    auth = args.auth or conf.default_auth
    if not auth:
        logging.error('No auth endpoint specified, and no default defined')
        return

    if client.can_use_swift_agent():
        authenticator = agent.AgentAuthenticator({'auth_name': auth})
    else:
        authenticator = conf.get_auth(auth)
    storage_url, token, expiry = authenticator.get_credentials()

    if args.verify is None:
        verify = conf.get_default_verify(auth)
    else:
        verify = args.verify

    if verify and client.can_use_swift_agent():
        try:
            models.Cluster(authenticator).default_account.info()
        except base.Unauthorized:
            if authenticator.ever_prompted:
                raise

            password = getpass.getpass()
            sock = os.environ[client.SOCKET_ENV_VAR]
            with client.SwiftAgentClient(sock) as agent_client:
                agent_client.unlock(auth, password)
                storage_url, token, expiry = agent_client.auth(auth)

    io.export({'OS_STORAGE_URL': storage_url,
               'OS_AUTH_TOKEN': token,
               'OS_AUTH_TOKEN_EXPIRES': expiry})
예제 #4
0
def main(args):
    '''Get a storage URL and auth token for a Swift cluster.

    If a swift-agent server seems to be running, that will be used
    to authenticate.
    '''
    parser = argparse.ArgumentParser(description=main.__doc__)
    parser.add_argument('--debug',
                        action='store_true',
                        help='include debugging information')
    parser.add_argument('auth',
                        default=None,
                        nargs='?',
                        help='the auth endpoint to use')
    parser.add_argument(
        '--verify',
        action='store_true',
        default=None,
        help='verify the token is still valid if received from '
        'a swift-agent server')
    parser.add_argument('--no-verify',
                        action='store_false',
                        dest='verify',
                        help='skip token verification')
    args = parser.parse_args(args[1:])

    if args.debug:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.WARNING)

    conf = config.SwiftConfig()

    auth = args.auth or conf.default_auth
    if not auth:
        logging.error('No auth endpoint specified, and no default defined')
        return

    if client.can_use_swift_agent():
        authenticator = agent.AgentAuthenticator({'auth_name': auth})
    else:
        authenticator = conf.get_auth(auth)
    storage_url, token, expiry = authenticator.get_credentials()

    if args.verify is None:
        verify = conf.get_default_verify(auth)
    else:
        verify = args.verify

    if verify and client.can_use_swift_agent():
        try:
            models.Cluster(authenticator).default_account.info()
        except base.Unauthorized:
            if authenticator.ever_prompted:
                raise

            password = getpass.getpass()
            sock = os.environ[client.SOCKET_ENV_VAR]
            with client.SwiftAgentClient(sock) as agent_client:
                agent_client.unlock(auth, password)
                storage_url, token, expiry = agent_client.auth(auth)

    io.export({
        'OS_STORAGE_URL': storage_url,
        'OS_AUTH_TOKEN': token,
        'OS_AUTH_TOKEN_EXPIRES': expiry
    })