def run(project, command, directory, slack_webhook_url):
"""
Run the specified command as the user that owns the directory
"""
try:
st = os.stat(directory)
# order is important: after seteuid() call the effective UID isn't 0 anymore, so seteuid() will not be allowed
os.setegid(st.st_uid)
os.seteuid(st.st_gid)
logger.info("Changing working directory to '{0}'".format(directory))
logger.info("Spawning background command '{0}' as {1}:{2} for '{3}'...".format(command, st.st_uid, st.st_gid, project))
def background():
"""
I don't care how long it takes to run the command, but Bitbucket gets angry when it takes longer
than 10 seconds. My npm build takes around 15 secs, so I'd get 3 Webhooks from Bitbucket, because
it thinks each Webhook timedout.
Easy way out is to return response immediately and start a background thread that
does all of the heavy lifting.
"""
start_time = time.time()
output = subprocess.check_output(command, shell=True, cwd=directory, stderr=subprocess.STDOUT)
completed_in = time.time() - start_time
logger.info("'{0}' background command finished in {1:.2f} seconds".format(project, completed_in))
if slack_webhook_url:
slack_notification(slack_webhook_url, "Deployed `{0}` in {1:.2f} seconds! :rocket:".format(project, completed_in), output)
Thread(target=background).start()
except PermissionError:
logger.error("Insufficient permissions to set uid/gid")
except subprocess.CalledProcessError as e:
logger.error("Error: {0}".format(e.output))
finally:
logger.info("Restoring root permissions")
os.setegid(0)
os.seteuid(0)
评论列表
文章目录