MediaWikiServerTools 0.2.7__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/PKG-INFO +1 -1
  2. mediawikiservertools-0.3.0/mwstools_backend/__init__.py +1 -0
  3. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/mwstools_backend/tsite.py +119 -31
  4. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/mwstools_backend/version.py +1 -1
  5. mediawikiservertools-0.2.7/mwstools_backend/__init__.py +0 -1
  6. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/.github/workflows/build.yml +0 -0
  7. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/.github/workflows/upload-to-pypi.yml +0 -0
  8. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/.gitignore +0 -0
  9. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/.project +0 -0
  10. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/.pydevproject +0 -0
  11. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/AGENTS.md +0 -0
  12. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/LICENSE +0 -0
  13. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/README.md +0 -0
  14. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/mwstools_backend/cron_backup.py +0 -0
  15. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/mwstools_backend/html_table.py +0 -0
  16. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/mwstools_backend/remote.py +0 -0
  17. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/mwstools_backend/server.py +0 -0
  18. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/mwstools_backend/site.py +0 -0
  19. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/mwstools_backend/sql_backup.py +0 -0
  20. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/mwstools_backend/webscrape.py +0 -0
  21. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/mwstools_backend/wikibackup.py +0 -0
  22. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/pyproject.toml +0 -0
  23. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/scripts/blackisort +0 -0
  24. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/scripts/doc +0 -0
  25. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/scripts/install +0 -0
  26. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/scripts/release +0 -0
  27. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/scripts/test +0 -0
  28. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/tests/__init__.py +0 -0
  29. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/tests/smw_access.py +0 -0
  30. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/tests/test_remote.py +0 -0
  31. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/tests/test_server.py +0 -0
  32. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/tests/test_site.py +0 -0
  33. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/tests/test_sqlbackup.py +0 -0
  34. {mediawikiservertools-0.2.7 → mediawikiservertools-0.3.0}/tests/test_wikibackup.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: MediaWikiServerTools
3
- Version: 0.2.7
3
+ Version: 0.3.0
4
4
  Summary: MediaWikiServerTools: MediaWiki Server Management System
5
5
  Project-URL: Home, https://github.com/BITPlan/MediaWikiServerTools
6
6
  Project-URL: Source, https://github.com/BITPlan/MediaWikiServerTools
@@ -0,0 +1 @@
1
+ __version__ = "0.3.0"
@@ -21,7 +21,6 @@ from typing import Dict, Iterable, Iterator, List, TypeVar
21
21
  from basemkit.base_cmd import BaseCmd
22
22
  from basemkit.persistent_log import Log
23
23
  from basemkit.profiler import Profiler
24
- from mwstools_backend.version import Version
25
24
  from tqdm import tqdm
26
25
  from wikibot3rd.smw import SMWClient
27
26
  from wikibot3rd.wikiclient import WikiClient
@@ -31,6 +30,7 @@ from mwstools_backend.remote import Remote, RunConfig
31
30
  from mwstools_backend.server import Server, Servers
32
31
  from mwstools_backend.site import Site, WikiSite
33
32
  from mwstools_backend.sql_backup import SqlBackup
33
+ from mwstools_backend.version import Version
34
34
 
35
35
  # enable generic types
36
36
  T = TypeVar("T")
@@ -69,7 +69,7 @@ class TransferTask:
69
69
  def __post_init__(self):
70
70
  self.force = self.args.force
71
71
  self.update = self.args.update
72
- self.debug=self.args.debug
72
+ self.debug = self.args.debug
73
73
  self.timeout = self.args.timeout
74
74
  self.use_git = self.args.git
75
75
  self.wikiUser = WikiUser.ofWikiId(self.wiki_site.wikiId, lenient=True)
@@ -217,7 +217,9 @@ class TransferTask:
217
217
  return False
218
218
  rsync_prof.time(f" {self.wiki_site.hostname}")
219
219
  result = True
220
- if self.use_git:
220
+ if self.args.backup:
221
+ result = self.adapt_backup_server(site_path)
222
+ if result and self.use_git:
221
223
  git_prof = Profiler("git commit", profile=self.args.profile)
222
224
  proc = self.git_target(target_path=site_path)
223
225
  if proc.returncode == 0:
@@ -226,6 +228,47 @@ class TransferTask:
226
228
  result = False
227
229
  return result
228
230
 
231
+ def backup_server_name(self) -> str:
232
+ """
233
+ the hostname of a backup copy: the --alias if given
234
+ else <name>-<target hostname>
235
+ """
236
+ alias = getattr(self.args, "alias", None)
237
+ if alias:
238
+ server_name = alias
239
+ else:
240
+ server_name = f"{self.wiki_site.name}-{self.target.hostname}"
241
+ return server_name
242
+
243
+ def adapt_backup_server(self, site_path: str) -> bool:
244
+ """
245
+ set $wgServer of the backup copy to its own name -
246
+ the only target-local setting of a copy (#18)
247
+
248
+ Args:
249
+ site_path: the site directory on the target
250
+
251
+ Returns:
252
+ True if the setting is in place
253
+ """
254
+ server_name = self.backup_server_name()
255
+ settings_path = f"{site_path}/LocalSettings.php"
256
+ wg_server = f"https://{server_name}"
257
+ sed_cmd = (
258
+ "sed -i -E "
259
+ f"'s#^([[:space:]]*[$]wgServer[[:space:]]*=[[:space:]]*).*$#\\1\"{wg_server}\";#' "
260
+ f"{settings_path}"
261
+ )
262
+ # base64 transport avoids quoting through the local and the ssh shell
263
+ b64 = base64.b64encode(sed_cmd.encode()).decode()
264
+ proc = self.target.remote.run(f"printf %s {b64!r} | base64 -d | sudo bash")
265
+ lines = self.target.remote.readlines(settings_path) or []
266
+ actual = WikiSite.getVarFromSettings("wgServer", lines)
267
+ ok = proc.returncode == 0 and actual == wg_server
268
+ marker = "✅" if ok else "❌"
269
+ print(f"{marker} $wgServer of the copy {settings_path}: {actual}")
270
+ return ok
271
+
229
272
  def get_wiki_sql(self):
230
273
  wiki = self.wiki_site
231
274
  wiki.configure_of_settings()
@@ -258,11 +301,15 @@ class TransferTask:
258
301
  remote_cmd = f"printf %s {b64!r} | base64 -d | {mysql_cmd}"
259
302
  proc = self.target.remote.run(remote_cmd)
260
303
  success = proc.returncode == 0
304
+ # never show the grant statement, it carries the db password
305
+ shown = name if name == "grant" else sql_cmd
261
306
  if success:
262
- print(f"✅ {sql_cmd}:{proc.stdout}")
307
+ lines = proc.stdout.splitlines()
308
+ summary = proc.stdout if len(lines) <= 3 else f"{len(lines)} lines"
309
+ print(f"✅ {shown}:{summary}")
263
310
  sql_results[name] = proc.stdout
264
311
  else:
265
- print(f"❌ {sql_cmd}:{proc.stderr}")
312
+ print(f"❌ {shown}:{proc.stderr}")
266
313
  break
267
314
  return sql_results
268
315
 
@@ -283,11 +330,15 @@ class TransferTask:
283
330
  ("show_tables", "SHOW TABLES", database),
284
331
  ]
285
332
  sql_results = self.run_sql_cmds(sql_cmds)
333
+ do_restore = False
286
334
  # ping means the database is accessible
287
335
  if "ping" in sql_results:
288
336
  result = True
337
+ # an existing database is refreshed only on --update (#16)
338
+ if "show_tables" in sql_results:
339
+ do_restore = bool(self.args.update)
289
340
  # if show_tables fails the database does not exist
290
- if "show_tables" not in sql_results:
341
+ else:
291
342
  # (name, sql, database)
292
343
  sql_cmds = [
293
344
  ("create_db", f"CREATE DATABASE IF NOT EXISTS `{database}`", None),
@@ -305,25 +356,21 @@ class TransferTask:
305
356
  ]
306
357
  sql_results = self.run_sql_cmds(sql_cmds)
307
358
  # restore if update flag is set or no rev_count
308
- do_restore = (
309
- result and self.args.update or "rev_count" not in sql_results
359
+ do_restore = bool(self.args.update) or "rev_count" not in sql_results
360
+ if do_restore:
361
+ restore_cmd = f"pv {self.target_backup_path} | {mysqlr} -D {database}"
362
+ # honor the user supplied --timeout for the (potentially long)
363
+ # SQL restore instead of the 10s RunConfig default (closes #6)
364
+ restore_run_config = RunConfig(timeout=self.timeout)
365
+ proc = self.target.remote.run(
366
+ restore_cmd, run_config=restore_run_config
310
367
  )
311
- if do_restore:
312
- restore_cmd = (
313
- f"pv {self.target_backup_path} | {mysqlr} -D {database}"
314
- )
315
- # honor the user supplied --timeout for the (potentially long)
316
- # SQL restore instead of the 10s RunConfig default (closes #6)
317
- restore_run_config = RunConfig(timeout=self.timeout)
318
- proc = self.target.remote.run(
319
- restore_cmd, run_config=restore_run_config
320
- )
321
- success = proc.returncode == 0
322
- result = result and success
323
- if success:
324
- print(f"✅ restore:{proc.stdout}")
325
- else:
326
- print(f"❌ restore:{proc.stderr}")
368
+ success = proc.returncode == 0
369
+ result = result and success
370
+ if success:
371
+ print(f"✅ restore {self.target_backup_path} into {database}")
372
+ else:
373
+ print(f"❌ restore:{proc.stderr}")
327
374
  if result:
328
375
  sql_cmds = [
329
376
  ("user_count", "SELECT COUNT(*) AS user_count FROM user;", database),
@@ -362,7 +409,8 @@ class TransferTask:
362
409
  print("❌ Backup creation failed or still outdated.")
363
410
  return False
364
411
 
365
- if target_age:
412
+ age_diff_secs = 0
413
+ if target_age is not None:
366
414
  age_diff_secs = (target_age - source_age) * 86400
367
415
  # more than 1 minute difference
368
416
  if target_age is None or age_diff_secs > 60:
@@ -379,7 +427,8 @@ class TransferTask:
379
427
  "chgrp": f"sudo chgrp {self.target.remote.gid} {self.target_base_path}",
380
428
  }
381
429
  )
382
- run_config = RunConfig(force_local=True, ignore_container=True)
430
+ # pull on the target server, not on the local machine (#17)
431
+ run_config = RunConfig(timeout=self.timeout, ignore_container=True)
383
432
  scp_cmd = f"scp -p {source_path} {target_path}"
384
433
  proc = self.target.remote.run(scp_cmd, run_config=run_config)
385
434
  result = proc.returncode == 0
@@ -450,7 +499,7 @@ class TransferTask:
450
499
  result = False
451
500
  server_name = f"{self.wiki_site.hostname}"
452
501
  if self.args.backup:
453
- server_name = f"{self.wiki_site.name}-{self.target.hostname}"
502
+ server_name = self.backup_server_name()
454
503
  self.target.probe_apache_configs()
455
504
  # first check the configuration
456
505
  config_path = self.target.apache_configs.get(server_name)
@@ -530,19 +579,46 @@ class TransferSite:
530
579
  for filepath in files:
531
580
  Checks.check_age(server.remote, filepath, 1.0)
532
581
 
582
+ def check_backup(self):
583
+ """
584
+ check the backup state of the selected wikis:
585
+ the age of the full SQL dump of each selected wiki
586
+ on each selected server (#12)
587
+ """
588
+ servers = self.get_selected_servers_list()
589
+ for wiki in self.get_selected_wikis():
590
+ for server in servers:
591
+ filepath = f"{server.sql_backup_path}/today/{wiki.database}_full.sql"
592
+ age = Checks.check_age(server.remote, filepath, 1.0)
593
+ if age is None:
594
+ print(f"{server.remote.host}:{filepath} ❌ missing")
595
+
533
596
  def check_database(self):
534
597
  """
535
598
  check the database access
536
599
  """
537
600
  index = 0
601
+ target_server = self.servers.servers.get(self.target) if self.target else None
538
602
  for wiki in self.get_selected_wikis():
539
603
  if wiki.localSettings is None:
540
604
  wiki.configure_of_settings()
605
+ index += 1
606
+ print(
607
+ f"{index:2d}:{wiki.hostname} {wiki.localSettings}: {wiki.database} vs {wiki.database_setting} dbUser:{wiki.dbUser}"
608
+ )
609
+ # the copy of the wiki on the target uses the same site directory (#14)
610
+ if target_server is not None and target_server.sitedir:
611
+ settings_path = (
612
+ f"{target_server.sitedir}/{wiki.hostname}/LocalSettings.php"
613
+ )
614
+ lines = target_server.remote.readlines(settings_path) or []
615
+ copy_db = WikiSite.getVarFromSettings("wgDBname", lines)
616
+ copy_user = WikiSite.getVarFromSettings("wgDBuser", lines)
617
+ copy_server = WikiSite.getVarFromSettings("wgServer", lines)
541
618
  index += 1
542
619
  print(
543
- f"{index:2d}:{wiki.database} vs {wiki.database_setting} dbUser:{wiki.dbUser}"
620
+ f"{index:2d}:{copy_server} {target_server.hostname}:{settings_path}: {copy_db} dbUser:{copy_user}"
544
621
  )
545
- pass
546
622
 
547
623
  def check_endpoints(self):
548
624
  """
@@ -562,6 +638,16 @@ class TransferSite:
562
638
  for wiki in self.get_selected_wikis():
563
639
  index += 1
564
640
  self.check_remote(wiki.remote, index)
641
+ # the selected servers and the copy site named by --alias (#14)
642
+ for server in self.get_selected_servers():
643
+ index += 1
644
+ self.check_remote(server.remote, index)
645
+ if self.args.alias:
646
+ index += 1
647
+ alias_site = Site(self.args.alias)
648
+ proc = alias_site.ping()
649
+ ok = Site.state_symbol(proc.returncode == 0)
650
+ print(f"{index:02d}:{self.args.alias:<26} {ok}")
565
651
 
566
652
  def get_wiki_user(self, wikisite: WikiSite, purpose: str):
567
653
  """
@@ -618,7 +704,9 @@ class TransferSite:
618
704
  "php": "php -v",
619
705
  }
620
706
  remote.log.do_log = self.args.debug
621
- procs = remote.run_cmds(cmds, stop_on_error=False)
707
+ # continue after a failing command so every component is reported (#13)
708
+ run_config = RunConfig(stop_on_error=False, do_log=self.args.debug)
709
+ procs = remote.run_cmds(cmds, run_config=run_config)
622
710
  results = {}
623
711
  for key, proc in procs.items():
624
712
  if proc.returncode == 0:
@@ -1006,7 +1094,7 @@ class TransferSiteCmd(BaseCmd):
1006
1094
  handled = True
1007
1095
  if args.check_database:
1008
1096
  tsite.check_database()
1009
- handlet = True
1097
+ handled = True
1010
1098
  if args.check_endpoints:
1011
1099
  tsite.check_endpoints()
1012
1100
  handled = True
@@ -18,7 +18,7 @@ class Version(object):
18
18
  description = "MediaWiki Server Management System - Apache Site management, SQL backups, cron-based backups "
19
19
  version = mwstools_backend.__version__
20
20
  date = "2022-11-16"
21
- updated = "2026-06-10"
21
+ updated = "2026-11-24"
22
22
  authors = "Wolfgang Fahl"
23
23
  doc_url = "https://wiki.bitplan.com/index.php/MediaWikiServerTools"
24
24
  chat_url = "https://github.com/WolfgangFahl/MediaWikiServerTools/discussions"
@@ -1 +0,0 @@
1
- __version__ = "0.2.7"