2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2025-05-30 01:26:38 +00:00

Support mutiple commands in the same time

- Using PID tailing name to avoid the lock
This commit is contained in:
Bin Xu 2018-03-12 14:47:04 +08:00
parent 817c5c6e4c
commit 1d7e0a0042
3 changed files with 31 additions and 14 deletions

View File

@ -26,6 +26,11 @@ class AgentShell(object):
"with the client",
default='/var/run/xcat/agent.sock',
type=str)
parser.add_argument('--lockfile',
help="The lock file to communicate "
"with the xcat",
default='/var/lock/xcat/agent.lock',
type=str)
return parser
def do_help(self, args):
@ -38,7 +43,8 @@ class AgentShell(object):
if options.help:
self.do_help(options)
return 0
s = server.Server(options.sock, options.standalone)
s = server.Server(options.sock, options.standalone, options.lockfile)
s.start()
class HelpFormatter(argparse.HelpFormatter):

View File

@ -14,7 +14,7 @@ from xcatagent import base as xcat_manager
MSG_TYPE = 'message'
DB_TYPE = 'db'
LOCK_FILE = '/var/lock/xcat/agent.lock'
#LOCK_FILE = '/var/lock/xcat/agent.lock'
class XCATMessager(utils.Messager):
@ -50,7 +50,7 @@ class XCATMessager(utils.Messager):
class Server(object):
def __init__(self, address, standalone):
def __init__(self, address, standalone=True, lockfile=None):
try:
os.unlink(address)
except OSError:
@ -58,6 +58,7 @@ class Server(object):
raise
self.address = address
self.standalone = standalone
self.lockfile = lockfile
self.server = StreamServer(self._serve(), self._handle)
def _serve(self):
@ -117,7 +118,7 @@ class Server(object):
def keep_peer_alive(self):
def acquire():
fd = open(LOCK_FILE, "r+")
fd = open(self.lockfile, "r+")
fcntl.flock(fd.fileno(), fcntl.LOCK_EX)
# if reach here, parent process may exit
print("xcat process exit unexpectedly.", file=sys.stderr)
@ -128,6 +129,6 @@ class Server(object):
t.start()
def start(self):
if not self.standalone:
if not self.standalone and self.lockfile:
self.keep_peer_alive()
self.server.serve_forever()

View File

@ -64,10 +64,10 @@ sub send_request {
sub acquire_lock {
mkpath($LOCK_DIR);
# always create a new lock file
unlink($LOCK_PATH);
open($lock_fd, ">>", $LOCK_PATH) or return undef;
unlink($LOCK_PATH . $$);
open($lock_fd, ">>", $LOCK_PATH . $$) or return undef;
flock($lock_fd, LOCK_EX) or return undef;
return $lock_fd;
return $LOCK_PATH . $$;
}
sub exists_python_agent {
@ -78,30 +78,38 @@ sub exists_python_agent {
}
sub start_python_agent {
if (!defined(acquire_lock())) {
my $lockfile = acquire_lock();
if (!defined($lockfile)) {
xCAT::MsgUtils->message("S", "start_python_agent() Error: Failed to acquire lock");
return undef;
}
my $fd;
open($fd, '>', $AGENT_SOCK_PATH) && close($fd);
my $pid = fork;
if (!defined $pid) {
xCAT::MsgUtils->message("S", "start_python_agent() Error: Unable to fork process");
return undef;
} elsif ($pid){
open($fd, '>', $AGENT_SOCK_PATH . $pid) && close($fd);
return $pid;
}
$SIG{CHLD} = 'DEFAULT';
if (!$pid) {
# child
open($fd, ">>", $PYTHON_LOG_PATH) && close($fd);
open(STDOUT, '>>', $PYTHON_LOG_PATH) or die("open: $!");
open(STDERR, '>>&', \*STDOUT) or die("open: $!");
my $ret = exec ($PYTHON_AGENT_FILE);
my @args = ( "$PYTHON_AGENT_FILE --sock $AGENT_SOCK_PATH$$ --lockfile $lockfile" );
my $ret = exec @args;
if (!defined($ret)) {
xCAT::MsgUtils->message("S", "start_python_agent() Error: Failed to start the xCAT Python agent.");
exit(1);
}
}
return $pid;
}
sub handle_message {
@ -131,7 +139,7 @@ sub submit_agent_request {
my $sock;
my $retry = 0;
while($retry < 30) {
$sock = IO::Socket::UNIX->new(Peer => $AGENT_SOCK_PATH, Type => SOCK_STREAM, Timeout => 10, Blocking => 1);
$sock = IO::Socket::UNIX->new(Peer => $AGENT_SOCK_PATH . $pid, Type => SOCK_STREAM, Timeout => 10, Blocking => 1);
if (!defined($sock)) {
sleep(0.1);
} else {
@ -140,7 +148,7 @@ sub submit_agent_request {
$retry++;
}
if (!defined($sock)) {
xCAT::MsgUtils->message("E", { data => ["OpenBMC management is using a Python framework. An error has occurred when trying to create socket $AGENT_SOCK_PATH."] }, $callback);
xCAT::MsgUtils->message("E", { data => ["OpenBMC management is using a Python framework. An error has occurred when trying to create socket $AGENT_SOCK_PATH$pid."] }, $callback);
kill('TERM', $pid);
return;
}
@ -201,6 +209,8 @@ sub wait_agent {
xCAT::MsgUtils->message("E", { data => ["Agent exited unexpectedly. See $PYTHON_LOG_PATH for details."] }, $callback);
xCAT::MsgUtils->message("I", { data => ["To revert to Perl framework: chdef -t site clustersite openbmcperl=ALL"] }, $callback);
}
unlink($AGENT_SOCK_PATH . $pid);
unlink($LOCK_PATH . $$);
}
#--------------------------------------------------------------------------------