Skip to content

Commit

Permalink
Merge pull request #3 from grycap/links
Browse files Browse the repository at this point in the history
properly handling links and including the usecases in the documentation
  • Loading branch information
dealfonso authored Dec 12, 2017
2 parents 25baa0f + f9f73cb commit 238b4b9
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 102 deletions.
162 changes: 75 additions & 87 deletions README.md

Large diffs are not rendered by default.

96 changes: 81 additions & 15 deletions minicon
Original file line number Diff line number Diff line change
Expand Up @@ -216,23 +216,22 @@ function copy() {
# copies one file (or folder) to the same destination in the root filesystem
# - it does not overwrite contents
local SRC="$1"
local FILE="$2"

if [ "$SRC" == "." -o "$SRC" == ".." ]; then
p_warning "cowardly refusing to copy folder $SRC"
return
fi

local DST
if [ "$VERBOSE" == "true" ]; then
p_info " processing $SRC... "
fi
if [ ! -e "$SRC" ]; then
p_error 1 "failed to read $SRC"
fi
if [ "$FILE" != "" ]; then
mkdir -p "$(dirname "$ROOTFS/$FILE")"
fi
local SRCDIR="$(dirname "$SRC")"
DST="$ROOTFS/$SRCDIR"
mkdir -p "$DST"
if [ "$FILE" != "" ]; then
echo "$SRCDIR" >> "$ROOTFS/$FILE"
fi
p_debug " copying $SRC to $DST"
if [ -d "$SRC" ]; then
cp -n -r "$SRC" "$DST"
Expand All @@ -241,6 +240,19 @@ function copy() {
fi
}

# Credits fot this function go to https://stackoverflow.com/a/18898782
# Return relative path from canonical absolute dir path $1 to canonical
# absolute dir path $2 ($1 and/or $2 may end with one or no "/").
# Does only need POSIX shell builtins (no external command)
function relPath () {
local common path up
common=${1%/} path=${2%/}/
while test "${path#"$common"/}" = "$path"; do
common=${common%/*} up=../$up
done
path=$up${path#"$common"/}; path=${path%/}; printf %s "${path:-.}"
}

function is_plugin_active() {
# Checks whether a plugin is activated or not
if [[ "$PLUGINS_ACTIVATED" =~ (^|,)$1(|:[^,]+)(,|$) ]]; then
Expand Down Expand Up @@ -271,7 +283,41 @@ function plugin_parameter() {
return 1
}

function PLUGIN_00_folder() {
function PLUGIN_00_link() {
# If the path is a link to other path, we will create the link and analyze the real path
local L_PATH="$1"

if [ -h "$L_PATH" ]; then
local L_DST="$ROOTFS/$(dirname "$L_PATH")"
local R_PATH="$(readlink -f "$L_PATH")"
local R_DST="$ROOTFS/$(dirname "$R_PATH")"
mkdir -p "$L_DST"

if [ ! -e "$L_DST/$(basename $L_PATH)" ]; then
local REL_PATH="$(relPath "$L_DST" "$R_DST")"
p_debug "$L_PATH is a link to $REL_PATH/$(basename $R_PATH)"
ln -s $REL_PATH/$(basename $R_PATH) $L_DST/$(basename $L_PATH)
fi

add_command "$R_PATH"
return 1
fi
}

function PLUGIN_01_which() {
# This plugin tries to guess whether the command to analize is in the path or not.
# If the command can be obtained calling which, we'll analyze the actual command and not the short name.
local S_PATH="$1"
local W_PATH="$(which $S_PATH)"

if [ "$W_PATH" != "" -a "$W_PATH" != "$S_PATH" ]; then
p_debug "$1 is $W_PATH"
add_command "$W_PATH"
return 1
fi
}

function PLUGIN_02_folder() {
# If it is a folder, just copy it to its location in the new FS
local S_PATH="$1"

Expand All @@ -289,6 +335,7 @@ function PLUGIN_09_ldd() {
local S_PATH="$1"
local LIBS= LIB=
local COMMAND="$(which -- $S_PATH)"
local LIB_DIR=
if [ "$COMMAND" == "" ]; then
COMMAND="$S_PATH"
fi
Expand All @@ -304,7 +351,12 @@ function PLUGIN_09_ldd() {
if [ $? -eq 0 ]; then
LIBS="$(ldd "$COMMAND" | grep -v 'linux-vdso' | grep -v 'statically' | sed 's/^[ \t]*//g' | sed 's/^.* => //g' | sed 's/(.*)//' | sed '/^[ ]*$/d')"
for LIB in $LIBS; do
copy "$LIB" "$LDCONFIGGILE"
# Here we build the ld config file to add the new paths where the libraries are located
if [ "$LDCONFIGFILE" != "" ]; then
LIB_DIR="$(dirname "$LIB")"
mkdir -p "$ROOTFS/$(dirname $LDCONFIGFILE)"
echo "$LIB_DIR" >> "$ROOTFS/$LDCONFIGFILE"
fi
add_command "$LIB"
done
fi
Expand Down Expand Up @@ -465,7 +517,8 @@ function PLUGIN_11_scripts() {
# If it is, adds the interpreter to the list of commands to add to the container
p_debug "trying to guess if $1 is a interpreted script"

local S_PATH="$(which -- $1)"
local S_PATH="$(which $1)"
local ADD_PATHS=

if [ "$S_PATH" == "" ]; then
p_warning "$1 cannot be executed (if it should, please check the path)"
Expand All @@ -477,16 +530,26 @@ function PLUGIN_11_scripts() {
FILE_RES="${FILE_RES,,}"
local SHELL_EXEC=
case "$FILE_RES" in
python) SHELL_EXEC=$(which python);;
python) SHELL_EXEC=$(which python)
ADD_PATHS="$(python -c 'import sys;print "\n".join(sys.path)' | grep -v '/home')";;
"bourne-again shell") SHELL_EXEC=$(which bash);;
"a /usr/bin/perl") SHELL_EXEC=$(which perl);;
"a /usr/bin/perl") SHELL_EXEC=$(which perl)
ADD_PATHS="$(perl -e "print qq(@INC)" | tr ' ' '\n' | grep -v '/home')";;
esac

# If we found a known interpreter, we'll add to the list of dependencies
if [ "$SHELL_EXEC" != "" ]; then
p_debug "found that $S_PATH needs $SHELL_EXEC"
add_command "$SHELL_EXEC"
fi
# The include folders
if [ "$ADD_PATHS" != "" ]; then
p_debug "found that $S_PATH needs $ADD_PATHS"
local P
while read P; do
add_command "$P"
done <<< "$ADD_PATHS"
fi
return 0
}

Expand All @@ -504,7 +567,7 @@ function plugin_list() {
}

# Now we are activating the basic plugins
PLUGINS_ACTIVATED=folder,ldd
PLUGINS_ACTIVATED=link,which,folder,ldd
COMMANDS_TO_ADD=()
FORCEFOLDER=false
ROOTFS=
Expand Down Expand Up @@ -533,7 +596,7 @@ while [ $n -lt ${#ARR[@]} ]; do
--quiet|-q) QUIET=true;;
--tarfile|-t) n=$(($n+1))
TARFILE="${ARR[$n]}";;
--ldconfig|-l) LDCONFIGGILE=/etc/ld.so.conf;;
--ldconfig|-l) LDCONFIGFILE=/etc/ld.so.conf;;
--verbose|-v) VERBOSE=true;;
--debug) DEBUG=true;;
--force|-f) FORCEFOLDER=true;;
Expand Down Expand Up @@ -606,8 +669,11 @@ while [ $i_current -lt ${#COMMANDS_TO_ADD[@]} ]; do
i_current=$(($i_current+1))
done

if [ "$LDCONFIGGILE" != "" -a -e "$ROOTFS/$LDCONFIGGILE" ]; then
if [ "$LDCONFIGFILE" != "" -a -e "$ROOTFS/$LDCONFIGFILE" ]; then
p_debug "creating ldconfig"
TMPFILE=$(tempfile)
awk '!a[$0]++' "$ROOTFS/$LDCONFIGFILE" > "$TMPFILE"
mv "$TMPFILE" "$ROOTFS/$LDCONFIGFILE"
ldconfig -r "$ROOTFS"
fi

Expand Down
31 changes: 31 additions & 0 deletions usecases/uc1/uc1.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash
#
# minicon - Minimization of filesystems for containers
# https://github.com/grycap/minicon
#
# Copyright (C) GRyCAP - I3M - UPV
# Developed by Carlos A. caralla@upv.es
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

UC=1
UCFOLDER=usecases/uc${UC}
CURDIR=$(dirname $0)
MINICONDIR=$(readlink -f $CURDIR/../..)

set -x
docker images ubuntu:latest
docker run --rm -it -v $MINICONDIR:/tmp/minicon ubuntu:latest /tmp/minicon/minicon -t /tmp/minicon/$UCFOLDER/uc${UC}.tar bash ls mkdir less cat find
docker import $MINICONDIR/$UCFOLDER/uc${UC}.tar minicon:uc${UC}
docker images minicon:uc${UC}
2 changes: 2 additions & 0 deletions usecases/uc2/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM ubuntu
RUN apt-get update && apt-get install -y ssh iproute2 iputils-ping wget
42 changes: 42 additions & 0 deletions usecases/uc2/uc2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash
#
# minicon - Minimization of filesystems for containers
# https://github.com/grycap/minicon
#
# Copyright (C) GRyCAP - I3M - UPV
# Developed by Carlos A. caralla@upv.es
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

UC=2
UCFOLDER=usecases/uc${UC}
REFIMAGE=minicon:uc${UC}fat
MINICONIMAGE=minicon:uc${UC}

CURDIR=$(dirname $0)
MINICONDIR=$(readlink -f $CURDIR/../..)

docker build $UCFOLDER -t $REFIMAGE

cat > $UCFOLDER/execfile-cmd <<\EOF
ssh localhost
/usr/bin/ssh localhost
/bin/ping -c 1 www.google.es
EOF

set -x
docker images $REFIMAGE
docker run --privileged --rm -it -v $MINICONDIR:/tmp/minicon $REFIMAGE bash -c "apt-get install -y strace && /tmp/minicon/minicon -t /tmp/minicon/$UCFOLDER/uc${UC}.tar -l --plugin=strace:execfile=/tmp/minicon/$UCFOLDER/execfile-cmd bash ssh ip id cat ls mkdir ping wget"
docker import $MINICONDIR/$UCFOLDER/uc${UC}.tar $MINICONIMAGE
docker images $MINICONIMAGE
14 changes: 14 additions & 0 deletions usecases/uc3/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM node:latest

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
COPY miniapp/package.json /usr/src/app/
RUN npm install

# Bundle app source
COPY miniapp /usr/src/app

EXPOSE 3000
36 changes: 36 additions & 0 deletions usecases/uc3/uc3.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash
#
# minicon - Minimization of filesystems for containers
# https://github.com/grycap/minicon
#
# Copyright (C) GRyCAP - I3M - UPV
# Developed by Carlos A. caralla@upv.es
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

UC=3
UCFOLDER=usecases/uc${UC}
REFIMAGE=minicon:uc${UC}fat
MINICONIMAGE=minicon:uc${UC}

CURDIR=$(dirname $0)
MINICONDIR=$(readlink -f $CURDIR/../..)

set -x
express -f ./usecases/uc3/miniapp
docker build $UCFOLDER -t $REFIMAGE
docker images $REFIMAGE
docker run --rm -it -v $MINICONDIR:/tmp/minicon $REFIMAGE /tmp/minicon/minicon -l -t /tmp/minicon/$UCFOLDER/uc${UC}.tar node /usr/src/app
docker import $MINICONDIR/$UCFOLDER/uc${UC}.tar $MINICONIMAGE
docker images $MINICONIMAGE
2 changes: 2 additions & 0 deletions usecases/uc4/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM ubuntu
RUN apt-get update && apt-get install -y ffmpeg
35 changes: 35 additions & 0 deletions usecases/uc4/uc4.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash
#
# minicon - Minimization of filesystems for containers
# https://github.com/grycap/minicon
#
# Copyright (C) GRyCAP - I3M - UPV
# Developed by Carlos A. caralla@upv.es
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

UC=4
UCFOLDER=usecases/uc${UC}
REFIMAGE=minicon:uc${UC}fat
MINICONIMAGE=minicon:uc${UC}

CURDIR=$(dirname $0)
MINICONDIR=$(readlink -f $CURDIR/../..)

set -x
docker build $UCFOLDER -t $REFIMAGE
docker images $REFIMAGE
docker run --rm -it -v $MINICONDIR:/tmp/minicon $REFIMAGE /tmp/minicon/minicon -l -t /tmp/minicon/$UCFOLDER/uc${UC}.tar ffmpeg
docker import $MINICONDIR/$UCFOLDER/uc${UC}.tar $MINICONIMAGE
docker images $MINICONIMAGE

0 comments on commit 238b4b9

Please sign in to comment.