Extract fly helpers for Mix tasks

This commit is contained in:
Claudio Ortolina
2024-12-01 12:27:07 +00:00
parent bdfae15f35
commit c4cafa0bf6
4 changed files with 17 additions and 3 deletions
@@ -7,10 +7,12 @@ defmodule Mix.Tasks.MusicLibrary.Prod.DbMigrate do
Requires the `flyctl` CLI to be installed and authenticated. Requires the `flyctl` CLI to be installed and authenticated.
""" """
import Mix.Tasks.MusicLibrary.Prod.Helpers
@impl Mix.Task @impl Mix.Task
def run(_args) do def run(_args) do
IO.puts("Running migrations on production database") IO.puts("Running migrations on production database")
System.cmd("flyctl", ["ssh", "console", "--command", "bin/migrate"], into: IO.stream()) fly_ssh("bin/migrate")
end end
end end
+2 -1
View File
@@ -6,6 +6,7 @@ defmodule Mix.Tasks.MusicLibrary.Prod.DbPull do
Requires the `flyctl` CLI to be installed and authenticated. Requires the `flyctl` CLI to be installed and authenticated.
""" """
import Mix.Tasks.MusicLibrary.Prod.Helpers
@impl Mix.Task @impl Mix.Task
def run(_args) do def run(_args) do
@@ -15,7 +16,7 @@ defmodule Mix.Tasks.MusicLibrary.Prod.DbPull do
remote_db = "/mnt/music_library/music_library_prod.db" remote_db = "/mnt/music_library/music_library_prod.db"
local_db = "data/music_library_prod_#{DateTime.to_unix(current_time)}.db" local_db = "data/music_library_prod_#{DateTime.to_unix(current_time)}.db"
case System.cmd("flyctl", ["ssh", "sftp", "get", remote_db, local_db], into: IO.stream()) do case fly_sftp_get(remote_db, local_db) do
{_stream, 1} -> {_stream, 1} ->
IO.puts("Failed to pull the database") IO.puts("Failed to pull the database")
System.halt(1) System.halt(1)
@@ -8,12 +8,14 @@ defmodule Mix.Tasks.MusicLibrary.Prod.DbVacuum do
Requires the `flyctl` CLI to be installed and authenticated. Requires the `flyctl` CLI to be installed and authenticated.
""" """
import Mix.Tasks.MusicLibrary.Prod.Helpers
@impl Mix.Task @impl Mix.Task
def run(_args) do def run(_args) do
IO.puts("Running VACUUM on the production database") IO.puts("Running VACUUM on the production database")
command = ~s(bin/music_library rpc 'MusicLibrary.Repo.query\("VACUUM"\)') command = ~s(bin/music_library rpc 'MusicLibrary.Repo.query\("VACUUM"\)')
System.cmd("flyctl", ["ssh", "console", "--command", command], into: IO.stream()) fly_ssh(command)
end end
end end
@@ -0,0 +1,9 @@
defmodule Mix.Tasks.MusicLibrary.Prod.Helpers do
def fly_ssh(command) do
System.cmd("flyctl", ["ssh", "console", "--command", command], into: IO.stream())
end
def fly_sftp_get(remote_path, local_path) do
System.cmd("flyctl", ["ssh", "sftp", "get", remote_path, local_path], into: IO.stream())
end
end