sync.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. from __future__ import annotations
  2. import configparser
  3. import os
  4. import shlex
  5. import subprocess
  6. from devenv import constants
  7. from devenv.lib import colima, config, fs, limactl, proc, venv, volta
  8. # TODO: need to replace this with a nicer process executor in devenv.lib
  9. def run_procs(
  10. repo: str,
  11. reporoot: str,
  12. venv_path: str,
  13. _procs: tuple[tuple[str, tuple[str, ...], dict[str, str]], ...],
  14. ) -> bool:
  15. procs: list[tuple[str, tuple[str, ...], subprocess.Popen[bytes]]] = []
  16. for name, cmd, extra_env in _procs:
  17. print(f"⏳ {name}")
  18. if constants.DEBUG:
  19. proc.xtrace(cmd)
  20. env = {
  21. **constants.user_environ,
  22. **proc.base_env,
  23. "VIRTUAL_ENV": venv_path,
  24. "VOLTA_HOME": f"{reporoot}/.devenv/bin/volta-home",
  25. "PATH": f"{venv_path}/bin:{reporoot}/.devenv/bin:{proc.base_path}",
  26. }
  27. if extra_env:
  28. env = {**env, **extra_env}
  29. procs.append(
  30. (
  31. name,
  32. cmd,
  33. subprocess.Popen(
  34. cmd,
  35. stdout=subprocess.PIPE,
  36. stderr=subprocess.STDOUT,
  37. env=env,
  38. cwd=reporoot,
  39. ),
  40. )
  41. )
  42. all_good = True
  43. for name, final_cmd, p in procs:
  44. out, _ = p.communicate()
  45. if p.returncode != 0:
  46. all_good = False
  47. print(
  48. f"""
  49. ❌ {name}
  50. failed command (code p.returncode):
  51. {shlex.join(final_cmd)}
  52. Output:
  53. {out.decode()}
  54. """
  55. )
  56. else:
  57. print(f"✅ {name}")
  58. return all_good
  59. def main(context: dict[str, str]) -> int:
  60. repo = context["repo"]
  61. reporoot = context["reporoot"]
  62. FRONTEND_ONLY = os.environ.get("SENTRY_DEVENV_FRONTEND_ONLY") is not None
  63. # venv's still needed for frontend because repo-local devenv and pre-commit
  64. # exist inside it
  65. venv_dir, python_version, requirements, editable_paths, bins = venv.get(reporoot, repo)
  66. url, sha256 = config.get_python(reporoot, python_version)
  67. print(f"ensuring {repo} venv at {venv_dir}...")
  68. venv.ensure(venv_dir, python_version, url, sha256)
  69. # TODO: move volta version into per-repo config
  70. try:
  71. volta.install(reporoot)
  72. except TypeError:
  73. # this is needed for devenv <=1.4.0,>1.2.3 to finish syncing and therefore update itself
  74. volta.install()
  75. if constants.DARWIN:
  76. repo_config = configparser.ConfigParser()
  77. repo_config.read(f"{reporoot}/devenv/config.ini")
  78. try:
  79. colima.install(
  80. repo_config["colima"]["version"],
  81. repo_config["colima"][constants.SYSTEM_MACHINE],
  82. repo_config["colima"][f"{constants.SYSTEM_MACHINE}_sha256"],
  83. reporoot,
  84. )
  85. except TypeError:
  86. # this is needed for devenv <=1.4.0,>1.2.3 to finish syncing and therefore update itself
  87. colima.install(
  88. repo_config["colima"]["version"],
  89. repo_config["colima"][constants.SYSTEM_MACHINE],
  90. repo_config["colima"][f"{constants.SYSTEM_MACHINE}_sha256"],
  91. )
  92. # TODO: move limactl version into per-repo config
  93. try:
  94. limactl.install(reporoot)
  95. except TypeError:
  96. # this is needed for devenv <=1.4.0,>1.2.3 to finish syncing and therefore update itself
  97. limactl.install()
  98. if not run_procs(
  99. repo,
  100. reporoot,
  101. venv_dir,
  102. (
  103. # TODO: devenv should provide a job runner (jobs run in parallel, tasks run sequentially)
  104. (
  105. "python dependencies (1/4)",
  106. (
  107. # upgrading pip first
  108. "pip",
  109. "install",
  110. "--constraint",
  111. "requirements-dev-frozen.txt",
  112. "pip",
  113. ),
  114. {},
  115. ),
  116. ),
  117. ):
  118. return 1
  119. if not run_procs(
  120. repo,
  121. reporoot,
  122. venv_dir,
  123. (
  124. (
  125. # Spreading out the network load by installing js,
  126. # then py in the next batch.
  127. "javascript dependencies (1/1)",
  128. (
  129. "yarn",
  130. "install",
  131. "--frozen-lockfile",
  132. "--no-progress",
  133. "--non-interactive",
  134. ),
  135. {
  136. "NODE_ENV": "development",
  137. },
  138. ),
  139. (
  140. "python dependencies (2/4)",
  141. (
  142. "pip",
  143. "uninstall",
  144. "-qqy",
  145. "djangorestframework-stubs",
  146. "django-stubs",
  147. ),
  148. {},
  149. ),
  150. ),
  151. ):
  152. return 1
  153. if not run_procs(
  154. repo,
  155. reporoot,
  156. venv_dir,
  157. (
  158. # could opt out of syncing python if FRONTEND_ONLY but only if repo-local devenv
  159. # and pre-commit were moved to inside devenv and not the sentry venv
  160. (
  161. "python dependencies (3/4)",
  162. (
  163. "pip",
  164. "install",
  165. "--constraint",
  166. "requirements-dev-frozen.txt",
  167. "-r",
  168. "requirements-dev-frozen.txt",
  169. ),
  170. {},
  171. ),
  172. ),
  173. ):
  174. return 1
  175. if not run_procs(
  176. repo,
  177. reporoot,
  178. venv_dir,
  179. (
  180. (
  181. "python dependencies (4/4)",
  182. ("python3", "-m", "tools.fast_editable", "--path", "."),
  183. {},
  184. ),
  185. ("pre-commit dependencies", ("pre-commit", "install", "--install-hooks", "-f"), {}),
  186. ),
  187. ):
  188. return 1
  189. fs.ensure_symlink("../../config/hooks/post-merge", f"{reporoot}/.git/hooks/post-merge")
  190. if not os.path.exists(f"{constants.home}/.sentry/config.yml") or not os.path.exists(
  191. f"{constants.home}/.sentry/sentry.conf.py"
  192. ):
  193. proc.run((f"{venv_dir}/bin/sentry", "init", "--dev"))
  194. # Frontend engineers don't necessarily always have devservices running and
  195. # can configure to skip them to save on local resources
  196. if FRONTEND_ONLY:
  197. print("Skipping python migrations since SENTRY_DEVENV_FRONTEND_ONLY is set.")
  198. return 0
  199. # TODO: check healthchecks for redis and postgres to short circuit this
  200. proc.run(
  201. (
  202. f"{venv_dir}/bin/{repo}",
  203. "devservices",
  204. "up",
  205. "redis",
  206. "postgres",
  207. ),
  208. pathprepend=f"{reporoot}/.devenv/bin",
  209. exit=True,
  210. )
  211. if run_procs(
  212. repo,
  213. reporoot,
  214. venv_dir,
  215. (
  216. (
  217. "python migrations",
  218. ("make", "apply-migrations"),
  219. {},
  220. ),
  221. ),
  222. ):
  223. return 0
  224. return 1