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
d0ded631
Commit
d0ded631
authored
Jun 08, 2013
by
Michael DeHaan
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3148 from jlaska/rhnreg_ks
Add rhnreg_ks module
parents
3b86690b
6059ce34
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
531 additions
and
96 deletions
+531
-96
library/packaging/rhnreg_ks
+371
-0
library/packaging/subscription_manager
+160
-96
No files found.
library/packaging/rhnreg_ks
0 → 100644
View file @
d0ded631
#!/usr/bin/python
DOCUMENTATION
=
'''
---
module
:
rhnreg_ks
short_description
:
Manage
Red
Hat
Network
registration
using
the
C
(
rhnreg_ks
)
command
description
:
-
Manage
registration
to
the
Red
Hat
Network
.
version_added
:
"1.2"
author
:
James
Laska
notes
:
-
In
order
to
register
a
system
,
rhnreg_ks
requires
either
a
username
and
password
,
or
an
activationkey
.
requirements
:
-
rhnreg_ks
options
:
state
:
description
:
-
whether
to
register
(
C
(
present
)),
or
unregister
(
C
(
absent
))
a
system
required
:
false
choices
:
[
"present"
,
"absent"
]
default
:
"present"
username
:
description
:
-
Red
Hat
Network
username
required
:
False
default
:
null
password
:
description
:
-
Red
Hat
Network
password
required
:
False
default
:
null
server_url
:
description
:
-
Specify
an
alternative
Red
Hat
Network
server
URL
required
:
False
default
:
Current
value
of
I
(
serverURL
)
from
C
(
/
etc
/
sysconfig
/
rhn
/
up2date
)
is
the
default
activationkey
:
description
:
-
supply
an
activation
key
for
use
with
registration
required
:
False
default
:
null
channels
:
description
:
-
Optionally
specify
a
list
of
comma
-
separated
channels
to
subscribe
to
upon
successful
registration
.
required
:
false
default
:
[]
examples
:
-
code
:
rhnreg_ks
state
=
absent
username
=
joe_user
password
=
somepass
description
:
Unregister
system
from
RHN
.
-
code
:
rhnreg_ks
state
=
present
username
=
joe_user
password
=
somepass
description
:
Register
as
user
I
(
joe_user
)
with
password
I
(
somepass
)
and
auto
-
subscribe
to
available
content
.
-
code
:
rhnreg_ks
state
=
present
activationkey
=
1
-
222333444
enable_eus
=
true
description
:
Register
with
activationkey
I
(
1
-
222333444
)
and
enable
extended
update
support
.
-
code
:
rhnreg_ks
state
=
present
username
=
joe_user
password
=
somepass
server_url
=
https
:
//xmlrpc.my.satellite/XMLRPC
description:
Register
as
user
I
(
joe_user
)
with
password
I
(
somepass
)
against
a
satellite
server
specified
by
I
(
server_url
).
-
code
:
rhnreg_ks
state
=
present
username
=
joe_user
password
=
somepass
channels
=
rhel
-
x86_64
-
server
-
6
-
foo
-
1
,
rhel
-
x86_64
-
server
-
6
-
bar
-
1
description
:
Register
as
user
I
(
joe_user
)
with
password
I
(
somepass
)
and
enable
channels
I
(
rhel
-
x86_64
-
server
-
6
-
foo
-
1
)
and
I
(
rhel
-
x86_64
-
server
-
6
-
bar
-
1
).
'''
import
sys
import
os
import
re
import
types
import
subprocess
import
ConfigParser
import
shlex
import
xmlrpclib
import
urlparse
# Attempt to import rhn client tools
sys
.
path
.
insert
(
0
,
'
/
usr
/
share
/
rhn
'
)
try
:
import
up2date_client
import
up2date_client
.
config
except
ImportError
,
e
:
module
.
fail_json
(
msg
=
"Unable to import up2date_client. Is 'rhn-client-tools' installed?
\n
%s"
%
e
)
class
CommandException
(
Exception
)
:
pass
def
run_command
(
args
)
:
'''
Convenience
method
to
run
a
command
,
specified
as
a
list
of
arguments
.
Returns
:
*
tuple
-
(
stdout
,
stder
,
retcode
)
'''
# Coerce into a string
if
isinstance
(
args
,
str
)
:
args
=
shlex
.
split
(
args
)
# Run desired command
proc
=
subprocess
.
Popen
(
args
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
STDOUT
)
(
stdout
,
stderr
)
=
proc
.
communicate
()
returncode
=
proc
.
poll
()
if
returncode
!=
0
:
cmd
=
' '
.
join
(
args
)
raise
CommandException
(
"Command failed (%s): %s
\n
%s"
%
(
returncode
,
cmd
,
stdout
))
return
(
stdout
,
stderr
,
returncode
)
class
RegistrationBase
(
object
)
:
def
__init__
(
self
,
username
=
None
,
password
=
None
)
:
self
.
username
=
username
self
.
password
=
password
def
configure
(
self
)
:
raise
NotImplementedError
(
"Must be implemented by a sub-class"
)
def
enable
(
self
)
:
# Remove any existing redhat.repo
redhat_repo
=
'
/
etc
/
yum
.
repos
.
d
/
redhat
.
repo
'
if
os
.
path
.
isfile
(
redhat_repo
)
:
os
.
unlink
(
redhat_repo
)
def
register
(
self
)
:
raise
NotImplementedError
(
"Must be implemented by a sub-class"
)
def
unregister
(
self
)
:
raise
NotImplementedError
(
"Must be implemented by a sub-class"
)
def
unsubscribe
(
self
)
:
raise
NotImplementedError
(
"Must be implemented by a sub-class"
)
def
update_plugin_conf
(
self
,
plugin
,
enabled
=
True
)
:
plugin_conf
=
'
/
etc
/
yum
/
pluginconf
.
d
/%
s
.
conf
'
%
plugin
if
os
.
path
.
isfile
(
plugin_conf
)
:
cfg
=
ConfigParser
.
ConfigParser
()
cfg
.
read
([
plugin_conf
])
if
enabled
:
cfg
.
set
(
'
main
'
,
'
enabled
'
,
1
)
else
:
cfg
.
set
(
'
main
'
,
'
enabled
'
,
0
)
fd
=
open
(
plugin_conf
,
'
rwa
+
'
)
cfg
.
write
(
fd
)
fd
.
close
()
def
subscribe
(
self
,
**
kwargs
)
:
raise
NotImplementedError
(
"Must be implemented by a sub-class"
)
class
Rhn
(
RegistrationBase
)
:
def
__init__
(
self
,
username
=
None
,
password
=
None
,
filename
=
None
)
:
RegistrationBase
.
__init__
(
self
,
username
,
password
)
self
.
config
=
self
.
_read_config
(
filename
)
def
_read_config
(
self
,
filename
=
None
)
:
'''
Read
configuration
from
/
etc
/
sysconfig
/
rhn
/
up2date
'''
self
.
config
=
up2date_client
.
config
.
initUp2dateConfig
(
filename
)
# Add support for specifying a default value w/o having to standup some
# configuration. Yeah, I know this should be subclassed ... but, oh
# well
def
get_option_default
(
self
,
key
,
default
=
''
)
:
if
self
.
has_key
(
key
)
:
return
self
[
key
]
else
:
return
default
self
.
config
.
get_option
=
types
.
MethodType
(
get_option_default
,
self
.
config
,
up2date_client
.
config
.
Config
)
return
self
.
config
@property
def
hostname
(
self
)
:
'''
Return
the
non
-
xmlrpc
RHN
hostname
.
This
is
a
convenience
method
used
for
displaying
a
more
readable
RHN
hostname
.
Returns
:
str
'''
url
=
urlparse
.
urlparse
(
self
.
config
[
'
serverURL
'
])
return
url
.
netloc
.
replace
(
'
xmlrpc
.
','
'
)
@property
def
systemid
(
self
)
:
systemid
=
None
xpath_str
=
"//member[name='system_id']/value/string"
if
os
.
path
.
isfile
(
self
.
config
[
'
systemIdPath
'
])
:
fd
=
open
(
self
.
config
[
'
systemIdPath
'
],
'r'
)
xml_data
=
fd
.
read
()
fd
.
close
()
# Ugh, xml parsing time ...
# First, try parsing with libxml2 ...
if
systemid
is
None
:
try
:
import
libxml2
doc
=
libxml2
.
parseDoc
(
xml_data
)
ctxt
=
doc
.
xpathNewContext
()
systemid
=
ctxt
.
xpathEval
(
xpath_str
)[
0
].
content
doc
.
freeDoc
()
ctxt
.
xpathFreeContext
()
except
ImportError
:
pass
# m-kay, let's try with lxml now ...
if
systemid
is
None
:
try
:
from
lxml
import
etree
root
=
etree
.
fromstring
(
xml_data
)
systemid
=
root
.
xpath
(
xpath_str
)[
0
].
text
except
ImportError
:
pass
# Strip the 'ID-' prefix
if
systemid
is
not
None
and
systemid
.
startswith
(
'
ID
-
'
)
:
systemid
=
systemid
[
3
:
]
return
int
(
systemid
)
@property
def
is_registered
(
self
)
:
'''
Determine
whether
the
current
system
is
registered
.
Returns
:
True
|
False
'''
return
os
.
path
.
isfile
(
self
.
config
[
'
systemIdPath
'
])
def
configure
(
self
,
server_url
)
:
'''
Configure
system
for
registration
'''
self
.
config
.
set
(
'
serverURL
'
,
server_url
)
self
.
config
.
save
()
def
enable
(
self
)
:
'''
Prepare
the
system
for
RHN
registration
.
This
includes
...
*
enabling
the
rhnplugin
yum
plugin
*
disabling
the
subscription
-
manager
yum
plugin
'''
RegistrationBase
.
enable
(
self
)
self
.
update_plugin_conf
(
'
rhnplugin
'
,
True
)
self
.
update_plugin_conf
(
'
subscription
-
manager
'
,
False
)
def
register
(
self
,
enable_eus
=
False
,
activationkey
=
None
)
:
'''
Register
system
to
RHN
.
If
enable_eus
=
True
,
extended
update
support
will
be
requested
.
'''
register_cmd
=
"rhnreg_ks --username '%s' --password '%s' --force"
%
(
self
.
username
,
self
.
password
)
if
enable_eus
:
register_cmd
+=
" --use-eus-channel"
if
activationkey
is
not
None
:
register_cmd
+=
" --activationkey '%s'"
%
activationkey
# FIXME - support --profilename
# FIXME - support --systemorgid
run_command
(
register_cmd
)
def
api
(
self
,
method
,
*
args
)
:
'''
Convenience
RPC
wrapper
'''
if
not
hasattr
(
self
,
'
server
'
)
or
self
.
server
is
None
:
url
=
"https://xmlrpc.%s/rpc/api"
%
self
.
hostname
self
.
server
=
xmlrpclib
.
Server
(
url
,
verbose
=
0
)
self
.
session
=
self
.
server
.
auth
.
login
(
self
.
username
,
self
.
password
)
func
=
getattr
(
self
.
server
,
method
)
return
func
(
self
.
session
,
*
args
)
def
unregister
(
self
)
:
'''
Unregister
a
previously
registered
system
'''
# Initiate RPC connection
self
.
api
(
'
system
.
deleteSystems
'
,
[
self
.
systemid
])
# Remove systemid file
os
.
unlink
(
self
.
config
[
'
systemIdPath
'
])
def
subscribe
(
self
,
channels
=
[])
:
if
len
(
channels
)
<=
0
:
return
current_channels
=
self
.
api
(
'
channel
.
software
.
listSystemChannels
'
,
self
.
systemid
)
new_channels
=
[
item
[
'
channel_label
'
]
for
item
in
current_channels
]
new_channels
.
extend
(
channels
)
return
self
.
api
(
'
channel
.
software
.
setSystemChannels
'
,
self
.
systemid
,
new_channels
)
def
_subscribe
(
self
,
channels
=
[])
:
'''
Subscribe
to
requested
yum
repositories
using
'
rhn
-
channel
'
command
'''
rhn_channel_cmd
=
"rhn-channel --user='%s' --password='%s'"
%
(
self
.
username
,
self
.
password
)
(
stdout
,
stderr
,
rc
)
=
run_command
(
rhn_channel_cmd
+
" --available-channels"
)
# Enable requested repoid's
for
wanted_channel
in
channels
:
# Each inserted repo regexp will be matched. If no match, no success.
for
availaible_channel
in
stdout
.
rstrip
().
split
(
'\n'
)
:
#
.
rstrip
()
because
of
\
n
at
the
end
->
empty
string
at
the
end
if
re
.
search
(
wanted_repo
,
available_channel
)
:
run_command
(
rhn_channel_cmd
+
" --add --channel=%s"
%
available_channel
)
def
main
()
:
# Read system RHN configuration
rhn
=
Rhn
()
module
=
AnsibleModule
(
argument_spec
=
dict
(
state
=
dict
(
default
=
'
present
'
,
choices
=
[
'
present
'
,
'
absent
'
]),
username
=
dict
(
default
=
None
,
required
=
False
),
password
=
dict
(
default
=
None
,
required
=
False
),
server_url
=
dict
(
default
=
rhn
.
config
.
get_option
(
'
serverURL
'
),
required
=
False
),
activationkey
=
dict
(
default
=
None
,
required
=
False
),
enable_eus
=
dict
(
default
=
False
,
type
=
'
bool
'
),
channels
=
dict
(
default
=
[],
type
=
'
list
'
),
)
)
state
=
module
.
params
[
'
state
'
]
rhn
.
username
=
module
.
params
[
'
username
'
]
rhn
.
password
=
module
.
params
[
'
password
'
]
rhn
.
configure
(
module
.
params
[
'
server_url
'
])
activationkey
=
module
.
params
[
'
activationkey
'
]
channels
=
module
.
params
[
'
channels
'
]
# Ensure system is registered
if
state
==
'
present
'
:
# Check for missing parameters ...
if
not
(
activationkey
or
rhn
.
username
or
rhn
.
password
)
:
module
.
fail_json
(
msg
=
"Missing arguments, must supply an activationkey (%s) or username (%s) and password (%s)"
%
(
activationkey
,
rhn
.
username
,
rhn
.
password
))
if
not
activationkey
and
not
(
rhn
.
username
and
rhn
.
password
)
:
module
.
fail_json
(
msg
=
"Missing arguments, If registering without an activationkey, must supply username or password"
)
# Register system
if
rhn
.
is_registered
:
module
.
exit_json
(
changed
=
False
,
msg
=
"System already registered."
)
else
:
try
:
rhn
.
enable
()
rhn
.
register
(
module
.
params
[
'
enable_eus
'
]
==
True
,
activationkey
)
rhn
.
subscribe
(
channels
)
except
CommandException
,
e
:
module
.
fail_json
(
msg
=
"Failed to register with '%s': %s"
%
(
rhn
.
hostname
,
e
))
else
:
module
.
exit_json
(
changed
=
True
,
msg
=
"System successfully registered to '%s'."
%
rhn
.
hostname
)
# Ensure system is *not* registered
if
state
==
'
absent
'
:
if
not
rhn
.
is_registered
:
module
.
exit_json
(
changed
=
False
,
msg
=
"System already unregistered."
)
else
:
try
:
rhn
.
unregister
()
except
CommandException
,
e
:
module
.
fail_json
(
msg
=
"Failed to unregister: %s"
%
e
)
else
:
module
.
exit_json
(
changed
=
True
,
msg
=
"System successfully unregistered from %s."
%
rhn
.
hostname
)
# include magic from lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main
()
library/packaging/subscription_manager
View file @
d0ded631
...
@@ -100,67 +100,52 @@ def run_command(args):
...
@@ -100,67 +100,52 @@ def run_command(args):
return
(
stdout
,
stderr
,
returncode
)
return
(
stdout
,
stderr
,
returncode
)
class
RhsmPool
(
object
):
class
RegistrationBase
(
object
)
:
def
__init__
(
self
,
**
kwargs
):
def
__init__
(
self
,
username
=
None
,
password
=
None
)
:
for
k
,
v
in
kwargs
.
items
():
self
.
username
=
username
setattr
(
self
,
k
,
v
)
self
.
password
=
password
def
__str__
(
self
):
return
str
(
self
.
__getattribute__
(
'_name'
))
def
configure
(
self
)
:
def
subscribe
(
self
):
raise
NotImplementedError
(
"Must be implemented by a sub-class"
)
(
stdout
,
stderr
,
retcode
)
=
run_command
(
"subscription-manager subscribe --pool
%
s"
%
self
.
PoolId
)
return
True
def
enable
(
self
)
:
# Remove any existing redhat.repo
redhat_repo
=
'
/
etc
/
yum
.
repos
.
d
/
redhat
.
repo
'
class
RhsmPools
(
object
):
if
os
.
path
.
isfile
(
redhat_repo
)
:
"""
os
.
unlink
(
redhat_repo
)
This class is used for manipulating pools subscriptions with RHSM
"""
def
register
(
self
)
:
def
__init__
(
self
):
raise
NotImplementedError
(
"Must be implemented by a sub-class"
)
self
.
products
=
self
.
_load_product_list
()
def
unregister
(
self
)
:
def
__iter__
(
self
):
raise
NotImplementedError
(
"Must be implemented by a sub-class"
)
return
self
.
products
.
__iter__
()
def
unsubscribe
(
self
)
:
def
_load_product_list
(
self
):
raise
NotImplementedError
(
"Must be implemented by a sub-class"
)
"""
Loads list of all availaible pools for system in data structure
def
update_plugin_conf
(
self
,
plugin
,
enabled
=
True
)
:
"""
plugin_conf
=
'
/
etc
/
yum
/
pluginconf
.
d
/%
s
.
conf
'
%
plugin
(
stdout
,
stderr
,
retval
)
=
run_command
(
"subscription-manager list --available"
)
if
os
.
path
.
isfile
(
plugin_conf
)
:
cfg
=
ConfigParser
.
ConfigParser
()
cfg
.
read
([
plugin_conf
])
if
enabled
:
cfg
.
set
(
'
main
'
,
'
enabled
'
,
1
)
else
:
cfg
.
set
(
'
main
'
,
'
enabled
'
,
0
)
fd
=
open
(
plugin_conf
,
'
rwa
+
'
)
cfg
.
write
(
fd
)
fd
.
close
()
products
=
[]
def
subscribe
(
self
,
**
kwargs
)
:
for
line
in
stdout
.
split
(
'
\n
'
):
raise
NotImplementedError
(
"Must be implemented by a sub-class"
)
# Remove leading+trailing whitespace
line
=
line
.
strip
()
# An empty line implies the end of a output group
if
len
(
line
)
==
0
:
continue
# If a colon ':' is found, parse
elif
':'
in
line
:
(
key
,
value
)
=
line
.
split
(
':'
,
1
)
key
=
key
.
strip
()
.
replace
(
" "
,
""
)
# To unify
value
=
value
.
strip
()
if
key
in
[
'ProductName'
,
'SubscriptionName'
]:
# Remember the name for later processing
products
.
append
(
RhsmPool
(
_name
=
value
,
key
=
value
))
elif
products
:
# Associate value with most recently recorded product
products
[
-
1
]
.
__setattr__
(
key
,
value
)
# FIXME - log some warning?
#else:
# warnings.warn("Unhandled subscription key/value: %s/%s" % (key,value))
return
products
def
filter
(
self
,
regexp
=
'^$'
):
'''
Return a list of RhsmPools whose name matches the provided regular expression
'''
r
=
re
.
compile
(
regexp
)
for
product
in
self
.
products
:
if
r
.
search
(
product
.
_name
):
yield
product
class
Rhsm
(
RegistrationBase
)
:
def
__init__
(
self
,
username
=
None
,
password
=
None
)
:
RegistrationBase
.
__init__
(
self
,
username
,
password
)
self
.
config
=
self
.
_read_config
()
def
read_rhsm_config
(
rhsm_conf
=
'/etc/rhsm/rhsm.conf'
):
def
_read_config
(
self
,
rhsm_conf
=
'
/
etc
/
rhsm
/
rhsm
.
conf
'
)
:
'''
'''
Load
RHSM
configuration
from
/
etc
/
rhsm
/
rhsm
.
conf
.
Load
RHSM
configuration
from
/
etc
/
rhsm
/
rhsm
.
conf
.
Returns
:
Returns
:
...
@@ -169,7 +154,6 @@ def read_rhsm_config(rhsm_conf='/etc/rhsm/rhsm.conf'):
...
@@ -169,7 +154,6 @@ def read_rhsm_config(rhsm_conf='/etc/rhsm/rhsm.conf'):
# Read RHSM defaults ...
# Read RHSM defaults ...
cp
=
ConfigParser
.
ConfigParser
()
cp
=
ConfigParser
.
ConfigParser
()
if
os
.
path
.
isfile
(
rhsm_conf
):
cp
.
read
(
rhsm_conf
)
cp
.
read
(
rhsm_conf
)
# Add support for specifying a default value w/o having to standup some configuration
# Add support for specifying a default value w/o having to standup some configuration
...
@@ -185,8 +169,35 @@ def read_rhsm_config(rhsm_conf='/etc/rhsm/rhsm.conf'):
...
@@ -185,8 +169,35 @@ def read_rhsm_config(rhsm_conf='/etc/rhsm/rhsm.conf'):
return
cp
return
cp
def
enable
(
self
)
:
'''
Enable
the
system
to
receive
updates
from
subscription
-
manager
.
This
involves
updating
affected
yum
plugins
and
removing
any
conflicting
yum
repositories
.
'''
RegistrationBase
.
enable
(
self
)
self
.
update_plugin_conf
(
'
rhnplugin
'
,
False
)
self
.
update_plugin_conf
(
'
subscription
-
manager
'
,
True
)
def
configure
(
self
,
**
kwargs
)
:
'''
Configure
the
system
as
directed
for
registration
with
RHN
Raises
:
*
Exception
-
if
error
occurs
while
running
command
'''
args
=
[
'
subscription
-
manager
'
,
'
config
'
]
# Pass supplied **kwargs as parameters to subscription-manager. Ignore
# non-configuration parameters and replace '_' with '.'. For example,
# 'server_hostname' becomes '--system.hostname'.
for
k
,
v
in
kwargs
.
items
()
:
if
re
.
search
(
r
'
^
(
system
|
rhsm
)
_
'
,
k
)
:
args
.
append
(
'
--%
s
=%
s
'
%
(
k
.
replace
(
'_'
,
'.'
),
v
))
def
is_registered
():
run_command
(
args
)
@property
def
is_registered
(
self
)
:
'''
'''
Determine
whether
the
current
system
Determine
whether
the
current
system
Returns
:
Returns
:
...
@@ -207,26 +218,7 @@ def is_registered():
...
@@ -207,26 +218,7 @@ def is_registered():
# Display some debug output
# Display some debug output
return
True
return
True
def
register
(
self
,
username
,
password
,
autosubscribe
,
activationkey
)
:
def
configure
(
**
kwargs
):
'''
Configure the system as directed for registration with RHN
Raises:
* Exception - if error occurs while running command
'''
args
=
[
'subscription-manager'
,
'config'
]
# Pass supplied **kwargs as parameters to subscription-manager. Ignore
# non-configuration parameters and replace '_' with '.'. For example,
# 'server_hostname' becomes '--system.hostname'.
for
k
,
v
in
kwargs
.
items
():
if
re
.
search
(
r'^(system|rhsm)_'
,
k
):
args
.
append
(
'--
%
s=
%
s'
%
(
k
.
replace
(
'_'
,
'.'
),
v
))
run_command
(
args
)
def
register
(
username
,
password
,
autosubscribe
,
activationkey
):
'''
'''
Register
the
current
system
to
the
provided
RHN
server
Register
the
current
system
to
the
provided
RHN
server
Raises
:
Raises
:
...
@@ -248,8 +240,25 @@ def register(username, password, autosubscribe, activationkey):
...
@@ -248,8 +240,25 @@ def register(username, password, autosubscribe, activationkey):
# Do the needful...
# Do the needful...
run_command
(
args
)
run_command
(
args
)
def
unsubscribe
(
self
)
:
'''
Unsubscribe
a
system
from
all
subscribed
channels
Raises
:
*
Exception
-
if
error
occurs
while
running
command
'''
args
=
[
'
subscription
-
manager
'
,
'
unsubscribe
'
,
'
--
all
'
]
run_command
(
args
)
def
unregister
(
self
)
:
'''
Unregister
a
currently
registered
system
Raises
:
*
Exception
-
if
error
occurs
while
running
command
'''
args
=
[
'
subscription
-
manager
'
,
'
unregister
'
]
run_command
(
args
)
def
subscribe
(
regexp
):
def
subscribe
(
self
,
regexp
)
:
'''
'''
Subscribe
current
system
to
available
pools
matching
the
specified
Subscribe
current
system
to
available
pools
matching
the
specified
regular
expression
regular
expression
...
@@ -264,29 +273,82 @@ def subscribe(regexp):
...
@@ -264,29 +273,82 @@ def subscribe(regexp):
pool
.
subscribe
()
pool
.
subscribe
()
def
unregister
(
):
class
RhsmPool
(
object
)
:
'''
'''
Unregister a currently registered system
Convenience
class
for
housing
subscription
information
Raises:
* Exception - if error occurs while running command
'''
'''
args
=
[
'subscription-manager'
,
'unregister'
]
def
__init__
(
self
,
**
kwargs
)
:
return
run_command
(
args
)
for
k
,
v
in
kwargs
.
items
()
:
setattr
(
self
,
k
,
v
)
def
__str__
(
self
)
:
return
str
(
self
.
__getattribute__
(
'
_name
'
))
def
subscribe
(
self
)
:
(
stdout
,
stderr
,
retcode
)
=
run_command
(
"subscription-manager subscribe --pool %s"
%
self
.
PoolId
)
return
True
class
RhsmPools
(
object
)
:
"""
This class is used for manipulating pools subscriptions with RHSM
"""
def
__init__
(
self
)
:
self
.
products
=
self
.
_load_product_list
()
def
__iter__
(
self
)
:
return
self
.
products
.
__iter__
()
def
_load_product_list
(
self
)
:
"""
Loads list of all availaible pools for system in data structure
"""
(
stdout
,
stderr
,
retval
)
=
run_command
(
"subscription-manager list --available"
)
products
=
[]
for
line
in
stdout
.
split
(
'\n'
)
:
# Remove leading+trailing whitespace
line
=
line
.
strip
()
# An empty line implies the end of a output group
if
len
(
line
)
==
0
:
continue
# If a colon ':' is found, parse
elif
':'
in
line
:
(
key
,
value
)
=
line
.
split
(
':'
,
1
)
key
=
key
.
strip
().
replace
(
" "
,
""
)
#
To
unify
value
=
value
.
strip
()
if
key
in
[
'
ProductName
'
,
'
SubscriptionName
'
]
:
# Remember the name for later processing
products
.
append
(
RhsmPool
(
_name
=
value
,
key
=
value
))
elif
products
:
# Associate value with most recently recorded product
products
[
-
1
].
__setattr__
(
key
,
value
)
# FIXME - log some warning?
#else:
# warnings.warn("Unhandled subscription key/value: %s/%s" % (key,value))
return
products
def
filter
(
self
,
regexp
=
'
^
$'
)
:
'''
Return
a
list
of
RhsmPools
whose
name
matches
the
provided
regular
expression
'''
r
=
re
.
compile
(
regexp
)
for
product
in
self
.
products
:
if
r
.
search
(
product
.
_name
)
:
yield
product
def
main
()
:
def
main
()
:
# Load RHSM configuration from file
# Load RHSM configuration from file
cp
=
read_rhsm_config
()
rhn
=
Rhsm
()
module
=
AnsibleModule
(
module
=
AnsibleModule
(
argument_spec
=
dict
(
argument_spec
=
dict
(
state
=
dict
(
default
=
'
present
'
,
choices
=
[
'
present
'
,
'
absent
'
]),
state
=
dict
(
default
=
'
present
'
,
choices
=
[
'
present
'
,
'
absent
'
]),
username
=
dict
(
default
=
None
,
required
=
False
),
username
=
dict
(
default
=
None
,
required
=
False
),
password
=
dict
(
default
=
None
,
required
=
False
),
password
=
dict
(
default
=
None
,
required
=
False
),
server_hostname
=
dict
(
default
=
cp
.
get_option
(
'server.hostname'
),
required
=
False
),
server_hostname
=
dict
(
default
=
rhn
.
config
.
get_option
(
'
server
.
hostname
'
),
required
=
False
),
server_insecure
=
dict
(
default
=
cp
.
get_option
(
'server.insecure'
),
required
=
False
),
server_insecure
=
dict
(
default
=
rhn
.
config
.
get_option
(
'
server
.
insecure
'
),
required
=
False
),
rhsm_baseurl
=
dict
(
default
=
cp
.
get_option
(
'rhsm.baseurl'
),
required
=
False
),
rhsm_baseurl
=
dict
(
default
=
rhn
.
config
.
get_option
(
'
rhsm
.
baseurl
'
),
required
=
False
),
autosubscribe
=
dict
(
default
=
False
,
type
=
'
bool
'
),
autosubscribe
=
dict
(
default
=
False
,
type
=
'
bool
'
),
activationkey
=
dict
(
default
=
None
,
required
=
False
),
activationkey
=
dict
(
default
=
None
,
required
=
False
),
pool
=
dict
(
default
=
'
^
$'
,
required
=
False
,
type
=
'
str
'
),
pool
=
dict
(
default
=
'
^
$'
,
required
=
False
,
type
=
'
str
'
),
...
@@ -313,13 +375,14 @@ def main():
...
@@ -313,13 +375,14 @@ def main():
module
.
fail_json
(
msg
=
"Missing arguments, If registering without an activationkey, must supply username or password"
)
module
.
fail_json
(
msg
=
"Missing arguments, If registering without an activationkey, must supply username or password"
)
# Register system
# Register system
if
is_registered
()
:
if
rhn
.
is_registered
:
module
.
exit_json
(
changed
=
False
,
msg
=
"System already registered."
)
module
.
exit_json
(
changed
=
False
,
msg
=
"System already registered."
)
else
:
else
:
try
:
try
:
configure
(
**
module
.
params
)
rhn
.
enable
()
register
(
username
,
password
,
autosubscribe
,
activationkey
)
rhn
.
configure
(
**
module
.
params
)
subscribe
(
pool
)
rhn
.
register
(
username
,
password
,
autosubscribe
,
activationkey
)
rhn
.
subscribe
(
pool
)
except
CommandException
,
e
:
except
CommandException
,
e
:
module
.
fail_json
(
msg
=
"Failed to register with '%s': %s"
%
(
server_hostname
,
e
))
module
.
fail_json
(
msg
=
"Failed to register with '%s': %s"
%
(
server_hostname
,
e
))
else
:
else
:
...
@@ -327,11 +390,12 @@ def main():
...
@@ -327,11 +390,12 @@ def main():
# Ensure system is *not* registered
# Ensure system is *not* registered
if
state
==
'
absent
'
:
if
state
==
'
absent
'
:
if
not
is_registered
()
:
if
not
rhn
.
is_registered
:
module
.
exit_json
(
changed
=
False
,
msg
=
"System already unregistered."
)
module
.
exit_json
(
changed
=
False
,
msg
=
"System already unregistered."
)
else
:
else
:
try
:
try
:
unregister
()
rhn
.
unsubscribe
()
rhn
.
unregister
()
except
CommandException
,
e
:
except
CommandException
,
e
:
module
.
fail_json
(
msg
=
"Failed to unregister: %s"
%
e
)
module
.
fail_json
(
msg
=
"Failed to unregister: %s"
%
e
)
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