Commit 810c50b7 authored by Iustin Pop's avatar Iustin Pop

Add source file option for {add,remove}-tags

This patch adds a '--from' option to the {add,remove}-tags commands that
allows the command to read from a file or from stdin (when passing a
dash for the filename).

Reviewed-by: imsnah
parent cc425644
...@@ -41,7 +41,7 @@ __all__ = ["DEBUG_OPT", "NOHDR_OPT", "SEP_OPT", "GenericMain", "SubmitOpCode", ...@@ -41,7 +41,7 @@ __all__ = ["DEBUG_OPT", "NOHDR_OPT", "SEP_OPT", "GenericMain", "SubmitOpCode",
"cli_option", "GenerateTable", "AskUser", "cli_option", "GenerateTable", "AskUser",
"ARGS_NONE", "ARGS_FIXED", "ARGS_ATLEAST", "ARGS_ANY", "ARGS_ONE", "ARGS_NONE", "ARGS_FIXED", "ARGS_ATLEAST", "ARGS_ANY", "ARGS_ONE",
"USEUNITS_OPT", "FIELDS_OPT", "FORCE_OPT", "USEUNITS_OPT", "FIELDS_OPT", "FORCE_OPT",
"ListTags", "AddTags", "RemoveTags", "ListTags", "AddTags", "RemoveTags", "TAG_SRC_OPT",
] ]
...@@ -66,6 +66,35 @@ def _ExtractTagsObject(opts, args): ...@@ -66,6 +66,35 @@ def _ExtractTagsObject(opts, args):
return retval return retval
def _ExtendTags(opts, args):
"""Extend the args if a source file has been given.
This function will extend the tags with the contents of the file
passed in the 'tags_source' attribute of the opts parameter. A file
named '-' will be replaced by stdin.
"""
fname = opts.tags_source
if fname is None:
return
if fname == "-":
new_fh = sys.stdin
else:
new_fh = open(fname, "r")
new_data = []
try:
# we don't use the nice 'new_data = [line.strip() for line in fh]'
# because of python bug 1633941
while True:
line = new_fh.readline()
if not line:
break
new_data.append(line.strip())
finally:
new_fh.close()
args.extend(new_data)
def ListTags(opts, args): def ListTags(opts, args):
"""List the tags on a given object. """List the tags on a given object.
...@@ -94,6 +123,7 @@ def AddTags(opts, args): ...@@ -94,6 +123,7 @@ def AddTags(opts, args):
""" """
kind, name = _ExtractTagsObject(opts, args) kind, name = _ExtractTagsObject(opts, args)
_ExtendTags(opts, args)
if not args: if not args:
raise errors.OpPrereqError("No tags to be added") raise errors.OpPrereqError("No tags to be added")
op = opcodes.OpAddTags(kind=kind, name=name, tags=args) op = opcodes.OpAddTags(kind=kind, name=name, tags=args)
...@@ -110,6 +140,7 @@ def RemoveTags(opts, args): ...@@ -110,6 +140,7 @@ def RemoveTags(opts, args):
""" """
kind, name = _ExtractTagsObject(opts, args) kind, name = _ExtractTagsObject(opts, args)
_ExtendTags(opts, args)
if not args: if not args:
raise errors.OpPrereqError("No tags to be removed") raise errors.OpPrereqError("No tags to be removed")
op = opcodes.OpDelTags(kind=kind, name=name, tags=args) op = opcodes.OpDelTags(kind=kind, name=name, tags=args)
...@@ -143,6 +174,9 @@ FORCE_OPT = make_option("-f", "--force", dest="force", action="store_true", ...@@ -143,6 +174,9 @@ FORCE_OPT = make_option("-f", "--force", dest="force", action="store_true",
_LOCK_OPT = make_option("--lock-retries", default=None, _LOCK_OPT = make_option("--lock-retries", default=None,
type="int", help=SUPPRESS_HELP) type="int", help=SUPPRESS_HELP)
TAG_SRC_OPT = make_option("--from", dest="tags_source",
default=None, help="File with tag names")
def ARGS_FIXED(val): def ARGS_FIXED(val):
"""Macro-like function denoting a fixed number of arguments""" """Macro-like function denoting a fixed number of arguments"""
return -val return -val
......
...@@ -60,6 +60,7 @@ ...@@ -60,6 +60,7 @@
<cmdsynopsis> <cmdsynopsis>
<command>add-tags</command> <command>add-tags</command>
<arg choice="opt">--from <replaceable>file</replaceable></arg>
<arg choice="req" <arg choice="req"
rep="repeat"><replaceable>tag</replaceable></arg> rep="repeat"><replaceable>tag</replaceable></arg>
</cmdsynopsis> </cmdsynopsis>
...@@ -68,6 +69,14 @@ ...@@ -68,6 +69,14 @@
Add tags to the cluster. If any of the tags contains invalid Add tags to the cluster. If any of the tags contains invalid
characters, the entire operation will abort. characters, the entire operation will abort.
</para> </para>
<para>
If the <option>--from</option> option is given, the list of
tags will be extended with the contents of that file (each
line becomes a tag). In this case, there is not need to pass
tags on the command line (if you do, both sources will be
used). A file name of - will be interpreted as stdin.
</para>
</refsect2> </refsect2>
<refsect2> <refsect2>
...@@ -250,6 +259,7 @@ ...@@ -250,6 +259,7 @@
<cmdsynopsis> <cmdsynopsis>
<command>remove-tags</command> <command>remove-tags</command>
<arg choice="opt">--from <replaceable>file</replaceable></arg>
<arg choice="req" <arg choice="req"
rep="repeat"><replaceable>tag</replaceable></arg> rep="repeat"><replaceable>tag</replaceable></arg>
</cmdsynopsis> </cmdsynopsis>
...@@ -258,6 +268,14 @@ ...@@ -258,6 +268,14 @@
Remove tags from the cluster. If any of the tags are not Remove tags from the cluster. If any of the tags are not
existing on the cluster, the entire operation will abort. existing on the cluster, the entire operation will abort.
</para> </para>
<para>
If the <option>--from</option> option is given, the list of
tags will be extended with the contents of that file (each
line becomes a tag). In this case, there is not need to pass
tags on the command line (if you do, both sources will be
used). A file name of - will be interpreted as stdin.
</para>
</refsect2> </refsect2>
<refsect2> <refsect2>
......
...@@ -758,6 +758,7 @@ node1.example.com:sdb:/dev/md1 ...@@ -758,6 +758,7 @@ node1.example.com:sdb:/dev/md1
<cmdsynopsis> <cmdsynopsis>
<command>add-tags</command> <command>add-tags</command>
<arg choice="opt">--from <replaceable>file</replaceable></arg>
<arg choice="req"><replaceable>instancename</replaceable></arg> <arg choice="req"><replaceable>instancename</replaceable></arg>
<arg choice="req" <arg choice="req"
rep="repeat"><replaceable>tag</replaceable></arg> rep="repeat"><replaceable>tag</replaceable></arg>
...@@ -767,6 +768,13 @@ node1.example.com:sdb:/dev/md1 ...@@ -767,6 +768,13 @@ node1.example.com:sdb:/dev/md1
Add tags to the given instance. If any of the tags contains Add tags to the given instance. If any of the tags contains
invalid characters, the entire operation will abort. invalid characters, the entire operation will abort.
</para> </para>
<para>
If the <option>--from</option> option is given, the list of
tags will be extended with the contents of that file (each
line becomes a tag). In this case, there is not need to pass
tags on the command line (if you do, both sources will be
used). A file name of - will be interpreted as stdin.
</para>
</refsect3> </refsect3>
<refsect3> <refsect3>
...@@ -784,6 +792,7 @@ node1.example.com:sdb:/dev/md1 ...@@ -784,6 +792,7 @@ node1.example.com:sdb:/dev/md1
<title>REMOVE-TAGS</title> <title>REMOVE-TAGS</title>
<cmdsynopsis> <cmdsynopsis>
<command>remove-tags</command> <command>remove-tags</command>
<arg choice="opt">--from <replaceable>file</replaceable></arg>
<arg choice="req"><replaceable>instancename</replaceable></arg> <arg choice="req"><replaceable>instancename</replaceable></arg>
<arg choice="req" <arg choice="req"
rep="repeat"><replaceable>tag</replaceable></arg> rep="repeat"><replaceable>tag</replaceable></arg>
...@@ -793,6 +802,14 @@ node1.example.com:sdb:/dev/md1 ...@@ -793,6 +802,14 @@ node1.example.com:sdb:/dev/md1
Remove tags from the given instance. If any of the tags are Remove tags from the given instance. If any of the tags are
not existing on the node, the entire operation will abort. not existing on the node, the entire operation will abort.
</para> </para>
<para>
If the <option>--from</option> option is given, the list of
tags will be extended with the contents of that file (each
line becomes a tag). In this case, there is not need to pass
tags on the command line (if you do, both sources will be
used). A file name of - will be interpreted as stdin.
</para>
</refsect3> </refsect3>
</refsect2> </refsect2>
......
...@@ -105,6 +105,7 @@ ...@@ -105,6 +105,7 @@
<cmdsynopsis> <cmdsynopsis>
<command>add-tags</command> <command>add-tags</command>
<arg choice="opt">--from <replaceable>file</replaceable></arg>
<arg choice="req"><replaceable>nodename</replaceable></arg> <arg choice="req"><replaceable>nodename</replaceable></arg>
<arg choice="req" <arg choice="req"
rep="repeat"><replaceable>tag</replaceable></arg> rep="repeat"><replaceable>tag</replaceable></arg>
...@@ -114,6 +115,14 @@ ...@@ -114,6 +115,14 @@
Add tags to the given node. If any of the tags contains Add tags to the given node. If any of the tags contains
invalid characters, the entire operation will abort. invalid characters, the entire operation will abort.
</para> </para>
<para>
If the <option>--from</option> option is given, the list of
tags will be extended with the contents of that file (each
line becomes a tag). In this case, there is not need to pass
tags on the command line (if you do, both sources will be
used). A file name of - will be interpreted as stdin.
</para>
</refsect2> </refsect2>
<refsect2> <refsect2>
...@@ -292,6 +301,7 @@ ...@@ -292,6 +301,7 @@
<title>REMOVE-TAGS</title> <title>REMOVE-TAGS</title>
<cmdsynopsis> <cmdsynopsis>
<command>remove-tags</command> <command>remove-tags</command>
<arg choice="opt">--from <replaceable>file</replaceable></arg>
<arg choice="req"><replaceable>nodename</replaceable></arg> <arg choice="req"><replaceable>nodename</replaceable></arg>
<arg choice="req" <arg choice="req"
rep="repeat"><replaceable>tag</replaceable></arg> rep="repeat"><replaceable>tag</replaceable></arg>
...@@ -301,6 +311,14 @@ ...@@ -301,6 +311,14 @@
Remove tags from the given node. If any of the tags are not Remove tags from the given node. If any of the tags are not
existing on the node, the entire operation will abort. existing on the node, the entire operation will abort.
</para> </para>
<para>
If the <option>--from</option> option is given, the list of
tags will be extended with the contents of that file (each
line becomes a tag). In this case, there is not need to pass
tags on the command line (if you do, both sources will be
used). A file name of - will be interpreted as stdin.
</para>
</refsect2> </refsect2>
<refsect2> <refsect2>
......
...@@ -265,9 +265,9 @@ commands = { ...@@ -265,9 +265,9 @@ commands = {
"", "Show cluster configuration"), "", "Show cluster configuration"),
'list-tags': (ListTags, ARGS_NONE, 'list-tags': (ListTags, ARGS_NONE,
[DEBUG_OPT], "", "List the tags of the cluster"), [DEBUG_OPT], "", "List the tags of the cluster"),
'add-tags': (AddTags, ARGS_ANY, [DEBUG_OPT], 'add-tags': (AddTags, ARGS_ANY, [DEBUG_OPT, TAG_SRC_OPT],
"tag...", "Add tags to the cluster"), "tag...", "Add tags to the cluster"),
'remove-tags': (RemoveTags, ARGS_ANY, [DEBUG_OPT], 'remove-tags': (RemoveTags, ARGS_ANY, [DEBUG_OPT, TAG_SRC_OPT],
"tag...", "Remove tags from the cluster"), "tag...", "Remove tags from the cluster"),
} }
......
...@@ -756,9 +756,9 @@ commands = { ...@@ -756,9 +756,9 @@ commands = {
"Deactivate an instance's disks"), "Deactivate an instance's disks"),
'list-tags': (ListTags, ARGS_ONE, [DEBUG_OPT], 'list-tags': (ListTags, ARGS_ONE, [DEBUG_OPT],
"<node_name>", "List the tags of the given instance"), "<node_name>", "List the tags of the given instance"),
'add-tags': (AddTags, ARGS_ATLEAST(1), [DEBUG_OPT], 'add-tags': (AddTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT],
"<node_name> tag...", "Add tags to the given instance"), "<node_name> tag...", "Add tags to the given instance"),
'remove-tags': (RemoveTags, ARGS_ATLEAST(1), [DEBUG_OPT], 'remove-tags': (RemoveTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT],
"<node_name> tag...", "Remove tags from given instance"), "<node_name> tag...", "Remove tags from given instance"),
} }
......
...@@ -183,9 +183,9 @@ commands = { ...@@ -183,9 +183,9 @@ commands = {
"[<node_name>...]", "List logical volumes on node(s)"), "[<node_name>...]", "List logical volumes on node(s)"),
'list-tags': (ListTags, ARGS_ONE, [DEBUG_OPT], 'list-tags': (ListTags, ARGS_ONE, [DEBUG_OPT],
"<node_name>", "List the tags of the given node"), "<node_name>", "List the tags of the given node"),
'add-tags': (AddTags, ARGS_ATLEAST(1), [DEBUG_OPT], 'add-tags': (AddTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT],
"<node_name> tag...", "Add tags to the given node"), "<node_name> tag...", "Add tags to the given node"),
'remove-tags': (RemoveTags, ARGS_ATLEAST(1), [DEBUG_OPT], 'remove-tags': (RemoveTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT],
"<node_name> tag...", "Remove tags from the given node"), "<node_name> tag...", "Remove tags from the given node"),
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment