Skip to content

Commit be627f0

Browse files
author
Simca
committed
Improvement subset for newer OpenStack versions
1 parent dd22219 commit be627f0

File tree

10 files changed

+240
-124
lines changed

10 files changed

+240
-124
lines changed

src/openstack_cli/commands/exec.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one or more
2+
# contributor license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright ownership.
4+
# The ASF licenses this file to You under the Apache License, Version 2.0
5+
# (the "License"); you may not use this file except in compliance with
6+
# the License. You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
from openstack_cli.core.config import Configuration
17+
from openstack_cli.modules.apputils.discovery import CommandMetaInfo
18+
19+
__module__ = CommandMetaInfo("exec", "Execute command on host")
20+
21+
22+
23+
24+
def __init__(conf:Configuration):
25+
pass
26+

src/openstack_cli/commands/info.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
__module__ = CommandMetaInfo("info", "Shows the detailed information about requested VMs")
3030
__args__ = __module__.arg_builder\
3131
.add_default_argument("search_pattern", str, "Search query", default="") \
32-
.add_argument("own", bool, "Display only owned by user items", default=False)
32+
.add_argument("own", bool, "Display only owned by user items", default=False) \
33+
.add_argument("showid", bool, "Display instances ID", default=False)
3334

3435

3536
class WidthConst(Enum):
@@ -38,7 +39,9 @@ class WidthConst(Enum):
3839
max_net_len = 2
3940

4041

41-
def print_cluster(servers: Dict[str, List[OpenStackVMInfo]], vh: ValueHolder = None, ostack: OpenStack = None):
42+
def print_cluster(servers: Dict[str, List[OpenStackVMInfo]], vh: ValueHolder = None, ostack: OpenStack = None,
43+
showid:bool = False):
44+
4245
__run_ico = Symbols.PLAY.green()
4346
__pause_ico = Symbols.PAUSE.yellow()
4447
__stop_ico = Symbols.STOP.red()
@@ -50,27 +53,33 @@ def print_cluster(servers: Dict[str, List[OpenStackVMInfo]], vh: ValueHolder = N
5053
if vh is None:
5154
vh = ValueHolder(3, [50, 30, 15])
5255

53-
to = TableOutput(
56+
columns = [
5457
TableColumn("", 1, inv_ch=Colors.GREEN.wrap_len()),
5558
TableColumn("Host name", vh.get(WidthConst.max_fqdn_len)),
5659
TableColumn("Host IP", 16),
5760
TableColumn("SSH Key", vh.get(WidthConst.max_key_len)),
5861
TableColumn("Network name", length=vh.get(WidthConst.max_net_len))
59-
)
62+
]
63+
if showid:
64+
columns.append(TableColumn("ID"))
6065

66+
to = TableOutput(*columns)
6167
to.print_header()
6268
for cluster_name, servers in servers.items():
6369
servers = sorted(servers, key=lambda x: x.fqdn)
6470
for server in servers:
65-
to.print_row(
71+
_row = [
6672
__state[server.state] if server.state in __state else __stop_ico,
6773
server.fqdn,
6874
server.ip_address if server.ip_address else "0.0.0.0",
6975
server.key_name,
7076
server.net_name
71-
)
77+
]
78+
if showid:
79+
_row.append(server.id)
80+
to.print_row(*_row)
7281

73-
def __init__(conf: Configuration, search_pattern: str, debug: bool, own: bool):
82+
def __init__(conf: Configuration, search_pattern: str, debug: bool, own: bool, showid: bool):
7483
vh: ValueHolder = ValueHolder(3)
7584
def __fake_filter(s: OpenStackVMInfo):
7685
vh.set_if_bigger(WidthConst.max_fqdn_len, len(s.fqdn))
@@ -82,6 +91,6 @@ def __fake_filter(s: OpenStackVMInfo):
8291
clusters = ostack.get_server_by_cluster(search_pattern=search_pattern, sort=True, only_owned=own,
8392
filter_func=__fake_filter)
8493

85-
print_cluster(clusters, vh, ostack)
94+
print_cluster(clusters, vh, ostack, showid=showid)
8695

8796

src/openstack_cli/commands/networks.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ def get_default_network(conf: Configuration, ostack: OpenStack, force: bool = Fa
4040
def print_networks(ostack: OpenStack, select: bool = False) -> OSNetworkItem or None:
4141
tbl = TableOutput(
4242
TableColumn("N.", 3),
43-
TableColumn("ID", 36),
4443
TableColumn("Zone", 10),
4544
TableColumn("Name", 20),
4645
TableColumn("CIDR", 15),
@@ -61,7 +60,6 @@ def print_networks(ostack: OpenStack, select: bool = False) -> OSNetworkItem or
6160

6261
tbl.print_row(
6362
str(counter),
64-
net.network_id,
6563
",".join(net.orig_network.availability_zones),
6664
net.name,
6765
net.cidr,

src/openstack_cli/commands/quota.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
def get_percents(current: float, fmax: float):
3434
if fmax == 0:
3535
return 0
36+
elif fmax == -1:
37+
return 100
3638

3739
return (current * 100) / fmax
3840

@@ -306,11 +308,15 @@ def __init__(conf: Configuration, details: bool, show_clusters: bool, graph: boo
306308
c_end = Colors.RESET if prc > 80 else ""
307309
c_start = Colors.RED if prc > 90 else Colors.YELLOW if prc > 80 else ""
308310

311+
if metric.max_count < 0 :
312+
c_start = ""
313+
c_end = ""
314+
309315
to.print_row(
310316
f"{c_start}{metric.name}{c_end}",
311317
f"{c_start}{metric.used}{c_end}",
312-
metric.available,
313-
metric.max_count,
318+
"-" if metric.available < 0 else metric.available,
319+
"-" if metric.max_count < 0 else metric.max_count,
314320
f"{get_progressbar(prc, c_start)} {c_start}{prc:.1f}%{c_end}"
315321
)
316322

src/openstack_cli/commands/ssh.py

Lines changed: 4 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616
import os
1717
import sys
1818
import subprocess
19-
from typing import Dict, List
2019

2120
from openstack_cli.core.shell import shell
2221
from openstack_cli.modules.apputils.terminal.get_terminal_size import get_terminal_size
23-
from openstack_cli.modules.apputils.terminal import TableOutput, TableColumn, Console
24-
from openstack_cli.modules.openstack.objects import OpenStackVMInfo
22+
from openstack_cli.modules.apputils.terminal import Console
2523
from openstack_cli.modules.openstack import OpenStack
2624
from openstack_cli.core.config import Configuration
2725
from openstack_cli.modules.apputils.discovery import CommandMetaInfo
@@ -37,6 +35,7 @@
3735
.add_argument("port", int, "SSH Port", default=22)\
3836
.add_argument("internal", bool, "Use internal SSH Client", alias="buildin", default=False)
3937

38+
from openstack_cli.modules.utils import host_selector
4039

4140
IS_WIN: bool = sys.platform == "win32"
4241

@@ -129,70 +128,12 @@ def _open_console(internal:bool, host: str, port: int = 22, user_name: str = "ro
129128

130129
def __init__(conf: Configuration, name: str, node_number: int, user_name: str, use_password: bool, use_key: str,
131130
own: bool, port: int, internal: bool):
132-
ostack = OpenStack(conf)
133-
if name == "None":
134-
name = ""
135131

136132
if use_key == "None":
137133
use_key = None
138134

139-
if name and node_number == -1:
140-
_name, _, _node_number = name.rpartition("-")
141-
142-
try:
143-
node_number = int(_node_number)
144-
name = _name
145-
except (ValueError, TypeError):
146-
pass
147-
148-
if "." in name:
149-
name, _ = name.split(".")
150-
151-
search_result: Dict[str, List[OpenStackVMInfo]] = ostack.get_server_by_cluster(name, sort=True, only_owned=own)
152-
to = TableOutput(
153-
TableColumn("Cluster name", 40),
154-
print_row_number=True
155-
)
156-
if len(search_result.keys()) > 1:
157-
to.print_header()
158-
for cluster_name in search_result.keys():
159-
to.print_row(cluster_name)
160-
161-
selection: int = Console.ask("Choose cluster from the list", int)
162-
try:
163-
name = list(search_result.keys())[selection:][0]
164-
except IndexError:
165-
raise ValueError("Wrong selection, please select item within an provided range")
166-
elif search_result:
167-
name = list(search_result.keys())[0]
168-
else:
169-
raise ValueError(f"No matching cluster matching pattern'{name}' found")
170-
171-
nodes: List[OpenStackVMInfo] = search_result[name]
172-
if node_number == -1:
173-
if len(nodes) > 1:
174-
to = TableOutput(
175-
TableColumn("IP", 18),
176-
TableColumn("Host name", 40),
177-
178-
print_row_number=True
179-
)
180-
to.print_header()
181-
for node in nodes:
182-
to.print_row(node.ip_address, node.fqdn)
183-
node_number: int = Console.ask("Choose host from the list", int)
184-
if node_number > len(nodes):
185-
raise ValueError("Wrong selection, please select item within an provided range")
186-
else:
187-
node_number = 0
188-
else:
189-
node_number -= 1 # the node name starts for 1, while list from 0
190-
191-
try:
192-
node: OpenStackVMInfo = nodes[node_number]
193-
except IndexError:
194-
raise ValueError("Unknown host name, please check the name")
195-
135+
ostack = OpenStack(conf)
136+
node = host_selector(ostack, name, node_number, own)
196137
print(f"Establishing connection to {node.fqdn}({node.ip_address}) as '{user_name}' user...")
197138
if use_password:
198139
_open_console(internal, node.ip_address, port=port, user_name=user_name, password=True)

src/openstack_cli/core/config.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,11 @@ def interface(self):
204204

205205
@property
206206
def region(self):
207-
return "RegionOne"
207+
return self._storage.get_property(self._options_table, "region").value
208+
209+
@region.setter
210+
def region(self, value):
211+
self._storage.set_text_property(self._options_table, "region", value, encrypted=True)
208212

209213
@property
210214
def supported_os_names(self) -> List[str]:

src/openstack_cli/core/updates/upgrade_catalog_11.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ def __call__(self, *args, **kwargs):
4242
osvm = OpenStack(conf)
4343
so = StatusOutput(additional_errors=osvm.last_errors)
4444

45+
"""
46+
Login sequence:
47+
------------------
48+
- unscoped loing
49+
- set project id
50+
- scoped login
51+
- fetch region
52+
- relogin to fetch API endpoints
53+
"""
54+
4555
if not conf.project.id:
4656
print("Fetching available projects....")
4757
if not osvm.login(_type=AuthRequestType.UNSCOPED):
@@ -62,7 +72,7 @@ def __call__(self, *args, **kwargs):
6272
for prj in projects:
6373
to.print_row(prj.id, prj.name, prj.enabled)
6474

65-
n: int = Console.ask("Select the project number to be used: ", _type=int)
75+
n: int = Console.ask("Select the project number to be used", _type=int)
6676
conf.project = VMProject(id=projects[n].id, name=projects[n].name, domain=projects[n].domain_id)
6777
osvm.logout()
6878

@@ -73,6 +83,26 @@ def __call__(self, *args, **kwargs):
7383
self.reset()
7484
raise RuntimeError("Unable to continue")
7585

86+
if not conf.region:
87+
print(f"Fetching available regions for the project '{conf.project.name}'...")
88+
to = TableOutput(
89+
TableColumn("Id", 33),
90+
TableColumn("Descriuption"),
91+
print_row_number=True
92+
)
93+
to.print_header()
94+
regions = osvm.regions
95+
for region in regions:
96+
to.print_row(region.id, region.description)
97+
98+
n: int = Console.ask("Select the region number to be used", _type=int)
99+
conf.region = regions[n].id
100+
osvm.logout()
101+
102+
if not osvm.login():
103+
conf.reset()
104+
raise RuntimeError("Unable to continue")
105+
76106
if not conf.default_network:
77107
print("Please select default network for the VM (could be changed via 'conf network' command):")
78108
_net = print_networks(ostack=osvm, select=True)

0 commit comments

Comments
 (0)