slapos CLI: add slapos list and slapos info.

parent 4a7c73de
......@@ -65,11 +65,17 @@ request
.. program-output:: python slapos help request
Request the deployment of a service (instance).
Examples
* Request a wordpress instance named "mybeautifulinstance" on Node named "COMP-12345"::
* Request a wordpress instance named "mybeautifulinstance" on any available machine (either owned by user, public, or shared by other user to current user)::
$ slapos request mybeautifulinstance wordpress
$ slapos request mybeautifulinstance wordpress --node computer_guid=COMP-12345
* Request a wordpress instance named "My Beautiful Instance" on Node named "COMP-12345"::
$ slapos request "My Beautiful Instance" wordpress --node computer_guid=COMP-12345
* Request a kvm instance named "mykvm" on Node named "COMP-12345", specifying nbd-host and nbd-ip parameters::
......@@ -81,14 +87,33 @@ Examples
$ slapos request mykvm \
http://git.erp5.org/gitweb/slapos.git/blob_plain/refs/tags/slapos-0.156:/software/kvm/software.cfg
..
XXX update me
In these examples, ``wordpress`` and ``kvm`` are aliases for the full URL, and are defined in :file:`slapos-client.cfg`.
..
XXX Change in slaplib: allow to fetch instance params without changing anything.
i.e we should do "slapos request myalreadyrequestedinstance" to fetch connection parameters
without erasing previously defined instance parameters.
info
~~~~
.. program-output:: python slapos help info
Get informations of specified instance, like connection parameters, Software Release.
Return an error if instance does not exist for the current user.
Examples:
* Ask informations about an instance named "My Service"
$ slapos info "My Service"
list
~~~~
.. program-output:: python slapos help list
List all deployed services owned by current user.
From SlapOS Master point of view, it should return the list of all non-destroyed Hosting Subscriptions.
..
search
......
......@@ -99,11 +99,14 @@ setup(name=name,
'console = slapos.cli.console:ConsoleCommand',
'configure local = slapos.cli.configure_local:ConfigureLocalCommand',
'configure client = slapos.cli.configure_client:ConfigureClientCommand',
'proxy start = slapos.cli.proxy_start:ProxyStartCommand',
'proxy show = slapos.cli.proxy_show:ProxyShowCommand',
'info = slapos.cli.info:InfoCommand',
'list = slapos.cli.list:ListCommand',
'supply = slapos.cli.supply:SupplyCommand',
'remove = slapos.cli.remove:RemoveCommand',
'request = slapos.cli.request:RequestCommand',
# SlapOS Proxy commands
'proxy start = slapos.cli.proxy_start:ProxyStartCommand',
'proxy show = slapos.cli.proxy_show:ProxyShowCommand',
]
},
test_suite="slapos.tests",
......
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2010-2014 Vifib SARL and Contributors.
# All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation; either version 2.1
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
import pprint
from slapos.cli.config import ClientConfigCommand
from slapos.client import init, ClientConfig
from slapos.slap import ResourceNotReady, NotFoundError
class InfoCommand(ClientConfigCommand):
"""get status, software_release and parameters of an instance"""
def get_parser(self, prog_name):
ap = super(InfoCommand, self).get_parser(prog_name)
ap.add_argument('reference',
help='Your instance reference')
return ap
def take_action(self, args):
configp = self.fetch_config(args)
conf = ClientConfig(args, configp)
local = init(conf, self.app.log)
exit_code = do_info(self.app.log, conf, local)
if exit_code != 0:
exit(exit_code)
def do_info(logger, conf, local):
try:
instance = local['slap'].registerOpenOrder().getInformation(
partition_reference=conf.reference,
)
except ResourceNotReady:
logger.warning('Instance does not exist or is not ready yet.')
return(2)
except NotFoundError:
logger.warning('Instance %s does not exist.', conf.reference)
return(2)
logger.info('Software Release URL: %s', instance._software_release_url)
logger.info('Instance state: %s', instance._requested_state)
logger.info('Instance parameters:')
logger.info(pprint.pformat(instance._parameter_dict))
logger.info('Connection parameters:')
logger.info(pprint.pformat(instance._connection_dict))
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2010-2014 Vifib SARL and Contributors.
# All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation; either version 2.1
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
from slapos.cli.config import ClientConfigCommand
from slapos.client import init, ClientConfig
from slapos.slap import ResourceNotReady
class ListCommand(ClientConfigCommand):
"""request an instance and get status and parameters of instance"""
def get_parser(self, prog_name):
ap = super(ListCommand, self).get_parser(prog_name)
return ap
def take_action(self, args):
configp = self.fetch_config(args)
conf = ClientConfig(args, configp)
local = init(conf, self.app.log)
do_list(self.app.log, conf, local)
def do_list(logger, conf, local):
# XXX catch exception
instance_dict = local['slap'].getOpenOrderDict()
if instance_dict == {}:
logger.info('No existing service.')
return
logger.info('List of services:')
for title, instance in instance_dict.iteritems():
logger.info('%s %s', title, instance._software_release_url)
......@@ -75,6 +75,8 @@ class ClientConfig(object):
else:
self.master_url = master_url
self.master_rest_url = configuration_dict.get('master_rest_url')
if self.key_file:
self.key_file = os.path.expanduser(self.key_file)
......@@ -87,7 +89,7 @@ def init(conf, logger):
aliases to common software releases"""
# XXX check certificate and key existence
slap = slapos.slap.slap()
slap.initializeConnection(conf.master_url,
slap.initializeConnection(conf.master_url, conf.master_rest_url,
key_file=conf.key_file, cert_file=conf.cert_file)
local = globals().copy()
local['slap'] = slap
......
......@@ -25,11 +25,29 @@
#
##############################################################################
import logging
import pprint
import unittest
from mock import patch, create_autospec
import slapos.cli.list
import slapos.cli.info
from slapos.client import ClientConfig
import slapos.proxy
import slapos.slap
def raiseNotFoundError(*args, **kwargs):
raise slapos.slap.NotFoundError()
class CliMixin(unittest.TestCase):
def setUp(self):
slap = slapos.slap.slap()
self.local = {'slap': slap}
self.logger = create_autospec(logging.Logger)
self.conf = create_autospec(ClientConfig)
class TestCliProxy(unittest.TestCase):
class TestCliProxy(CliMixin):
def test_generateSoftwareProductListFromString(self):
"""
Test that generateSoftwareProductListFromString correctly parses a parameter
......@@ -52,4 +70,59 @@ product2 url2"""
self.assertEqual(
slapos.proxy._generateSoftwareProductListFromString(''),
{}
)
\ No newline at end of file
)
class TestCliList(CliMixin):
def test_list(self):
"""
Test "slapos list" command output.
"""
return_value = {
'instance1': slapos.slap.SoftwareInstance(_title='instance1', _software_release_url='SR1'),
'instance2': slapos.slap.SoftwareInstance(_title='instance2', _software_release_url='SR2'),
}
with patch.object(slapos.slap.slap, 'getOpenOrderDict', return_value=return_value) as _:
slapos.cli.list.do_list(self.logger, None, self.local)
self.logger.info.assert_any_call('%s %s', 'instance1', 'SR1')
self.logger.info.assert_any_call('%s %s', 'instance2', 'SR2')
def test_emptyList(self):
with patch.object(slapos.slap.slap, 'getOpenOrderDict', return_value={}) as _:
slapos.cli.list.do_list(self.logger, None, self.local)
self.logger.info.assert_called_once_with('No existing service.')
@patch.object(slapos.slap.slap, 'registerOpenOrder', return_value=slapos.slap.OpenOrder())
class TestCliInfo(CliMixin):
def test_info(self, _):
"""
Test "slapos info" command output.
"""
setattr(self.conf, 'reference', 'instance1')
instance = slapos.slap.SoftwareInstance(
_software_release_url='SR1',
_requested_state = 'mystate',
_connection_dict = {'myconnectionparameter': 'value1'},
_parameter_dict = {'myinstanceparameter': 'value2'}
)
with patch.object(slapos.slap.OpenOrder, 'getInformation', return_value=instance):
slapos.cli.info.do_info(self.logger, self.conf, self.local)
self.logger.info.assert_any_call(pprint.pformat(instance._parameter_dict))
self.logger.info.assert_any_call('Software Release URL: %s', instance._software_release_url)
self.logger.info.assert_any_call('Instance state: %s', instance._requested_state)
self.logger.info.assert_any_call(pprint.pformat(instance._parameter_dict))
self.logger.info.assert_any_call(pprint.pformat(instance._connection_dict))
def test_unknownReference(self, _):
"""
Test "slapos info" command output in case reference
of service is not known.
"""
setattr(self.conf, 'reference', 'instance1')
with patch.object(slapos.slap.OpenOrder, 'getInformation', side_effect=raiseNotFoundError):
slapos.cli.info.do_info(self.logger, self.conf, self.local)
self.logger.warning.assert_called_once_with('Instance %s does not exist.', self.conf.reference)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment