示例#1
0
import optparse

import gce_credentials


def parse_args():
    parser = optparse.OptionParser(usage="%s [options] <prefix>" % (sys.argv[0],), description=__doc__)
    gce_credentials.add_credentials_options(parser)
    parser.add_option(
        "--prefix", action="store", dest="prefix", help="String used to prefix GCE resource names (default: %default)"
    )

    (opts, args) = parser.parse_args()
    gce_credentials.check_required(opts, parser)
    if not args:
        parser.error("Missing required argument: name prefix")
    return (opts, args)


if __name__ == "__main__":

    (opts, args) = parse_args()
    gce = gce_credentials.get_gce_driver(opts)
    prefix = args[0].lower()
    try:
        base_volume = gce.create_volume(size=10, name=prefix + "-base", location="us-central1-a")
        gce.create_volume_snapshot(base_volume, name=prefix + "-snapshot")
        gce.create_volume(size=10, name=prefix + "-extra", location="us-central1-a")
    except KeyboardInterrupt, e:
        print "\nExiting on user command."
示例#2
0
        dest="match_re",
        default="^ansible-testing-",
        help="Regular expression used to find GCE resources (default: %default)"
    )

    (opts, args) = parser.parse_args()
    gce_credentials.check_required(opts, parser)
    return (opts, args)


if __name__ == '__main__':

    (opts, args) = parse_args()

    # Connect to GCE
    gce = gce_credentials.get_gce_driver(opts)

    try:
        # Delete matching instances
        delete_gce_resources(gce.list_nodes, 'name', opts)

        # Delete matching snapshots
        def get_snapshots():
            for volume in gce.list_volumes():
                for snapshot in gce.list_volume_snapshots(volume):
                    yield snapshot

        delete_gce_resources(get_snapshots, 'name', opts)
        # Delete matching disks
        delete_gce_resources(gce.list_volumes, 'name', opts)
    except KeyboardInterrupt as e: