Skip to content

Commit 04b3214

Browse files
committed
clean/fip: Add new clean command
* Remove refs between floating-ip-pool and floatinip (which do not exist anymore).
1 parent 6b7d2c4 commit 04b3214

File tree

4 files changed

+68
-10
lines changed

4 files changed

+68
-10
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# -*- coding: utf-8 -*-
2+
from contrail_api_cli.utils import printo
3+
from contrail_api_cli.exceptions import ResourceNotFound, CollectionNotFound
4+
5+
from ..utils import server_type, CassandraCommand, CheckCommand, PathCommand
6+
from refs import CleanRefs
7+
8+
class CleanFipRefs(CassandraCommand, CheckCommand, PathCommand):
9+
"""Clean broken references related to floating IPs.
10+
11+
Broken refs related to FIP can be found with gremlin (floating-ip-pool <->
12+
floating-ip).
13+
14+
The command will take all existing floating-ip-pool and checks the existence
15+
of each floating-ip it points to and removes the reference if that latter
16+
does not exist.
17+
18+
You can run the command by putting the floating-ip-pool path to be more
19+
precise or you can run it without any resources path, in that case it will
20+
iterates over all floating-ip-pool.
21+
22+
"""
23+
description = "Clean broken references related to floating IPs"
24+
25+
@property
26+
def resource_type(self):
27+
return "floating-ip-pool"
28+
29+
def _clean_refs(self, pool, fip, **kwargs):
30+
try:
31+
fip.fetch()
32+
except ResourceNotFound:
33+
CleanRefs(ref_type="children",
34+
target_type="floating_ip",
35+
paths=[pool.uuid, fip.uuid],
36+
**kwargs)
37+
38+
def __call__(self, **kwargs):
39+
super(CleanFipRefs, self).__call__(**kwargs)
40+
for pool in self.resources:
41+
try:
42+
pool.fetch()
43+
for fip in pool.children:
44+
self._clean_refs(pool, fip, **kwargs)
45+
46+
except ResourceNotFound as e:
47+
printo(e)

contrail_api_cli_extra/clean/refs.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
from contrail_api_cli.command import Option, Arg
99
from contrail_api_cli.utils import printo
1010

11-
from ..utils import server_type, CheckCommand
11+
from ..utils import server_type, CheckCommand, CassandraCommand
1212

1313

14-
class CleanRefs(CheckCommand):
14+
class CleanRefs(CheckCommand, CassandraCommand):
1515
"""Clean references in Contrail DB.
1616
1717
Broken refs can be found with gremlin.
@@ -57,18 +57,13 @@ class CleanRefs(CheckCommand):
5757
target_type = Option(help="resource type of the target",
5858
required=True)
5959

60-
cassandra_servers = Option(help="cassandra server list' (default: %(default)s)",
61-
nargs='+',
62-
type=server_type,
63-
default=['localhost:9160'])
64-
6560
def _remove_refs(self, paths):
6661
if len(paths) == 0:
6762
return
6863
source, target = paths[:2]
6964
# when the parent doesn't exists anymore,
7065
# we don't need to keep the source
71-
if not self.check:
66+
if not self.check and not self.dry_run:
7267
if self.ref_type == "parent":
7368
self.uuid_cf.remove(target)
7469
self.uuid_cf.remove(source)
@@ -85,9 +80,9 @@ def _read_file(self, resources_file):
8580
paths = paths + l.split()
8681
return paths
8782

88-
def __call__(self, paths=None, resources_file=None, ref_type=None, target_type=None, cassandra_servers=None, **kwargs):
83+
def __call__(self, paths=None, resources_file=None, ref_type=None, target_type=None, **kwargs):
8984
super(CleanRefs, self).__call__(**kwargs)
90-
pool = ConnectionPool('config_db_uuid', server_list=cassandra_servers)
85+
pool = ConnectionPool('config_db_uuid', server_list=self.cassandra_servers)
9186
self.uuid_cf = ColumnFamily(pool, 'obj_uuid_table')
9287
self.ref_type = ref_type
9388
self.target_type = target_type

contrail_api_cli_extra/utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,21 @@ def __call__(self, zk_server=None, **kwargs):
220220
super(ZKCommand, self).__call__(**kwargs)
221221

222222

223+
class CassandraCommand(Command):
224+
"""Inherit from this class to add `--cassandra-servers` options.
225+
226+
Cassandra servers list value is stored in `self.cassandra_servers`.
227+
"""
228+
cassandra_servers = Option(help="cassandra server list' (default: %(default)s)",
229+
nargs='+',
230+
type=server_type,
231+
default=['localhost:9160'])
232+
233+
def __call__(self, cassandra_servers=None, **kwargs):
234+
self.cassandra_servers = cassandra_servers
235+
super(CassandraCommand, self).__call__(**kwargs)
236+
237+
223238
class CheckCommand(Command):
224239
"""Inherit from this class to add `--check` and `--dry-run` options.
225240

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
'clean-si-scheduling = contrail_api_cli_extra.clean.si:CleanSIScheduling',
9999
'clean-subnet = contrail_api_cli_extra.clean.subnet:CleanSubnet',
100100
'clean-refs = contrail_api_cli_extra.clean.refs:CleanRefs',
101+
'clean-fip-refs = contrail_api_cli_extra.clean.fip:CleanFipRefs',
101102
]
102103
},
103104
classifiers=[

0 commit comments

Comments
 (0)