trivial: apply black

This commit is contained in:
Frédéric Péters 2021-01-30 20:47:28 +01:00
parent ef933e5fad
commit 6adea28798
1 changed files with 43 additions and 34 deletions

View File

@ -69,10 +69,10 @@ class Server:
# node1.dev, node1.dev.entrouvert, node1.dev.entrouvert.org,
# dev.entrouvert, dev.entrouvert.org, entrouvert.org
parts = servername.split('.')
for i in range(len(parts)-1):
for i in range(len(parts) - 1):
for j in range(i, len(parts)):
if i != j:
self.keywords.add('.'.join(parts[i:j+1]))
self.keywords.add('.'.join(parts[i : j + 1]))
if i == 0:
# add first component without trailing digits, this allows
# matching db1.prod.saas.entrouvert.org with the db
@ -89,10 +89,12 @@ def get_servers():
config.read(os.path.join(os.path.expanduser('~/.config/eoptasks.ini')))
serversfile = config.get('config', 'servers', fallback=None)
if serversfile is None:
print("You need to create ~/.config/eoptasks.ini with such a content:\n"
"\n"
" [config]\n"
" servers = /home/user/src/puppet/data/servers.yaml\n")
print(
"You need to create ~/.config/eoptasks.ini with such a content:\n"
"\n"
" [config]\n"
" servers = /home/user/src/puppet/data/servers.yaml\n"
)
sys.exit(1)
ignorelist = [x.strip() for x in config.get('config', 'ignore', fallback='').split(',')]
@ -101,7 +103,7 @@ def get_servers():
def get_display_name(x):
for stripsuffix in stripsuffixes:
if stripsuffix and x.endswith(stripsuffix):
return x[:-len(stripsuffix)]
return x[: -len(stripsuffix)]
return x
for s in yaml.safe_load(open(serversfile))['servers']:
@ -128,14 +130,15 @@ def parse_args():
args = parser.parse_args()
return args
def filter_servers(servers, args):
selected_servers = []
if args.keywords:
for keyword in args.keywords.split(','):
keywords = set(keyword.split('/'))
selected_servers.extend([
x for x in servers
if keywords.issubset(x.keywords) and not x in selected_servers])
selected_servers.extend(
[x for x in servers if keywords.issubset(x.keywords) and not x in selected_servers]
)
for keyword in args.keywords.split(','):
if keyword.startswith('!') or keyword.startswith('-'):
selected_servers = [x for x in selected_servers if keyword[1:] not in x.keywords]
@ -183,12 +186,12 @@ def status_window(args):
try:
height, width = window.getmaxyx()
max_length = max([len(x['display_name']) for x in servers_info.values()]) + 4
nb_columns = (width-4) // max_length
nb_columns = (width - 4) // max_length
for i, server_name in enumerate(servers_info):
y = 2 + (i//nb_columns)
x = 1 + (width//nb_columns) * (i%nb_columns)
window.addstr(y, x+3, servers_info[server_name]['display_name'])
y = 2 + (i // nb_columns)
x = 1 + (width // nb_columns) * (i % nb_columns)
window.addstr(y, x + 3, servers_info[server_name]['display_name'])
status_icon = {
'running': '',
'done': '🆗',
@ -196,7 +199,7 @@ def status_window(args):
if servers_results.get(server_name) == 'error':
status_icon = ''
window.addstr(y, x, status_icon)
if y > height-4:
if y > height - 4:
break
window.refresh()
total_servers = len(servers_info.keys())
@ -243,7 +246,9 @@ def command_window(args):
sudo -u passerelle passerelle-manage collectstatic --noinput;
sudo -u welco welco-manage collectstatic --noinput;
sudo -u wcs wcs-manage collectstatic;
/bin/true'''.replace('\n', ''),
/bin/true'''.replace(
'\n', ''
),
# combo.reload is useful to get a new {% start_timestamp %} after an
# upgrade of publik-base-theme.
'combo.reload': '''sudo service combo reload; /bin/true''',
@ -272,13 +277,13 @@ def command_window(args):
# -t: force a tty for interactive commands.
rc = subprocess.call(['ssh', '-t', args.command_server_name] + [cmd])
if rc == 0:
send_status_message(tmux_session_name,
{'@type': 'server-result',
'info': {args.command_server_name: 'success'}})
send_status_message(
tmux_session_name, {'@type': 'server-result', 'info': {args.command_server_name: 'success'}}
)
break
send_status_message(tmux_session_name,
{'@type': 'server-result',
'info': {args.command_server_name: 'error'}})
send_status_message(
tmux_session_name, {'@type': 'server-result', 'info': {args.command_server_name: 'error'}}
)
if args.noinput:
break
choice = None
@ -291,6 +296,7 @@ def command_window(args):
elif choice == 'q':
break
args = parse_args()
if args.status_window:
@ -317,6 +323,7 @@ if not args.cmd:
sys.stderr.write('Missing command\n')
sys.exit(1)
def init_tmux_session():
if os.environ.get('TMUX'): # already in a tmux
sys.stderr.write('Cannot run embedded in tmux\n')
@ -329,16 +336,20 @@ def init_tmux_session():
except OSError:
pass
os.environ['SHELL'] = '/bin/sh'
os.system('tmux new-session -s %s -n 🌑 -d %s --status-window --session-name %s' % (
tmux_session_name, sys.argv[0], tmux_session_name))
os.system(
'tmux new-session -s %s -n 🌑 -d %s --status-window --session-name %s'
% (tmux_session_name, sys.argv[0], tmux_session_name)
)
return tmux_session_name
tmux_session_name = init_tmux_session()
pid = os.fork()
if pid:
os.system('tmux attach-session -t %s' % tmux_session_name)
else:
def cluster_name(server_name):
cluster_name = re.sub(r'\d', '', server_name)
for location in ('rbx', 'gra', 'sbg'):
@ -375,16 +386,14 @@ else:
continue
selected_servers.remove(server)
window_cmd = '%s --session-name %s --command-window --command-server-name %s %s "%s" %s' % (
sys.argv[0],
tmux_session_name,
server.name,
'--noinput' if args.noinput else '',
args.cmd,
' '.join(['"%s"' % x for x in args.args]))
session.new_window(
attach=False,
window_name=server.name,
window_shell=window_cmd)
sys.argv[0],
tmux_session_name,
server.name,
'--noinput' if args.noinput else '',
args.cmd,
' '.join(['"%s"' % x for x in args.args]),
)
session.new_window(attach=False, window_name=server.name, window_shell=window_cmd)
break
else:
time.sleep(0.1)