Skip to content

Commit de77beb

Browse files
author
Simca
committed
Make sync proccess observable to user
1 parent 9120450 commit de77beb

File tree

3 files changed

+84
-36
lines changed

3 files changed

+84
-36
lines changed

.editorconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
1+
#https://www.jetbrains.com/help/idea/configuring-code-style.html#editorconfig
12
root = true
23

34
[*]
45
charset = utf-8
56
end_of_line = lf
67
trim_trailing_whitespace = true
8+
ij_wrap_on_typing = false
79

810

911
[*.py]
12+
tab_width = 2
13+
max_line_length = 120
1014
indent_style = space
1115
indent_size = 2
1216
insert_final_newline = true
17+
ij_continuation_indent_size = 2
18+
ij_smart_tabs = true
19+
ij_visual_guides = 120,120
20+
ij_python_blank_line_at_file_end = true
21+
ij_python_blank_lines_after_imports = 1
22+
ij_python_dict_alignment = 0
23+
ij_python_from_import_wrapping = 1
24+
ij_python_optimize_imports_join_from_imports_with_same_source = true
25+
ij_python_optimize_imports_sort_by_type_first = true
26+
ij_python_optimize_imports_sort_imports = true
27+
ij_python_optimize_imports_sort_names_in_from_imports = true
28+
ij_python_wrap_long_lines = false
1329

1430
[*.md]
1531
trim_trailing_whitespace = false

src/openstack_cli/commands/quota.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ def get_percents(current: float, fmax: float):
3737
return (current * 100) / fmax
3838

3939

40-
def get_plain_progress(percents, color:str = "", width: int = 30):
40+
def get_plain_progress(percents, color:str = "", width: int = 30, ch: str = "#"):
4141
f = int(round((percents * width) / 100))
4242
nf = int(width - f)
4343
if color:
44-
return f"{color}{'#' * f}{' '* nf}{Colors.RESET}"
44+
return f"{color}{ch * f}{' '* nf}{Colors.RESET}"
4545
else:
46-
return f"{'#' * f}{' '* nf}"
46+
return f"{ch * f}{' '* nf}"
4747

4848
def get_progressbar(percents, color:str = "", width: int = 30):
4949
return f"[{get_plain_progress(percents, color, width)}]"
@@ -162,7 +162,7 @@ def display_combined_graph(metrics: Dict[str, Dict[OpenStackQuotaType, int]], us
162162

163163

164164
caption_title = ", ".join([f"{_quota_colors[quota]}{quota.name}{Colors.RESET}" for quota in _combined_quotas])
165-
print(f"{caption_title} Graph")
165+
print(f"{caption_title} Quota Consuming Graph")
166166
print(f"{'-'*len(caption_title)}---------\n")
167167

168168
for user_id, user_metric in metrics.items():
@@ -176,7 +176,7 @@ def display_combined_graph(metrics: Dict[str, Dict[OpenStackQuotaType, int]], us
176176
actual_percents = get_percents(user_metric[_t], quotas.get_quota(_t)[0])
177177
user_metric_str = f"{int(user_metric[_t]/1024):>4} GB" if _t == OpenStackQuotaType.RAM_MB else user_metric[_t]
178178

179-
progress_bar[_t] = get_plain_progress(percents, "", column2_max_width)
179+
progress_bar[_t] = get_plain_progress(percents, "", column2_max_width, ch="⣿")
180180
metric_title[_t] = f"{user_metric_str:<6}/{actual_percents:6.2f}%"
181181

182182
for _t in _combined_quotas:

src/openstack_cli/modules/openstack/__init__.py

Lines changed: 63 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,19 @@
2222
from enum import Enum
2323
from inspect import FrameInfo
2424
from json import JSONDecodeError
25-
from typing import Dict, List, Callable, Iterable, TypeVar, Optional, Union
25+
from typing import Callable, Dict, Iterable, List, Optional, TypeVar, Union
2626

27+
from openstack_cli.modules.apputils.curl import CURLResponse, CurlRequestType, curl
28+
from openstack_cli.modules.apputils.progressbar import CharacterStyles, ProgressBar, ProgressBarFormat, \
29+
ProgressBarOptions
2730
from openstack_cli.modules.apputils.terminal.colors import Colors
28-
from openstack_cli.modules.apputils.curl import curl, CurlRequestType, CURLResponse
29-
from openstack_cli.modules.openstack.api_objects import ComputeLimits, VolumeV3Limits, DiskImages, \
30-
DiskImageInfo, ComputeServers, NetworkLimits, ComputeFlavors, ComputeFlavorItem, Networks, NetworkItem, Subnets, \
31-
VMCreateResponse, ComputeServerInfo, ComputeServerActions, ComputeServerActionRebootType, VMKeypairItem, \
32-
VMKeypairItemValue, VMKeypairs, LoginResponse, Token, APIProjects
33-
from openstack_cli.modules.openstack.objects import OpenStackEndpoints, EndpointTypes, OpenStackQuotas, ImageStatus, \
34-
OpenStackUsers, OpenStackVM, OpenStackVMInfo, OSImageInfo, OSFlavor, OSNetwork, VMCreateBuilder, ServerPowerState, \
35-
ServerState, AuthRequestBuilder, AuthRequestType, OpenStackQuotaType
31+
from openstack_cli.modules.openstack.api_objects import APIProjects, ComputeFlavorItem, ComputeFlavors, ComputeLimits, \
32+
ComputeServerActionRebootType, ComputeServerActions, ComputeServerInfo, ComputeServers, DiskImageInfo, DiskImages, \
33+
LoginResponse, NetworkItem, NetworkLimits, Networks, Subnets, Token, VMCreateResponse, VMKeypairItem, \
34+
VMKeypairItemValue, VMKeypairs, VolumeV3Limits
35+
from openstack_cli.modules.openstack.objects import AuthRequestBuilder, AuthRequestType, EndpointTypes, ImageStatus, \
36+
OSFlavor, OSImageInfo, OSNetwork, OpenStackEndpoints, OpenStackQuotaType, OpenStackQuotas, OpenStackUsers, \
37+
OpenStackVM, OpenStackVMInfo, ServerPowerState, ServerState, VMCreateBuilder
3638

3739
T = TypeVar('T')
3840

@@ -97,27 +99,7 @@ def __get_local_cache(self, cache_type: LocalCacheType) -> T:
9799
return self.__local_cache[cache_type.value]
98100

99101
def __init_after_auth__(self):
100-
# getting initial data
101-
if self._conf.cache.exists(DiskImageInfo):
102-
self.__cache_images = {k: DiskImageInfo(serialized_obj=v) for k, v in json.loads(self._conf.cache.get(DiskImageInfo)).items()}
103-
else:
104-
self.images
105-
106-
if self._conf.cache.exists(OSFlavor):
107-
self.__flavors_cache = {k: OSFlavor(serialized_obj=v) for k, v in json.loads(self._conf.cache.get(OSFlavor)).items()}
108-
else:
109-
self.flavors
110-
111-
if self._conf.cache.exists(OSNetwork):
112-
self.__networks_cache = OSNetwork(serialized_obj=self._conf.cache.get(OSNetwork))
113-
else:
114-
self.networks
115-
116-
if not self.__users_cache:
117-
self.users
118-
119-
# fake cache, used to re-sync server keys from time to time
120-
if not self._conf.cache.exists(VMKeypairItemValue):
102+
def __cache_ssh_keys():
121103
conf_keys_hashes = [hash(k) for k in self._conf.get_keys()]
122104
server_keys = self.get_keypairs()
123105
for server_key in server_keys:
@@ -128,8 +110,57 @@ def __init_after_auth__(self):
128110
print(f"Key {server_key.name} is present locally but have wrong hash, replacing with server key")
129111
self._conf.delete_key(server_key.name)
130112
self._conf.add_key(server_key)
113+
131114
self._conf.cache.set(VMKeypairItemValue, "this super cache")
132115

116+
def __cached_network():
117+
self.__networks_cache = OSNetwork(serialized_obj=self._conf.cache.get(OSNetwork))
118+
119+
def __cached_images():
120+
self.__cache_images = {k: DiskImageInfo(serialized_obj=v) for k, v in json.loads(self._conf.cache.get(DiskImageInfo)).items()}
121+
122+
def __cached_flavors():
123+
self.__flavors_cache = {k: OSFlavor(serialized_obj=v) for k, v in json.loads(self._conf.cache.get(OSFlavor)).items()}
124+
125+
def __cached_ssh_keys():
126+
return True
127+
128+
_cached_objects = {
129+
DiskImageInfo: {
130+
False: lambda: self.images,
131+
True: lambda: __cached_images()
132+
},
133+
OSFlavor: {
134+
False: lambda: self.flavors,
135+
True: lambda: __cached_flavors()
136+
},
137+
OSNetwork: {
138+
False: lambda: self.networks,
139+
True: lambda: __cached_network()
140+
},
141+
VMKeypairItemValue: {
142+
False: lambda: __cache_ssh_keys(),
143+
True: lambda: __cached_ssh_keys()
144+
}
145+
}
146+
147+
need_recache: bool = False in [self._conf.cache.exists(obj) for obj in _cached_objects]
148+
if need_recache and not self.__debug:
149+
p = ProgressBar("Syncing to the server data",20,
150+
ProgressBarOptions(CharacterStyles.simple, ProgressBarFormat.PROGRESS_FORMAT_STATUS)
151+
)
152+
p.start(len(_cached_objects))
153+
for cache_item, funcs in _cached_objects.items():
154+
p.progress_inc(1, cache_item.__name__)
155+
funcs[self._conf.cache.exists(cache_item)]()
156+
p.stop(hide_progress=True)
157+
else:
158+
for cache_item, funcs in _cached_objects.items():
159+
funcs[self._conf.cache.exists(cache_item)]()
160+
161+
if not self.__users_cache:
162+
self.users
163+
133164
def __check_token(self) -> bool:
134165
headers = {
135166
"X-Auth-Token": self._conf.auth_token,
@@ -176,7 +207,8 @@ def __auth(self, _type: AuthRequestType = AuthRequestType.SCOPED) -> bool:
176207

177208
if not r:
178209
from openstack_cli.core.output import Console
179-
Console.print_error("Authentication server is not accessible: " + r.raw)
210+
data = r.raw if r else "none"
211+
Console.print_error(f"Authentication server is not accessible: {data}")
180212
return False
181213

182214
if r.code not in [200, 201]:

0 commit comments

Comments
 (0)