简化docker容器的创建逻辑
This commit is contained in:
parent
dae690b20e
commit
8a108ec34d
@ -121,51 +121,74 @@
|
||||
-record('ContainerDeployParams',
|
||||
{container_name = <<>> :: unicode:chardata() | undefined, % = 1, optional
|
||||
container_dir = <<>> :: unicode:chardata() | undefined, % = 2, optional
|
||||
spec = undefined :: message_pb:'ContainerSpec'() | undefined % = 3, optional
|
||||
create = undefined :: message_pb:'DockerCreateOptions'() | undefined % = 3, optional
|
||||
}).
|
||||
-endif.
|
||||
|
||||
-ifndef('CONTAINERSPEC_PB_H').
|
||||
-define('CONTAINERSPEC_PB_H', true).
|
||||
-record('ContainerSpec',
|
||||
-ifndef('DOCKERCREATEOPTIONS_PB_H').
|
||||
-define('DOCKERCREATEOPTIONS_PB_H', true).
|
||||
-record('DockerCreateOptions',
|
||||
{config = undefined :: message_pb:'DockerContainerConfig'() | undefined, % = 1, optional
|
||||
host_config = undefined :: message_pb:'DockerHostConfig'() | undefined, % = 2, optional
|
||||
networking_config = undefined :: message_pb:'DockerNetworkingConfig'() | undefined % = 3, optional
|
||||
}).
|
||||
-endif.
|
||||
|
||||
-ifndef('DOCKERCONTAINERCONFIG_PB_H').
|
||||
-define('DOCKERCONTAINERCONFIG_PB_H', true).
|
||||
-record('DockerContainerConfig',
|
||||
{image = <<>> :: unicode:chardata() | undefined, % = 1, optional
|
||||
command = [] :: [unicode:chardata()] | undefined, % = 2, repeated
|
||||
cmd = [] :: [unicode:chardata()] | undefined, % = 2, repeated
|
||||
entrypoint = [] :: [unicode:chardata()] | undefined, % = 3, repeated
|
||||
env = [] :: [unicode:chardata()] | undefined, % = 4, repeated
|
||||
labels = [] :: [{unicode:chardata(), unicode:chardata()}] | undefined, % = 5
|
||||
volumes = [] :: [message_pb:'VolumeBind'()] | undefined, % = 6, repeated
|
||||
volumes = [] :: [unicode:chardata()] | undefined, % = 6, repeated
|
||||
user = <<>> :: unicode:chardata() | undefined, % = 7, optional
|
||||
working_dir = <<>> :: unicode:chardata() | undefined, % = 8, optional
|
||||
hostname = <<>> :: unicode:chardata() | undefined, % = 9, optional
|
||||
expose = [] :: [message_pb:'PortExpose'()] | undefined, % = 10, repeated
|
||||
networks = [] :: [unicode:chardata()] | undefined, % = 11, repeated
|
||||
network_mode = <<>> :: unicode:chardata() | undefined, % = 12, optional
|
||||
healthcheck = undefined :: message_pb:'Healthcheck'() | undefined, % = 13, optional
|
||||
restart = undefined :: message_pb:'RestartPolicy'() | undefined, % = 14, optional
|
||||
privileged = false :: boolean() | 0 | 1 | undefined, % = 15, optional
|
||||
cap_add = [] :: [unicode:chardata()] | undefined, % = 16, repeated
|
||||
cap_drop = [] :: [unicode:chardata()] | undefined, % = 17, repeated
|
||||
devices = [] :: [message_pb:'DeviceMapping'()] | undefined, % = 18, repeated
|
||||
resources = undefined :: message_pb:'ResourceLimits'() | undefined, % = 19, optional
|
||||
ulimits = [] :: [message_pb:'Ulimit'()] | undefined, % = 20, repeated
|
||||
tmpfs = [] :: [message_pb:'TmpfsMount'()] | undefined, % = 21, repeated
|
||||
sysctls = [] :: [{unicode:chardata(), unicode:chardata()}] | undefined, % = 22
|
||||
extra_hosts = [] :: [unicode:chardata()] | undefined % = 23, repeated
|
||||
exposed_ports = [] :: [message_pb:'DockerExposedPort'()] | undefined, % = 10, repeated
|
||||
healthcheck = undefined :: message_pb:'Healthcheck'() | undefined % = 11, optional
|
||||
}).
|
||||
-endif.
|
||||
|
||||
-ifndef('VOLUMEBIND_PB_H').
|
||||
-define('VOLUMEBIND_PB_H', true).
|
||||
-record('VolumeBind',
|
||||
{host_path = <<>> :: unicode:chardata() | undefined, % = 1, optional
|
||||
container_path = <<>> :: unicode:chardata() | undefined, % = 2, optional
|
||||
read_only = false :: boolean() | 0 | 1 | undefined % = 3, optional
|
||||
-ifndef('DOCKERHOSTCONFIG_PB_H').
|
||||
-define('DOCKERHOSTCONFIG_PB_H', true).
|
||||
-record('DockerHostConfig',
|
||||
{binds = [] :: [unicode:chardata()] | undefined, % = 1, repeated
|
||||
network_mode = <<>> :: unicode:chardata() | undefined, % = 2, optional
|
||||
restart_policy = undefined :: message_pb:'RestartPolicy'() | undefined, % = 3, optional
|
||||
privileged = false :: boolean() | 0 | 1 | undefined, % = 4, optional
|
||||
cap_add = [] :: [unicode:chardata()] | undefined, % = 5, repeated
|
||||
cap_drop = [] :: [unicode:chardata()] | undefined, % = 6, repeated
|
||||
devices = [] :: [message_pb:'DeviceMapping'()] | undefined, % = 7, repeated
|
||||
memory = 0 :: non_neg_integer() | undefined, % = 8, optional, 64 bits
|
||||
memory_reservation = 0 :: non_neg_integer() | undefined, % = 9, optional, 64 bits
|
||||
nano_cpus = 0 :: non_neg_integer() | undefined, % = 10, optional, 64 bits
|
||||
cpu_shares = 0 :: non_neg_integer() | undefined, % = 11, optional, 64 bits
|
||||
ulimits = [] :: [message_pb:'Ulimit'()] | undefined, % = 12, repeated
|
||||
tmpfs = [] :: [{unicode:chardata(), unicode:chardata()}] | undefined, % = 13
|
||||
sysctls = [] :: [{unicode:chardata(), unicode:chardata()}] | undefined, % = 14
|
||||
extra_hosts = [] :: [unicode:chardata()] | undefined % = 15, repeated
|
||||
}).
|
||||
-endif.
|
||||
|
||||
-ifndef('PORTEXPOSE_PB_H').
|
||||
-define('PORTEXPOSE_PB_H', true).
|
||||
-record('PortExpose',
|
||||
-ifndef('DOCKERNETWORKINGCONFIG_PB_H').
|
||||
-define('DOCKERNETWORKINGCONFIG_PB_H', true).
|
||||
-record('DockerNetworkingConfig',
|
||||
{endpoints = [] :: [message_pb:'DockerNetworkEndpoint'()] | undefined % = 1, repeated
|
||||
}).
|
||||
-endif.
|
||||
|
||||
-ifndef('DOCKERNETWORKENDPOINT_PB_H').
|
||||
-define('DOCKERNETWORKENDPOINT_PB_H', true).
|
||||
-record('DockerNetworkEndpoint',
|
||||
{name = <<>> :: unicode:chardata() | undefined % = 1, optional
|
||||
}).
|
||||
-endif.
|
||||
|
||||
-ifndef('DOCKEREXPOSEDPORT_PB_H').
|
||||
-define('DOCKEREXPOSEDPORT_PB_H', true).
|
||||
-record('DockerExposedPort',
|
||||
{container_port = 0 :: non_neg_integer() | undefined, % = 1, optional, 32 bits
|
||||
protocol = <<>> :: unicode:chardata() | undefined % = 2, optional
|
||||
}).
|
||||
@ -198,16 +221,6 @@
|
||||
}).
|
||||
-endif.
|
||||
|
||||
-ifndef('RESOURCELIMITS_PB_H').
|
||||
-define('RESOURCELIMITS_PB_H', true).
|
||||
-record('ResourceLimits',
|
||||
{memory_bytes = 0 :: non_neg_integer() | undefined, % = 1, optional, 64 bits
|
||||
memory_reservation_bytes = 0 :: non_neg_integer() | undefined, % = 2, optional, 64 bits
|
||||
nano_cpus = 0 :: non_neg_integer() | undefined, % = 3, optional, 64 bits
|
||||
cpu_shares = 0 :: non_neg_integer() | undefined % = 4, optional, 64 bits
|
||||
}).
|
||||
-endif.
|
||||
|
||||
-ifndef('ULIMIT_PB_H').
|
||||
-define('ULIMIT_PB_H', true).
|
||||
-record('Ulimit',
|
||||
@ -217,14 +230,6 @@
|
||||
}).
|
||||
-endif.
|
||||
|
||||
-ifndef('TMPFSMOUNT_PB_H').
|
||||
-define('TMPFSMOUNT_PB_H', true).
|
||||
-record('TmpfsMount',
|
||||
{path = <<>> :: unicode:chardata() | undefined, % = 1, optional
|
||||
options = <<>> :: unicode:chardata() | undefined % = 2, optional
|
||||
}).
|
||||
-endif.
|
||||
|
||||
-ifndef('AUTHREQUEST_PB_H').
|
||||
-define('AUTHREQUEST_PB_H', true).
|
||||
-record('AuthRequest',
|
||||
|
||||
@ -91,48 +91,57 @@ message ContainerRequest {
|
||||
message ContainerDeployParams {
|
||||
string container_name = 1;
|
||||
string container_dir = 2;
|
||||
ContainerSpec spec = 3;
|
||||
DockerCreateOptions create = 3;
|
||||
}
|
||||
|
||||
message ContainerSpec {
|
||||
message DockerCreateOptions {
|
||||
DockerContainerConfig config = 1;
|
||||
DockerHostConfig host_config = 2;
|
||||
DockerNetworkingConfig networking_config = 3;
|
||||
}
|
||||
|
||||
message DockerContainerConfig {
|
||||
string image = 1;
|
||||
repeated string command = 2;
|
||||
repeated string cmd = 2;
|
||||
repeated string entrypoint = 3;
|
||||
|
||||
repeated string env = 4;
|
||||
map<string, string> labels = 5;
|
||||
|
||||
repeated VolumeBind volumes = 6;
|
||||
repeated string volumes = 6;
|
||||
string user = 7;
|
||||
string working_dir = 8;
|
||||
string hostname = 9;
|
||||
|
||||
repeated PortExpose expose = 10;
|
||||
repeated string networks = 11;
|
||||
string network_mode = 12;
|
||||
|
||||
Healthcheck healthcheck = 13;
|
||||
RestartPolicy restart = 14;
|
||||
|
||||
bool privileged = 15;
|
||||
repeated string cap_add = 16;
|
||||
repeated string cap_drop = 17;
|
||||
repeated DeviceMapping devices = 18;
|
||||
|
||||
ResourceLimits resources = 19;
|
||||
repeated Ulimit ulimits = 20;
|
||||
repeated TmpfsMount tmpfs = 21;
|
||||
map<string, string> sysctls = 22;
|
||||
repeated string extra_hosts = 23;
|
||||
repeated DockerExposedPort exposed_ports = 10;
|
||||
Healthcheck healthcheck = 11;
|
||||
}
|
||||
|
||||
message VolumeBind {
|
||||
string host_path = 1;
|
||||
string container_path = 2;
|
||||
bool read_only = 3;
|
||||
message DockerHostConfig {
|
||||
repeated string binds = 1;
|
||||
string network_mode = 2;
|
||||
RestartPolicy restart_policy = 3;
|
||||
bool privileged = 4;
|
||||
repeated string cap_add = 5;
|
||||
repeated string cap_drop = 6;
|
||||
repeated DeviceMapping devices = 7;
|
||||
uint64 memory = 8;
|
||||
uint64 memory_reservation = 9;
|
||||
uint64 nano_cpus = 10;
|
||||
uint64 cpu_shares = 11;
|
||||
repeated Ulimit ulimits = 12;
|
||||
map<string, string> tmpfs = 13;
|
||||
map<string, string> sysctls = 14;
|
||||
repeated string extra_hosts = 15;
|
||||
}
|
||||
|
||||
message PortExpose {
|
||||
message DockerNetworkingConfig {
|
||||
repeated DockerNetworkEndpoint endpoints = 1;
|
||||
}
|
||||
|
||||
message DockerNetworkEndpoint {
|
||||
string name = 1;
|
||||
}
|
||||
|
||||
message DockerExposedPort {
|
||||
uint32 container_port = 1;
|
||||
string protocol = 2; // tcp / udp
|
||||
}
|
||||
@ -155,24 +164,12 @@ message DeviceMapping {
|
||||
string cgroup_permissions = 3; // 默认可填 rwm
|
||||
}
|
||||
|
||||
message ResourceLimits {
|
||||
uint64 memory_bytes = 1;
|
||||
uint64 memory_reservation_bytes = 2;
|
||||
uint64 nano_cpus = 3;
|
||||
uint64 cpu_shares = 4;
|
||||
}
|
||||
|
||||
message Ulimit {
|
||||
string name = 1;
|
||||
uint64 soft = 2;
|
||||
uint64 hard = 3;
|
||||
}
|
||||
|
||||
message TmpfsMount {
|
||||
string path = 1;
|
||||
string options = 2;
|
||||
}
|
||||
|
||||
message AuthRequest {
|
||||
bytes uuid = 1;
|
||||
bytes username = 2;
|
||||
|
||||
@ -36,19 +36,14 @@ check_image_exist(Image) when is_binary(Image) ->
|
||||
{ok, ContainerId :: binary()} | {error, Reason :: any()}.
|
||||
create_container(ContainerDir, #'ContainerDeployParams'{
|
||||
container_name = ContainerName,
|
||||
spec = Spec0 = #'ContainerSpec'{volumes = Volumes0}
|
||||
create = Create0
|
||||
}) when is_binary(ContainerName), is_list(ContainerDir) ->
|
||||
|
||||
Url = lists:flatten(io_lib:format("/containers/create?name=~s", [binary_to_list(ContainerName)])),
|
||||
%% 挂载预留的目录,用来作为配置文件的存放
|
||||
ConfigFile = list_to_binary(docker_helper:get_config_file(ContainerDir)),
|
||||
ConfigVolume = #'VolumeBind'{
|
||||
host_path = ConfigFile,
|
||||
container_path = <<"/usr/local/etc/service.conf">>,
|
||||
read_only = false
|
||||
},
|
||||
Spec = Spec0#'ContainerSpec'{volumes = [ConfigVolume | Volumes0]},
|
||||
Options = build_options(ContainerName, Spec),
|
||||
Create = patch_create_options(ContainerName, ConfigFile, Create0),
|
||||
Options = build_options(Create),
|
||||
display_options(Options),
|
||||
|
||||
Body = iolist_to_binary(jiffy:encode(Options, [force_utf8])),
|
||||
@ -239,45 +234,100 @@ inspect_container(ContainerId) when is_binary(ContainerId) ->
|
||||
%%% helper methods
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
patch_create_options(ContainerName, ConfigFile, undefined) ->
|
||||
patch_create_options(ContainerName, ConfigFile, #'DockerCreateOptions'{});
|
||||
patch_create_options(ContainerName, ConfigFile, Create = #'DockerCreateOptions'{
|
||||
config = Config0,
|
||||
host_config = HostConfig0
|
||||
}) when is_binary(ContainerName), is_binary(ConfigFile) ->
|
||||
Config = patch_container_config(ContainerName, ensure_container_config(Config0)),
|
||||
HostConfig = patch_host_config(ConfigFile, ensure_host_config(HostConfig0)),
|
||||
Create#'DockerCreateOptions'{config = Config, host_config = HostConfig}.
|
||||
|
||||
patch_container_config(ContainerName, Config = #'DockerContainerConfig'{env = Env0, volumes = Volumes0})
|
||||
when is_binary(ContainerName) ->
|
||||
ConfigVolume = <<"/usr/local/etc/service.conf">>,
|
||||
Envs0 = [<<"CONTAINER_NAME=", ContainerName/binary>>],
|
||||
Envs = add_unique_front(Envs0, [to_binary(EnvItem) || EnvItem <- Env0]),
|
||||
Volumes = add_unique_front([ConfigVolume], [to_binary(Volume) || Volume <- Volumes0]),
|
||||
Config#'DockerContainerConfig'{env = Envs, volumes = Volumes}.
|
||||
|
||||
patch_host_config(ConfigFile, HostConfig = #'DockerHostConfig'{binds = Binds0}) when is_binary(ConfigFile) ->
|
||||
ConfigBind = <<ConfigFile/binary, ":/usr/local/etc/service.conf">>,
|
||||
Binds = add_unique_front([ConfigBind], [to_binary(Bind) || Bind <- Binds0]),
|
||||
HostConfig#'DockerHostConfig'{binds = Binds}.
|
||||
|
||||
ensure_container_config(undefined) ->
|
||||
#'DockerContainerConfig'{};
|
||||
ensure_container_config(Config = #'DockerContainerConfig'{}) ->
|
||||
Config.
|
||||
|
||||
ensure_host_config(undefined) ->
|
||||
#'DockerHostConfig'{};
|
||||
ensure_host_config(HostConfig = #'DockerHostConfig'{}) ->
|
||||
HostConfig.
|
||||
|
||||
%% 构建最终 JSON Map
|
||||
build_options(ContainerName, Spec = #'ContainerSpec'{
|
||||
image = Image,
|
||||
command = Command,
|
||||
entrypoint = Entrypoint,
|
||||
env = Env,
|
||||
labels = Labels,
|
||||
user = User,
|
||||
working_dir = WorkingDir,
|
||||
hostname = Hostname
|
||||
}) when is_binary(ContainerName) ->
|
||||
%% 容器名称作为环境变量,注入到运行时环境中
|
||||
Envs0 = [to_binary(EnvItem) || EnvItem <- Env],
|
||||
Envs = [<<"CONTAINER_NAME=", ContainerName/binary>>|Envs0],
|
||||
build_options(#'DockerCreateOptions'{
|
||||
config = #'DockerContainerConfig'{
|
||||
image = Image,
|
||||
cmd = Cmd,
|
||||
entrypoint = Entrypoint,
|
||||
env = Env,
|
||||
labels = Labels,
|
||||
volumes = Volumes,
|
||||
user = User,
|
||||
working_dir = WorkingDir,
|
||||
hostname = Hostname,
|
||||
exposed_ports = ExposedPorts,
|
||||
healthcheck = Healthcheck
|
||||
},
|
||||
host_config = #'DockerHostConfig'{
|
||||
binds = Binds,
|
||||
network_mode = NetworkMode,
|
||||
restart_policy = RestartPolicy,
|
||||
privileged = Privileged,
|
||||
cap_add = CapAdd,
|
||||
cap_drop = CapDrop,
|
||||
devices = Devices,
|
||||
memory = Memory,
|
||||
memory_reservation = MemoryReservation,
|
||||
nano_cpus = NanoCpus,
|
||||
cpu_shares = CpuShares,
|
||||
ulimits = Ulimits,
|
||||
tmpfs = Tmpfs,
|
||||
sysctls = Sysctls,
|
||||
extra_hosts = ExtraHosts
|
||||
},
|
||||
networking_config = #'DockerNetworkingConfig'{
|
||||
endpoints = Endpoints
|
||||
}
|
||||
}) ->
|
||||
#{
|
||||
<<"Image">> => to_binary(Image),
|
||||
<<"Cmd">> => [to_binary(CommandItem) || CommandItem <- Command],
|
||||
<<"Cmd">> => [to_binary(CommandItem) || CommandItem <- Cmd],
|
||||
<<"Entrypoint">> => [to_binary(EntrypointItem) || EntrypointItem <- Entrypoint],
|
||||
<<"Env">> => Envs,
|
||||
<<"Env">> => [to_binary(EnvItem) || EnvItem <- Env],
|
||||
<<"Labels">> => maps:from_list([{to_binary(Key), to_binary(Value)} || {Key, Value} <- Labels]),
|
||||
<<"Volumes">> => build_volumes(Spec),
|
||||
<<"Volumes">> => build_volumes(Volumes),
|
||||
<<"User">> => to_binary(User),
|
||||
<<"WorkingDir">> => to_binary(WorkingDir),
|
||||
<<"Hostname">> => to_binary(Hostname),
|
||||
<<"ExposedPorts">> => build_expose(Spec),
|
||||
<<"NetworkingConfig">> => build_networks(Spec),
|
||||
<<"Healthcheck">> => build_healthcheck(Spec),
|
||||
<<"ExposedPorts">> => build_expose(ExposedPorts),
|
||||
<<"NetworkingConfig">> => build_networking_config(Endpoints),
|
||||
<<"Healthcheck">> => build_healthcheck(Healthcheck),
|
||||
<<"HostConfig">> => fold_merge([
|
||||
build_network_mode(Spec),
|
||||
build_binds(Spec),
|
||||
build_restart(Spec),
|
||||
build_privileged(Spec),
|
||||
build_cap_add_drop(Spec),
|
||||
build_devices(Spec),
|
||||
build_resources(Spec),
|
||||
build_ulimits(Spec),
|
||||
build_tmpfs(Spec),
|
||||
build_sysctls(Spec),
|
||||
build_extra_hosts(Spec)
|
||||
build_binds(Binds),
|
||||
build_network_mode(NetworkMode),
|
||||
build_restart(RestartPolicy),
|
||||
build_privileged(Privileged),
|
||||
build_cap_add_drop(CapAdd, CapDrop),
|
||||
build_devices(Devices),
|
||||
build_resources(Memory, MemoryReservation, NanoCpus, CpuShares),
|
||||
build_ulimits(Ulimits),
|
||||
build_tmpfs(Tmpfs),
|
||||
build_sysctls(Sysctls),
|
||||
build_extra_hosts(ExtraHosts)
|
||||
])
|
||||
}.
|
||||
|
||||
@ -286,50 +336,50 @@ fold_merge(List) ->
|
||||
lists:foldl(fun maps:merge/2, #{}, List).
|
||||
|
||||
%% --- 构建子字段 ---
|
||||
build_expose(#'ContainerSpec'{expose = Ports}) ->
|
||||
build_expose(Ports) when is_list(Ports) ->
|
||||
case Ports of
|
||||
[] -> #{};
|
||||
_ ->
|
||||
maps:from_list([{normalize_expose_port(P), #{}} || P <- Ports])
|
||||
end.
|
||||
|
||||
build_volumes(#'ContainerSpec'{volumes = Vols}) ->
|
||||
build_volumes(Vols) when is_list(Vols) ->
|
||||
case Vols of
|
||||
[] ->
|
||||
#{};
|
||||
_ ->
|
||||
maps:from_list([{to_binary(Cont), #{}} || #'VolumeBind'{container_path = Cont} <- Vols])
|
||||
maps:from_list([{to_binary(Cont), #{}} || Cont <- Vols])
|
||||
end.
|
||||
|
||||
build_binds(#'ContainerSpec'{volumes = Vols}) ->
|
||||
case Vols of
|
||||
build_binds(Binds) when is_list(Binds) ->
|
||||
case Binds of
|
||||
[] ->
|
||||
#{};
|
||||
_ ->
|
||||
#{<<"Binds">> => [volume_bind(Vol) || Vol <- Vols]}
|
||||
#{<<"Binds">> => [to_binary(Bind) || Bind <- Binds]}
|
||||
end.
|
||||
|
||||
build_networks(#'ContainerSpec'{networks = Nets}) ->
|
||||
case Nets of
|
||||
build_networking_config(Endpoints) when is_list(Endpoints) ->
|
||||
case Endpoints of
|
||||
[] -> #{};
|
||||
_ ->
|
||||
NetCfg = maps:from_list([{to_binary(N), #{}} || N <- Nets]),
|
||||
NetCfg = maps:from_list([{to_binary(Name), #{}} || #'DockerNetworkEndpoint'{name = Name} <- Endpoints]),
|
||||
#{<<"EndpointsConfig">> => NetCfg}
|
||||
end.
|
||||
|
||||
build_network_mode(#'ContainerSpec'{network_mode = <<>>}) ->
|
||||
build_network_mode(<<>>) ->
|
||||
#{};
|
||||
build_network_mode(#'ContainerSpec'{network_mode = NetworkMode}) ->
|
||||
build_network_mode(NetworkMode) ->
|
||||
#{<<"NetworkMode">> => to_binary(NetworkMode)}.
|
||||
|
||||
build_healthcheck(#'ContainerSpec'{healthcheck = undefined}) ->
|
||||
build_healthcheck(undefined) ->
|
||||
#{};
|
||||
build_healthcheck(#'ContainerSpec'{healthcheck = #'Healthcheck'{
|
||||
build_healthcheck(#'Healthcheck'{
|
||||
test = Test,
|
||||
interval_ns = IntervalNs,
|
||||
timeout_ns = TimeoutNs,
|
||||
retries = Retries
|
||||
}}) ->
|
||||
}) ->
|
||||
#{
|
||||
<<"Test">> => [to_binary(Item) || Item <- Test],
|
||||
<<"Interval">> => IntervalNs,
|
||||
@ -337,12 +387,12 @@ build_healthcheck(#'ContainerSpec'{healthcheck = #'Healthcheck'{
|
||||
<<"Retries">> => Retries
|
||||
}.
|
||||
|
||||
build_restart(#'ContainerSpec'{restart = undefined}) ->
|
||||
build_restart(undefined) ->
|
||||
#{};
|
||||
build_restart(#'ContainerSpec'{restart = #'RestartPolicy'{
|
||||
build_restart(#'RestartPolicy'{
|
||||
name = Name,
|
||||
maximum_retry_count = RetryCount
|
||||
}}) ->
|
||||
}) ->
|
||||
RestartPolicy = #{
|
||||
<<"Name">> => to_binary(Name)
|
||||
},
|
||||
@ -355,7 +405,7 @@ build_restart(#'ContainerSpec'{restart = #'RestartPolicy'{
|
||||
}}
|
||||
end.
|
||||
|
||||
build_privileged(#'ContainerSpec'{privileged = Privileged}) ->
|
||||
build_privileged(Privileged) ->
|
||||
case to_bool(Privileged) of
|
||||
true ->
|
||||
#{<<"Privileged">> => true};
|
||||
@ -363,7 +413,7 @@ build_privileged(#'ContainerSpec'{privileged = Privileged}) ->
|
||||
#{}
|
||||
end.
|
||||
|
||||
build_cap_add_drop(#'ContainerSpec'{cap_add = Add, cap_drop = Drop}) ->
|
||||
build_cap_add_drop(Add, Drop) when is_list(Add), is_list(Drop) ->
|
||||
case {Add, Drop} of
|
||||
{[], []} ->
|
||||
#{};
|
||||
@ -374,7 +424,7 @@ build_cap_add_drop(#'ContainerSpec'{cap_add = Add, cap_drop = Drop}) ->
|
||||
}
|
||||
end.
|
||||
|
||||
build_devices(#'ContainerSpec'{devices = Devs}) ->
|
||||
build_devices(Devs) when is_list(Devs) ->
|
||||
case Devs of
|
||||
[] ->
|
||||
#{};
|
||||
@ -391,14 +441,7 @@ build_devices(#'ContainerSpec'{devices = Devs}) ->
|
||||
#{<<"Devices">> => DevObjs}
|
||||
end.
|
||||
|
||||
build_resources(#'ContainerSpec'{resources = undefined}) ->
|
||||
#{};
|
||||
build_resources(#'ContainerSpec'{resources = #'ResourceLimits'{
|
||||
memory_bytes = MemoryBytes,
|
||||
memory_reservation_bytes = ReservationBytes,
|
||||
nano_cpus = NanoCpus,
|
||||
cpu_shares = CpuShares
|
||||
}}) ->
|
||||
build_resources(MemoryBytes, ReservationBytes, NanoCpus, CpuShares) ->
|
||||
HostConfig0 = #{},
|
||||
HostConfig1 = case MemoryBytes of
|
||||
0 ->
|
||||
@ -425,7 +468,7 @@ build_resources(#'ContainerSpec'{resources = #'ResourceLimits'{
|
||||
HostConfig3#{<<"CpuShares">> => CpuShares}
|
||||
end.
|
||||
|
||||
build_ulimits(#'ContainerSpec'{ulimits = Ulimits}) ->
|
||||
build_ulimits(Ulimits) when is_list(Ulimits) ->
|
||||
case Ulimits of
|
||||
[] ->
|
||||
#{};
|
||||
@ -437,7 +480,7 @@ build_ulimits(#'ContainerSpec'{ulimits = Ulimits}) ->
|
||||
} || #'Ulimit'{name = Name, soft = Soft, hard = Hard} <- Ulimits]}
|
||||
end.
|
||||
|
||||
build_sysctls(#'ContainerSpec'{sysctls = Sysctls}) ->
|
||||
build_sysctls(Sysctls) when is_list(Sysctls) ->
|
||||
case Sysctls of
|
||||
[] ->
|
||||
#{};
|
||||
@ -445,16 +488,15 @@ build_sysctls(#'ContainerSpec'{sysctls = Sysctls}) ->
|
||||
#{<<"Sysctls">> => maps:from_list([{to_binary(Key), to_binary(Value)} || {Key, Value} <- Sysctls])}
|
||||
end.
|
||||
|
||||
build_tmpfs(#'ContainerSpec'{tmpfs = Tmpfs}) ->
|
||||
build_tmpfs(Tmpfs) when is_list(Tmpfs) ->
|
||||
case Tmpfs of
|
||||
[] ->
|
||||
#{};
|
||||
_ ->
|
||||
#{<<"Tmpfs">> => maps:from_list([{to_binary(Path), to_binary(Options)} ||
|
||||
#'TmpfsMount'{path = Path, options = Options} <- Tmpfs])}
|
||||
#{<<"Tmpfs">> => maps:from_list([{to_binary(Path), to_binary(Options)} || {Path, Options} <- Tmpfs])}
|
||||
end.
|
||||
|
||||
build_extra_hosts(#'ContainerSpec'{extra_hosts = Hosts}) ->
|
||||
build_extra_hosts(Hosts) when is_list(Hosts) ->
|
||||
case Hosts of
|
||||
[] ->
|
||||
#{};
|
||||
@ -467,7 +509,7 @@ display_options(Options) when is_map(Options) ->
|
||||
logger:debug("deploy options: ~p", [jiffy:encode(Options, [force_utf8])]),
|
||||
lists:foreach(fun({K, V}) -> logger:debug("~p => ~p", [K, V]) end, maps:to_list(Options)).
|
||||
|
||||
normalize_expose_port(#'PortExpose'{container_port = Port, protocol = Protocol}) ->
|
||||
normalize_expose_port(#'DockerExposedPort'{container_port = Port, protocol = Protocol}) ->
|
||||
PortBin = integer_to_binary(Port),
|
||||
ProtocolBin = to_binary(Protocol),
|
||||
case ProtocolBin of
|
||||
@ -484,15 +526,14 @@ device_permissions(<<>>) ->
|
||||
device_permissions(Permissions) ->
|
||||
to_binary(Permissions).
|
||||
|
||||
volume_bind(#'VolumeBind'{host_path = HostPath, container_path = ContainerPath, read_only = ReadOnly}) ->
|
||||
HostPathBin = to_binary(HostPath),
|
||||
ContainerPathBin = to_binary(ContainerPath),
|
||||
case to_bool(ReadOnly) of
|
||||
true ->
|
||||
<<HostPathBin/binary, ":", ContainerPathBin/binary, ":ro">>;
|
||||
false ->
|
||||
<<HostPathBin/binary, ":", ContainerPathBin/binary>>
|
||||
end.
|
||||
add_unique_front([], List) ->
|
||||
List;
|
||||
add_unique_front([Item | Rest], List) ->
|
||||
NList = case lists:member(Item, List) of
|
||||
true -> List;
|
||||
false -> [Item | List]
|
||||
end,
|
||||
add_unique_front(Rest, NList).
|
||||
|
||||
build_stop_container_url(ContainerName, 0) ->
|
||||
lists:flatten(io_lib:format("/containers/~s/stop", [binary_to_list(ContainerName)]));
|
||||
|
||||
@ -45,7 +45,7 @@ start_monitor(TaskId, ContainerDir, Params)
|
||||
-spec deploy(TaskId :: integer(), ContainerDir :: string(), Params :: message_pb:'ContainerDeployParams'()) -> no_return().
|
||||
deploy(TaskId, ContainerDir, Params = #'ContainerDeployParams'{
|
||||
container_name = ContainerName,
|
||||
spec = #'ContainerSpec'{image = Image0}
|
||||
create = #'DockerCreateOptions'{config = #'DockerContainerConfig'{image = Image0}}
|
||||
}) when is_integer(TaskId), is_list(ContainerDir) ->
|
||||
report_task_event(TaskId, <<"info">>, <<"开始部署容器:"/utf8, ContainerName/binary>>),
|
||||
try
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -27,75 +27,84 @@ test_commands() ->
|
||||
test_create_container() ->
|
||||
Params = #'ContainerDeployParams'{
|
||||
container_name = <<"my_nginx_new1">>,
|
||||
spec = #'ContainerSpec'{
|
||||
image = <<"docker.1ms.run/library/nginx:latest">>,
|
||||
command = [
|
||||
<<"nginx">>,
|
||||
<<"-g">>,
|
||||
<<"daemon off;">>
|
||||
],
|
||||
entrypoint = [
|
||||
<<"/docker-entrypoint.sh">>
|
||||
],
|
||||
env = [
|
||||
<<"ENV1=val1">>,
|
||||
<<"ENV2=val2">>
|
||||
],
|
||||
expose = [
|
||||
#'PortExpose'{container_port = 80, protocol = <<"tcp">>},
|
||||
#'PortExpose'{container_port = 443, protocol = <<"tcp">>}
|
||||
],
|
||||
volumes = [
|
||||
#'VolumeBind'{host_path = <<"/host/data">>, container_path = <<"/data">>},
|
||||
#'VolumeBind'{host_path = <<"/host/log">>, container_path = <<"/var/log">>}
|
||||
],
|
||||
networks = [
|
||||
<<"mynet">>
|
||||
],
|
||||
labels = [
|
||||
{<<"role">>, <<"web">>},
|
||||
{<<"env">>, <<"prod">>}
|
||||
],
|
||||
restart = #'RestartPolicy'{name = <<"always">>},
|
||||
user = <<"www-data">>,
|
||||
working_dir = <<"/app">>,
|
||||
hostname = <<"myhost">>,
|
||||
privileged = true,
|
||||
cap_add = [
|
||||
<<"NET_ADMIN">>
|
||||
],
|
||||
cap_drop = [
|
||||
<<"MKNOD">>
|
||||
],
|
||||
devices = [
|
||||
#'DeviceMapping'{host_path = <<"/dev/snd">>, container_path = <<"/dev/snd">>}
|
||||
],
|
||||
resources = #'ResourceLimits'{
|
||||
memory_bytes = 512 * 1024 * 1024,
|
||||
memory_reservation_bytes = 256 * 1024 * 1024,
|
||||
cpu_shares = 512,
|
||||
nano_cpus = 1500000000
|
||||
create = #'DockerCreateOptions'{
|
||||
config = #'DockerContainerConfig'{
|
||||
image = <<"docker.1ms.run/library/nginx:latest">>,
|
||||
cmd = [
|
||||
<<"nginx">>,
|
||||
<<"-g">>,
|
||||
<<"daemon off;">>
|
||||
],
|
||||
entrypoint = [
|
||||
<<"/docker-entrypoint.sh">>
|
||||
],
|
||||
env = [
|
||||
<<"ENV1=val1">>,
|
||||
<<"ENV2=val2">>
|
||||
],
|
||||
volumes = [
|
||||
<<"/data">>,
|
||||
<<"/var/log">>
|
||||
],
|
||||
labels = [
|
||||
{<<"role">>, <<"web">>},
|
||||
{<<"env">>, <<"prod">>}
|
||||
],
|
||||
user = <<"www-data">>,
|
||||
working_dir = <<"/app">>,
|
||||
hostname = <<"myhost">>,
|
||||
exposed_ports = [
|
||||
#'DockerExposedPort'{container_port = 80, protocol = <<"tcp">>},
|
||||
#'DockerExposedPort'{container_port = 443, protocol = <<"tcp">>}
|
||||
],
|
||||
healthcheck = #'Healthcheck'{
|
||||
test = [
|
||||
<<"CMD-SHELL">>,
|
||||
<<"curl -f http://localhost || exit 1">>
|
||||
],
|
||||
interval_ns = 30000000000,
|
||||
timeout_ns = 10000000000,
|
||||
retries = 3
|
||||
}
|
||||
},
|
||||
ulimits = [
|
||||
#'Ulimit'{name = <<"nofile">>, soft = 1024, hard = 2048}
|
||||
],
|
||||
sysctls = [
|
||||
{<<"net.ipv4.ip_forward">>, <<"1">>}
|
||||
],
|
||||
tmpfs = [
|
||||
#'TmpfsMount'{path = <<"/tmp">>}
|
||||
],
|
||||
extra_hosts = [
|
||||
<<"host1:192.168.0.1">>
|
||||
],
|
||||
healthcheck = #'Healthcheck'{
|
||||
test = [
|
||||
<<"CMD-SHELL">>,
|
||||
<<"curl -f http://localhost || exit 1">>
|
||||
],
|
||||
interval_ns = 30000000000,
|
||||
timeout_ns = 10000000000,
|
||||
retries = 3
|
||||
host_config = #'DockerHostConfig'{
|
||||
binds = [
|
||||
<<"/host/data:/data">>,
|
||||
<<"/host/log:/var/log">>
|
||||
],
|
||||
network_mode = <<"bridge">>,
|
||||
restart_policy = #'RestartPolicy'{name = <<"always">>},
|
||||
privileged = true,
|
||||
cap_add = [
|
||||
<<"NET_ADMIN">>
|
||||
],
|
||||
cap_drop = [
|
||||
<<"MKNOD">>
|
||||
],
|
||||
devices = [
|
||||
#'DeviceMapping'{host_path = <<"/dev/snd">>, container_path = <<"/dev/snd">>}
|
||||
],
|
||||
memory = 512 * 1024 * 1024,
|
||||
memory_reservation = 256 * 1024 * 1024,
|
||||
cpu_shares = 512,
|
||||
nano_cpus = 1500000000,
|
||||
ulimits = [
|
||||
#'Ulimit'{name = <<"nofile">>, soft = 1024, hard = 2048}
|
||||
],
|
||||
sysctls = [
|
||||
{<<"net.ipv4.ip_forward">>, <<"1">>}
|
||||
],
|
||||
tmpfs = [
|
||||
{<<"/tmp">>, <<>>}
|
||||
],
|
||||
extra_hosts = [
|
||||
<<"host1:192.168.0.1">>
|
||||
]
|
||||
},
|
||||
networking_config = #'DockerNetworkingConfig'{
|
||||
endpoints = [
|
||||
#'DockerNetworkEndpoint'{name = <<"mynet">>}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user