Uh oh!
There was an error while loading. Please reload this page.
Util to gather what user is running Atomic App and which home dir it should use - #658
Conversation
cdrage
commented
Mar 31, 2016
dustymabe
commented
Apr 1, 2016
@cdrage - this doesn't work when running inside a container. I modified it to print out the results of a call to this function and here is what you see when running in a container: here is what you get when not running inside a container: |
cdrage
commented
Apr 1, 2016
@dustymabe |
cdrage
commented
Apr 1, 2016
http://stackoverflow.com/questions/20010199/determining-if-a-process-runs-inside-lxc-docker seems like the best way, i'll modify this a bit. @dustymabe |
cdrage
commented
Apr 4, 2016
Okay, so essentially it's two things that we push for AtomicApp. Running it via Atomic CLI or AtomicApp CLI. Thus the only change we need to add is -e SUDO and -e SUDO_USER to the RUN label within our Dockerfile. P.S. Although I'll still need to implement whether or not we are in a container or not in order to get the the config from inside the |
6b8b64f to
27aadbaComparecdrage
commented
Apr 4, 2016
Updated this in order to detect if we are running within a container. Thus when we use atomic CLI we can still detect what user is running it as from a usage perspective the only two options we give is to run via Tested this with Remember: We'll only use this function when need be, not putting this randomly in the This function is intended to:
|
| # This ONLY checks to see if we are running in a Docker container | ||
| # Atomic App does not support any other container platform (yet). | ||
| # Within a Docker container, the .dockerenv and .dockerinit files ALWAYS exist. | ||
| @staticmethod |
There was a problem hiding this comment.
what about the inContainer() function that we have in this same file?
There was a problem hiding this comment.
Oh yeah, I see that now! Adding .dockerenv and .docketinit to it would be viable too.
27aadba to
b2e198dComparedustymabe
commented
Apr 4, 2016
Also it would be cool to have the part that figures out what "user" we are as part of a separate function that is called from getUserHome(). I can see that being used in other places. |
b2e198d to
339a1bcComparecdrage
commented
Apr 4, 2016
@dustymabe The part of the function that gets sudo + sudo_user env variables? Otherwise, I've updated with your patch as well as removed the container function I had to inContainer! |
| # the /host path for finding the user directory. We are unable to use | ||
| # os.path.expanduser due to the passed volume | ||
| if Utils.inContainer(): | ||
| logger.debug("Atomic App is running in a container") |
There was a problem hiding this comment.
since inContainer checks this I don't think we need it anymore.
dustymabe
commented
Apr 4, 2016
yes |
| (bool): True == we are in a container | ||
| """ | ||
| if os.path.isdir(HOST_DIR): | ||
| if os.path.isfile('/.dockerenv') and os.path.isfile('/.dockerinit') and os.path.isdir(HOST_DIR): |
There was a problem hiding this comment.
are we sure we want to make this change here? if we do this should probably be in a separate commit
There was a problem hiding this comment.
I'll do this a separate commit then )
dustymabe
commented
Apr 4, 2016
Also we need to update all dockerfiles.. don't want them getting out of sync |
339a1bc to
d869888Comparecdrage
commented
Apr 4, 2016
@dustymabe |
cdrage
commented
Apr 5, 2016
#dotests |
dustymabe
commented
Apr 7, 2016
So I refactored the code a bit.. there is a new getUserName() function as well as refactored the getUserHome() function to not repeat code. What do you think? @staticmethoddefgetUserName():
""" Finds the username of the user running the application. Uses the SUDO_USER and USER environment variables. If runnning within a container, SUDO_USER and USER varibles must be passed for proper detection. Ex. docker run -v /:/host -e SUDO_USER -e USER foobar """sudo_user=os.environ.get('SUDO_USER')
ifos.getegid() ==0andsudo_userisNone:
user='root'elifsudo_userisnotNone:
user=sudo_userelse:
user=os.environ.get('USER')
returnuser@staticmethoddefgetUserHome():
""" Finds the home directory of the user running the application. If runnning within a container, the root dir must be passed as a volume. Ex. docker run -v /:/host -e SUDO_USER -e USER foobar """logger.debug("Finding the users home directory")
user=Utils.getUserName()
incontainer=Utils.inContainer()
# Check to see if we are running in a container. If we are we# will chroot into the /host path before calling os.path.expanduserifincontainer: os.chroot(HOST_DIR)
# Call os.path.expanduser to determine the user's home dir.# See https://docs.python.org/2/library/os.path.html#os.path.expanduser# Warn if none is detected, don't error as not having a home# dir doesn't mean we fail.home=os.path.expanduser("~%s"%user)
ifhome== ("~%s"%user):
logger.error("No home directory exists for user %s"%user)
# Back out of chroot if necessaryifincontainer: os.chroot("../..")
logger.debug("Running as user %s. Using home directory %s for configuration data"% (user, home))
returnhome |
d869888 to
99bc6aaCompare…should use This call finds out what user is running the Atomic App. Whether they are: sudo root user If it is running under sudo, this util will find the user running the sudo command. Once the user is found, os.path.expanduser(user) is utilized to find the home directory. If a home directory is not found atomic app will error out. If Atomic App is running within a Docker container it will utilize the passed /host dir (from the RUN/STOP label of Atomic CLI) in order to find the directory of the corresponding user
As we don't support any other container platform (yet), check to see if this is a Docker container.
99bc6aa to
303a548Comparecdrage
commented
Apr 7, 2016
@dustymabe I've updated the PR with your changes as well as re-based against master. |
dustymabe
commented
Apr 7, 2016
#dotests |
dustymabe
commented
Apr 7, 2016
That darn pesky kubernetes test needs some love :( cdrage most all looks good to me. as part of this do you mind bringing the |
cdrage
commented
Apr 7, 2016
Agreed. Let's bring them back up-to-date and let's get this merged. WOOHOO! |
cdrage
commented
Apr 7, 2016
@dustymabe |
dustymabe
commented
Apr 7, 2016
LGTM. |
dustymabe
commented
Apr 7, 2016
#dotests |
cdrage
commented
Apr 7, 2016
Woo! 🎊 |
This call finds out what user is running the Atomic App.
Whether they are:
sudo
root
user
If it is running under sudo, this util will find the user running the
sudo command.
Once the user is found, os.path.expanduser(user) is utilized to find the
home directory. If a home directory is not found atomic app will error
out.
Fixes#656
This util will be used to implement default config locations for OpenShift and k8s providers.