====
[plcapi.git] / PLC / Methods / DeleteAllPeerEntries.py
1 #
2 # Thierry Parmentelat - INRIA
3 #
4 # utility to clear all entries from a peer
5 # initially duplicated from RefreshPeer
6
7
8 import sys
9
10 from PLC.Logger import logger
11 from PLC.Faults import *
12 from PLC.Method import Method
13 from PLC.Parameter import Parameter, Mixed
14 from PLC.Auth import Auth
15
16 from PLC.Peers import Peer, Peers
17 from PLC.Sites import Site, Sites
18 from PLC.Persons import Person, Persons
19 from PLC.KeyTypes import KeyType, KeyTypes
20 from PLC.Keys import Key, Keys
21 from PLC.BootStates import BootState, BootStates
22 from PLC.Nodes import Node, Nodes
23 from PLC.SliceInstantiations import SliceInstantiations
24 from PLC.Slices import Slice, Slices
25 from PLC.Roles import Role, Roles
26
27 commit_mode = True
28
29 dry_run = False
30 # debug
31 #dry_run = True
32
33 ########## helpers
34
35 def message(to_print=None, verbose_only=False):
36     if verbose_only and not verbose:
37         return
38     logger.info(to_print)
39
40
41 def message_verbose(to_print=None, header='VERBOSE'):
42     message("{}> {}".format(header, to_print), verbose_only=True)
43
44
45 class DeleteAllPeerEntries(Method):
46     """
47     This method is designed for situations where a federation link
48     is misbehaving and one wants to restart from a clean slate.
49     It is *not* designed for regular operations, but as a repairing
50     tool only.
51
52     As the name suggests, clear all local entries that are marked as
53     belonging to peer peer_id - or peername
54     if verbose is True said entries are only printed
55
56     Note that remote/foreign entities cannot be deleted
57     normally with the API
58
59     Returns 1 if successful, faults otherwise.
60     """
61
62     roles = ['admin']
63
64     accepts = [
65         Auth(),
66         Mixed(Peer.fields['peer_id'],
67               Peer.fields['peername']),
68     ]
69
70     returns = Parameter(int, "1 if successful")
71
72     def call(self, auth, peer_id_or_peername):
73
74         peer = Peers(self.api, [peer_id_or_peername])[0]
75         peer_id = peer['peer_id']
76         peername = peer['peername']
77
78         logger.info("DeleteAllPeerEntries on peer {} = {}"
79                     .format(peername, peer_id))
80         for singular, plural in (
81                 (Slice, Slices),
82                 (Key, Keys),
83                 (Person, Persons),
84                 (Node, Nodes),
85                 (Site, Sites)):
86             classname = singular.__name__
87             objs = plural(self.api, {'peer_id': peer_id})
88             print("Found {len} {classname}s from peer {peername}"
89                   .format(len=len(objs),
90                           classname=classname,
91                           peername=peername))
92             if dry_run:
93                 print("dry-run mode: skipping actual deletion")
94             else:
95                 print("Deleting {classname}s".format(classname=classname))
96                 for obj in objs:
97                     print '.',
98                     sys.stdout.flush()
99                     obj.delete(commit=commit_mode)
100                 print
101
102         # Update peer itself and commit
103         peer.sync(commit=True)
104
105         return 1