|
@@ -1,185 +0,0 @@
|
1
|
|
-#!/usr/bin/python
|
2
|
|
-
|
3
|
|
-import os
|
4
|
|
-import sys
|
5
|
|
-
|
6
|
|
-# print 'Number of arguments:', len(sys.argv), 'arguments.'
|
7
|
|
-# print 'Argument List:', str(sys.argv)
|
8
|
|
-
|
9
|
|
-actions = [
|
10
|
|
- 'help',
|
11
|
|
- 'create_database',
|
12
|
|
- 'create_user',
|
13
|
|
- 'delete_database',
|
14
|
|
- 'delete_user',
|
15
|
|
- 'grant_all_privileges',
|
16
|
|
- 'test_connection'
|
17
|
|
-]
|
18
|
|
-
|
19
|
|
-def usage():
|
20
|
|
- print('')
|
21
|
|
- print('USAGE: '+__file__+' ['+'|'.join(action for action in actions)+'] [parameters]')
|
22
|
|
- print('')
|
23
|
|
- print(' Role management')
|
24
|
|
- print('')
|
25
|
|
- print(' '+__file__+' create_user <user_name> <password>')
|
26
|
|
- print(' '+__file__+' delete_user <user_name>')
|
27
|
|
- print('')
|
28
|
|
- print(' Database management')
|
29
|
|
- print('')
|
30
|
|
- print(' '+__file__+' create_database <database_name>')
|
31
|
|
- print(' '+__file__+' delete_database <database_name> --force')
|
32
|
|
- print(' '+__file__+' grant_all_privileges <database_name> <user_name>')
|
33
|
|
- print('')
|
34
|
|
- print(' Other')
|
35
|
|
- print('')
|
36
|
|
- print(' '+__file__+' test_connection <database_name> <user_name> <password> [<hostname>]')
|
37
|
|
- print('')
|
38
|
|
- print('')
|
39
|
|
- print('NOTES:')
|
40
|
|
- print(' - this tool is intended to be used as \'postgres\' system user')
|
41
|
|
- print(' - default hostname is 127.0.0.1')
|
42
|
|
- print('')
|
43
|
|
- print('')
|
44
|
|
-
|
45
|
|
-
|
46
|
|
-def show_help_and_exit():
|
47
|
|
- usage()
|
48
|
|
- exit()
|
49
|
|
-
|
50
|
|
-def on_result_and_exit(error_code):
|
51
|
|
- if error_code==0:
|
52
|
|
- print('')
|
53
|
|
- print('')
|
54
|
|
- exit(0)
|
55
|
|
-
|
56
|
|
- print('ERRROR')
|
57
|
|
- print('')
|
58
|
|
- print('')
|
59
|
|
- exit(error_code)
|
60
|
|
-
|
61
|
|
-#######
|
62
|
|
-
|
63
|
|
-
|
64
|
|
-if len(sys.argv)<=1:
|
65
|
|
- show_help_and_exit()
|
66
|
|
-
|
67
|
|
-if sys.argv[1] not in actions:
|
68
|
|
- print('Unknown command %s'%sys.argv[1])
|
69
|
|
- show_help_and_exit()
|
70
|
|
-
|
71
|
|
-if sys.argv[1]=='help':
|
72
|
|
- show_help_and_exit()
|
73
|
|
-
|
74
|
|
-########################################
|
75
|
|
-#
|
76
|
|
-# BELOW ARE STANDARD ACTIONS
|
77
|
|
-#
|
78
|
|
-########################################
|
79
|
|
-
|
80
|
|
-print('')
|
81
|
|
-if sys.argv[1]=='create_user':
|
82
|
|
- if len(sys.argv)!=4:
|
83
|
|
- show_help_and_exit()
|
84
|
|
- user_name = sys.argv[2]
|
85
|
|
- user_pass = sys.argv[3]
|
86
|
|
-
|
87
|
|
- print('PG # CREATE USER')
|
88
|
|
- print('----------------')
|
89
|
|
- print('name: '+user_name)
|
90
|
|
- print('password: XXX')
|
91
|
|
-
|
92
|
|
- sql = '"CREATE USER %s WITH PASSWORD \'%s\';"' %(user_name, user_pass)
|
93
|
|
- result = os.system('echo %s|psql' % (sql))
|
94
|
|
- on_result_and_exit(result)
|
95
|
|
-
|
96
|
|
-
|
97
|
|
-if sys.argv[1]=='delete_user':
|
98
|
|
- if len(sys.argv)!=3:
|
99
|
|
- show_help_and_exit()
|
100
|
|
-
|
101
|
|
- user_name = sys.argv[2]
|
102
|
|
-
|
103
|
|
- print('PG # DELETE USER')
|
104
|
|
- print('------------------')
|
105
|
|
- print('name: '+user_name)
|
106
|
|
- print('')
|
107
|
|
-
|
108
|
|
- sql = '"DROP ROLE %s;"' %(user_name)
|
109
|
|
- result = os.system('echo %s|psql' % (sql))
|
110
|
|
- on_result_and_exit(result)
|
111
|
|
-
|
112
|
|
-
|
113
|
|
-if sys.argv[1]=='create_database':
|
114
|
|
- if len(sys.argv)!=3:
|
115
|
|
- show_help_and_exit()
|
116
|
|
- name = sys.argv[2]
|
117
|
|
-
|
118
|
|
- print('PG # CREATE DATABASE')
|
119
|
|
- print('--------------------')
|
120
|
|
- print('name: '+name)
|
121
|
|
- print('')
|
122
|
|
-
|
123
|
|
- sql = '"CREATE DATABASE %s;"' %(name)
|
124
|
|
- result = os.system('echo %s|psql' % (sql))
|
125
|
|
- on_result_and_exit(result)
|
126
|
|
-
|
127
|
|
-if sys.argv[1]=='delete_database':
|
128
|
|
- if len(sys.argv)!=4:
|
129
|
|
- show_help_and_exit()
|
130
|
|
- name = sys.argv[2]
|
131
|
|
- force = sys.argv[3]
|
132
|
|
-
|
133
|
|
- print('PG # DROP DATABASE')
|
134
|
|
- print('------------------')
|
135
|
|
- print('name: '+name)
|
136
|
|
- print('')
|
137
|
|
-
|
138
|
|
- if force!='--force':
|
139
|
|
- print('Add --force option in order to drop a database')
|
140
|
|
- on_result_and_exit(-1)
|
141
|
|
-
|
142
|
|
- sql = '"DROP DATABASE %s;"' %(name)
|
143
|
|
- result = os.system('echo %s|psql' % (sql))
|
144
|
|
- on_result_and_exit(result)
|
145
|
|
-
|
146
|
|
-
|
147
|
|
-if sys.argv[1]=='grant_all_privileges':
|
148
|
|
- if len(sys.argv)!=4:
|
149
|
|
- show_help_and_exit()
|
150
|
|
- database_name = sys.argv[2]
|
151
|
|
- user_name = sys.argv[3]
|
152
|
|
-
|
153
|
|
- print('PG # GRANT ALL PRIVILEGES ON DATABASE')
|
154
|
|
- print('-------------------------------------')
|
155
|
|
- print('database: '+database_name)
|
156
|
|
- print('user: '+user_name)
|
157
|
|
- print('')
|
158
|
|
-
|
159
|
|
- sql = '"GRANT ALL PRIVILEGES ON DATABASE %s to %s;"' % (database_name, user_name)
|
160
|
|
- result = os.system('echo %s|psql' % (sql))
|
161
|
|
- on_result_and_exit(result)
|
162
|
|
-
|
163
|
|
-if sys.argv[1]=='test_connection':
|
164
|
|
- if len(sys.argv)<5:
|
165
|
|
- show_help_and_exit()
|
166
|
|
-
|
167
|
|
- hostname = '127.0.0.1'
|
168
|
|
- database_name = sys.argv[2]
|
169
|
|
- user_name = sys.argv[3]
|
170
|
|
- password = sys.argv[4]
|
171
|
|
-
|
172
|
|
- if len(sys.argv)>=6:
|
173
|
|
- hostname = sys.argv[5]
|
174
|
|
-
|
175
|
|
- print('PG # CONNECT TO DATABASE')
|
176
|
|
- print('------------------------')
|
177
|
|
- print('server: '+hostname)
|
178
|
|
- print('database: '+database_name)
|
179
|
|
- print('username: '+user_name)
|
180
|
|
- print('')
|
181
|
|
-
|
182
|
|
- result = os.system('export PGPASSWORD=%s; echo "SELECT NOW();"|psql -h %s -U %s %s' % (password, hostname, user_name, database_name))
|
183
|
|
- on_result_and_exit(result)
|
184
|
|
-
|
185
|
|
-
|