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

Commit 59075d5

Browse files
committed
Add support for privileged container
1 parent 70e0128 commit 59075d5

File tree

8 files changed

+88
-6
lines changed

8 files changed

+88
-6
lines changed

.formatter.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ locals_without_parens = [
99
env: 2,
1010
expose_port: 2,
1111
proxy: 1,
12-
publish_on_domain: 2
12+
publish_on_domain: 2,
13+
privileged?: 1
1314
]
1415

1516
[

lib/makina/dsl/app.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,13 @@ defmodule Makina.DSL.App do
215215
end
216216
end
217217
end
218+
219+
defmacro privileged?(flag) when is_boolean(flag) do
220+
quote do
221+
@current_application Application.set_privileged(
222+
@current_application,
223+
unquote(flag)
224+
)
225+
end
226+
end
218227
end

lib/makina/infrastructure/docker.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ defmodule Makina.Infrastructure.Docker do
1414
def run(%Server{} = server, %Application{} = app) do
1515
docker(server, "run", [
1616
"-d",
17+
privileged?(app),
1718
"--restart",
1819
"unless-stopped",
1920
name(app),
@@ -132,6 +133,14 @@ defmodule Makina.Infrastructure.Docker do
132133
["--name", app_name(app)]
133134
end
134135

136+
defp privileged?(%Application{privileged?: true}) do
137+
["--privileged"]
138+
end
139+
140+
defp privileged?(%Application{privileged?: false}) do
141+
[]
142+
end
143+
135144
defp volumes(%Application{} = app) do
136145
app.volumes
137146
|> Enum.flat_map(fn v ->

lib/makina/models/application.ex

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ defmodule Makina.Models.Application do
2525
are subject to changes and should never be relied on.
2626
These are:
2727
* `:__hash__` which is the hash of all properties (except the private ones)
28-
* `:__docker__` specific internal configurations used for docker that are not (yet)
28+
* `:__docker__` specific internal configurations used for docker that are not (yet) public
2929
* `:__scope__` collects nesting levels in the makina file in order to reliably distinguish apps
3030
exposed to the DSL.
3131
"""
3232

3333
alias Makina.Models.Internal
3434

35-
@hashable_keys ~w[name docker_image docker_registry dockerfile env_vars volumes exposed_ports domains load_balancing_port]a
35+
@hashable_keys ~w[name docker_image docker_registry dockerfile env_vars volumes exposed_ports domains load_balancing_port privileged?]a
3636

3737
@derive {JSON.Encoder, []}
3838
defstruct __hash__: nil,
@@ -50,7 +50,8 @@ defmodule Makina.Models.Application do
5050
env_vars: [],
5151
exposed_ports: [],
5252
domains: [],
53-
load_balancing_port: nil
53+
load_balancing_port: nil,
54+
privileged?: false
5455

5556
def new(opts) do
5657
app = struct(__MODULE__, opts)
@@ -167,10 +168,16 @@ defmodule Makina.Models.Application do
167168
not (is_nil(docker_registry.user) and is_nil(docker_registry.password))
168169
end
169170

170-
def set_private(%__MODULE{} = app, key, value) do
171+
def set_private(%__MODULE__{} = app, key, value) do
171172
app |> Map.put(key, value)
172173
end
173174

175+
def set_privileged(%__MODULE__{} = app, flag) do
176+
app = %__MODULE__{app | privileged?: flag}
177+
178+
set_private(app, :__hash__, hash(app))
179+
end
180+
174181
defp hash(%__MODULE__{} = app) do
175182
keys = @hashable_keys |> Enum.sort()
176183

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.22",
7+
version: "0.2.0",
88
elixir: "~> 1.18",
99
start_permanent: Mix.env() == :prod,
1010
deps: deps(),

test/makina/dsl_test.exs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,24 @@ defmodule Makina.DSLTest do
6161
assert app.docker_image[:tag] == "tag"
6262
end
6363

64+
test "allow configuring a container as priviledged" do
65+
import DSL
66+
67+
term =
68+
makina "app-test-allow-docker-privileged" do
69+
app name: "test" do
70+
privileged? true
71+
end
72+
end
73+
74+
module = elem(term, 1)
75+
context = module.collect_context()
76+
77+
app = List.first(context.applications)
78+
79+
assert app.privileged? == true
80+
end
81+
6482
test "allow volumes to be specified" do
6583
import DSL
6684

test/makina/infrastructure/docker_test.exs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,22 @@ defmodule Makina.Infrastructure.DockerTest do
151151
assert cmd.cmd ==
152152
"docker run -d --restart unless-stopped --name foo --label org.makina.app.hash=#{app.__hash__} --label traefik.enable=true --label traefik.http.middlewares.foo.compress=true --label traefik.http.routers.foo.rule=\"Host(\\`example.com\\`)\" --label traefik.http.routers.foo.tls.certresolver=letsencrypt --label traefik.http.services.foo.loadBalancer.server.port=80 --network makina-web-net nginx:1.16"
153153
end
154+
155+
test "sets container as priviledged if needed" do
156+
server =
157+
Server.new(host: "example.com")
158+
|> Server.put_private(:conn_ref, self())
159+
160+
app =
161+
Application.new(name: "foo")
162+
|> Application.set_docker_image(name: "nginx", tag: "1.16")
163+
|> Application.set_privileged(true)
164+
165+
cmd = Docker.run(server, app)
166+
167+
assert cmd.cmd ==
168+
"docker run -d --privileged --restart unless-stopped --name foo --label org.makina.app.hash=#{app.__hash__} nginx:1.16"
169+
end
154170
end
155171

156172
describe "stop/2" do

test/makina/models/application_test.exs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,28 @@ defmodule Makina.Models.ApplicationTest do
171171
end
172172
end
173173

174+
describe "set_privileged/2" do
175+
test "defaults to false unless set" do
176+
params = [name: "foo"]
177+
178+
app = Application.new(params)
179+
180+
assert app.privileged? == false
181+
end
182+
183+
test "sets if the applications should run as priviledged" do
184+
params = [name: "foo"]
185+
186+
app = Application.new(params)
187+
init_hash = app.__hash__
188+
189+
app = app |> Application.set_privileged(true)
190+
191+
assert app.privileged? == true
192+
assert app.__hash__ != init_hash
193+
end
194+
end
195+
174196
describe "set_private/3" do
175197
test "sets private fields" do
176198
params = [name: "foo"]

0 commit comments

Comments
 (0)