From 8ff40642f0ac5db80e3a77d02460f41710dc7174 Mon Sep 17 00:00:00 2001 From: Robert Sander Date: Tue, 1 Oct 2024 14:25:40 +0200 Subject: [PATCH] adds token support for auth --- proxmox_migrate.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/proxmox_migrate.py b/proxmox_migrate.py index 155e6b4..682f9e3 100755 --- a/proxmox_migrate.py +++ b/proxmox_migrate.py @@ -241,7 +241,9 @@ def hostname_from_fqdn(fqdn): parser = argparse.ArgumentParser() parser.add_argument('-u', '--username', required=True) -parser.add_argument('-p', '--password', required=True) +auth = parser.add_mutually_exclusive_group(required=True) +auth.add_argument('-p', '--password', help="Use the account's password.") +auth.add_argument('-t', '--token', nargs=2, metavar=("NAME", "VALUE"), help="Use an API token. Arguments are token name and token value.") parser.add_argument('-n', '--dryrun', action='store_true', required=False) parser.add_argument('-d', '--debug', action='store_true', required=False) parser.add_argument('-w', '--wait', action='store_true', required=False) @@ -272,7 +274,10 @@ def hostname_from_fqdn(fqdn): if args.func == 'evacuate': - proxmox = ProxmoxAPIext(args.source, user=args.username, password=args.password, verify_ssl=vssl) + if args.password: + proxmox = ProxmoxAPIext(args.source, user=args.username, password=args.password, verify_ssl=vssl) + else: + proxmox = ProxmoxAPIext(args.source, user=args.username, token_name=args.token[0], token_value=args.token[1], verify_ssl=vssl) vms = proxmox.get_vms(lambda x: x['status'] == 'running' and x['node'] == hostname_from_fqdn(args.source)) if args.debug: @@ -284,7 +289,10 @@ def hostname_from_fqdn(fqdn): if len(args.nodes) < 2: raise RuntimeError('List of nodes is too short: %s' % ', '.join(args.nodes)) - proxmox = ProxmoxAPIext(args.nodes[0], user=args.username, password=args.password, verify_ssl=vssl) + if args.password: + proxmox = ProxmoxAPIext(args.nodes[0], user=args.username, password=args.password, verify_ssl=vssl) + else: + proxmox = ProxmoxAPIext(args.nodes[0], user=args.username, token_name=args.token[0], token_value=args.token[1], verify_ssl=vssl) vms = proxmox.get_vms(lambda x: x['status'] == 'running' and x['node'] in map(hostname_from_fqdn, args.nodes)) if args.debug: @@ -294,6 +302,9 @@ def hostname_from_fqdn(fqdn): elif args.func == 'migrate': - proxmox = ProxmoxAPIext(args.dst, user=args.username, password=args.password, verify_ssl=vssl) + if args.password: + proxmox = ProxmoxAPIext(args.dst, user=args.username, password=args.password, verify_ssl=vssl) + else: + proxmox = ProxmoxAPIext(args.dst, user=args.username, token_name=args.token[0], token_value=args.token[1], verify_ssl=vssl) if not proxmox.migrate_vmid(args.vmid, hostname_from_fqdn(args.dst)): sys.exit(1)