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>') |
249 | 249 | ENCODING = Option('Encoding of cfg files', default=sys.getdefaultencoding(), cmd='-E', odesc='<encoding>', |
250 | 250 | cf=('components', 'encoding')) |
251 | 251 | |
| 252 | OMIT_LOCK_CHECK = Option('Omit lock check', default=False, cmd='-O') |
| 253 | |
252 | 254 | class OptionParser(OptionSet): |
253 | 255 | '''OptionParser bootstraps option parsing, getting the value of the config file''' |
254 | 256 | def __init__(self, args): |
diff --git a/bcfg2/src/sbin/bcfg2 b/bcfg2/src/sbin/bcfg2
index 5ac1e3a..4757c12 100755
a
|
b
|
import sys |
11 | 11 | import tempfile |
12 | 12 | import time |
13 | 13 | import xmlrpclib |
| 14 | import fcntl |
14 | 15 | import Bcfg2.Options |
15 | 16 | import Bcfg2.Client.XML |
16 | 17 | import Bcfg2.Client.Frame |
… |
… |
def cb_sigint_handler(signum, frame): |
31 | 32 | DECISION_LIST = Bcfg2.Options.Option('Decision List', default=False, |
32 | 33 | cmd="--decision-list", odesc='<file>', |
33 | 34 | long_arg=True) |
| 35 | LOCKFILE = "/var/lock/bcfg2.run" |
34 | 36 | |
35 | 37 | class FPProxyCall(object): |
36 | 38 | def __init__(self, proxy, method): |
… |
… |
class Client: |
117 | 119 | 'key': Bcfg2.Options.SERVER_KEY, |
118 | 120 | 'decision-list': DECISION_LIST, |
119 | 121 | 'encoding': Bcfg2.Options.ENCODING, |
| 122 | 'omit-lock-check': Bcfg2.Options.OMIT_LOCK_CHECK, |
120 | 123 | } |
121 | 124 | |
122 | 125 | self.setup = Bcfg2.Options.OptionParser(optinfo) |
… |
… |
class Client: |
287 | 290 | times, self.setup['drivers'], |
288 | 291 | self.setup['dryrun']) |
289 | 292 | |
| 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 |
290 | 304 | self.tools.Execute() |
291 | 305 | |
| 306 | if not self.setup['omit-lock-check']: |
| 307 | #unlock here |
| 308 | fcntl.lockf(lockfile.fileno(), fcntl.LOCK_UN) |
| 309 | os.remove(LOCKFILE) |
| 310 | |
292 | 311 | if not self.setup['file']: |
293 | 312 | # upload statistics |
294 | 313 | feedback = self.tools.GenerateStats() |