Ejemplo n.º 1
0
 def f():
     app = App(org, space, appname)
     app.find_hosts(configuration)
     app.block(configuration)
     if configuration.get('database'):
         # TODO: Implement writing to a DB what we targeted
         assert False
     return app
Ejemplo n.º 2
0
def main(*args):
    """
    The function which should be called if this is being used as an executable and not being imported as a library.
    It should also give an idea of what functions need to be called an in what order to block or unblock an application.
    """

    parser = ArgumentParser(description='Block Cloud Foundry Applications or their Services.')
    parser.add_argument('org', type=str,
                        help='Cloud Foundry Organization the Application is in.')
    parser.add_argument('space', type=str,
                        help='Cloud Foundry Space the Application is in.')
    parser.add_argument('app', type=str,
                        help='Name of the application in Cloud Foundry.')

    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument('--block', dest='action', action='store_const', const='block',
                       help='Block access to the application.')
    group.add_argument('--block-services', dest='action', action='store_const', const='block_services',
                       help='Block the app from accessing its bound services.')
    group.add_argument('--unblock', dest='action', action='store_const', const='unblock',
                       help='Unblock the app and its services.')
    group.add_argument('--discover', dest='action', action='store_const', const='discover',
                       help='Discover the application hosts and bound service information.')

    parser.add_argument('--config', metavar='PATH', type=str, default=DEFAULT_CONFIG,
                        help='Specify an alternative config path.')
    parser.add_argument('--targeted', metavar='PATH', type=str, default=DEFAULT_TARGETED,
                        help='Specify an alternative storage location for targeted applications and services.')

    if args[0].endswith('.py'):
        args = args[1:]

    args = parser.parse_args(args)

    with open(args.config, 'r') as file:
        cfg = yaml.load(file)

    action = args.action
    org, space, appname = args.org, args.space, args.app

    if cf_target(org, space, cfg):
        logger.error("Failed to target {} and {}. Make sure you are logged in and the names are correct!"
                     .format(org, space))
        exit(1)

    if action == 'block':
        app = App(org, space, appname)
        app.find_hosts(cfg)
        save_targeted(args.targeted, app)
        app.block(cfg)
    elif action == 'unblock':
        app = load_targeted(args.targeted, org, space, appname)
        if app is None:
            exit(0)

        app.unblock(cfg)
        app.unblock_services(cfg)
    elif action == 'block_services':
        app = App(org, space, appname)
        app.find_hosts(cfg)
        app.find_services(cfg)
        save_targeted(args.targeted, app)
        app.block_services(cfg)
    elif action == 'discover':
        app = App(org, space, appname)
        app.find_hosts(cfg)
        app.find_services(cfg)

        print('\n---')  # add a newline
        yaml.dump(app.serialize(), stream=sys.stdout)
    else:
        # With argument parsing, this should never happen
        assert False

    print("\n=======\n Done!\n=======")