Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
91b0149c
Commit
91b0149c
authored
Nov 17, 2014
by
Brian Coca
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
a cache plugin that stores facts persistently in local json dumps
parent
25607e5c
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
137 additions
and
0 deletions
+137
-0
lib/ansible/cache/jsonfile.py
+137
-0
No files found.
lib/ansible/cache/jsonfile.py
0 → 100644
View file @
91b0149c
# (c) 2014, Brian Coca, Josh Drake, et al
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
import
os
import
time
import
json
import
errno
from
ansible
import
constants
as
C
from
ansible
import
utils
from
ansible.cache.base
import
BaseCacheModule
class
CacheModule
(
BaseCacheModule
):
"""
A caching module backed by json files.
"""
def
__init__
(
self
,
*
args
,
**
kwargs
):
self
.
_timeout
=
float
(
C
.
CACHE_PLUGIN_TIMEOUT
)
self
.
_cache
=
{}
self
.
_cache_dir
=
C
.
CACHE_PLUGIN_CONNECTION
# expects a dir path
if
not
os
.
path
.
exists
(
self
.
_cache_dir
):
try
:
os
.
makedirs
(
self
.
_cache_dir
)
except
(
OSError
,
IOError
),
e
:
utils
.
warning
(
"error while trying to create cache dir
%
s :
%
s"
%
(
self
.
_cache_dir
,
str
(
e
)))
return
None
def
get
(
self
,
key
):
if
key
in
self
.
_cache
:
return
self
.
_cache
.
get
(
key
)
if
self
.
has_expired
(
key
):
raise
KeyError
cachefile
=
"
%
s/
%
s"
%
(
self
.
_cache_dir
,
key
)
try
:
f
=
open
(
cachefile
,
'r'
)
except
(
OSError
,
IOError
),
e
:
utils
.
warning
(
"error while trying to write to
%
s :
%
s"
%
(
cachefile
,
str
(
e
)))
else
:
value
=
json
.
load
(
f
)
self
.
_cache
[
key
]
=
value
return
value
finally
:
f
.
close
()
def
set
(
self
,
key
,
value
):
self
.
_cache
[
key
]
=
value
cachefile
=
"
%
s/
%
s"
%
(
self
.
_cache_dir
,
key
)
try
:
#TODO: check if valid keys can have invalid FS chars, base32?
f
=
open
(
cachefile
,
'w'
)
except
(
OSError
,
IOError
),
e
:
utils
.
warning
(
"error while trying to read
%
s :
%
s"
%
(
cachefile
,
str
(
e
)))
else
:
json
.
dump
(
value
,
f
,
ensure_ascii
=
False
)
finally
:
f
.
close
()
def
has_expired
(
self
,
key
):
cachefile
=
"
%
s/
%
s"
%
(
self
.
_cache_dir
,
key
)
try
:
st
=
os
.
stat
(
cachefile
)
except
(
OSError
,
IOError
),
e
:
if
e
.
errno
==
errno
.
ENOENT
:
return
False
else
:
utils
.
warning
(
"error while trying to stat
%
s :
%
s"
%
(
cachefile
,
str
(
e
)))
if
time
.
time
()
-
st
.
st_mtime
<=
self
.
_timeout
:
return
False
if
key
in
self
.
_cache
:
del
self
.
_cache
[
key
]
return
True
def
keys
(
self
):
keys
=
[]
for
k
in
os
.
listdir
(
self
.
_cache_dir
):
if
not
self
.
has_expired
(
k
):
keys
.
append
(
k
)
return
keys
def
contains
(
self
,
key
):
if
key
in
self
.
_cache
:
return
True
if
self
.
has_expired
(
key
):
return
False
try
:
st
=
os
.
stat
(
"
%
s/
%
s"
%
(
self
.
_cache_dir
,
key
))
return
True
except
(
OSError
,
IOError
),
e
:
if
e
.
errno
==
errno
.
ENOENT
:
return
False
else
:
utils
.
warning
(
"error while trying to stat
%
s :
%
s"
%
(
cachefile
,
str
(
e
)))
def
delete
(
self
,
key
):
del
self
.
_cache
[
key
]
try
:
os
.
remove
(
"
%
s/
%
s"
%
(
self
.
_cache_dir
,
key
))
except
(
OSError
,
IOError
),
e
:
pass
#TODO: only pass on non existing?
def
flush
(
self
):
self
.
_cache
=
{}
for
key
in
self
.
keys
():
self
.
delete
(
key
)
def
copy
(
self
):
ret
=
dict
()
for
key
in
self
.
keys
():
ret
[
key
]
=
self
.
get
(
key
)
return
ret
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment