diff --git a/cli/bin/katello b/cli/bin/katello index 27c6225c0c5..9367f2c15fa 100755 --- a/cli/bin/katello +++ b/cli/bin/katello @@ -38,6 +38,7 @@ from katello.client.core import ( user_role, provider, ping, + version, product, repo, packagegroup, @@ -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()) diff --git a/cli/src/katello/client/api/version.py b/cli/src/katello/client/api/version.py new file mode 100644 index 00000000000..cfcd3367a84 --- /dev/null +++ b/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'] + diff --git a/cli/src/katello/client/cli/base.py b/cli/src/katello/client/cli/base.py index 1b5c29230e2..c46d1413074 100644 --- a/cli/src/katello/client/cli/base.py +++ b/cli/src/katello/client/cli/base.py @@ -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 @@ -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')) @@ -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')) @@ -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: diff --git a/cli/src/katello/client/core/version.py b/cli/src/katello/client/core/version.py new file mode 100644 index 00000000000..35f087abf92 --- /dev/null +++ b/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') diff --git a/cli/test/test_data.py b/cli/test/test_data.py index 95d1b1e54a1..f7532610306 100644 --- a/cli/test/test_data.py +++ b/cli/test/test_data.py @@ -752,6 +752,7 @@ "key": "ACME_Corporation" } } +VERSION_INFO = "Foo 10000" PING_STATUS = { 'result': 'failed', diff --git a/cli/test/version_test.py b/cli/test/version_test.py new file mode 100644 index 00000000000..872f9cb6f0b --- /dev/null +++ b/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 ) + diff --git a/src/app/controllers/api/ping_controller.rb b/src/app/controllers/api/ping_controller.rb index c117184a50b..3889cde0ab5 100644 --- a/src/app/controllers/api/ping_controller.rb +++ b/src/app/controllers/api/ping_controller.rb @@ -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 diff --git a/src/config/initializers/app_config.rb b/src/config/initializers/app_config.rb index 9612dccf11f..f21934f8d21 100644 --- a/src/config/initializers/app_config.rb +++ b/src/config/initializers/app_config.rb @@ -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 diff --git a/src/config/routes.rb b/src/config/routes.rb index 228f17ef0bb..b0c05ee6509 100644 --- a/src/config/routes.rb +++ b/src/config/routes.rb @@ -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