Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] sharing context with subprocess #138

Closed
MartykQ opened this issue Mar 27, 2023 · 2 comments
Closed

[Question] sharing context with subprocess #138

MartykQ opened this issue Mar 27, 2023 · 2 comments

Comments

@MartykQ
Copy link

MartykQ commented Mar 27, 2023

Hello,

is there a way to share a context with subprocess library, for example:

import openshift as oc
import subprocess

HOST = 'https://localhost:6443'
USER = 'ocuser'
PASSWORD = 'password'
PROJECT = 'my-proj'

with oc.api_server(HOST):
    with oc.login(USER, PASSWORD):
        with oc.project(PROJECT):
            subprocess.run('oc get pods | grep ABCD')

I would like to execute some commands directly from the subprocess library, making sure I'm in the correct server/project context

@bradmwilliams
Copy link
Contributor

Hi @MartykQ,
The majority of the actions performed by this library are already issued through the subprocess library: https://github.com/openshift/openshift-client-python/blob/main/packages/openshift/action.py#L351

To accomplish what you've asked above, you could simply do something like:

import openshift as oc

HOST = 'https://localhost:6443'
USER = 'ocuser'
PASSWORD = 'password'
PROJECT = 'my-proj'

with oc.api_server(HOST):
    with oc.login(USER, PASSWORD):
        with oc.project(PROJECT):
            pods = oc.selector('pods').objects()
            for pod in pods:
                if 'abcd' in pod.name():
                    result = pod.execute(cmd_to_exec=['ls', '-al'])
                    print(f'Result: {result.out()}')

@MartykQ
Copy link
Author

MartykQ commented Mar 28, 2023

Thanks @bradmwilliams! I think that one combined with oc.invoke will be enough for my use case

@MartykQ MartykQ closed this as completed Mar 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants