Persistent subprocess session

233 Views Asked by At

I'm having trouble thinking of the best way to approach this, but I want to login to confluent cloud from Python.

I have a function which retrieves the user/pass to log into Confluent, I'd then like a function to login, and then a function to do what I want with the cluster..

def login():
    email, password = get_secret()
    env_vars = os.environ.copy()
    env_vars["CONFLUENT_CLOUD_EMAIL"] = email
    env_vars["CONFLUENT_CLOUD_PASSWORD"] = password

    subprocess.run("confluent login", shell=True, env=env_vars)
    subprocess.run("confluent cluster list", shell=True)

I'm testing by just using subprocess.run() within the same function, and of course.. the second command fails, saying not logged in! Because when the first subprocess run closes, it ends the session.

I've tinkered with having Popen outside of the functions, something like;

p = subprocess.Popen("/bin/sh", env=env_vars, stdin=subprocess.PIPE, stdout=subprocess.PIPE

Then inside the relevant function just using something like p.stdin.write("command"), but that doesn't work either.

I'm scratching my head as to the best way to achieve this.

2

There are 2 best solutions below

0
On

this is how I have been doing, its one time login that will save your credentials in $HOME/.netrc, then you shell out confluent how you want.

confluent login --no-browser --prompt --save
0
On

The reason for doing it this way was because the Terraform provider for confluent cloud didn't have a resource to create cluster api-keys.

The provider has been updated in the last 48 hours, and now has the resource. So I no longer have a need for this. Updating here in case anyone else needs to know the provider is updated..