-
Notifications
You must be signed in to change notification settings - Fork 70
Util to gather what user is running Atomic App and which home dir it should use #658
Conversation
|
@dustymabe @kadel :) |
|
@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: |
|
@dustymabe |
|
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 |
|
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
27aadba
Compare
|
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:
|
atomicapp/utils.py
Outdated
| # 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about the inContainer() function that we have in this same file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yeah, I see that now! Adding .dockerenv and .docketinit to it would be viable too.
27aadba to
b2e198d
Compare
|
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
339a1bc
Compare
|
@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! |
atomicapp/utils.py
Outdated
| # 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since inContainer checks this I don't think we need it anymore.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll do this a separate commit then )
|
Also we need to update all dockerfiles.. don't want them getting out of sync |
339a1bc to
d869888
Compare
|
@dustymabe |
|
#dotests |
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? @staticmethod
def getUserName():
"""
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')
if os.getegid() == 0 and sudo_user is None:
user = 'root'
elif sudo_user is not None:
user = sudo_user
else:
user = os.environ.get('USER')
return user
@staticmethod
def getUserHome():
"""
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.expanduser
if incontainer: 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)
if home == ("~%s" % user):
logger.error("No home directory exists for user %s" % user)
# Back out of chroot if necessary
if incontainer: os.chroot("../..")
logger.debug("Running as user %s. Using home directory %s for configuration data"
% (user, home))
return home |
d869888 to
99bc6aa
Compare
…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
303a548
Compare
|
@dustymabe I've updated the PR with your changes as well as re-based against master. |
|
#dotests |
|
That darn pesky kubernetes test needs some love :( cdrage most all looks good to me. as part of this do you mind bringing the |
|
Agreed. Let's bring them back up-to-date and let's get this merged. WOOHOO! |
|
@dustymabe |
|
LGTM. |
|
#dotests |
|
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.