Ticket #575: 0001-Added-lock-for-execution-of-client.patch

File 0001-Added-lock-for-execution-of-client.patch, 3.0 KB (added by [email protected]…, 15 years ago)

patch to prevent concurrent update of configuration

  • bcfg2/src/lib/Options.py

    From dc8b38fb0a84007530e2ad58cb5808923394b7fc Mon Sep 17 00:00:00 2001
    From: Steve Tousignant <[email protected](none)>
    Date: Wed, 9 Jul 2008 13:47:19 -0400
    Subject: [PATCH] Added lock for execution of client
    
    This patch add a file lock for the execution part so that the agent and the cli can work side-by-side. An option to omit the check as been done also
    
    diff --git a/bcfg2/src/lib/Options.py b/bcfg2/src/lib/Options.py
    index 712b5a5..3c5b397 100644
    a b AGENT_HOST = Option('Remote host', default=False, cmd='-H', odesc='<hostname>') 
    249249ENCODING = Option('Encoding of cfg files', default=sys.getdefaultencoding(), cmd='-E', odesc='<encoding>', 
    250250                  cf=('components', 'encoding')) 
    251251 
     252OMIT_LOCK_CHECK = Option('Omit lock check', default=False, cmd='-O') 
     253 
    252254class OptionParser(OptionSet): 
    253255    '''OptionParser bootstraps option parsing, getting the value of the config file''' 
    254256    def __init__(self, args): 
  • bcfg2/src/sbin/bcfg2

    diff --git a/bcfg2/src/sbin/bcfg2 b/bcfg2/src/sbin/bcfg2
    index 5ac1e3a..4757c12 100755
    a b import sys 
    1111import tempfile 
    1212import time 
    1313import xmlrpclib 
     14import fcntl 
    1415import Bcfg2.Options 
    1516import Bcfg2.Client.XML 
    1617import Bcfg2.Client.Frame 
    def cb_sigint_handler(signum, frame): 
    3132DECISION_LIST = Bcfg2.Options.Option('Decision List', default=False, 
    3233                                     cmd="--decision-list", odesc='<file>', 
    3334                                     long_arg=True) 
     35LOCKFILE = "/var/lock/bcfg2.run" 
    3436 
    3537class FPProxyCall(object): 
    3638    def __init__(self, proxy, method): 
    class Client: 
    117119            'key': Bcfg2.Options.SERVER_KEY, 
    118120            'decision-list': DECISION_LIST, 
    119121            'encoding': Bcfg2.Options.ENCODING, 
     122            'omit-lock-check': Bcfg2.Options.OMIT_LOCK_CHECK, 
    120123            } 
    121124 
    122125        self.setup = Bcfg2.Options.OptionParser(optinfo) 
    class Client: 
    287290                                              times, self.setup['drivers'], 
    288291                                              self.setup['dryrun']) 
    289292 
     293        if not self.setup['omit-lock-check']: 
     294            #check lock here 
     295            lockfile = open(LOCKFILE, 'w') 
     296            try: 
     297                fcntl.lockf(lockfile.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) 
     298            except IOError: 
     299                #otherwise exit and give a warning to the user 
     300                self.fatal_error("An other instance of bcfg2 is running. If you what to bypass the check, run with %s option" % 
     301                    (Bcfg2.Options.OMIT_LOCK_CHECK.cmd)) 
     302                return(1) 
     303        # execute the said configuration 
    290304        self.tools.Execute() 
    291305 
     306        if not self.setup['omit-lock-check']: 
     307            #unlock here 
     308            fcntl.lockf(lockfile.fileno(), fcntl.LOCK_UN) 
     309            os.remove(LOCKFILE) 
     310         
    292311        if not self.setup['file']: 
    293312            # upload statistics 
    294313            feedback = self.tools.GenerateStats()