Example #1
0
def main():
    """
    Let this thing fly
    """
    args = get_args()

    # connect this thing
    si = None
    if args.no_ssl:
        si = SmartConnectNoSSL(host=args.host,
                               user=args.user,
                               pwd=args.password,
                               port=args.port)
    else:
        si = SmartConnect(host=args.host,
                          user=args.user,
                          pwd=args.password,
                          port=args.port)
    # disconnect this thing
    atexit.register(Disconnect, si)

    content = si.RetrieveContent()
    template = None

    template = get_obj(content, [vim.VirtualMachine], args.template)

    if template:
        clone_vm(content, template, args.vm_name, si, args.datacenter_name,
                 args.vm_folder, args.datastore_name, args.cluster_name,
                 args.resource_pool, args.power_on, args.datastorecluster_name)
        if args.opaque_network:
            vm = get_obj(content, [vim.VirtualMachine], args.vm_name)
            add_nic(si, vm, args.opaque_network)
    else:
        print("template not found")
def main():
    """
    Let this thing fly
    """
    parser = cli.Parser()
    parser.add_required_arguments(cli.Argument.VM_NAME, cli.Argument.TEMPLATE)
    # if no locationis provided, thefirst available datacenter, datastore, etc. will be used
    parser.add_optional_arguments(
        cli.Argument.DATACENTER_NAME, cli.Argument.VMFOLDER,
        cli.Argument.DATASTORE_NAME, cli.Argument.DATASTORECLUSTER_NAME,
        cli.Argument.CLUSTER_NAME, cli.Argument.RESOURCE_POOL,
        cli.Argument.POWER_ON, cli.Argument.OPAQUE_NETWORK_NAME)
    args = parser.get_args()
    si = service_instance.connect(args)

    content = si.RetrieveContent()
    template = pchelper.get_obj(content, [vim.VirtualMachine], args.template)

    if template:
        clone_vm(content, template, args.vm_name, args.datacenter_name,
                 args.vm_folder, args.datastore_name, args.cluster_name,
                 args.resource_pool, args.power_on, args.datastorecluster_name)
        if args.opaque_network_name:
            vm = pchelper.get_obj(content, [vim.VirtualMachine], args.vm_name)
            add_nic(si, vm, args.opaque_network_name)
    else:
        print("template not found")
def main():
    """
    Simple command-line program for creating Dummy VM based on Marvel character
    names
    """

    args = get_args()

    if args.public_key_file:
        with open(args.public_key_file) as key_file:
            marvel_public_key = key_file.readline().strip()
    else:
        marvel_public_key = input('Marvel public key: ').strip()

    if args.private_key_file:
        with open(args.private_key_file) as key_file:
            marvel_private_key = key_file.readline().strip()
    else:
        marvel_private_key = input('Marvel private key: ').strip()

    if args.disable_ssl_verification:
        service_instance = connect.SmartConnectNoSSL(host=args.host,
                                                     user=args.user,
                                                     pwd=args.password,
                                                     port=int(args.port))
    else:
        service_instance = connect.SmartConnect(host=args.host,
                                                user=args.user,
                                                pwd=args.password,
                                                port=int(args.port))
    if not service_instance:
        print("Could not connect to the specified host using specified "
              "username and password")
        return -1

    atexit.register(connect.Disconnect, service_instance)

    content = service_instance.RetrieveContent()
    # datacenter = get_obj(content, [vim.Datacenter], args.datacenter)
    vmfolder = get_obj(content, [vim.Folder], args.folder)
    resource_pool = get_obj(content, [vim.ResourcePool], args.resource_pool)

    print("Connecting to Marvel API and retrieving " + str(args.count) +
          " random character(s) ...")

    characters = get_marvel_characters(args.count,
                                       marvel_public_key,
                                       marvel_private_key)

    for name in characters:
        vm_name = 'MARVEL-' + name
        create_dummy_vm(vm_name, service_instance, vmfolder, resource_pool,
                        args.datastore)
        if args.opaque_network:
            vm = get_obj(content, [vim.VirtualMachine], vm_name)
            add_nic(service_instance, vm, args.opaque_network)
    return 0
Example #4
0
def main():
    """
    Simple command-line program for creating Dummy VM based on Marvel character
    names
    """

    args = get_args()

    if args.public_key_file:
        with open(args.public_key_file) as key_file:
            marvel_public_key = key_file.readline().strip()
    else:
        marvel_public_key = input('Marvel public key: ').strip()

    if args.private_key_file:
        with open(args.private_key_file) as key_file:
            marvel_private_key = key_file.readline().strip()
    else:
        marvel_private_key = input('Marvel private key: ').strip()

    if args.disable_ssl_verification:
        service_instance = connect.SmartConnectNoSSL(host=args.host,
                                                     user=args.user,
                                                     pwd=args.password,
                                                     port=int(args.port))
    else:
        service_instance = connect.SmartConnect(host=args.host,
                                                user=args.user,
                                                pwd=args.password,
                                                port=int(args.port))
    if not service_instance:
        print("Could not connect to the specified host using specified "
              "username and password")
        return -1

    atexit.register(connect.Disconnect, service_instance)

    content = service_instance.RetrieveContent()
    # datacenter = get_obj(content, [vim.Datacenter], args.datacenter)
    vmfolder = get_obj(content, [vim.Folder], args.folder)
    resource_pool = get_obj(content, [vim.ResourcePool], args.resource_pool)

    print("Connecting to Marvel API and retrieving " + str(args.count) +
          " random character(s) ...")

    characters = get_marvel_characters(args.count, marvel_public_key,
                                       marvel_private_key)

    for name in characters:
        vm_name = 'MARVEL-' + name
        create_dummy_vm(vm_name, service_instance, vmfolder, resource_pool,
                        args.datastore)
        if args.opaque_network:
            vm = get_obj(content, [vim.VirtualMachine], vm_name)
            add_nic(service_instance, vm, args.opaque_network)
    return 0
Example #5
0
def process_stanza(args):
    '''
    This was put into a function so that we could process multiple "stanzas"
    (there's probably a better name for it) of vm instantion instructions in
    the yaml file.

    However, for now this program is not doing that because we'd need logic
    to separate stanzas that succeeded and failed. Ideally it'd be atomic, but
    in order to achieve that, we'd have to remove the VMs created from
    succeeded stanzas upon hitting a failed one, and that wouldadd complexity
    we don't want at this poin"t.

    :param args: args from parser.parse_args()
    :return:
    '''
    si = None

    if args.no_ssl:
        si = SmartConnectNoSSL(host=args.host,
                               user=args.user,
                               pwd=args.password,
                               port=args.port)
    else:
        si = SmartConnect(host=args.host,
                          user=args.user,
                          pwd=args.password,
                          port=args.port)

    content = si.RetrieveContent()
    template = None
    template = get_obj(content, [vim.VirtualMachine], args.template)
    logging.debug(template)

    if template:
        if type(args.vms) is not list:
            vms = list(args.vms)
        else:
            vms = args.vms

        for one_vm_name in vms:
            clone_vm(content, template, one_vm_name, si, args.datacenter_name,
                     args.vm_folder, args.datastore_name, args.cluster_name,
                     args.resource_pool, args.power_on,
                     args.datastorecluster_name)
            if args.opaque_network:
                vm = get_obj(content, [vim.VirtualMachine], one_vm_name)
                add_nic(si, vm, args.opaque_network)
    else:
        logging.ERROR("template not found")

    Disconnect(si)
def main():
    """
    Simple command-line program for creating Dummy VM based on Marvel character
    names
    """

    parser = cli.Parser()
    parser.add_required_arguments(cli.Argument.DATASTORE_NAME,
                                  cli.Argument.FOLDER_NAME,
                                  cli.Argument.RESOURCE_POOL,
                                  cli.Argument.OPAQUE_NETWORK_NAME)
    parser.add_custom_argument('--count',
                               type=int,
                               required=True,
                               action='store',
                               help='Number of VMs to create')
    # NOTE (hartsock): as a matter of good security practice, never ever
    # save a credential of any kind in the source code of a file. As a
    # matter of policy we want to show people good programming practice in
    # these samples so that we don't encourage security audit problems for
    # people in the future.
    parser.add_custom_argument(
        '--public_key_file',
        required=False,
        action='store',
        help='Name of the file holding your marvel public key,'
        ' the key should be the first only of the file. '
        'Set one up at developer.marvel.com/account')
    parser.add_custom_argument(
        '--private_key_file',
        required=False,
        action='store',
        help='Name of the file holding your marvel private '
        'key, the key should be the only line of the '
        'file. '
        'Set one up at developer.marvel.com/account')

    args = parser.get_args()
    si = service_instance.connect(args)

    if args.public_key_file:
        with open(args.public_key_file) as key_file:
            marvel_public_key = key_file.readline().strip()
    else:
        marvel_public_key = input('Marvel public key: ').strip()

    if args.private_key_file:
        with open(args.private_key_file) as key_file:
            marvel_private_key = key_file.readline().strip()
    else:
        marvel_private_key = input('Marvel private key: ').strip()

    content = si.RetrieveContent()
    vmfolder = pchelper.get_obj(content, [vim.Folder], args.folder_name)
    resource_pool = pchelper.get_obj(content, [vim.ResourcePool],
                                     args.resource_pool)

    print("Connecting to Marvel API and retrieving " + str(args.count) +
          " random character(s) ...")

    characters = get_marvel_characters(args.count, marvel_public_key,
                                       marvel_private_key)

    for name in characters:
        vm_name = 'MARVEL-' + name
        create_dummy_vm(vm_name, si, vmfolder, resource_pool,
                        args.datastore_name)
        if args.opaque_network_name:
            vm = pchelper.get_obj(content, [vim.VirtualMachine], vm_name)
            add_nic(si, vm, args.opaque_network_name)
    return 0