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
96e815d2
Commit
96e815d2
authored
Jul 10, 2014
by
Steve Salevan
Committed by
Steve Salevan
Jul 10, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Don't write to cache if queries fail
parent
71c46efe
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
17 deletions
+34
-17
plugins/inventory/collins.py
+34
-17
No files found.
plugins/inventory/collins.py
View file @
96e815d2
...
@@ -93,10 +93,18 @@ class CollinsDefaults(object):
...
@@ -93,10 +93,18 @@ class CollinsDefaults(object):
LOG_FORMAT
=
'
%(asctime)-15
s
%(message)
s'
LOG_FORMAT
=
'
%(asctime)-15
s
%(message)
s'
class
Error
(
Exception
):
pass
class
MaxRetriesError
(
Error
):
pass
class
CollinsInventory
(
object
):
class
CollinsInventory
(
object
):
def
__init__
(
self
):
def
__init__
(
self
):
"""
Main execution path
"""
"""
Constructs CollinsInventory object and reads all configuration.
"""
self
.
inventory
=
dict
()
# A list of groups and the hosts in that group
self
.
inventory
=
dict
()
# A list of groups and the hosts in that group
self
.
cache
=
dict
()
# Details about hosts in the inventory
self
.
cache
=
dict
()
# Details about hosts in the inventory
...
@@ -109,7 +117,28 @@ class CollinsInventory(object):
...
@@ -109,7 +117,28 @@ class CollinsInventory(object):
filename
=
self
.
log_location
)
filename
=
self
.
log_location
)
self
.
log
=
logging
.
getLogger
(
'CollinsInventory'
)
self
.
log
=
logging
.
getLogger
(
'CollinsInventory'
)
def
_asset_get_attribute
(
self
,
asset
,
attrib
):
""" Returns a user-defined attribute from an asset if it exists; otherwise,
returns None. """
if
'ATTRIBS'
in
asset
:
for
attrib_block
in
asset
[
'ATTRIBS'
]
.
keys
():
if
attrib
in
asset
[
'ATTRIBS'
][
attrib_block
]:
return
asset
[
'ATTRIBS'
][
attrib_block
][
attrib
]
return
None
def
_asset_has_attribute
(
self
,
asset
,
attrib
):
""" Returns whether a user-defined attribute is present on an asset. """
if
'ATTRIBS'
in
asset
:
for
attrib_block
in
asset
[
'ATTRIBS'
]
.
keys
():
if
attrib
in
asset
[
'ATTRIBS'
][
attrib_block
]:
return
True
return
False
def
run
(
self
):
def
run
(
self
):
""" Main execution path """
# Updates cache if cache is not present or has expired.
# Updates cache if cache is not present or has expired.
successful
=
True
successful
=
True
if
self
.
args
.
refresh_cache
:
if
self
.
args
.
refresh_cache
:
...
@@ -158,9 +187,8 @@ class CollinsInventory(object):
...
@@ -158,9 +187,8 @@ class CollinsInventory(object):
# Locates all assets matching the provided query, exhausting pagination.
# Locates all assets matching the provided query, exhausting pagination.
while
True
:
while
True
:
if
num_retries
==
self
.
collins_max_retries
:
if
num_retries
==
self
.
collins_max_retries
:
self
.
log
.
error
(
"Maximum of
%
s retries reached; giving up"
,
raise
MaxRetriesError
(
"Maximum of
%
s retries reached; giving up"
%
\
self
.
collins_max_retries
)
self
.
collins_max_retries
)
break
query_parameters
[
'page'
]
=
cur_page
query_parameters
[
'page'
]
=
cur_page
query_url
=
"
%
s?
%
s"
%
(
query_url
=
"
%
s?
%
s"
%
(
(
CollinsDefaults
.
ASSETS_API_ENDPOINT
%
self
.
collins_host
),
(
CollinsDefaults
.
ASSETS_API_ENDPOINT
%
self
.
collins_host
),
...
@@ -249,20 +277,6 @@ class CollinsInventory(object):
...
@@ -249,20 +277,6 @@ class CollinsInventory(object):
action
=
'store_true'
,
default
=
False
,
help
=
'Pretty print all JSON output'
)
action
=
'store_true'
,
default
=
False
,
help
=
'Pretty print all JSON output'
)
self
.
args
=
parser
.
parse_args
()
self
.
args
=
parser
.
parse_args
()
def
_asset_has_attribute
(
self
,
asset
,
attrib
):
if
'ATTRIBS'
in
asset
:
for
attrib_block
in
asset
[
'ATTRIBS'
]
.
keys
():
if
attrib
in
asset
[
'ATTRIBS'
][
attrib_block
]:
return
True
return
False
def
_asset_get_attribute
(
self
,
asset
,
attrib
):
if
'ATTRIBS'
in
asset
:
for
attrib_block
in
asset
[
'ATTRIBS'
]
.
keys
():
if
attrib
in
asset
[
'ATTRIBS'
][
attrib_block
]:
return
asset
[
'ATTRIBS'
][
attrib_block
][
attrib
]
return
None
def
update_cache
(
self
):
def
update_cache
(
self
):
""" Make calls to Collins and saves the output in a cache """
""" Make calls to Collins and saves the output in a cache """
...
@@ -351,6 +365,9 @@ class CollinsInventory(object):
...
@@ -351,6 +365,9 @@ class CollinsInventory(object):
return
True
return
True
def
push
(
self
,
dictionary
,
key
,
value
):
def
push
(
self
,
dictionary
,
key
,
value
):
""" Adds a value to a list at a dictionary key, creating the list if it doesn't
exist. """
if
key
not
in
dictionary
:
if
key
not
in
dictionary
:
dictionary
[
key
]
=
[]
dictionary
[
key
]
=
[]
dictionary
[
key
]
.
append
(
value
)
dictionary
[
key
]
.
append
(
value
)
...
...
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