Start a minion process
()
| 134 | |
| 135 | |
| 136 | def minion_process(): |
| 137 | """ |
| 138 | Start a minion process |
| 139 | """ |
| 140 | # Because the minion is going to start on a separate process, |
| 141 | # salt._logging.in_mainprocess() will return False. |
| 142 | # We'll just force it to return True for this particular case so |
| 143 | # that proper logging can be set up. |
| 144 | import salt._logging |
| 145 | |
| 146 | salt._logging.in_mainprocess.__pid__ = os.getpid() |
| 147 | # Now the remaining required imports |
| 148 | import salt.cli.daemons |
| 149 | import salt.utils.platform |
| 150 | |
| 151 | # salt_minion spawns this function in a new process |
| 152 | |
| 153 | def handle_hup(manager, sig, frame): |
| 154 | manager.minion.reload() |
| 155 | |
| 156 | lock = threading.RLock() |
| 157 | |
| 158 | def suicide_when_without_parent(parent_pid): |
| 159 | """ |
| 160 | Have the minion suicide if the parent process is gone |
| 161 | |
| 162 | NOTE: small race issue where the parent PID could be replace |
| 163 | with another process with same PID! |
| 164 | """ |
| 165 | while lock.acquire(blocking=False): |
| 166 | lock.release() |
| 167 | time.sleep(5) |
| 168 | try: |
| 169 | # check pid alive (Unix only trick!) |
| 170 | if os.getuid() == 0 and not salt.utils.platform.is_windows(): |
| 171 | os.kill(parent_pid, 0) |
| 172 | except OSError as exc: |
| 173 | # forcibly exit, regular sys.exit raises an exception-- which |
| 174 | # isn't sufficient in a thread |
| 175 | log.error("Minion process encountered exception: %s", exc) |
| 176 | os._exit(salt.defaults.exitcodes.EX_GENERIC) |
| 177 | |
| 178 | try: |
| 179 | if not salt.utils.platform.is_windows(): |
| 180 | thread = threading.Thread( |
| 181 | target=suicide_when_without_parent, args=(os.getppid(),) |
| 182 | ) |
| 183 | thread.start() |
| 184 | |
| 185 | minion = salt.cli.daemons.Minion() |
| 186 | signal.signal(signal.SIGHUP, functools.partial(handle_hup, minion)) |
| 187 | minion.start() |
| 188 | except (SaltClientError, SaltReqTimeoutError, SaltSystemExit) as exc: |
| 189 | lock.acquire(blocking=True) |
| 190 | log.warning( |
| 191 | "Fatal functionality error caught by minion handler:\n", exc_info=True |
| 192 | ) |
| 193 | log.warning("** Restarting minion **") |