Skip to content
This repository has been archived by the owner on Aug 25, 2021. It is now read-only.

Dockerize Medea #35

Merged
merged 10 commits into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
*

# Medea sources and manifest.
!Cargo.toml
!Cargo.lock
!src/

# Medea uses this sub-crates.
!crates/
!proto/

# We don't need Jason while building Medea, but since it's in Medea's workspace,
# Cargo should be able to see these files.
!jason/Cargo.toml
!jason/src/lib.rs

# Medea dependencies cache.
!.cache/cargo/

# !target/{mode} is added and removed dynamically to reduce image build times.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ indent_size = 4
[Dockerfile]
indent_style = space
indent_size = 4

[hooks/*]
indent_style = space
indent_size = 2
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RUST_LOG=debug
MEDEA_CONF=config.toml

COMPOSE_PROJECT_NAME=medea
COMPOSE_IMAGE_NAME=instrumentisto/medea
994 changes: 507 additions & 487 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ macro-attr = "0.2"
medea-client-api-proto = { path = "proto/client-api", features = ["medea"] }
medea-macro = { path = "crates/medea-macro" }
newtype_derive = "0.1"
rand = "0.6"
rand = "0.7"
redis = "0.10"
rust-crypto = "0.2"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
slog = "2.4"
slog = "2.5"
slog-async = "2.3"
slog-envlogger = "2.1"
slog-json = "2.3"
Expand Down
49 changes: 49 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#
# Stage 'dist' creates project distribution.
#

# https://hub.docker.com/_/rust
ARG rust_ver=latest
FROM rust:${rust_ver} AS dist
ARG rustc_mode=release
ARG rustc_opts=--release
ARG cargo_home=/usr/local/cargo

# Create user and group files, which will be used in a running container to
# run the process as an unprivileged user.
RUN mkdir -p /out/etc/ \
&& echo 'nobody:x:65534:65534:nobody:/:' > /out/etc/passwd \
&& echo 'nobody:x:65534:' > /out/etc/group

COPY / /app/

# Build project distribution.
RUN cd /app \
# Compile project.
&& CARGO_HOME="${cargo_home}" \
# TODO: use --out-dir once stabilized
# TODO: https://github.com/rust-lang/cargo/issues/6790
cargo build --bin=medea ${rustc_opts} \
# Prepare the binary and all dependent dynamic libraries.
&& cp /app/target/${rustc_mode}/medea /out/medea \
&& ldd /out/medea \
| awk 'BEGIN{ORS=" "}$1~/^\//{print $1}$3~/^\//{print $3}' \
| sed 's/,$/\n/' \
| tr ' ' "\n" \
| xargs -I '{}' cp -fL --parents '{}' /out/




#
# Stage 'runtime' creates final Docker image to use in runtime.
#

# https://hub.docker.com/_/scratch
FROM scratch AS runtime

COPY --from=dist /out/ /

USER nobody:nobody

ENTRYPOINT ["/medea"]
60 changes: 59 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,24 @@ eq = $(if $(or $(1),$(2)),$(and $(findstring $(1),$(2)),\



######################
# Project parameters #
######################

IMAGE_NAME := $(strip $(shell grep 'COMPOSE_IMAGE_NAME=' .env | cut -d '=' -f2))

RUST_VER := 1.36




###########
# Aliases #
###########

build: docker.build


# Resolve all project dependencies.
#
# Usage:
Expand Down Expand Up @@ -154,6 +168,49 @@ endif



###################
# Docker commands #
###################

# Build medea project Docker image.
#
# Usage:
# make docker.build [TAG=(dev|<tag>)]
# [debug=(yes|no)] [no-cache=(no|yes)]

docker-build-image-name = $(IMAGE_NAME)

docker.build:
ifneq ($(no-cache),yes)
docker run --rm --network=host -v "$(PWD)":/app -w /app \
-u $(shell id -u):$(shell id -g) \
-e CARGO_HOME=.cache/cargo \
rust:$(RUST_VER) \
cargo build --bin=medea \
$(if $(call eq,$(debug),no),--release,)
endif
$(call docker.build.clean.ignore)
@echo "!target/$(if $(call eq,$(debug),no),release,debug)/" >> .dockerignore
docker build --network=host --force-rm \
$(if $(call eq,$(no-cache),yes),\
--no-cache --pull,) \
$(if $(call eq,$(IMAGE),),\
--build-arg rust_ver=$(RUST_VER) \
--build-arg rustc_mode=$(if \
$(call eq,$(debug),no),release,debug) \
--build-arg rustc_opts=$(if \
$(call eq,$(debug),no),--release,) \
--build-arg cargo_home=.cache/cargo,) \
-t $(docker-build-image-name):$(if $(call eq,$(TAG),),dev,$(TAG)) .
$(call docker.build.clean.ignore)
define docker.build.clean.ignore
@sed -i $(if $(call eq,$(shell uname -s),Darwin),'',) \
/^!target\/d .dockerignore
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've refactored this to support macOS.

endef




####################
# Running commands #
####################
Expand Down Expand Up @@ -191,7 +248,8 @@ up.medea:
# .PHONY section #
##################

.PHONY: cargo cargo.fmt cargo.lint \
.PHONY: build cargo cargo.fmt cargo.lint \
docker.build \
docs docs.rust \
test test.unit \
up up.coturn up.jason up.medea \
Expand Down
12 changes: 12 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,15 @@
#
# Default:
# connection_timeout = "5s"




[log]
# Maximum allowed level of application log entries.
#
# Possible values:
# "OFF", "CRITICAL", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"
#
# Default:
# level = "INFO"
24 changes: 24 additions & 0 deletions hooks/post_push
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash
# Docker Hub post-push hook that is responsible to tag built image properly.

set -e

# Parse image name and tag.
tagStart=$(expr index "$IMAGE_NAME" :)
repoName=${IMAGE_NAME:0:tagStart-1}
origTag=${IMAGE_NAME:tagStart}

# For full-versioned tag provide minor/major versions and 'latest' tags.
if [[ "$origTag" == *"."*"."* ]]; then
dot=$(expr index "$origTag" .)
majorVer=${origTag:0:dot-1}

rest=${origTag:dot}
dot=$(expr index "$rest" .)
minorVer="$majorVer.${rest:0:dot-1}"

for tag in {"$minorVer","$majorVer",latest}; do
docker tag $IMAGE_NAME ${repoName}:${tag}
docker push ${repoName}:${tag}
done
fi
16 changes: 5 additions & 11 deletions src/api/client/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,7 @@ mod test {
use futures::{future::IntoFuture as _, sink::Sink as _, Stream as _};

use crate::{
api::control::Member,
conf::{Conf, Server, Turn},
media::create_peers,
signalling::Room,
api::control::Member, media::create_peers, signalling::Room,
turn::new_turn_auth_service_mock,
};

Expand Down Expand Up @@ -174,13 +171,10 @@ mod test {

#[test]
fn ping_pong_and_disconnects_on_idle() {
let conf = Conf {
rpc: Rpc {
idle_timeout: Duration::new(2, 0),
reconnect_timeout: Default::default(),
},
turn: Turn::default(),
server: Server::default(),
let mut conf = Conf::default();
conf.rpc = Rpc {
idle_timeout: Duration::new(2, 0),
reconnect_timeout: Default::default(),
};

let mut server = ws_server(conf.clone());
Expand Down
22 changes: 22 additions & 0 deletions src/conf/log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//! Logging settings.
use std::str::FromStr as _;

use serde::{Deserialize, Serialize};
use smart_default::SmartDefault;

/// Logging settings.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize, SmartDefault)]
#[serde(default)]
pub struct Log {
/// Maximum allowed level of application log entries.
/// Defaults to `INFO`.
#[default(String::from("INFO"))]
pub level: String,
}

impl Log {
/// Returns configured application logging level. `None` if disabled.
pub fn level(&self) -> Option<slog::Level> {
slog::Level::from_str(&self.level).ok()
}
}
26 changes: 24 additions & 2 deletions src/conf/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Provides application configuration options.

pub mod log;
pub mod rpc;
pub mod server;
pub mod turn;
Expand All @@ -11,6 +12,7 @@ use failure::Error;
use serde::{Deserialize, Serialize};

pub use self::{
log::Log,
rpc::Rpc,
server::Server,
turn::{Redis, Turn},
Expand All @@ -33,6 +35,8 @@ pub struct Conf {
pub server: Server,
/// TURN server settings.
pub turn: Turn,
/// Logging settings.
pub log: Log,
}

impl Conf {
Expand Down Expand Up @@ -196,7 +200,7 @@ mod tests {

#[test]
#[serial]
fn redis_conf_test() {
fn redis_conf() {
let default_conf = Conf::default();

env::set_var("MEDEA_TURN.DB.REDIS.IP", "5.5.5.5");
Expand Down Expand Up @@ -225,7 +229,7 @@ mod tests {

#[test]
#[serial]
fn turn_conf_test() {
fn turn_conf() {
let default_conf = Conf::default();

env::set_var("MEDEA_TURN.IP", "5.5.5.5");
Expand All @@ -240,4 +244,22 @@ mod tests {
assert_eq!(env_conf.turn.port, 1234);
assert_eq!(env_conf.turn.addr(), "5.5.5.5:1234".parse().unwrap());
}

#[test]
#[serial]
fn log_conf() {
let default_conf = Conf::default();

env::set_var("MEDEA_LOG.LEVEL", "WARN");

let env_conf = Conf::parse().unwrap();

assert_ne!(default_conf.log.level(), env_conf.log.level());

assert_eq!(env_conf.log.level(), Some(slog::Level::Warning));

env::set_var("MEDEA_LOG.LEVEL", "OFF");

assert_eq!(Conf::parse().unwrap().log.level(), None);
}
}
2 changes: 1 addition & 1 deletion src/conf/rpc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! RPC connection settings.
use serde::{Deserialize, Serialize};
use smart_default::*;
use smart_default::SmartDefault;

use std::time::Duration;

Expand Down
2 changes: 1 addition & 1 deletion src/conf/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::net::{IpAddr, Ipv4Addr, SocketAddr, ToSocketAddrs as _};

use serde::{Deserialize, Serialize};
use smart_default::*;
use smart_default::SmartDefault;

/// HTTP server settings.
#[derive(Clone, Debug, Deserialize, Serialize, SmartDefault)]
Expand Down
2 changes: 1 addition & 1 deletion src/conf/turn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
};

use serde::{Deserialize, Serialize};
use smart_default::*;
use smart_default::SmartDefault;

/// STUN/TURN server settings.
#[derive(Clone, Debug, Deserialize, Serialize, SmartDefault)]
Expand Down
Loading