Ejemplo n.º 1
0
 def f():
     app = App(org, space, appname)
     app.find_hosts(configuration)
     app.find_services(configuration)
     if configuration.get('database'):
         # TODO: Implement writing to a DB what we targeted
         assert False
     app.block_services(configuration, services=services)
     return app
Ejemplo n.º 2
0
 def f():
     app = App(org, space, appname)
     if configuration.get('database'):
         # TODO: Implement reading from a DB what we targeted
         assert False
     else:
         app.find_hosts(configuration)
         app.find_services(configuration)
     app.unblock_services(configuration, services=services)
     return app
Ejemplo n.º 3
0
def load_targeted(filename, org, space, name):
    """
    Load a json file of known hosts and services. This allows for the same hosts that were blocked to be unblocked even
    if cloud foundry moves application instances to a different container or diego-cell. This will remove the entries
    for the specific app from the json file.
    :param filename: String; Name of the file to load from.
    :param org: String; Name of the organization the app is in within cloud foundry.
    :param space: String; Name of the space the app is in within cloud foundry.
    :param name: String; Name of the app within cloud foundry.
    :return: Optional[App]; The application with the org, space, and name or None if it was not present.
    """
    with open(filename, 'r') as file:
        j = json.load(file)

    app = App.deserialize(j, org, space, name, readonly=False)

    with open(filename, 'w') as file:
        # dump the json missing the hosts that we are unblocking
        json.dump(j, file, indent=2, sort_keys=True)

    return app
Ejemplo n.º 4
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=======")
Ejemplo n.º 5
0
 def f():
     app = App(org, space, appname)
     app.find_hosts(configuration)
     app.find_services(configuration)
     return app