Revision 05484a24 lib/cli.py
b/lib/cli.py | ||
---|---|---|
2868 | 2868 |
|
2869 | 2869 |
|
2870 | 2870 |
def GetOnlineNodes(nodes, cl=None, nowarn=False, secondary_ips=False, |
2871 |
filter_master=False): |
|
2871 |
filter_master=False, nodegroup=None):
|
|
2872 | 2872 |
"""Returns the names of online nodes. |
2873 | 2873 |
|
2874 | 2874 |
This function will also log a warning on stderr with the names of |
... | ... | |
2889 | 2889 |
@param filter_master: if True, do not return the master node in the list |
2890 | 2890 |
(useful in coordination with secondary_ips where we cannot check our |
2891 | 2891 |
node name against the list) |
2892 |
@type nodegroup: string |
|
2893 |
@param nodegroup: If set, only return nodes in this node group |
|
2892 | 2894 |
|
2893 | 2895 |
""" |
2894 | 2896 |
if cl is None: |
2895 | 2897 |
cl = GetClient() |
2896 | 2898 |
|
2897 |
if secondary_ips: |
|
2898 |
name_idx = 2 |
|
2899 |
else: |
|
2900 |
name_idx = 0 |
|
2899 |
filter_ = [] |
|
2900 |
|
|
2901 |
if nodes: |
|
2902 |
filter_.append(qlang.MakeSimpleFilter("name", nodes)) |
|
2903 |
|
|
2904 |
if nodegroup is not None: |
|
2905 |
filter_.append([qlang.OP_OR, [qlang.OP_EQUAL, "group", nodegroup], |
|
2906 |
[qlang.OP_EQUAL, "group.uuid", nodegroup]]) |
|
2901 | 2907 |
|
2902 | 2908 |
if filter_master: |
2903 |
master_node = cl.QueryConfigValues(["master_node"])[0] |
|
2904 |
filter_fn = lambda x: x != master_node |
|
2909 |
filter_.append([qlang.OP_NOT, [qlang.OP_TRUE, "master"]]) |
|
2910 |
|
|
2911 |
if filter_: |
|
2912 |
if len(filter_) > 1: |
|
2913 |
final_filter = [qlang.OP_AND] + filter_ |
|
2914 |
else: |
|
2915 |
assert len(filter_) == 1 |
|
2916 |
final_filter = filter_[0] |
|
2905 | 2917 |
else: |
2906 |
filter_fn = lambda _: True |
|
2918 |
final_filter = None |
|
2919 |
|
|
2920 |
result = cl.Query(constants.QR_NODE, ["name", "offline", "sip"], final_filter) |
|
2921 |
|
|
2922 |
def _IsOffline(row): |
|
2923 |
(_, (_, offline), _) = row |
|
2924 |
return offline |
|
2925 |
|
|
2926 |
def _GetName(row): |
|
2927 |
((_, name), _, _) = row |
|
2928 |
return name |
|
2929 |
|
|
2930 |
def _GetSip(row): |
|
2931 |
(_, _, (_, sip)) = row |
|
2932 |
return sip |
|
2933 |
|
|
2934 |
(offline, online) = compat.partition(result.data, _IsOffline) |
|
2907 | 2935 |
|
2908 |
result = cl.QueryNodes(names=nodes, fields=["name", "offline", "sip"], |
|
2909 |
use_locking=False) |
|
2910 |
offline = [row[0] for row in result if row[1]] |
|
2911 | 2936 |
if offline and not nowarn: |
2912 |
ToStderr("Note: skipping offline node(s): %s" % utils.CommaJoin(offline)) |
|
2913 |
return [row[name_idx] for row in result if not row[1] and filter_fn(row[0])] |
|
2937 |
ToStderr("Note: skipping offline node(s): %s" % |
|
2938 |
utils.CommaJoin(map(_GetName, offline))) |
|
2939 |
|
|
2940 |
if secondary_ips: |
|
2941 |
fn = _GetSip |
|
2942 |
else: |
|
2943 |
fn = _GetName |
|
2944 |
|
|
2945 |
return map(fn, online) |
|
2914 | 2946 |
|
2915 | 2947 |
|
2916 | 2948 |
def _ToStream(stream, txt, *args): |
Also available in: Unified diff