Created
January 29, 2016 14:10
-
-
Save omaciel/640fc7b260734909d95d to your computer and use it in GitHub Desktop.
New cli.Client module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from pulp_smash import cli, config | |
server_config = config.ServerConfig( | |
base_url=u'qe-sat6-rhel7', | |
cli_transport='ssh' | |
) | |
client = cli.Client(server_config) | |
cmd = u'hammer -u admin -p changeme ping' | |
response = client.run(cmd.split()) | |
print(response.args) | |
# [u'hammer', u'-u', u'admin', u'-p', u'changeme', u'ping'] | |
print(response.check_returncode) | |
############################################################################### | |
# <bound method CompletedProcess.check_returncode of CompletedProcess( # | |
# args=[u'hammer', u'-u', u'admin', u'-p', u'changeme', u'ping'], # | |
# returncode=0, # | |
# stdout=u'candlepin: \n Status: ok\n # | |
# Server Response: Duration: 31ms\ncandlepin_auth: \n # | |
# Status: ok\n # | |
# Server Response: Duration: 31ms\n # | |
# pulp: \n Status: ok\n # | |
# Server Response: Duration: 80ms\n # | |
# pulp_auth: \n Status: ok\n # | |
# Server Response: Duration: 50ms\n # | |
# elasticsearch: \n Status: ok\n # | |
# Server Response: Duration: 13ms\n # | |
# foreman_tasks: \n Status: ok\n # | |
# Server Response: Duration: 1ms\n\n', # | |
# stderr=u"Warning: Permanently added 'qe-sat6-rhel7.xxx.yyy.zzz,x.x.x.x' # | |
# (ECDSA) to the list of known hosts.\r\n")> # | |
############################################################################### | |
print(response.returncode) | |
# 0 | |
print(response.stderr) | |
# u"Warning: Permanently added 'qe-sat6-rhel7.xxx.yyy.zzz,x.x.x.x' | |
# (ECDSA) to the list of known hosts.\r\n" | |
print(response.stdout) | |
####################################### | |
# candlepin: # | |
# Status: ok # | |
# Server Response: Duration: 31ms # | |
# candlepin_auth: # | |
# Status: ok # | |
# Server Response: Duration: 31ms # | |
# pulp: # | |
# Status: ok # | |
# Server Response: Duration: 80ms # | |
# pulp_auth: # | |
# Status: ok # | |
# Server Response: Duration: 50ms # | |
# elasticsearch: # | |
# Status: ok # | |
# Server Response: Duration: 13ms # | |
# foreman_tasks: # | |
# Status: ok # | |
# Server Response: Duration: 1ms # | |
####################################### | |
cmd = u'hammer -u admin -p changeme --output json organization list' | |
response = client.run(cmd.split()) | |
json.loads(response.stdout) | |
################################################################################### | |
# [{u'Description': None, # | |
# u'Id': 1, # | |
# u'Label': u'Default_Organization', # | |
# u'Name': u'Default Organization'}, # | |
# {u'Description': None, u'Id': 4, u'Label': u'IBM_Org', u'Name': u'IBM Org'}, # | |
# {u'Description': None, # | |
# u'Id': 3, # | |
# u'Label': u'OracleOrg', # | |
# u'Name': u'OracleOrg'}, # | |
# {u'Description': None, # | |
# u'Id': 7, # | |
# u'Label': u'09378691-0612-4831-9a5f-1ccb70e12cb5', # | |
# u'Name': u'\u1029\u4795\u5ae1\u30be\u70f4\u5d9a\uc5b7\ua934\ucd58\u4133\u149b # | |
# \u2da2\uc442\ubc92\u426c\u6427\u818e\ua91e\ud1e3\u150e\ua71c\u4b42\u46e3 # | |
# \u96ee\u8ff8'}, # | |
# {u'Description': None, # | |
# u'Id': 5, # | |
# u'Label': u'b0296176-0355-49a3-b5bf-878fc4ba7286', # | |
# u'Name': u'\u9647\u64f1\u7e2a\u7136\u8d6d\u4e07\u2c2e\u2c8b\u3a4e\u5313\ub3f6 # | |
# \ufd57\u8bbb'}, # | |
# {u'Description': None, # | |
# u'Id': 9, # | |
# u'Label': u'362ebe33-0112-4a1d-ad08-3a8260f310e6', # | |
# u'Name': u'\u9c2c\u16b2\ube2e\uc507\u41cd\u59de\u4cdd\u1972\u99b5\u7b32'}] # | |
################################################################################### | |
cmd = u'hammer -u admin -p changeme --output json organization info --id 1' | |
response = client.run(cmd.split()) | |
json.loads(response.stdout) | |
{u'Compute resources': [], | |
u'Created at': u'2015-12-09T20:47:43Z', | |
u'Description': None, | |
u'Domains': [{u'id': 1, u'name': u'lab.eng.rdu2.redhat.com'}], | |
u'Environments': [{u'id': 4, | |
u'name': u'KT_Default_Organization_Library_CV_RHEL6_7'}, | |
{u'id': 1, u'name': u'production'}], | |
u'Hostgroups': [], | |
u'Id': 1, | |
u'Installation media': [{u'id': 7, | |
u'name': u'Default_Organization/Library/Red_Hat_Server/Red_Hat_Enterprise_Linux_5_Server_Kickstart_i386_5_11'}, | |
{u'id': 10, | |
u'name': u'Default_Organization/Library/Red_Hat_Server/Red_Hat_Enterprise_Linux_5_Server_Kickstart_x86_64_5_11'}, | |
{u'id': 8, | |
u'name': u'Default_Organization/Library/Red_Hat_Server/Red_Hat_Enterprise_Linux_6_Server_Kickstart_i386_6_7'}, | |
{u'id': 9, | |
u'name': u'Default_Organization/Library/Red_Hat_Server/Red_Hat_Enterprise_Linux_6_Server_Kickstart_x86_64_6_7'}, | |
{u'id': 11, | |
u'name': u'Default_Organization/Library/Red_Hat_Server/Red_Hat_Enterprise_Linux_7_Server_Kickstart_x86_64_7_2'}], | |
u'Label': u'Default_Organization', | |
u'Locations': [{u'id': 2, | |
u'name': u'Default Location', | |
u'title': u'Default Location'}], | |
u'Name': u'Default Organization', | |
u'Parameters': [], | |
u'Red Hat Repository URL': u'https://cdn.redhat.com', | |
u'Smart proxies': [{u'id': 1, | |
u'name': u'ibm-x3250m4-06.lab.eng.rdu2.redhat.com', | |
u'url': u'https://ibm-x3250m4-06.lab.eng.rdu2.redhat.com:9090'}], | |
u'Subnets': [], | |
u'Templates': [{u'id': 44, | |
u'name': u'Boot disk iPXE - generic host', | |
u'template_kind_id': 1, | |
u'template_kind_name': u'Bootdisk'}, | |
{u'id': 43, | |
u'name': u'Boot disk iPXE - host', | |
u'template_kind_id': 1, | |
u'template_kind_name': u'Bootdisk'}, | |
{u'id': 50, | |
u'name': u'Discovery Red Hat kexec', | |
u'template_kind_id': 10, | |
u'template_kind_name': u'kexec'}, | |
{u'id': 48, | |
u'name': u'idm_register', | |
u'template_kind_id': None, | |
u'template_kind_name': None}, | |
{u'id': 22, | |
u'name': u'Kickstart default iPXE', | |
u'template_kind_id': 4, | |
u'template_kind_name': u'iPXE'}, | |
{u'id': 21, | |
u'name': u'Kickstart default PXELinux', | |
u'template_kind_id': 2, | |
u'template_kind_name': u'PXELinux'}, | |
{u'id': 39, | |
u'name': u'kickstart_networking_setup', | |
u'template_kind_id': None, | |
u'template_kind_name': None}, | |
{u'id': 40, | |
u'name': u'puppet.conf', | |
u'template_kind_id': None, | |
u'template_kind_name': None}, | |
{u'id': 2, | |
u'name': u'PXELinux default local boot', | |
u'template_kind_id': 2, | |
u'template_kind_name': u'PXELinux'}, | |
{u'id': 1, | |
u'name': u'PXELinux global default', | |
u'template_kind_id': 2, | |
u'template_kind_name': u'PXELinux'}, | |
{u'id': 45, | |
u'name': u'Satellite Kickstart Default', | |
u'template_kind_id': 5, | |
u'template_kind_name': u'provision'}, | |
{u'id': 47, | |
u'name': u'Satellite Kickstart Default Finish', | |
u'template_kind_id': 6, | |
u'template_kind_name': u'finish'}, | |
{u'id': 46, | |
u'name': u'Satellite Kickstart Default User Data', | |
u'template_kind_id': 8, | |
u'template_kind_name': u'user_data'}, | |
{u'id': 49, | |
u'name': u'subscription_manager_registration', | |
u'template_kind_id': None, | |
u'template_kind_name': None}], | |
u'Updated at': u'2015-12-14T20:36:20Z', | |
u'Users': []} |
Line 20 has this:
print(response.check_returncode)
You may want to write this:
print(response.check_returncode())
The check_returncode()
is analagous to Requests' raise_for_status()
method. It will raise an exception if the return code is non-zero, and otherwise has no side effects and returns None
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist decodes JSON like so:
Here's a way to automatically decode JSON on each command: