123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- #!/usr/bin/python
-
- import os
- import sys
-
- # print 'Number of arguments:', len(sys.argv), 'arguments.'
- # print 'Argument List:', str(sys.argv)
-
- actions = [
- 'help',
- 'create_database',
- 'create_user',
- 'delete_database',
- 'delete_user',
- 'grant_all_privileges',
- 'test_connection'
- ]
-
- def usage():
- print('')
- print('USAGE: '+__file__+' ['+'|'.join(action for action in actions)+'] [parameters]')
- print('')
- print(' Role management')
- print('')
- print(' '+__file__+' create_user <user_name> <password>')
- print(' '+__file__+' delete_user <user_name>')
- print('')
- print(' Database management')
- print('')
- print(' '+__file__+' create_database <database_name>')
- print(' '+__file__+' delete_database <database_name> --force')
- print(' '+__file__+' grant_all_privileges <database_name> <user_name>')
- print('')
- print(' Other')
- print('')
- print(' '+__file__+' test_connection <database_name> <user_name> <password> [<hostname>]')
- print('')
- print('')
- print('NOTES:')
- print(' - this tool is intended to be used as \'postgres\' system user')
- print(' - default hostname is 127.0.0.1')
- print('')
- print('')
-
-
- def show_help_and_exit():
- usage()
- exit()
-
- def on_result_and_exit(error_code):
- if error_code==0:
- print('')
- print('')
- exit(0)
-
- print('ERRROR')
- print('')
- print('')
- exit(error_code)
-
- #######
-
-
- if len(sys.argv)<=1:
- show_help_and_exit()
-
- if sys.argv[1] not in actions:
- print('Unknown command %s'%sys.argv[1])
- show_help_and_exit()
-
- if sys.argv[1]=='help':
- show_help_and_exit()
-
- ########################################
- #
- # BELOW ARE STANDARD ACTIONS
- #
- ########################################
-
- print('')
- if sys.argv[1]=='create_user':
- if len(sys.argv)!=4:
- show_help_and_exit()
- user_name = sys.argv[2]
- user_pass = sys.argv[3]
-
- print('PG # CREATE USER')
- print('----------------')
- print('name: '+user_name)
- print('password: XXX')
-
- sql = '"CREATE USER %s WITH PASSWORD \'%s\';"' %(user_name, user_pass)
- result = os.system('echo %s|psql' % (sql))
- on_result_and_exit(result)
-
-
- if sys.argv[1]=='delete_user':
- if len(sys.argv)!=3:
- show_help_and_exit()
-
- user_name = sys.argv[2]
-
- print('PG # DELETE USER')
- print('------------------')
- print('name: '+user_name)
- print('')
-
- sql = '"DROP ROLE %s;"' %(user_name)
- result = os.system('echo %s|psql' % (sql))
- on_result_and_exit(result)
-
-
- if sys.argv[1]=='create_database':
- if len(sys.argv)!=3:
- show_help_and_exit()
- name = sys.argv[2]
-
- print('PG # CREATE DATABASE')
- print('--------------------')
- print('name: '+name)
- print('')
-
- sql = '"CREATE DATABASE %s;"' %(name)
- result = os.system('echo %s|psql' % (sql))
- on_result_and_exit(result)
-
- if sys.argv[1]=='delete_database':
- if len(sys.argv)!=4:
- show_help_and_exit()
- name = sys.argv[2]
- force = sys.argv[3]
-
- print('PG # DROP DATABASE')
- print('------------------')
- print('name: '+name)
- print('')
-
- if force!='--force':
- print('Add --force option in order to drop a database')
- on_result_and_exit(-1)
-
- sql = '"DROP DATABASE %s;"' %(name)
- result = os.system('echo %s|psql' % (sql))
- on_result_and_exit(result)
-
-
- if sys.argv[1]=='grant_all_privileges':
- if len(sys.argv)!=4:
- show_help_and_exit()
- database_name = sys.argv[2]
- user_name = sys.argv[3]
-
- print('PG # GRANT ALL PRIVILEGES ON DATABASE')
- print('-------------------------------------')
- print('database: '+database_name)
- print('user: '+user_name)
- print('')
-
- sql = '"GRANT ALL PRIVILEGES ON DATABASE %s to %s;"' % (database_name, user_name)
- result = os.system('echo %s|psql' % (sql))
- on_result_and_exit(result)
-
- if sys.argv[1]=='test_connection':
- if len(sys.argv)<5:
- show_help_and_exit()
-
- hostname = '127.0.0.1'
- database_name = sys.argv[2]
- user_name = sys.argv[3]
- password = sys.argv[4]
-
- if len(sys.argv)>=6:
- hostname = sys.argv[5]
-
- print('PG # CONNECT TO DATABASE')
- print('------------------------')
- print('server: '+hostname)
- print('database: '+database_name)
- print('username: '+user_name)
- print('')
-
- result = os.system('export PGPASSWORD=%s; echo "SELECT NOW();"|psql -h %s -U %s %s' % (password, hostname, user_name, database_name))
- on_result_and_exit(result)
-
-
|