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
aa1e00e8
Commit
aa1e00e8
authored
Jul 31, 2015
by
Bernhard Lichtinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Forgot to use commit -a to submit all changes...
parent
38ff797d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
66 additions
and
31 deletions
+66
-31
contrib/inventory/spacewalk.py
+66
-31
No files found.
contrib/inventory/spacewalk.py
View file @
aa1e00e8
...
@@ -16,14 +16,15 @@ This script is dependent upon the spacealk-reports package being installed
...
@@ -16,14 +16,15 @@ This script is dependent upon the spacealk-reports package being installed
on the same machine. It is basically a CSV-to-JSON converter from the
on the same machine. It is basically a CSV-to-JSON converter from the
output of "spacewalk-report system-groups-systems|inventory".
output of "spacewalk-report system-groups-systems|inventory".
Tested with Ansible 1.
1
Tested with Ansible 1.
9.2 and spacewalk 2.3
"""
"""
#
#
# Author:: Jon Miller <jonEbird@gmail.com>
# Author:: Jon Miller <jonEbird@gmail.com>
# Copyright:: Copyright (c) 2013, Jon Miller
# Copyright:: Copyright (c) 2013, Jon Miller
#
#
# Extended for support of multiple organizations by
# Extended for support of multiple organizations and
# Bernhard Lichtinger <bernhard.lichtinger@lrz.de> 2014
# adding the "_meta" dictionary to --list output by
# Bernhard Lichtinger <bernhard.lichtinger@lrz.de> 2015
#
#
# This program is free software: you can redistribute it and/or modify
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# it under the terms of the GNU General Public License as published by
...
@@ -44,6 +45,7 @@ import os
...
@@ -44,6 +45,7 @@ import os
import
time
import
time
from
optparse
import
OptionParser
from
optparse
import
OptionParser
import
subprocess
import
subprocess
import
ConfigParser
try
:
try
:
import
json
import
json
...
@@ -54,8 +56,9 @@ base_dir = os.path.dirname(os.path.realpath(__file__))
...
@@ -54,8 +56,9 @@ base_dir = os.path.dirname(os.path.realpath(__file__))
SW_REPORT
=
'/usr/bin/spacewalk-report'
SW_REPORT
=
'/usr/bin/spacewalk-report'
CACHE_DIR
=
os
.
path
.
join
(
base_dir
,
".spacewalk_reports"
)
CACHE_DIR
=
os
.
path
.
join
(
base_dir
,
".spacewalk_reports"
)
CACHE_AGE
=
300
# 5min
CACHE_AGE
=
300
# 5min
INI_FILE
=
os
.
path
.
join
(
base_dir
,
"spacewalk.ini"
)
# Sanity check
# Sanity check
if
not
os
.
path
.
exists
(
SW_REPORT
):
if
not
os
.
path
.
exists
(
SW_REPORT
):
print
>>
sys
.
stderr
,
'Error:
%
s is required for operation.'
%
(
SW_REPORT
)
print
>>
sys
.
stderr
,
'Error:
%
s is required for operation.'
%
(
SW_REPORT
)
sys
.
exit
(
1
)
sys
.
exit
(
1
)
...
@@ -83,6 +86,8 @@ def spacewalk_report(name):
...
@@ -83,6 +86,8 @@ def spacewalk_report(name):
lines
=
open
(
cache_filename
,
'r'
)
.
readlines
()
lines
=
open
(
cache_filename
,
'r'
)
.
readlines
()
keys
=
lines
[
0
]
.
strip
()
.
split
(
','
)
keys
=
lines
[
0
]
.
strip
()
.
split
(
','
)
# add 'spacewalk_' prefix to the keys
keys
=
[
'spacewalk_'
+
key
for
key
in
keys
]
for
line
in
lines
[
1
:]:
for
line
in
lines
[
1
:]:
values
=
line
.
strip
()
.
split
(
','
)
values
=
line
.
strip
()
.
split
(
','
)
if
len
(
keys
)
==
len
(
values
):
if
len
(
keys
)
==
len
(
values
):
...
@@ -107,12 +112,25 @@ parser.add_option('-p', default=False, dest="prefix_org_name", action="store_tru
...
@@ -107,12 +112,25 @@ parser.add_option('-p', default=False, dest="prefix_org_name", action="store_tru
(
options
,
args
)
=
parser
.
parse_args
()
(
options
,
args
)
=
parser
.
parse_args
()
# read spacewalk.ini if present
#------------------------------
if
os
.
path
.
exists
(
INI_FILE
):
config
=
ConfigParser
.
SafeConfigParser
()
config
.
read
(
INI_FILE
)
if
config
.
has_option
(
'spacewalk'
,
'cache_age'
):
CACHE_AGE
=
config
.
get
(
'spacewalk'
,
'cache_age'
)
if
not
options
.
org_number
and
config
.
has_option
(
'spacewalk'
,
'org_number'
):
options
.
org_number
=
config
.
get
(
'spacewalk'
,
'org_number'
)
if
not
options
.
prefix_org_name
and
config
.
has_option
(
'spacewalk'
,
'prefix_org_name'
):
options
.
prefix_org_name
=
config
.
getboolean
(
'spacewalk'
,
'prefix_org_name'
)
# Generate dictionary for mapping group_id to org_id
# Generate dictionary for mapping group_id to org_id
#------------------------------
#------------------------------
org_groups
=
{}
org_groups
=
{}
try
:
try
:
for
group
in
spacewalk_report
(
'system-groups'
):
for
group
in
spacewalk_report
(
'system-groups'
):
org_groups
[
group
[
'
group_id'
]]
=
group
[
'org_id'
]
org_groups
[
group
[
'
spacewalk_group_id'
]]
=
group
[
'spacewalk_org_id'
]
except
(
OSError
),
e
:
except
(
OSError
),
e
:
print
>>
sys
.
stderr
,
'Problem executing the command "
%
s system-groups":
%
s'
%
\
print
>>
sys
.
stderr
,
'Problem executing the command "
%
s system-groups":
%
s'
%
\
...
@@ -124,32 +142,48 @@ except (OSError), e:
...
@@ -124,32 +142,48 @@ except (OSError), e:
#------------------------------
#------------------------------
if
options
.
list
:
if
options
.
list
:
# to build the "_meta"-Group with hostvars first create dictionary for later use
host_vars
=
{}
try
:
for
item
in
spacewalk_report
(
'inventory'
):
host_vars
[
item
[
'spacewalk_profile_name'
]
]
=
dict
(
(
key
,
(
value
.
split
(
';'
)
if
';'
in
value
else
value
)
)
for
key
,
value
in
item
.
items
()
)
except
(
OSError
),
e
:
print
>>
sys
.
stderr
,
'Problem executing the command "
%
s inventory":
%
s'
%
\
(
SW_REPORT
,
str
(
e
))
sys
.
exit
(
2
)
groups
=
{}
groups
=
{}
meta
=
{
"hostvars"
:
{}
}
try
:
try
:
for
system
in
spacewalk_report
(
'system-groups-systems'
):
for
system
in
spacewalk_report
(
'system-groups-systems'
):
# first get org_id of system
# first get org_id of system
org_id
=
org_groups
[
system
[
'
group_id'
]
]
org_id
=
org_groups
[
system
[
'spacewalk_
group_id'
]
]
# shall we add the org_id as prefix to the group name:
# shall we add the org_id as prefix to the group name:
if
options
.
prefix_org_name
:
if
options
.
prefix_org_name
:
prefix
=
org_id
+
"-"
prefix
=
org_id
+
"-"
group_name
=
prefix
+
system
[
'group_name'
]
group_name
=
prefix
+
system
[
'spacewalk_group_name'
]
else
:
else
:
group_name
=
system
[
'group_name'
]
group_name
=
system
[
'spacewalk_group_name'
]
# if we are limited to one organization:
# if we are limited to one organization:
if
options
.
org_number
:
if
options
.
org_number
:
if
org_id
==
options
.
org_number
:
if
org_id
==
options
.
org_number
:
if
group_name
not
in
groups
:
if
group_name
not
in
groups
:
groups
[
group_name
]
=
set
()
groups
[
group_name
]
=
set
()
groups
[
group_name
]
.
add
(
system
[
'server_name'
])
groups
[
group_name
]
.
add
(
system
[
'spacewalk_server_name'
])
# or we list all groups and systems:
if
system
[
'spacewalk_server_name'
]
in
host_vars
and
not
system
[
'spacewalk_server_name'
]
in
meta
[
"hostvars"
]:
else
:
meta
[
"hostvars"
][
system
[
'spacewalk_server_name'
]
]
=
host_vars
[
system
[
'spacewalk_server_name'
]
]
if
group_name
not
in
groups
:
# or we list all groups and systems:
groups
[
group_name
]
=
set
()
else
:
if
group_name
not
in
groups
:
groups
[
group_name
]
.
add
(
system
[
'server_name'
])
groups
[
group_name
]
=
set
()
groups
[
group_name
]
.
add
(
system
[
'spacewalk_server_name'
])
if
system
[
'spacewalk_server_name'
]
in
host_vars
and
not
system
[
'spacewalk_server_name'
]
in
meta
[
"hostvars"
]:
meta
[
"hostvars"
][
system
[
'spacewalk_server_name'
]
]
=
host_vars
[
system
[
'spacewalk_server_name'
]
]
except
(
OSError
),
e
:
except
(
OSError
),
e
:
print
>>
sys
.
stderr
,
'Problem executing the command "
%
s system-groups-systems":
%
s'
%
\
print
>>
sys
.
stderr
,
'Problem executing the command "
%
s system-groups-systems":
%
s'
%
\
...
@@ -160,8 +194,10 @@ if options.list:
...
@@ -160,8 +194,10 @@ if options.list:
for
group
,
systems
in
groups
.
iteritems
():
for
group
,
systems
in
groups
.
iteritems
():
print
'[
%
s]
\n
%
s
\n
'
%
(
group
,
'
\n
'
.
join
(
systems
))
print
'[
%
s]
\n
%
s
\n
'
%
(
group
,
'
\n
'
.
join
(
systems
))
else
:
else
:
print
json
.
dumps
(
dict
([
(
k
,
list
(
s
))
for
k
,
s
in
groups
.
iteritems
()
]))
final
=
dict
(
[
(
k
,
list
(
s
))
for
k
,
s
in
groups
.
iteritems
()
]
)
final
[
"_meta"
]
=
meta
print
json
.
dumps
(
final
)
#print json.dumps(groups)
sys
.
exit
(
0
)
sys
.
exit
(
0
)
...
@@ -172,7 +208,7 @@ elif options.host:
...
@@ -172,7 +208,7 @@ elif options.host:
host_details
=
{}
host_details
=
{}
try
:
try
:
for
system
in
spacewalk_report
(
'inventory'
):
for
system
in
spacewalk_report
(
'inventory'
):
if
system
[
'hostname'
]
==
options
.
host
:
if
system
[
'
spacewalk_
hostname'
]
==
options
.
host
:
host_details
=
system
host_details
=
system
break
break
...
@@ -186,8 +222,7 @@ elif options.host:
...
@@ -186,8 +222,7 @@ elif options.host:
for
k
,
v
in
host_details
.
iteritems
():
for
k
,
v
in
host_details
.
iteritems
():
print
'
%
s:
%
s'
%
(
k
,
'
\n
'
.
join
(
v
.
split
(
';'
)))
print
'
%
s:
%
s'
%
(
k
,
'
\n
'
.
join
(
v
.
split
(
';'
)))
else
:
else
:
print
json
.
dumps
(
host_details
)
print
json
.
dumps
(
dict
(
(
key
,
(
value
.
split
(
';'
)
if
';'
in
value
else
value
)
)
for
key
,
value
in
host_details
.
items
()
)
)
sys
.
exit
(
0
)
sys
.
exit
(
0
)
else
:
else
:
...
...
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