Skip to content

Commit

Permalink
adds token support for auth
Browse files Browse the repository at this point in the history
  • Loading branch information
gurubert committed Oct 1, 2024
1 parent 45709e8 commit 8ff4064
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions proxmox_migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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)

0 comments on commit 8ff4064

Please sign in to comment.