From 711d2321ba6ae86e40d1905f665cc19271489bf7 Mon Sep 17 00:00:00 2001 From: Guillem Jover Date: Wed, 13 Jun 2018 01:16:38 +0200 Subject: [PATCH] TT#38200 Switch to new perl ngcp.has_role function This replaces the old has_role Template "function" with a proper method of the ngcp object, anywhere we need to check for the host role, including other macros that could not use our previous "function" before. Which will make it possible to modify the has_role behavior in a single place, and affect all code paths. Change-Id: I73526aa77fab3dec8f33361e142164204d7c1481 --- lib/NGCP/Template.pm | 19 +++++++++++++++++++ lib/get_all_adv_ips | 2 +- lib/get_all_cluster_set_ip_maps | 2 +- lib/get_all_cluster_set_ip_maps_for_host | 2 +- lib/get_all_cluster_set_shared_ip_maps | 2 +- ...et_all_cluster_set_shared_ip_maps_for_host | 2 +- lib/get_all_cluster_sets | 2 +- lib/get_all_hosts_ips | 2 +- lib/get_all_ips | 2 +- lib/get_all_rtp_interfaces | 3 +-- lib/get_all_shared_ips | 2 +- lib/get_all_shared_names_by_role | 2 +- lib/get_all_shared_v6ips | 2 +- lib/get_all_v6ips | 2 +- lib/get_mgmt_node | 5 +---- lib/get_ndb_ips | 2 +- lib/has_role | 9 +-------- 17 files changed, 35 insertions(+), 27 deletions(-) diff --git a/lib/NGCP/Template.pm b/lib/NGCP/Template.pm index e4ce401a..8faddede 100644 --- a/lib/NGCP/Template.pm +++ b/lib/NGCP/Template.pm @@ -17,6 +17,21 @@ sub new return bless $self, $class; } +sub has_role +{ + my ($self, $hostname, $role) = @_; + + if (not defined $self->{config}{hosts}{$hostname}) { + $hostname = 'self'; + } + + if (any { m/^$role$/ } @{$self->{config}{hosts}{$hostname}{role}}) { + return 1; + } + + return 0; +} + 1; __END__ @@ -48,6 +63,10 @@ the B internal variable, such as C. The $config argument contains the deserialized ngcp-config YAML configuration. +=item $bool = $t->has_role($hostname, $role) + +Checks whether the $hostname node has the $role. + =back =head1 AUTHOR diff --git a/lib/get_all_adv_ips b/lib/get_all_adv_ips index 9f805c0b..3346fd49 100644 --- a/lib/get_all_adv_ips +++ b/lib/get_all_adv_ips @@ -30,7 +30,7 @@ blktmp.processed_hosts = {}; out = []; FOREACH host IN hosts.keys.sort; NEXT UNLESS status.item(hosts.$host.status); - NEXT UNLESS hosts.$host.role.grep('^' _ argv.role _ '$').size(); + NEXT UNLESS ngcp.has_role(host, argv.role); NEXT IF blktmp.processed_hosts.$host.defined; FOREACH iface IN hosts.$host.interfaces; diff --git a/lib/get_all_cluster_set_ip_maps b/lib/get_all_cluster_set_ip_maps index 2da22970..97e4a8c3 100644 --- a/lib/get_all_cluster_set_ip_maps +++ b/lib/get_all_cluster_set_ip_maps @@ -31,7 +31,7 @@ FOREACH set IN cluster_sets.keys; FOREACH host IN hosts.keys.sort; NEXT UNLESS status.item(hosts.$host.status); - NEXT UNLESS hosts.$host.role.grep('^' _ argv.role _ '$').size(); + NEXT UNLESS ngcp.has_role(host, argv.role); FOREACH iface IN hosts.$host.interfaces; NEXT UNLESS hosts.$host.$iface.cluster_sets.grep('^' _ set _ '$').size(); NEXT UNLESS hosts.$host.$iface.type.grep('^' _ argv.type _ '$').size(); diff --git a/lib/get_all_cluster_set_ip_maps_for_host b/lib/get_all_cluster_set_ip_maps_for_host index 3d980667..4add16c4 100644 --- a/lib/get_all_cluster_set_ip_maps_for_host +++ b/lib/get_all_cluster_set_ip_maps_for_host @@ -44,7 +44,7 @@ FOREACH set IN cluster_sets.keys; FOREACH host IN set_hosts; NEXT UNLESS status.item(hosts.$host.status); - NEXT UNLESS hosts.$host.role.grep('^' _ argv.role _ '$').size(); + NEXT UNLESS ngcp.has_role(host, argv.role); FOREACH iface IN hosts.$host.interfaces; NEXT UNLESS hosts.$host.$iface.cluster_sets.grep('^' _ set _ '$').size(); NEXT UNLESS hosts.$host.$iface.type.grep('^' _ argv.type _ '$').size(); diff --git a/lib/get_all_cluster_set_shared_ip_maps b/lib/get_all_cluster_set_shared_ip_maps index 78ddd76f..d06be6e1 100644 --- a/lib/get_all_cluster_set_shared_ip_maps +++ b/lib/get_all_cluster_set_shared_ip_maps @@ -34,7 +34,7 @@ FOREACH set IN cluster_sets.keys; FOREACH host IN hosts.keys.sort; NEXT UNLESS status.item(hosts.$host.status); NEXT IF blktmp.processed_hosts.$host.defined; - NEXT UNLESS hosts.$host.role.grep('^' _ argv.role _ '$').size(); + NEXT UNLESS ngcp.has_role(host, argv.role); peer = hosts.$host.peer; FOREACH iface IN hosts.$host.interfaces; NEXT UNLESS hosts.$host.$iface.cluster_sets.grep('^' _ set _ '$').size(); diff --git a/lib/get_all_cluster_set_shared_ip_maps_for_host b/lib/get_all_cluster_set_shared_ip_maps_for_host index 45185fac..e2514973 100644 --- a/lib/get_all_cluster_set_shared_ip_maps_for_host +++ b/lib/get_all_cluster_set_shared_ip_maps_for_host @@ -46,7 +46,7 @@ FOREACH set IN cluster_sets.keys; FOREACH host IN set_hosts; NEXT UNLESS status.item(hosts.$host.status); NEXT IF blktmp.processed_hosts.$host.defined; - NEXT UNLESS hosts.$host.role.grep('^' _ argv.role _ '$').size(); + NEXT UNLESS ngcp.has_role(host, argv.role); peer = hosts.$host.peer; FOREACH iface IN hosts.$host.interfaces; NEXT UNLESS hosts.$host.$iface.cluster_sets.grep('^' _ set _ '$').size(); diff --git a/lib/get_all_cluster_sets b/lib/get_all_cluster_sets index 2b90a14b..4e61f5fc 100644 --- a/lib/get_all_cluster_sets +++ b/lib/get_all_cluster_sets @@ -28,7 +28,7 @@ FOREACH set IN cluster_sets.keys; FOREACH host IN hosts.keys.sort; NEXT UNLESS status.item(hosts.$host.status); - NEXT UNLESS hosts.$host.role.grep('^' _ argv.type _ '$').size(); + NEXT UNLESS ngcp.has_role(host, argv.role); FOREACH iface IN hosts.$host.interfaces; NEXT UNLESS hosts.$host.$iface.cluster_sets.grep('^' _ set _ '$').size(); out.$set = cluster_sets.$set.dispatcher_id; diff --git a/lib/get_all_hosts_ips b/lib/get_all_hosts_ips index 769a35f2..2b8f06c6 100644 --- a/lib/get_all_hosts_ips +++ b/lib/get_all_hosts_ips @@ -32,7 +32,7 @@ END; out = []; FOREACH host IN hosts.keys.sort; NEXT UNLESS status.item(hosts.$host.status); - NEXT UNLESS hosts.$host.role.grep('^' _ argv.role _ '$').size(); + NEXT UNLESS ngcp.has_role(host, argv.role); FOREACH iface IN hosts.$host.interfaces; FOREACH realiface IN hosts.$host.keys; diff --git a/lib/get_all_ips b/lib/get_all_ips index 41545bf1..f6064951 100644 --- a/lib/get_all_ips +++ b/lib/get_all_ips @@ -29,7 +29,7 @@ END; out = []; FOREACH host IN hosts.keys; NEXT UNLESS status.item(hosts.$host.status); - NEXT UNLESS hosts.$host.role.grep('^' _ argv.role _ '$').size(); + NEXT UNLESS ngcp.has_role(host, argv.role); FOREACH iface IN hosts.$host.interfaces; FOREACH realiface IN hosts.$host.keys; diff --git a/lib/get_all_rtp_interfaces b/lib/get_all_rtp_interfaces index d51bbaf9..bdb24c15 100644 --- a/lib/get_all_rtp_interfaces +++ b/lib/get_all_rtp_interfaces @@ -24,9 +24,8 @@ X_out = {}; FOREACH X_host IN hosts.keys; X_hd = hosts.$X_host; - X_roles = X_hd.role; NEXT UNLESS status.item(X_hd.status); - NEXT IF X_roles.grep('^rtp$').size() == 0; + NEXT UNLESS ngcp.has_role(X_host, 'rtp'); FOREACH X_iface IN X_hd.interfaces; X_if = X_hd.$X_iface; diff --git a/lib/get_all_shared_ips b/lib/get_all_shared_ips index f5a16430..4345d196 100644 --- a/lib/get_all_shared_ips +++ b/lib/get_all_shared_ips @@ -30,7 +30,7 @@ blktmp.processed_hosts = {}; out = []; FOREACH host IN hosts.keys.sort; NEXT UNLESS status.item(hosts.$host.status); - NEXT UNLESS hosts.$host.role.grep('^' _ argv.role _ '$').size(); + NEXT UNLESS ngcp.has_role(host, argv.role); NEXT IF blktmp.processed_hosts.$host.defined; FOREACH iface IN hosts.$host.interfaces; diff --git a/lib/get_all_shared_names_by_role b/lib/get_all_shared_names_by_role index a0fa5606..6d725c88 100644 --- a/lib/get_all_shared_names_by_role +++ b/lib/get_all_shared_names_by_role @@ -28,7 +28,7 @@ out = []; FOREACH host IN hosts.keys.sort; NEXT UNLESS status.item(hosts.$host.status); NEXT IF blktmp.processed_hosts.$host.defined; - NEXT UNLESS hosts.$host.role.grep('^' _ argv.role _ '$').size(); + NEXT UNLESS ngcp.has_role(host, argv.role); peer = hosts.$host.peer; blktmp.processed_hosts.$peer = 1; matches = host.match('^(\w+[0-9])[ab]$'); diff --git a/lib/get_all_shared_v6ips b/lib/get_all_shared_v6ips index 17545cc1..8828072c 100644 --- a/lib/get_all_shared_v6ips +++ b/lib/get_all_shared_v6ips @@ -31,7 +31,7 @@ blktmp.processed_hosts = {}; out = []; FOREACH host IN hosts.keys.sort; NEXT UNLESS status.item(hosts.$host.status); - NEXT UNLESS hosts.$host.role.grep('^' _ argv.role _ '$').size(); + NEXT UNLESS ngcp.has_role(host, argv.role); NEXT IF blktmp.processed_hosts.$host.defined; FOREACH iface IN hosts.$host.interfaces; diff --git a/lib/get_all_v6ips b/lib/get_all_v6ips index 1c0ca42a..dcc94a12 100644 --- a/lib/get_all_v6ips +++ b/lib/get_all_v6ips @@ -29,7 +29,7 @@ END; out = []; FOREACH host IN hosts.keys.sort; NEXT UNLESS status.item(hosts.$host.status); - NEXT UNLESS hosts.$host.role.grep('^' _ argv.role _ '$').size(); + NEXT UNLESS ngcp.has_role(host, argv.role); FOREACH iface IN hosts.$host.interfaces; FOREACH realiface IN hosts.$host.keys; diff --git a/lib/get_mgmt_node b/lib/get_mgmt_node index 3be9a82f..33a7e7ec 100644 --- a/lib/get_mgmt_node +++ b/lib/get_mgmt_node @@ -10,10 +10,7 @@ ngcp_type = out; IF ngcp_type == 'carrier'; FOREACH host IN hosts.keys; - argv.host = host; - argv.role = 'mgmt'; - PROCESS '/usr/lib/ngcp-ngcpcfg/has_role'; - is_mgmt = out; + is_mgmt = ngcp.has_role(host, 'mgmt'); IF is_mgmt; matches = host.match('^(\w+[0-9])[ab]?$'); out = matches.0; diff --git a/lib/get_ndb_ips b/lib/get_ndb_ips index 6fc845f1..649d57c5 100644 --- a/lib/get_ndb_ips +++ b/lib/get_ndb_ips @@ -13,7 +13,7 @@ out = {}; FOREACH host IN hosts.keys; - NEXT UNLESS hosts.$host.role.grep('^' _ argv.role _ '$').size(); + NEXT UNLESS ngcp.has_role(host, argv.role); FOREACH iface IN hosts.$host.interfaces; FOREACH realiface IN hosts.$host.keys; diff --git a/lib/has_role b/lib/has_role index 07eeff13..8da224b5 100644 --- a/lib/has_role +++ b/lib/has_role @@ -8,13 +8,6 @@ # proxy, lb, mgmt # @return out 0 if false, 1 if true. -IF !hosts.${argv.host}.defined; - argv.host = 'self'; -END; - -out = 0; -IF hosts.${argv.host}.role.grep('^' _ argv.role _ '$').size(); - out = 1; -END; +out = ngcp.has_role(argv.host, argv.role); -%]