0

My goal is to configure my new AWS instances with a python script that installs my repositories and packages, then utilizing subprocess to configure, (in this case), postgres. Values are read from a yaml or json file. So far I have had success installing all the packages and repos for the environment. Where I'm stuck is a python script that will create a user in postgres then create a database.

import subprocess
# out is to show output, com is the stdin value(s) to pass
def run(command, out=False, com=False, ):

  process = subprocess.Popen(
    command,
    close_fds=True,
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT
  )

  if out:
    out = iter(process.stdout.readline, b'')
    for line in out:
      print(line)

  if com:
    process.stdin.write(com)
    # http://stackoverflow.com/questions/16091112/python-pipe-to-popen-stdin
    # process.communicate(com) doesn't fit, i need multiple stdin

run(['ls', '-l'], True) # works 
run(['sudo', 'su', '-', 'postgres']) # works 

run(['createuser'], False, 'testuser') 
# broken, also after i get this working, this should be a list of stdin values    
# this results in infinite lines:
# Shall the new role be a superuser? (y/n)
# I do have a workaround, run(['createuser', '-d', '-r', '-s', '-w', 'username'], False)
# Confirmed workaround created user successfully

1 Answer 1

0

I haven't used Postgres in ages, but I'm guessing you want to su to postgres, and then run a command that requires postgres' rights.

But your run(['sudo', 'su', '-', 'postgres']) will probably just open a shell and wait; run(['createuser'], False, 'testuser') will likely never get run.

Instead, I believe you should combine these two commands. See the "-c" option to su.

Alternatively, and a little more securely, you could give your source user postgres rights in sudo, to take root out of the equation and make your code a little simpler.

Sign up to request clarification or add additional context in comments.

3 Comments

it gets ran. See the infinite output of: Shall the new role be a superuser? (y/n)
So the best way to solve this, is not to run commands with subprocess that require multiple stdin's. I have a feeling this works in Python 3 though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.