forked from HariSekhon/DevOps-Python-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgres_cli.py
More file actions
executable file
·85 lines (76 loc) · 2.77 KB
/
postgres_cli.py
File metadata and controls
executable file
·85 lines (76 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python
# vim:ts=4:sts=4:sw=4:et
#
# Author: Hari Sekhon
# Date: 2020-03-12 17:39:57 +0000 (Thu, 12 Mar 2020)
#
# https://github.com/harisekhon/devops-python-tools
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn
# and optionally send me feedback to help steer this or other code I publish
#
# https://www.linkedin.com/in/harisekhon
#
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import os
import socket
import sys
import psycopg2
libdir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'pylib'))
sys.path.append(libdir)
try:
# pylint: disable=wrong-import-position
from harisekhon.utils import log, validate_host, validate_port, validate_user, validate_password
from harisekhon import CLI
except ImportError as _:
print('module import failed: %s' % _, file=sys.stderr)
print("Did you remember to build the project by running 'make'?", file=sys.stderr)
print("Alternatively perhaps you tried to copy this program out without it's adjacent libraries?", file=sys.stderr)
sys.exit(4)
__author__ = 'Hari Sekhon'
__version__ = '0.1.0'
class PostgreSQLCLI(CLI):
def __init__(self):
# Python 2.x
super(PostgreSQLCLI, self).__init__()
# Python 3.x
# super().__init__()
self.name = ['PostgreSQL', 'Postgres', 'PG']
self.host = None
self.port = None
self.default_host = socket.getfqdn()
self.default_port = 5432
self.user = None
self.password = None
#self.ssl = False
self.verbose_default = 1
self.timeout_default = None
def add_options(self):
super(PostgreSQLCLI, self).add_options()
self.add_hostoption()
self.add_useroption()
self.add_opt('-d', '--database', help='Database to connect to')
def process_options(self):
super(PostgreSQLCLI, self).process_options()
self.host = self.get_opt('host')
self.host = self.get_opt('host')
self.user = self.get_opt('user')
self.password = self.get_opt('password')
validate_host(self.host)
validate_port(self.port)
validate_user(self.user)
validate_password(self.password)
self.port = int(self.port)
#self.ssl = self.get_opt('ssl')
def connect(self, database):
log.info('connecting to %s:%s database %s as user %s', self.host, self.port, database)
return psycopg2.connect(host=self.host,
port=self.port,
database=database,
user=self.user,
password=self.password)