| 519 | return proc |
| 520 | |
| 521 | class LocalDaemon(object): |
| 522 | def __init__(self, daemon_type, daemon_id): |
| 523 | self.daemon_type = daemon_type |
| 524 | self.daemon_id = daemon_id |
| 525 | self.controller = LocalRemote() |
| 526 | self.proc = None |
| 527 | |
| 528 | @property |
| 529 | def remote(self): |
| 530 | return LocalRemote() |
| 531 | |
| 532 | def running(self): |
| 533 | return self._get_pid() is not None |
| 534 | |
| 535 | def check_status(self): |
| 536 | if self.proc: |
| 537 | return self.proc.poll() |
| 538 | |
| 539 | def _get_pid(self): |
| 540 | """ |
| 541 | Return PID as an integer or None if not found |
| 542 | """ |
| 543 | ps_txt = self.controller.run(args=["ps", "ww", "-u"+str(os.getuid())], |
| 544 | stdout=StringIO()).\ |
| 545 | stdout.getvalue().strip() |
| 546 | lines = ps_txt.split("\n")[1:] |
| 547 | |
| 548 | for line in lines: |
| 549 | if line.find("ceph-{0} -i {1}".format(self.daemon_type, self.daemon_id)) != -1: |
| 550 | log.debug("Found ps line for daemon: {0}".format(line)) |
| 551 | return int(line.split()[0]) |
| 552 | if not opt_log_ps_output: |
| 553 | ps_txt = '(omitted)' |
| 554 | log.debug("No match for {0} {1}: {2}".format( |
| 555 | self.daemon_type, self.daemon_id, ps_txt)) |
| 556 | return None |
| 557 | |
| 558 | def wait(self, timeout): |
| 559 | waited = 0 |
| 560 | while self._get_pid() is not None: |
| 561 | if waited > timeout: |
| 562 | raise MaxWhileTries("Timed out waiting for daemon {0}.{1}".format(self.daemon_type, self.daemon_id)) |
| 563 | time.sleep(1) |
| 564 | waited += 1 |
| 565 | |
| 566 | def stop(self, timeout=300): |
| 567 | if not self.running(): |
| 568 | log.error('tried to stop a non-running daemon') |
| 569 | return |
| 570 | |
| 571 | pid = self._get_pid() |
| 572 | if pid is None: |
| 573 | return |
| 574 | log.debug("Killing PID {0} for {1}.{2}".format(pid, self.daemon_type, self.daemon_id)) |
| 575 | os.kill(pid, signal.SIGTERM) |
| 576 | |
| 577 | waited = 0 |
| 578 | while pid is not None: |