Deploy.vala 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using GLib;
  2. namespace Spry.Cli {
  3. /**
  4. * `spry deploy` — packages and deploys the application as a container
  5. * image through USM, delegating to `usm manifest deploy` in the
  6. * application directory.
  7. *
  8. * USM resolves the whole graph inside the image build: the system
  9. * package manager provides the platform libraries and toolchain, the
  10. * configured (or `--repository`-named) USM repositories provide the
  11. * Web-Stack, and the application's own `MANIFEST.usm`/`usm-scripts/`
  12. * (scaffolded by `spry new`) drive its build and install. Flags pass
  13. * through to `usm manifest deploy` verbatim; `--exec` defaults to
  14. * `<app> 8080` so the container serves on the documented port.
  15. */
  16. public class Deploy : GLib.Object {
  17. /**
  18. * Runs `usm manifest deploy` in {@link app_dir} with the collected
  19. * flags. The USM binary is resolved from `--usm` or PATH; a missing
  20. * binary is reported with install guidance rather than a raw spawn
  21. * error. Returns the delegate process's exit status.
  22. */
  23. public static int run(string app_dir, string app_name, string? usm_path,
  24. string? exec_command, string? base_image, string[] repositories,
  25. string? installer_url, bool no_build, bool verbose = false) throws Error {
  26. var usm = usm_path != null ? (!)usm_path : Environment.find_program_in_path("usm");
  27. if (usm == null) {
  28. stderr.printf("Error: usm not found on PATH — install USM (or pass --usm <path-to-usm>) to deploy.\n");
  29. return 1;
  30. }
  31. string[] argv = { (!)usm, "manifest", "deploy",
  32. "--exec", exec_command ?? @"$app_name 8080" };
  33. if (base_image != null) {
  34. argv += "--base";
  35. argv += base_image;
  36. }
  37. foreach (var repository in repositories) {
  38. argv += "--repository";
  39. argv += repository;
  40. }
  41. if (installer_url != null) {
  42. argv += "--installer-url";
  43. argv += installer_url;
  44. }
  45. if (no_build) {
  46. argv += "--no-build";
  47. }
  48. if (verbose) {
  49. argv += "--verbose";
  50. }
  51. var shown = "";
  52. foreach (var word in argv[1:argv.length]) {
  53. shown += (shown.length > 0 ? " " : "") + (word.contains(" ") ? "\"" + word + "\"" : word);
  54. }
  55. stdout.printf("Running: %s\n", shown);
  56. var launcher = new SubprocessLauncher(SubprocessFlags.INHERIT_FDS);
  57. launcher.set_cwd(app_dir);
  58. var process = launcher.spawnv(argv);
  59. try {
  60. process.wait_check();
  61. return 0;
  62. }
  63. catch(Error e) {
  64. return process.get_exit_status();
  65. }
  66. }
  67. }
  68. }