sql.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. ###############################################################################
  2. # #
  3. # L3q - Light, light, lightweight queue #
  4. # Copyright (C) 2023 Marcus Pedersén marcus.pedersen@slu.se #
  5. # #
  6. # This program is free software: you can redistribute it and/or modify #
  7. # it under the terms of the GNU General Public License as published by #
  8. # the Free Software Foundation, either version 3 of the License, or #
  9. # (at your option) any later version. #
  10. # #
  11. # This program is distributed in the hope that it will be useful, #
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of #
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  14. # GNU General Public License for more details. #
  15. # #
  16. # You should have received a copy of the GNU General Public License #
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>. #
  18. # #
  19. ###############################################################################
  20. # import this with:
  21. # from libparaq import sql
  22. import sqlite3
  23. import os
  24. import pwd
  25. import grp
  26. from typing import Any
  27. '''
  28. This module contains functionality
  29. related to sql and interaction with
  30. sqlite3.
  31. '''
  32. def init_db(conf: Any, testing: bool=False) -> sqlite3.Connection:
  33. '''
  34. Checks if database file exists,
  35. if not it is created with the right
  36. permissions and owner and group.
  37. WAL mode is set and Row is configured.
  38. If required tables does not exist they
  39. are created.
  40. Connection object to database is returned.
  41. '''
  42. if(not os.path.exists(conf.db_file) and not testing):
  43. open(conf.db_file, 'a').close()
  44. uid = pwd.getpwnam(conf.validate_user).pw_uid
  45. gid = grp.getgrnam(conf.validate_user).gr_gid
  46. os.chown(conf.db_file, uid, gid)
  47. os.chmod(conf.db_file, conf.validate_mode)
  48. conn = sqlite3.connect(conf.db_file, isolation_level=None)
  49. conn.execute('pragma journal_mode=wal;')
  50. conn.row_factory = sqlite3.Row
  51. conn.execute('''
  52. CREATE TABLE IF NOT EXISTS validate_host
  53. (
  54. id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  55. host_type TEXT NOT NULL,
  56. ip TEXT NOT NULL,
  57. port INT,
  58. hostname TEXT NOT NULL,
  59. name TEXT NOT NULL,
  60. key TEXT NOT NULL,
  61. l3qd_key TEXT
  62. );''')
  63. # node.state can have
  64. # following states:
  65. # Online
  66. # Soft Online
  67. # Offline
  68. # Register
  69. # Maintenance
  70. # Maintenance draining
  71. conn.execute('''
  72. CREATE TABLE IF NOT EXISTS node
  73. (
  74. id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  75. total_cpus INT,
  76. used_cpus INT,
  77. alloc_cpus INT,
  78. total_memory INT,
  79. used_memory INT,
  80. total_swap INT,
  81. used_swap INT,
  82. state TEXT NOT NULL,
  83. validate REFERENCES validate_host(id)
  84. );''')
  85. # job.state can have
  86. # Running
  87. # Queued
  88. # Terminated
  89. # Canceled
  90. # Error
  91. # Depend
  92. conn.execute('''
  93. CREATE TABLE IF NOT EXISTS job
  94. (
  95. id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  96. name TEXT,
  97. type TEXT NOT NULL,
  98. init_date TEXT NOT NULL,
  99. start_date TEXT,
  100. end_date TEXT,
  101. hosts_alloc INT NOT NULL,
  102. cores_alloc INT NOT NULL,
  103. no_tasks_running INT NOT NULL,
  104. no_tasks_finished INT NOT NULL,
  105. no_tasks_err INT NOT NULL,
  106. nodes TEXT,
  107. depend TEXT,
  108. user TEXT NOT NULL,
  109. state TEXT NOT NULL
  110. );''')
  111. conn.execute('''
  112. CREATE TABLE IF NOT EXISTS task
  113. (
  114. id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  115. command TEXT NOT NULL,
  116. seqno INT,
  117. workdir TEXT,
  118. unitname TEXT,
  119. start_time TEXT,
  120. end_time TEXT,
  121. mono_start_time INT,
  122. mono_end_time INT,
  123. active_state TEXT,
  124. sub_state TEXT,
  125. exit_code INT,
  126. main_pid INT,
  127. memory_peak INT,
  128. pids_peak INT,
  129. stdout TEXT,
  130. stderr TEXT,
  131. jobid REFERENCES job(id),
  132. nodeid REFERENCES node(id)
  133. );''')
  134. return conn