Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
LOFAR
Manage
Activity
Members
Labels
Plan
Issues
Wiki
Jira issues
Open Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
RadioObservatory
LOFAR
Commits
c55f04ea
Commit
c55f04ea
authored
6 years ago
by
Jorrit Schaap
Browse files
Options
Downloads
Patches
Plain Diff
SW-415
: automatically create dbcredentials file with sane defaults when no credentials are found
parent
230cc3a7
No related branches found
No related tags found
1 merge request
!87
Lsmr epic
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
LCS/PyCommon/dbcredentials.py
+33
-10
33 additions, 10 deletions
LCS/PyCommon/dbcredentials.py
with
33 additions
and
10 deletions
LCS/PyCommon/dbcredentials.py
+
33
−
10
View file @
c55f04ea
...
...
@@ -130,9 +130,14 @@ class Credentials:
return
options
class
DBCredentials
:
NoSectionError
=
NoSectionError
def
__init__
(
self
,
filepatterns
=
None
):
self
.
filepatterns
=
filepatterns
if
filepatterns
else
[
"
{LOFARROOT}/etc/dbcredentials/*.ini
"
,
"
{HOME}/.lofar/dbcredentials/*.ini
"
,
]
self
.
read_config_from_files
()
def
read_config_from_files
(
self
,
filepatterns
=
None
):
"""
Read database credentials from all configuration files matched by any of the patterns.
...
...
@@ -154,13 +159,10 @@ class DBCredentials:
These database credentials can subsequently be queried under their
symbolic name (
"
OTDB
"
in the example).
"""
if
filepatterns
is
None
:
filepatterns
=
[
"
{LOFARROOT}/etc/dbcredentials/*.ini
"
,
"
{HOME}/.lofar/dbcredentials/*.ini
"
,
]
if
filepatterns
is
not
None
:
self
.
filepatterns
=
filepatterns
self
.
files
=
sum
([
findfiles
(
p
)
for
p
in
filepatterns
],[])
self
.
files
=
sum
([
findfiles
(
p
)
for
p
in
self
.
filepatterns
],[])
# make sure the files are mode 600 to hide passwords
for
file
in
self
.
files
:
...
...
@@ -175,6 +177,19 @@ class DBCredentials:
self
.
config
=
SafeConfigParser
()
self
.
config
.
read
(
self
.
files
)
def
create_default_file
(
self
,
database
):
"""
creates a dbcredentials file with defaults in ~/.lofar/dbcredentials/<database>.ini
:param database: name of the database/file
"""
new_path
=
os
.
path
.
join
(
user_info
.
pw_dir
,
'
.lofar
'
,
'
dbcredentials
'
,
database
+
'
.ini
'
)
if
not
os
.
path
.
exists
(
os
.
path
.
dirname
(
new_path
)):
os
.
makedirs
(
os
.
path
.
dirname
(
new_path
))
with
open
(
new_path
,
'
w
'
)
as
new_file
:
new_file
.
write
(
"
[database:%s]
\n
host=localhost
\n
user=%s
\n
password=unknown
\n
type=unknown
\n
port=0
\n
database=%s
"
%
(
database
,
user_info
.
pw_name
,
database
))
logger
.
info
(
"
created default dbcredentials file for database=%s at %s
"
,
database
,
new_path
)
def
get
(
self
,
database
):
"""
Return credentials for a given database.
...
...
@@ -182,8 +197,15 @@ class DBCredentials:
# create default credentials
creds
=
Credentials
()
try
:
# read configuration (can throw NoSectionError)
d
=
dict
(
self
.
config
.
items
(
self
.
_section
(
database
)))
except
NoSectionError
:
# create defaults file, and reload
self
.
create_default_file
(
database
)
self
.
read_config_from_files
()
# re-read configuration now that we have created a new file with defaults
d
=
dict
(
self
.
config
.
items
(
self
.
_section
(
database
)))
# save the full config to support custom fields
creds
.
config
=
d
...
...
@@ -202,6 +224,7 @@ class DBCredentials:
return
creds
def
set
(
self
,
database
,
credentials
):
"""
Add or overwrite credentials for a given database.
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment