blob: 06257c55be6244a87116dab8a11e869a08d1a5ee [file] [log] [blame]
Scott Bakerf0c38262018-03-12 12:07:07 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Zack Williams045b63d2019-01-22 16:30:57 -070015from __future__ import print_function
Zack Williams91d138f2019-06-07 15:42:50 -070016import argparse
Scott Bakerf0c38262018-03-12 12:07:07 -070017import psycopg2
Scott Bakerf0c38262018-03-12 12:07:07 -070018import time
19import traceback
20
21from xosconfig import Config
22
Zack Williams91d138f2019-06-07 15:42:50 -070023def wait_for_database(db_host, db_port, retry_interval):
Scott Bakerf0c38262018-03-12 12:07:07 -070024
Zack Williams91d138f2019-06-07 15:42:50 -070025 retry_count = 0
Zack Williams045b63d2019-01-22 16:30:57 -070026
Zack Williams91d138f2019-06-07 15:42:50 -070027 db_user = Config.get("database.username")
28 db_password = Config.get("database.password")
29
Scott Bakerf0c38262018-03-12 12:07:07 -070030 while True:
Zack Williams91d138f2019-06-07 15:42:50 -070031
Scott Bakerf0c38262018-03-12 12:07:07 -070032
33 try:
Zack Williams045b63d2019-01-22 16:30:57 -070034 myConnection = psycopg2.connect(
Zack Williams91d138f2019-06-07 15:42:50 -070035 host=db_host,
36 port=db_port,
37 user=db_user,
38 password=db_password,
39 connect_timeout=5,
Zack Williams045b63d2019-01-22 16:30:57 -070040 )
Scott Bakerf0c38262018-03-12 12:07:07 -070041
Scott Bakerf0c38262018-03-12 12:07:07 -070042 # Exit on successful connection
Zack Williams045b63d2019-01-22 16:30:57 -070043 print("Database is available")
Zack Williams91d138f2019-06-07 15:42:50 -070044
45 myConnection.close()
Scott Bakerf0c38262018-03-12 12:07:07 -070046 return
Zack Williams91d138f2019-06-07 15:42:50 -070047
48 except psycopg2.OperationalError:
49 # timeout reached, retrying
50 retry_count += 1
51 print("Timeout connecting to db, retrying (retry count: %d)" % retry_count)
52
Zack Williams045b63d2019-01-22 16:30:57 -070053 except BaseException:
Zack Williams91d138f2019-06-07 15:42:50 -070054 traceback.print_exc("Unknown exception while connecting to db")
55
56 # sleep for the retry interval between retries
57 time.sleep(float(retry_interval))
Scott Bakerf0c38262018-03-12 12:07:07 -070058
Zack Williams045b63d2019-01-22 16:30:57 -070059
Zack Williams91d138f2019-06-07 15:42:50 -070060# parse argumens
61parser = argparse.ArgumentParser()
62
63parser.add_argument(
64 "-c",
65 "--config",
66 dest="config",
67 default="/opt/xos/xos_config.yaml",
68 help="Location of XOS configuration file",
69)
70
71parser.add_argument(
72 "-s",
73 "--server",
74 dest="db_host",
75 default="xos-db",
76 help="Database server hostname (DNS or IP) to connect to",
77)
78
79parser.add_argument(
80 "-p",
81 "--port",
82 dest="db_port",
83 default="5432",
84 help="Database port on host to connect to",
85)
86
87parser.add_argument(
88 "-r",
89 "--retry-interval",
90 dest="retry_interval",
91 default="5",
92 help="Interval between connection retries in seconds",
93)
94
95args = parser.parse_args()
96
97Config.init(args.config)
98
99wait_for_database(args.db_host, args.db_port, args.retry_interval)