Skip to content
This repository was archived by the owner on Dec 24, 2025. It is now read-only.

Commit 0697423

Browse files
committed
Implement barebones update strategy for running apps
1 parent f564479 commit 0697423

5 files changed

Lines changed: 84 additions & 5 deletions

File tree

lib/makina/applications.ex

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,47 @@ defmodule Makina.Applications do
8585
end
8686
end
8787

88+
def update_application(%Server{} = server, %Application{} = app) do
89+
if Server.connected?(server) do
90+
do_update(server, app)
91+
else
92+
connect_and_update(server, app)
93+
end
94+
end
95+
96+
def do_update(%Server{} = server, %Application{} = app) do
97+
IO.puts(" Updating \"#{app.name}\"")
98+
99+
# rename current app to {name}__stale
100+
with {:ok, _} <- mark_app_as_stale(server, app),
101+
{:ok, _} <- do_deploy(server, app),
102+
{:ok, _} <- stop_stale_app(server, app) do
103+
{:ok, :updated}
104+
end
105+
end
106+
107+
def connect_and_update(%Server{} = server, %Application{} = app) do
108+
server = Servers.connect_to_server(server)
109+
result = do_update(server, app)
110+
Servers.disconnect_from_server(server)
111+
112+
result
113+
end
114+
88115
defp do_stop(%Server{} = server, %Application{} = app) do
89116
IO.puts(" Stopping \"#{app.name}\"")
90117

91118
Docker.stop(server, app) |> SSH.execute()
92119
end
93120

121+
defp mark_app_as_stale(%Server{} = server, %Application{} = app) do
122+
Docker.rename_container(server, app, suffix: "__stale") |> SSH.execute()
123+
end
124+
125+
defp stop_stale_app(%Server{} = server, %Application{} = app) do
126+
Docker.stop(server, "#{Docker.app_name(app)}__stale") |> SSH.execute()
127+
end
128+
94129
defp connect_and_stop(%Server{} = server, %Application{} = app) do
95130
server = Servers.connect_to_server(server)
96131
result = do_stop(server, app)
@@ -118,7 +153,7 @@ defmodule Makina.Applications do
118153
else
119154
{:ok, _container} ->
120155
Logger.debug("A version of #{app.name} is already running, skipping.")
121-
{:ok, :skipping}
156+
do_update(server, app)
122157

123158
{:error, reason} ->
124159
{:error, reason}

lib/makina/docker.ex

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,23 @@ defmodule Makina.Docker do
7979
docker(server, "stop", [app_name(app)])
8080
end
8181

82-
defp app_name(%Application{__scope__: []} = app) do
82+
def stop(%Server{} = server, name) when is_binary(name) do
83+
docker(server, "stop", [name])
84+
end
85+
86+
@rename_container_opts [suffix: [type: :string, required: true]]
87+
88+
def rename_container(%Server{} = server, %Application{} = app, opts) do
89+
opts = NimbleOptions.validate!(opts, @rename_container_opts)
90+
91+
docker(server, "rename", [app_name(app), app_name(app) <> opts[:suffix]])
92+
end
93+
94+
def app_name(%Application{__scope__: []} = app) do
8395
app.name
8496
end
8597

86-
defp app_name(%Application{} = app) do
98+
def app_name(%Application{} = app) do
8799
app.__scope__ |> Enum.reverse() |> Enum.join("_")
88100
end
89101

lib/makina/servers.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,15 @@ defmodule Makina.Servers do
9090
})
9191
end
9292

93+
defp expose_https_ports(app, nil) do
94+
app
95+
end
96+
9397
defp expose_https_ports(app, %ProxyConfig{https_enabled: https}) when not is_nil(https) do
9498
Application.put_exposed_port(app, internal: 443, external: 443)
9599
end
96100

97-
defp put_https_command_args(command, %ProxyConfig{https_enabled: nil} = _config) do
101+
defp put_https_command_args(command, nil) do
98102
command
99103
end
100104

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule Makina.MixProject do
44
def project do
55
[
66
app: :makina,
7-
version: "0.1.11",
7+
version: "0.1.15",
88
elixir: "~> 1.18",
99
start_permanent: Mix.env() == :prod,
1010
deps: deps(),

test/makina/models/docker_test.exs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,18 @@ defmodule Makina.Models.DockerTest do
168168

169169
assert cmd.cmd == "docker stop makina_app_foo"
170170
end
171+
172+
test "returns the command to stop an arbitrary container given a name" do
173+
server =
174+
Server.new(host: "example.com")
175+
|> Server.put_private(:conn_ref, self())
176+
177+
cmd = Docker.stop(server, "foo_bar")
178+
179+
assert is_struct(cmd, Command)
180+
181+
assert cmd.cmd == "docker stop foo_bar"
182+
end
171183
end
172184

173185
describe "login/2" do
@@ -217,6 +229,22 @@ defmodule Makina.Models.DockerTest do
217229
end
218230
end
219231

232+
describe "rename_container/3" do
233+
test "returns the command to rename an app's container adding a suffix" do
234+
app = basic_app_without_scope()
235+
236+
server =
237+
Server.new(host: "example.com")
238+
|> Server.put_private(:conn_ref, self())
239+
240+
cmd = Docker.rename_container(server, app, suffix: "__stale")
241+
242+
assert is_struct(cmd, Command)
243+
244+
assert cmd.cmd == "docker rename foo foo__stale"
245+
end
246+
end
247+
220248
defp basic_app_without_scope() do
221249
Application.new(name: "foo")
222250
end

0 commit comments

Comments
 (0)