Skip to content

Commit

Permalink
Merge pull request Katello#69 from parthaa/version-feature
Browse files Browse the repository at this point in the history
808172 - Added code to show version information for katello cli
  • Loading branch information
mccun934 committed May 2, 2012
2 parents 3c8cfc9 + 6b6971b commit a6bb7f8
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 4 deletions.
2 changes: 2 additions & 0 deletions cli/bin/katello
Expand Up @@ -38,6 +38,7 @@ from katello.client.core import (
user_role,
provider,
ping,
version,
product,
repo,
packagegroup,
Expand Down Expand Up @@ -113,6 +114,7 @@ def setup_admin(admin):
admin.add_command('permission', permission_cmd)

admin.add_command('ping', ping.Status())
admin.add_command('version', version.Info())

prod_cmd = product.Product()
prod_cmd.add_action('create', product.Create())
Expand Down
29 changes: 29 additions & 0 deletions cli/src/katello/client/api/version.py
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
#
# Copyright © 2011 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public License,
# version 2 (GPLv2). There is NO WARRANTY for this software, express or
# implied, including the implied warranties of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
# along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
#
# Red Hat trademarks are not licensed under GPLv2. No permission is
# granted to use or replicate Red Hat trademarks that are incorporated
# in this software or its documentation.

from katello.client.api.base import KatelloAPI

class VersionAPI(KatelloAPI):
"""
Connection class to access version information
"""
def version(self):
path = "/api/version"
return self.server.GET(path)[1]

def version_formatted(self):
v = self.version()
return v['name'] + ' ' + v['version']

12 changes: 11 additions & 1 deletion cli/src/katello/client/cli/base.py
Expand Up @@ -23,6 +23,7 @@
from katello.client.core.utils import parse_tokens
from katello.client.utils.encoding import u_str

from katello.client.api.version import VersionAPI
from katello.client.config import Config
from katello.client.logutil import getLogger
from katello.client import server
Expand Down Expand Up @@ -114,6 +115,8 @@ def setup_parser(self):
self.parser = OptionParser()
self.parser.disable_interspersed_args()
self.parser.set_usage(self.usage)
self.parser.add_option("-v", "--version", action="store_true", default=False,
dest="version", help=_('prints version information'))
credentials = OptionGroup(self.parser, _('Katello User Account Credentials'))
credentials.add_option('-u', '--username', dest='username',
default=None, help=_('account username'))
Expand Down Expand Up @@ -209,6 +212,13 @@ def main(self, args=sys.argv[1:]):
try:
self.setup_parser()
self.opts, args = self.parser.parse_args(args)

if self.opts.version:
self.setup_server()
self.setup_credentials()
api = VersionAPI()
print api.version_formatted()
return

if not args:
self.parser.error(_('No command given; please see --help'))
Expand All @@ -220,10 +230,10 @@ def main(self, args=sys.argv[1:]):
command_args = args[1:]
command.process_options(command_args)
self.setup_server()

action = command.extract_action(command_args)
if not action or action.require_credentials():
self.setup_credentials()

return command.main(command_args)

except OptionParserExitError, opee:
Expand Down
54 changes: 54 additions & 0 deletions cli/src/katello/client/core/version.py
@@ -0,0 +1,54 @@
#
# Katello Organization actions
# Copyright (c) 2010 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public License,
# version 2 (GPLv2). There is NO WARRANTY for this software, express or
# implied, including the implied warranties of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
# along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
#
# Red Hat trademarks are not licensed under GPLv2. No permission is
# granted to use or replicate Red Hat trademarks that are incorporated
# in this software or its documentation.
#

from gettext import gettext as _

from katello.client.api.version import VersionAPI
from katello.client.config import Config
from katello.client.core.base import Action, Command

Config()

# base ping action --------------------------------------------------------

class VersionAction(Action):

def __init__(self):
super(VersionAction, self).__init__()
self.api = VersionAPI()



# version actions ------------------------------------------------------------

class Info(VersionAction):

description = _('get the version of the katello server')

def setup_parser(self):
return 0

def check_options(self):
return 0

def run(self):
return self.api.version_formatted()


# ping command ------------------------------------------------------------

class Version(Command):
description = _('Check the version of the server')
1 change: 1 addition & 0 deletions cli/test/test_data.py
Expand Up @@ -752,6 +752,7 @@
"key": "ACME_Corporation"
}
}
VERSION_INFO = "Foo 10000"

PING_STATUS = {
'result': 'failed',
Expand Down
26 changes: 26 additions & 0 deletions cli/test/version_test.py
@@ -0,0 +1,26 @@
import unittest
import os
from mock import Mock

from cli_test_utils import CLIOptionTestCase, CLIActionTestCase
import test_data

import katello.client.core.version
from katello.client.core.version import Info



class VersionTest(CLIActionTestCase):

def setUp(self):
self.set_action(Info())
self.set_module(katello.client.core.version)
self.mock(self.action.api, 'version_formatted', test_data.VERSION_INFO)

def test_calls_the_api(self):
self.action.run()
self.action.api.version_formatted.assert_called_once()

def test_call_returns_correct_value(self):
self.assertEqual(self.action.run(),test_data.VERSION_INFO )

4 changes: 4 additions & 0 deletions src/app/controllers/api/ping_controller.rb
Expand Up @@ -21,4 +21,8 @@ def index
def status
render :json => {:version => "katello/#{AppConfig.katello_version}", :result => true}
end

def version
render :json => {:name => AppConfig.app_name, :version => AppConfig.katello_version}
end
end
4 changes: 2 additions & 2 deletions src/config/initializers/app_config.rb
Expand Up @@ -47,9 +47,9 @@ def initialize

# backticks gets you the equiv of a system() command in Ruby
if @hash["app_name"] == 'Katello'
version = `rpm -q katello-common --queryformat '%{VERSION}-%{RELEASE}\n'`
version = `rpm -q katello-common --queryformat '%{VERSION}-%{RELEASE}'`
else
version = `rpm -q katello-headpin --queryformat '%{VERSION}-%{RELEASE}\n'`
version = `rpm -q katello-headpin --queryformat '%{VERSION}-%{RELEASE}'`
end
exit_code = $?
if exit_code != 0
Expand Down
2 changes: 1 addition & 1 deletion src/config/routes.rb
Expand Up @@ -551,7 +551,7 @@ def matches?(request)
resources :tasks, :only => [:show]

match "/status" => "ping#status", :via => :get

match "/version" => "ping#version", :via => :get
# some paths conflicts with rhsm
scope 'katello' do

Expand Down

0 comments on commit a6bb7f8

Please sign in to comment.