Package yum :: Module failover
[hide private]
[frames] | no frames]

Source Code for Module yum.failover

  1  #!/usr/bin/python 
  2  # This program is free software; you can redistribute it and/or modify 
  3  # it under the terms of the GNU General Public License as published by 
  4  # the Free Software Foundation; either version 2 of the License, or 
  5  # (at your option) any later version. 
  6  # 
  7  # This program is distributed in the hope that it will be useful, 
  8  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
  9  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 10  # GNU Library General Public License for more details. 
 11  # 
 12  # You should have received a copy of the GNU General Public License 
 13  # along with this program; if not, write to the Free Software 
 14  # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
 15  # Copyright 2003 Jack Neely, NC State University 
 16   
 17  # Here we define a base class for failover methods.  The idea here is that each 
 18  # failover method uses a class derived from the base class so yum only has to 
 19  # worry about calling get_serverurl() and server_failed() and these classes will  
 20  # figure out which URL to cough up based on the failover method. 
 21   
 22  import random 
 23   
24 -class baseFailOverMethod:
25
26 - def __init__(self, repo):
27 self.repo = repo 28 self.failures = 0
29
30 - def get_serverurl(self, i=None):
31 """Returns a serverurl based on this failover method or None 32 if complete failure. If i is given it is a direct index 33 to pull a server URL from instead of using the failures 34 counter.""" 35 return None
36
37 - def server_failed(self):
38 "Tells the failover method that the current server is failed." 39 self.failures = self.failures + 1
40
41 - def reset(self, i=0):
42 "Reset the failures counter to a given index." 43 self.failures = i
44
45 - def get_index(self):
46 """Returns the current number of failures which is also the 47 index into the list this object represents. ger_serverurl() 48 should always be used to translate an index into a URL 49 as this object may change how indexs map. (See RoundRobin)""" 50 51 return self.failures
52
53 - def len(self):
54 """Returns the how many URLs we've got to cycle through.""" 55 56 return len(self.repo.urls)
57 58 59
60 -class priority(baseFailOverMethod):
61 62 """Chooses server based on the first success in the list.""" 63
64 - def get_serverurl(self, i=None):
65 "Returns a serverurl based on this failover method or None if complete failure." 66 67 if i == None: 68 index = self.failures 69 else: 70 index = i 71 72 if index >= len(self.repo.urls): 73 return None 74 75 return self.repo.urls[index]
76 77 78
79 -class roundRobin(baseFailOverMethod):
80 81 """Chooses server based on a round robin.""" 82
83 - def __init__(self, repo):
84 baseFailOverMethod.__init__(self, repo) 85 random.seed() 86 self.offset = random.randint(0, 37)
87
88 - def get_serverurl(self, i=None):
89 "Returns a serverurl based on this failover method or None if complete failure." 90 91 if i == None: 92 index = self.failures 93 else: 94 index = i 95 96 if index >= len(self.repo.urls): 97 return None 98 99 rr = (index + self.offset) % len(self.repo.urls) 100 return self.repo.urls[rr]
101 102 # SDG 103