saltstack-formulas/postgres-formula

User creation and DB creation in the same run not working

johnkeates opened this issue · 4 comments

Creating a user and assigning a database (or the other way around) doesn't work properly. Probably because users are fetched and cached?


      ID: postgres_user-provisioner_local
Function: postgres_user.present
    Name: provisioner_local
  Result: True
 Comment: The user provisioner_local has been created
 Started: 15:13:29.747481
Duration: 519.459 ms
 Changes:   
          ----------
          provisioner_local:
              Present

      ID: postgres_database-provisioner_local
Function: postgres_database.present
    Name: provisioner_local
  Result: False
 Comment: An exception occurred in this state: Traceback (most recent call last):
            File "/usr/lib/python2.7/dist-packages/salt/state.py", line 1746, in call
              **cdata['kwargs'])
            File "/usr/lib/python2.7/dist-packages/salt/loader.py", line 1704, in wrapper
              return f(*args, **kwargs)
            File "/usr/lib/python2.7/dist-packages/salt/states/postgres_database.py", line 98, in present
              dbs = __salt__['postgres.db_list'](**db_args)
            File "/usr/lib/python2.7/dist-packages/salt/modules/postgres.py", line 461, in db_list
              password=password)
            File "/usr/lib/python2.7/dist-packages/salt/modules/postgres.py", line 417, in psql_query
              password=password)
            File "/usr/lib/python2.7/dist-packages/salt/modules/postgres.py", line 364, in _psql_prepare_and_run
              rcmd, runas=runas, password=password, host=host, port=port, user=user)
            File "/usr/lib/python2.7/dist-packages/salt/modules/postgres.py", line 181, in _run_psql
              ret = __salt__['cmd.run_all'](cmd, python_shell=False, **kwargs)
            File "/usr/lib/python2.7/dist-packages/salt/modules/cmdmod.py", line 1662, in run_all
              **kwargs)
            File "/usr/lib/python2.7/dist-packages/salt/modules/cmdmod.py", line 394, in _run
              'User \'{0}\' is not available'.format(runas)
          CommandExecutionError: User 'provisioner_local' is not available
 Started: 15:13:30.268091
Duration: 13.714 ms
 Changes:   `

Pillar:
`

postgres:
  #pg_hba.conf: salt://postgres/pg_hba.conf
  conf_dir: /etc/postgresql/9.4/main
  lookup:
    pkg: 'postgresql-9.4'
    pkg_client: 'postgresql-client-9.4'
    pkg_dev: 'postgresql-server-dev-9.4'
    pkg_contrib: 'postgresql-contrib-9.4'
    pg_hba: '/etc/postgresql/9.4/main/pg_hba.conf'
  version: 9.4

  users:
      provisioner_local:
        password: 'dfsjkle45980dfgkjl'
        createdb: False

  acls:
    - ['host', 'provisioner_local', 'provisioner_local', '0.0.0.0/0', 'md5']


  databases:
    provisioner_local:
      owner: 'provisioner_local'
      user: 'provisioner_local'
      template: 'template0'
      lc_ctype: 'C.UTF-8'
      lc_collate: 'C.UTF-8'
   
  postgresconf: |
    listen_addresses = '0.0.0.0'

`

Well, it basically doesn't work no matter what actually. Manually creating the database and then running state.apply isn't working either.

I can confirm this. I've updated the formula today from upstream and receive the same error. For me it looks like the postgres.database.present gets fired and tries to execute the command as the user that is passed in as owner. The user is not a user in the system, but just a database role.

Oh, directly after commenting I decided to read the documentation linked above.

@johnkeates you should not pass the argument user as this forces the behavior I described above. Only pass owner to ensure the owner gets set correctly. user forces salt to execute the command as this unix user, which is not present.

Might be duplicated in #158