Skip to content
This repository was archived by the owner on Oct 10, 2020. It is now read-only.

[merged] Cleanup atomic command minor fixes#521

Closed
rhatdan wants to merge 1 commit intoprojectatomic:masterfrom
rhatdan:hasattr
Closed

[merged] Cleanup atomic command minor fixes#521
rhatdan wants to merge 1 commit intoprojectatomic:masterfrom
rhatdan:hasattr

Conversation

@rhatdan
Copy link
Member

@rhatdan rhatdan commented Aug 7, 2016

1 Remove debugging output when I mount an ostree image.
2 Cleanup cmd_env to not modify current processes os.environ

Atomic/atomic.py Outdated

if hasattr(self.args, 'opt1') and self.args.opt1:
if self.args.get('opt1', False):
os.environ['OPT1'] = self.args.opt1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm...not related to this change but this looks a bit dangerous - we're mutating the current process' environment, which means that conceptually we can only do one action at a time. If we ever supported running multiple containers in one go, this would have to change.

It's normally better regardless to prepare the environment for the child process rather than mutating self's. Anyways, not a blocker for this patch, just noting.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes good point, I would love to drop most of the opt handling, since we are now passing the environment from the atomic command into the containers runtime.

@cgwalters
Copy link
Member

Looks like the first commit was written by root@?

@rhatdan rhatdan force-pushed the hasattr branch 2 times, most recently from 7ceb02c to b338f26 Compare August 8, 2016 13:15
@rhatdan rhatdan changed the title Cleanup some of the hassattr calls by using self.attr.get("OPTION", VAL): Cleanup atomic command minor fixes Aug 8, 2016
@rhatdan
Copy link
Member Author

rhatdan commented Aug 8, 2016

@cgwalters Fixed root->dwalsh
Fixed cmd_env to use a copy of os.environ()
Now container three patches.

Atomic/atomic.py Outdated
def cmd_env(self):
os.environ['NAME'] = self.name or ""
os.environ['IMAGE'] = self.image or ""
newenv = os.environ
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need newenv = dict(os.environ) or so.

@rhatdan rhatdan force-pushed the hasattr branch 5 times, most recently from e47bd95 to aad1219 Compare August 9, 2016 20:56
@rhatdan rhatdan force-pushed the hasattr branch 2 times, most recently from 22b89e6 to 0cba782 Compare August 16, 2016 15:47
@adelton
Copy link
Contributor

adelton commented Aug 19, 2016

The reason for tests failing is the

    def sub_env_strings(self, in_string):
        """
        Performs substitutions on an input string based on defined
        environment variables.
        :param in_string: string to perform the subs on
        :return: string
        """
        # Set environment variables
        self.cmd_env()

        # Perform variable subs
        in_string = os.path.expandvars(in_string)

        # Replace undefined variables with blank
        in_string = re.sub(r'\$\{?\S*\}?', '', in_string)

        # Solve whitespacing
        in_string = " ".join(in_string.split())

        return in_string

That os.path.expandvars uses os.environ and cannot be passed different env.

So all ${IMAGE} and similar references get effectively cleared with the subsequent re.sub, resulting in

export PYTHONPATH=${PYTHONPATH:-$(pwd)}
export ATOMIC_LIBEXEC="$(pwd)"
./atomic install --display atomic-test-6
docker run --rm=true --net=host -v /:/host -e NAME= -e IMAGE= -e HOST=/host /bin/install.sh

@rhatdan
Copy link
Member Author

rhatdan commented Aug 19, 2016

@cgwalters What do you think? We could change the environment expand and then change it back?

@adelton
Copy link
Contributor

adelton commented Aug 19, 2016

@cgwalters What do you think? We could change the environment expand and then change it back?

Or reimplement os.path.expandvars to use/accept self.cmd_env().

By the way, why do you want to get rid of the os.environ modifications? The changes will go away when the process exits ...

@rhatdan rhatdan force-pushed the hasattr branch 3 times, most recently from dd8d3f5 to 72066b9 Compare August 19, 2016 12:04
@rhatdan
Copy link
Member Author

rhatdan commented Aug 19, 2016

How about this far simpler fix, Also @adelton does this fix your other problem where we were substituting twice.

@cgwalters
Copy link
Member

@adelton Regarding mutating os.environ - it's not threadsafe. https://sourceware.org/bugzilla/show_bug.cgi?id=15607

@adelton
Copy link
Contributor

adelton commented Aug 19, 2016

How many threads does a typical atomic command run?

@rhatdan
Copy link
Member Author

rhatdan commented Aug 19, 2016

1

@adelton
Copy link
Contributor

adelton commented Aug 19, 2016

I don't have enough Python foo to say if it should be dict(os.environ) or os.environ.copy() and whether that os.environ=oldenv does the right thing.

In any case, this patch now does not get rid of the modification of os.environ in cmd_env() which I thought was your goal and which the commit message still describes.

@rhatdan
Copy link
Member Author

rhatdan commented Aug 19, 2016

I think we can modify it, as long as we are about to exec. Modifying it permanently or running it multiple times can cause problems.

@cgwalters
Copy link
Member

Maybe 1 thread right now, but as soon as e.g. we port to GDBus it'll run a thread internally. Using GLib at all really may make your process multithreaded (there's also a worker thread).

@rhatdan
Copy link
Member Author

rhatdan commented Aug 19, 2016

I guess it would be best if somone took os.path.expandvars and made it take an environment param

@rhatdan
Copy link
Member Author

rhatdan commented Sep 2, 2016

No longer needed.

@rhatdan
Copy link
Member Author

rhatdan commented Sep 2, 2016

Added utils.expandvars. But I am not sure if this will work properly on python2 and python3

@rhatdan
Copy link
Member Author

rhatdan commented Sep 2, 2016

@adelton PTAL

@rhatdan
Copy link
Member Author

rhatdan commented Sep 2, 2016

My testing looks good for python3 and python2 and the test suite passed.

@rh-atomic-bot
Copy link

☔ The latest upstream changes (presumably 683c0fa) made this pull request unmergeable. Please resolve the merge conflicts.

Create a copy of os.environ and then modify the copy.
Also add a new function expandvars to util.  This function is a
copy of os.path.expandvars but takes an optional parameter of the
environment to expand.
@adelton
Copy link
Contributor

adelton commented Sep 5, 2016

LGTM.

@rhatdan
Copy link
Member Author

rhatdan commented Sep 5, 2016

@cgwalters PTAL

@cgwalters
Copy link
Member

@rh-atomic-bot r+ 9d5aa41

@rh-atomic-bot
Copy link

⌛ Testing commit 9d5aa41 with merge 83d87d5...

@rh-atomic-bot
Copy link

☀️ Test successful - status-atomicjenkins
Approved by: cgwalters
Pushing 83d87d5 to master...

@rh-atomic-bot rh-atomic-bot changed the title Cleanup atomic command minor fixes [merged] Cleanup atomic command minor fixes Sep 5, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants