3131from osc_lib .command import command
3232from osc_lib import exceptions
3333from osc_lib import utils
34+ from oslo_utils import strutils
3435
3536from openstackclient .i18n import _
3637from openstackclient .identity import common as identity_common
@@ -193,6 +194,24 @@ def _prep_server_detail(compute_client, image_client, server, refresh=True):
193194 return info
194195
195196
197+ def boolenv (* vars , default = False ):
198+ """Search for the first defined of possibly many bool-like env vars.
199+
200+ Returns the first environment variable defined in vars, or returns the
201+ default.
202+
203+ :param vars: Arbitrary strings to search for. Case sensitive.
204+ :param default: The default to return if no value found.
205+ :returns: A boolean corresponding to the value found, else the default if
206+ no value found.
207+ """
208+ for v in vars :
209+ value = os .environ .get (v , None )
210+ if value :
211+ return strutils .bool_from_string (value )
212+ return default
213+
214+
196215class AddFixedIP (command .Command ):
197216 _description = _ ("Add fixed IP address to server" )
198217
@@ -1322,6 +1341,15 @@ def get_parser(self, prog_name):
13221341 action = 'store_true' ,
13231342 help = _ ('Force delete server(s)' ),
13241343 )
1344+ parser .add_argument (
1345+ '--all-projects' ,
1346+ action = 'store_true' ,
1347+ default = boolenv ('ALL_PROJECTS' ),
1348+ help = _ (
1349+ 'Delete server(s) in another project by name (admin only)'
1350+ '(can be specified using the ALL_PROJECTS envvar)'
1351+ ),
1352+ )
13251353 parser .add_argument (
13261354 '--wait' ,
13271355 action = 'store_true' ,
@@ -1339,19 +1367,22 @@ def _show_progress(progress):
13391367 compute_client = self .app .client_manager .compute
13401368 for server in parsed_args .server :
13411369 server_obj = utils .find_resource (
1342- compute_client .servers , server )
1370+ compute_client .servers , server ,
1371+ all_tenants = parsed_args .all_projects )
13431372
13441373 if parsed_args .force :
13451374 compute_client .servers .force_delete (server_obj .id )
13461375 else :
13471376 compute_client .servers .delete (server_obj .id )
13481377
13491378 if parsed_args .wait :
1350- if not utils .wait_for_delete (compute_client .servers ,
1351- server_obj .id ,
1352- callback = _show_progress ):
1353- LOG .error (_ ('Error deleting server: %s' ),
1354- server_obj .id )
1379+ if not utils .wait_for_delete (
1380+ compute_client .servers ,
1381+ server_obj .id ,
1382+ callback = _show_progress ,
1383+ ):
1384+ msg = _ ('Error deleting server: %s' )
1385+ LOG .error (msg , server_obj .id )
13551386 self .app .stdout .write (_ ('Error deleting server\n ' ))
13561387 raise SystemExit
13571388
@@ -1446,8 +1477,11 @@ def get_parser(self, prog_name):
14461477 parser .add_argument (
14471478 '--all-projects' ,
14481479 action = 'store_true' ,
1449- default = bool (int (os .environ .get ("ALL_PROJECTS" , 0 ))),
1450- help = _ ('Include all projects (admin only)' ),
1480+ default = boolenv ('ALL_PROJECTS' ),
1481+ help = _ (
1482+ 'Include all projects (admin only) '
1483+ '(can be specified using the ALL_PROJECTS envvar)'
1484+ ),
14511485 )
14521486 parser .add_argument (
14531487 '--project' ,
@@ -3939,6 +3973,15 @@ def get_parser(self, prog_name):
39393973 nargs = "+" ,
39403974 help = _ ('Server(s) to start (name or ID)' ),
39413975 )
3976+ parser .add_argument (
3977+ '--all-projects' ,
3978+ action = 'store_true' ,
3979+ default = boolenv ('ALL_PROJECTS' ),
3980+ help = _ (
3981+ 'Start server(s) in another project by name (admin only)'
3982+ '(can be specified using the ALL_PROJECTS envvar)'
3983+ ),
3984+ )
39423985 return parser
39433986
39443987 def take_action (self , parsed_args ):
@@ -3947,6 +3990,7 @@ def take_action(self, parsed_args):
39473990 utils .find_resource (
39483991 compute_client .servers ,
39493992 server ,
3993+ all_tenants = parsed_args .all_projects ,
39503994 ).start ()
39513995
39523996
@@ -3961,6 +4005,15 @@ def get_parser(self, prog_name):
39614005 nargs = "+" ,
39624006 help = _ ('Server(s) to stop (name or ID)' ),
39634007 )
4008+ parser .add_argument (
4009+ '--all-projects' ,
4010+ action = 'store_true' ,
4011+ default = boolenv ('ALL_PROJECTS' ),
4012+ help = _ (
4013+ 'Stop server(s) in another project by name (admin only)'
4014+ '(can be specified using the ALL_PROJECTS envvar)'
4015+ ),
4016+ )
39644017 return parser
39654018
39664019 def take_action (self , parsed_args ):
@@ -3969,6 +4022,7 @@ def take_action(self, parsed_args):
39694022 utils .find_resource (
39704023 compute_client .servers ,
39714024 server ,
4025+ all_tenants = parsed_args .all_projects ,
39724026 ).stop ()
39734027
39744028
0 commit comments