argparse
This commit is contained in:
parent
6d2a43a357
commit
f9e940e509
1 changed files with 29 additions and 41 deletions
70
virt-back
70
virt-back
|
|
@ -16,8 +16,8 @@ the latest version may be downloaded here:
|
||||||
https://git.unturf.com/python/virt-back
|
https://git.unturf.com/python/virt-back
|
||||||
"""
|
"""
|
||||||
|
|
||||||
"""The varible doms represents a list of libvirt dom objects.
|
"""The variable doms represents a list of libvirt dom objects.
|
||||||
Use the Domfetcher class to aquire lists of dom objects."""
|
Use the Domfetcher class to acquire a list of dom objects."""
|
||||||
|
|
||||||
import libvirt
|
import libvirt
|
||||||
import tarfile
|
import tarfile
|
||||||
|
|
@ -26,11 +26,9 @@ import re
|
||||||
from time import sleep
|
from time import sleep
|
||||||
from datetime import date
|
from datetime import date
|
||||||
from sys import exit
|
from sys import exit
|
||||||
from optparse import OptionParser, OptionGroup
|
import argparse
|
||||||
from os import path
|
from os import path, remove
|
||||||
from os import remove
|
|
||||||
from shutil import move, copy2
|
from shutil import move, copy2
|
||||||
|
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
@ -417,12 +415,10 @@ def rotate(target, retention=3):
|
||||||
def getoptions():
|
def getoptions():
|
||||||
"""Fetch cli args, parse and map to python, test sanity"""
|
"""Fetch cli args, parse and map to python, test sanity"""
|
||||||
|
|
||||||
# create an option parcer object
|
# create an argument parser object
|
||||||
p = OptionParser()
|
parser = argparse.ArgumentParser(description=DESCRIPTION)
|
||||||
|
|
||||||
p.set_description(DESCRIPTION)
|
parser.add_argument(
|
||||||
|
|
||||||
p.add_option(
|
|
||||||
"-q",
|
"-q",
|
||||||
"--quiet",
|
"--quiet",
|
||||||
dest="quiet",
|
dest="quiet",
|
||||||
|
|
@ -431,7 +427,7 @@ def getoptions():
|
||||||
help="prevent output to stdout",
|
help="prevent output to stdout",
|
||||||
)
|
)
|
||||||
|
|
||||||
p.add_option(
|
parser.add_argument(
|
||||||
"-d",
|
"-d",
|
||||||
"--date",
|
"--date",
|
||||||
dest="tardate",
|
dest="tardate",
|
||||||
|
|
@ -440,7 +436,7 @@ def getoptions():
|
||||||
help="append date to tar filename [default: no date]",
|
help="append date to tar filename [default: no date]",
|
||||||
)
|
)
|
||||||
|
|
||||||
p.add_option(
|
parser.add_argument(
|
||||||
"-g",
|
"-g",
|
||||||
"--no-gzip",
|
"--no-gzip",
|
||||||
dest="nogzip",
|
dest="nogzip",
|
||||||
|
|
@ -449,17 +445,17 @@ def getoptions():
|
||||||
help="do not gzip the resulting tar file",
|
help="do not gzip the resulting tar file",
|
||||||
)
|
)
|
||||||
|
|
||||||
p.add_option(
|
parser.add_argument(
|
||||||
"-a",
|
"-a",
|
||||||
"--retention",
|
"--retention",
|
||||||
dest="retention",
|
dest="retention",
|
||||||
metavar="amount",
|
metavar="amount",
|
||||||
default=3,
|
default=3,
|
||||||
type="int",
|
type=int,
|
||||||
help="backups to retain [default: 3]",
|
help="backups to retain [default: 3]",
|
||||||
)
|
)
|
||||||
|
|
||||||
p.add_option(
|
parser.add_argument(
|
||||||
"-p",
|
"-p",
|
||||||
"--path",
|
"--path",
|
||||||
dest="backpath",
|
dest="backpath",
|
||||||
|
|
@ -468,19 +464,17 @@ def getoptions():
|
||||||
help="backup path [default: '/KVMBACK']",
|
help="backup path [default: '/KVMBACK']",
|
||||||
)
|
)
|
||||||
|
|
||||||
p.add_option(
|
parser.add_argument(
|
||||||
"-u", "--uri", dest="uri", metavar="'URI'", help="optional hypervisor uri"
|
"-u", "--uri", dest="uri", metavar="'URI'", help="optional hypervisor uri"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Actions for info testing: These options display info/ test a list of guests only.
|
# Actions for info testing: These options display info/ test a list of guests only.
|
||||||
|
info_group = parser.add_argument_group(
|
||||||
g0 = OptionGroup(
|
|
||||||
p,
|
|
||||||
"Actions for info testing",
|
"Actions for info testing",
|
||||||
"These options display info or test a list of guests.",
|
"These options display info or test a list of guests.",
|
||||||
)
|
)
|
||||||
|
|
||||||
g0.add_option(
|
info_group.add_argument(
|
||||||
"-i",
|
"-i",
|
||||||
"--info",
|
"--info",
|
||||||
dest="info",
|
dest="info",
|
||||||
|
|
@ -489,7 +483,7 @@ def getoptions():
|
||||||
help="info/test a list of guests (space delimited dom names)",
|
help="info/test a list of guests (space delimited dom names)",
|
||||||
)
|
)
|
||||||
|
|
||||||
g0.add_option(
|
info_group.add_argument(
|
||||||
"--info-all",
|
"--info-all",
|
||||||
dest="infoall",
|
dest="infoall",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
|
|
@ -498,14 +492,12 @@ def getoptions():
|
||||||
)
|
)
|
||||||
|
|
||||||
# WARNING: Dangerous options below, option grouping for scary actions
|
# WARNING: Dangerous options below, option grouping for scary actions
|
||||||
|
action_group = parser.add_argument_group(
|
||||||
g1 = OptionGroup(
|
|
||||||
p,
|
|
||||||
"Actions for a list of dom names",
|
"Actions for a list of dom names",
|
||||||
"WARNING: These options WILL bring down guests!",
|
"WARNING: These options WILL bring down guests!",
|
||||||
)
|
)
|
||||||
|
|
||||||
g1.add_option(
|
action_group.add_argument(
|
||||||
"-b",
|
"-b",
|
||||||
"--backup",
|
"--backup",
|
||||||
dest="backup",
|
dest="backup",
|
||||||
|
|
@ -514,7 +506,7 @@ def getoptions():
|
||||||
help="backup a list of guests (space delimited dom names)",
|
help="backup a list of guests (space delimited dom names)",
|
||||||
)
|
)
|
||||||
|
|
||||||
g1.add_option(
|
action_group.add_argument(
|
||||||
"-r",
|
"-r",
|
||||||
"--reboot",
|
"--reboot",
|
||||||
dest="reboot",
|
dest="reboot",
|
||||||
|
|
@ -523,7 +515,7 @@ def getoptions():
|
||||||
help="reboot a list of guests (space delimited dom names)",
|
help="reboot a list of guests (space delimited dom names)",
|
||||||
)
|
)
|
||||||
|
|
||||||
g1.add_option(
|
action_group.add_argument(
|
||||||
"-s",
|
"-s",
|
||||||
"--shutdown",
|
"--shutdown",
|
||||||
dest="shutdown",
|
dest="shutdown",
|
||||||
|
|
@ -532,7 +524,7 @@ def getoptions():
|
||||||
help="shutdown a list of guests (space delimited dom names)",
|
help="shutdown a list of guests (space delimited dom names)",
|
||||||
)
|
)
|
||||||
|
|
||||||
g1.add_option(
|
action_group.add_argument(
|
||||||
"-c",
|
"-c",
|
||||||
"--create",
|
"--create",
|
||||||
dest="create",
|
dest="create",
|
||||||
|
|
@ -541,11 +533,11 @@ def getoptions():
|
||||||
help="start a list of guests (space delimited dom names)",
|
help="start a list of guests (space delimited dom names)",
|
||||||
)
|
)
|
||||||
|
|
||||||
g2 = OptionGroup(
|
all_group = parser.add_argument_group(
|
||||||
p, "Actions for all doms", "WARNING: These options WILL bring down ALL guests!"
|
"Actions for all doms", "WARNING: These options WILL bring down ALL guests!"
|
||||||
)
|
)
|
||||||
|
|
||||||
g2.add_option(
|
all_group.add_argument(
|
||||||
"--backup-all",
|
"--backup-all",
|
||||||
dest="backupall",
|
dest="backupall",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
|
|
@ -553,7 +545,7 @@ def getoptions():
|
||||||
help="attempt to shutdown, backup, and start ALL guests",
|
help="attempt to shutdown, backup, and start ALL guests",
|
||||||
)
|
)
|
||||||
|
|
||||||
g2.add_option(
|
all_group.add_argument(
|
||||||
"--reboot-all",
|
"--reboot-all",
|
||||||
dest="rebootall",
|
dest="rebootall",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
|
|
@ -561,7 +553,7 @@ def getoptions():
|
||||||
help="attempt to shutdown and then start ALL guests",
|
help="attempt to shutdown and then start ALL guests",
|
||||||
)
|
)
|
||||||
|
|
||||||
g2.add_option(
|
all_group.add_argument(
|
||||||
"--shutdown-all",
|
"--shutdown-all",
|
||||||
dest="shutdownall",
|
dest="shutdownall",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
|
|
@ -569,7 +561,7 @@ def getoptions():
|
||||||
help="attempt to shutdown ALL guests",
|
help="attempt to shutdown ALL guests",
|
||||||
)
|
)
|
||||||
|
|
||||||
g2.add_option(
|
all_group.add_argument(
|
||||||
"--create-all",
|
"--create-all",
|
||||||
dest="createall",
|
dest="createall",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
|
|
@ -577,13 +569,8 @@ def getoptions():
|
||||||
help="attempt to start ALL guests",
|
help="attempt to start ALL guests",
|
||||||
)
|
)
|
||||||
|
|
||||||
# attach groups g1 and g2 to the parser p object
|
|
||||||
p.add_option_group(g0)
|
|
||||||
p.add_option_group(g1)
|
|
||||||
p.add_option_group(g2)
|
|
||||||
|
|
||||||
# parse options and args
|
# parse options and args
|
||||||
options, guest_names = p.parse_args()
|
options, unknown_args = parser.parse_known_args()
|
||||||
|
|
||||||
# the actionsum should be 1 to continue, bool math ftw
|
# the actionsum should be 1 to continue, bool math ftw
|
||||||
actions = [
|
actions = [
|
||||||
|
|
@ -601,6 +588,7 @@ def getoptions():
|
||||||
actionsum = sum(actions)
|
actionsum = sum(actions)
|
||||||
|
|
||||||
if actionsum == 1:
|
if actionsum == 1:
|
||||||
|
guest_names = unknown_args
|
||||||
return options, guest_names
|
return options, guest_names
|
||||||
else:
|
else:
|
||||||
exit(
|
exit(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue