Séparation méthodes en modules, début docstring

This commit is contained in:
wpetit 2017-01-18 17:43:46 +01:00
parent cc5df7cfcd
commit 86216e639e
7 changed files with 302 additions and 198 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
packages packages
*.log *.log
*~ *~
__pycache__

28
lib/system.py Normal file
View File

@ -0,0 +1,28 @@
import tarfile, os
def extract_tar(file_path, dest_dir = "."):
print('Extracting "{:s}" to "{:s}"'.format(file_path, dest_dir))
with tarfile.open(file_path) as tar:
tar.extractall(dest_dir)
tar.close()
def which(program, additional_paths = None):
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
paths = os.environ["PATH"].split(os.pathsep);
if additional_paths != None:
paths.append(additional_paths)
for path in paths:
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None

54
lib/tamarin.py Normal file
View File

@ -0,0 +1,54 @@
import os, glob, subprocess
import web, system
def get_workspace_dir():
"""Return the absolute path to the tamarin workspace ($HOME/.tamarin)"""
home = os.environ["HOME"]
return os.path.join(os.sep, home, '.tamarin')
def get_workspace_subdir(subdir):
"""Return the absolute path to a subdirectory in tamarin workspace"""
dir_path = os.path.join(os.sep, get_workspace_dir(), subdir)
os.makedirs(dir_path, exist_ok=True)
return dir_path
def get_acbuild_achive_dest_dir():
"""Return the first path matching the acbuild archive extraction destination in tamarin workspace"""
workspace_tmp = get_workspace_subdir('tmp')
return glob.glob(os.path.join(os.sep, workspace_tmp, 'acbuild-v*'))[0]
def get_rkt_achive_dest_dir():
"""Return the first path matching the rkt archive extraction destination in tamarin workspace"""
workspace_tmp = get_workspace_subdir('tmp')
return glob.glob(os.path.join(os.sep, workspace_tmp, 'rkt-v*'))[0]
def get_acbuild_workspace_dir():
"""Return the current path (linked to the process pid) to the acbuild workspace"""
return get_workspace_subdir("tmp/build_{:d}".format(os.getpid()))
def download_rkt():
"""Download a local copy of rkt in the tamarin workspace and return the absolute path to the archive"""
url = "https://github.com/coreos/rkt/releases/download/v1.22.0/rkt-v1.22.0.tar.gz"
file_path=os.path.join(os.sep, get_workspace_subdir('tmp'), "rkt.tar.gz")
web.download_file(file_url=url, dest_path=file_path)
return file_path
def download_acbuild():
"""Download a local copy of acbuild in the tamarin workspace and return the absolute path to the archive"""
url = "https://github.com/containers/build/releases/download/v0.4.0/acbuild-v0.4.0.tar.gz"
file_path=os.path.join(os.sep, get_workspace_subdir('tmp'), "acbuild.tar.gz")
web.download_file(file_url=url, dest_path=file_path)
return file_path
def run_rkt(args):
"""Run rkt with the specified args (use the local copy if rkt is not found in the $PATH)"""
rkt_bin = system.which('rkt', get_workspace_subdir('rkt'))
cmd = ( ["sudo", "-E", rkt_bin] if os.geteuid() != 0 else [rkt_bin] ) + args
print(" ".join(cmd))
return subprocess.call(cmd, stdin=subprocess.PIPE)
def run_acbuild(args):
"""Run acbuild with the specified args (use the local copy if acbuild is not found in the $PATH)"""
acbuild_bin = system.which('acbuild', get_workspace_subdir('acbuild'))
print(" ".join([acbuild_bin] + args))
return subprocess.call([acbuild_bin] + args, stdin=subprocess.PIPE)

View File

@ -1,8 +1,5 @@
from urllib import request from urllib import request
from os import environ import math, sys
import sys
import math
import tarfile
def print_progress_bar(percent_progress=0, char_size=50, clear_line=True): def print_progress_bar(percent_progress=0, char_size=50, clear_line=True):
@ -34,25 +31,3 @@ def download_file(file_url, dest_path, bulk_size = 8192):
dest_file.close() dest_file.close()
# Add linebreak # Add linebreak
print("") print("")
def extract_tar(file_path, dest_dir = "."):
print('Extracting "{:s}" to "{:s}"'.format(file_path, dest_dir))
with tarfile.open(file_path) as tar:
tar.extractall(dest_dir)
tar.close()
def download_rkt():
url = "https://github.com/coreos/rkt/releases/download/v1.22.0/rkt-v1.22.0.tar.gz"
file_path="rkt.tar.gz"
download_file(file_url=url, dest_path=file_path)
extract_tar(file_path)
def download_acbuild():
url = "https://github.com/containers/build/releases/download/v0.4.0/acbuild-v0.4.0.tar.gz"
file_path="acbuild.tar.gz"
download_file(file_url=url, dest_path=file_path)
extract_tar(file_path)
if __name__ == "__main__":
download_rkt()
download_acbuild()

208
package
View File

@ -1,182 +1,46 @@
#!/usr/bin/env bash #!/usr/bin/env python3
set -e if __name__ == "__main__":
TAMARIN_VERSION=0.0.1 import sys, os, shutil
TAMARIN_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
BASE_DIR="$TAMARIN_DIR" source "$TAMARIN_DIR/lib/util.sh" sys.path.append(os.path.dirname(__file__) + '/lib')
DEFAULT_64_IMAGE="debian:jessie"
DEFAULT_32_IMAGE="32bit/debian:jessie"
function show_usage { import tamarin, system
echo
echo "Usage: $0 -p project_path [-a arch] [-d destination] [-i image] [-k]"
echo
echo "Parameters: "
echo
echo " -p Path to the project to build"
echo " -v Optional : Show Tamarin version"
echo " -a Optional : Target architecture (default amd64)"
echo " -d Optional : Destination of the builed packages (default ./packages)"
echo " -i Optional : Name of the Docker image to use for build (default: debian:jessie)"
echo " -k Optional : Keep the Docker container after build "
echo " -B Optional : Build branch (for git projects only) (default dist/ubuntu/precise/master)"
echo
exit 2
}
# Create a build container based on the $BASE_IMAGE argument workspace = tamarin.get_workspace_dir()
function create_container { workspace_tmp = tamarin.get_workspace_subdir('tmp')
# Escape image name local_rkt_dir = tamarin.get_workspace_subdir('rkt')
local escaped_basename=$(echo "$BASE_IMAGE" | sed 's/[^a-z0-9\-\_\.]/\_/gi') if not system.which('rkt', local_rkt_dir):
# Generate container tag # Download and extract rkt
container_tag="tamarin:${escaped_basename}_$(date +%s)" rkt_archive_path = tamarin.download_rkt()
# Create temporary dir for the Dockerfile system.extract_tar(rkt_archive_path, workspace_tmp)
local temp_dir="$(mktemp -d)" rkt_archive_dir = tamarin.get_rkt_achive_dest_dir()
shutil.rmtree(local_rkt_dir, ignore_errors=True)
os.rename(rkt_archive_dir, local_rkt_dir)
local projectName=${1} local_acbuild_dir = tamarin.get_workspace_subdir('acbuild')
if not system.which('acbuild', local_acbuild_dir):
# Download and extract acbuild
acbuild_archive_path = tamarin.download_acbuild()
system.extract_tar(acbuild_archive_path, workspace_tmp)
acbuild_archive_dir = tamarin.get_acbuild_achive_dest_dir()
shutil.rmtree(local_acbuild_dir, ignore_errors=True)
os.rename(acbuild_archive_dir, local_acbuild_dir)
# Link lib & hooks folders acbuild_workspace = tamarin.get_acbuild_workspace_dir()
ln -s $(readlink -f "$TAMARIN_DIR/lib") "$temp_dir/lib" image_name = "image_{:d}.aci".format(os.getpid())
ln -s $(readlink -f "$TAMARIN_DIR/hooks") "$temp_dir/hooks" image_path = os.path.join(os.sep, acbuild_workspace, 'image.aci')
acbuild_flags = ["--work-path", acbuild_workspace]
# Create Dockerfile # Start building image
cat << EOF > "$temp_dir/Dockerfile" tamarin.run_acbuild(acbuild_flags+["begin"])
FROM ${BASE_IMAGE} tamarin.run_acbuild(acbuild_flags+["set-name", "test"])
tamarin.run_acbuild(acbuild_flags+["set-exec", "--", "/bin/sh", "-c", "ping 8.8.8.8"])
tamarin.run_acbuild(acbuild_flags+["dependency", "add", "quay.io/coreos/alpine-sh"])
tamarin.run_acbuild(acbuild_flags+["write", image_path])
tamarin.run_acbuild(acbuild_flags+["end"])
ENV DEBIAN_FRONTEND noninteractive # rkt run --insecure-options=image ./nginx.aci --volume html,kind=host,source=/path/to/test --net=host
tamarin.run_rkt(["run", "--insecure-options=image", image_path, "--net=host"])
RUN apt-get update &&\
apt-get install --yes --no-install-recommends build-essential devscripts equivs
RUN mkdir /root/.tamarin
RUN mkdir /project
ADD ./lib /root/.tamarin/lib
ADD ./hooks /hooks
RUN chmod +x /root/.tamarin/lib/build.sh
VOLUME /src
VOLUME /dist
EOF
exec_hooks "containerbuild" "$temp_dir"
echo "CMD /root/.tamarin/lib/build.sh ${projectName} ${BUILD_BRANCH} /tmp ${BUILD_TAG}" >> "$temp_dir/Dockerfile"
# Build image
tar -C "$temp_dir" -czh . | docker build -t "$container_tag" - 2> >(stderr) 1> >(stdout)
# Delete temporary folder
rm -rf "$temp_dir"
}
# Main function
function main {
info "Building container from $BASE_IMAGE..."
local project_name=$(basename ${PROJECT_PATH})
# Create container & "$container_tag" variable
create_container ${project_name}
local docker_opt="run -e \"DISTRIB=$BASE_IMAGE\" -e \"PROJECT_NAME=$project_name\""
# Expose host uid & gid
docker_opt="${docker_opt} -e HOST_UID=$(id -u) -e HOST_GID=$(id -g)"
# Expose host proxy variables
docker_opt="${docker_opt} -e HTTP_PROXY=${HTTP_PROXY} -e HTTPS_PROXY=${HTTPS_PROXY}"
docker_opt="${docker_opt} -e http_proxy=${http_proxy} -e https_proxy=${https_proxy}"
# Target architecture
docker_opt="${docker_opt} -e TARGET_ARCH=${TARGET_ARCH}"
# If running in terminal, set docker to interactive
if [[ -t 1 ]]; then
docker_opt="${docker_opt} -it"
fi
if [[ ${PERSIST_CONTAINER} -eq 0 ]]
then
docker_opt="${docker_opt} --rm "
else
docker_opt="${docker_opt}"
fi
docker_opt="${docker_opt} -v $PROJECT_PATH:/src:ro -v $PROJECT_DEST:/dist:rw $container_tag"
info "Switching to container..."
debug "docker ${docker_opt}"
docker ${docker_opt}
res=${?}
success "Done"
return ${res}
}
#
# Parsing options
#
while getopts "vkp:d:i:B:t:a:o:" option
do
case $option in
k)
PERSIST_CONTAINER=1
;;
p)
PROJECT_PATH=$(readlink -f ${OPTARG})
;;
d)
PROJECT_DEST=$(readlink -f ${OPTARG})
;;
i)
BASE_IMAGE="${OPTARG}"
;;
B)
BUILD_BRANCH=${OPTARG}
;;
t)
BUILD_TAG=${OPTARG}
;;
a)
TARGET_ARCH=${OPTARG}
;;
v)
echo "Tamarin v${TAMARIN_VERSION}"
exit
;;
*)
show_usage
;;
esac
done
[[ -z ${PROJECT_PATH} ]] && show_usage
[[ -z ${PROJECT_DEST} ]] && PROJECT_DEST=$(readlink -f "./packages")
[[ -z ${BUILD_BRANCH} ]] && BUILD_BRANCH="dist/ubuntu/precise/master"
[[ -z ${PERSIST_CONTAINER} ]] && PERSIST_CONTAINER=0
[[ -z ${TARGET_ARCH} ]] && TARGET_ARCH=amd64
if [[ "${TARGET_ARCH}" =~ ^i[0-9]86$ ]] && [ -z "${BASE_IMAGE}" ]; then
info "32bit architecture specified and no specific image given, switching to default 32bit image..."
BASE_IMAGE=${DEFAULT_32_IMAGE}
fi
[[ -z ${BASE_IMAGE} ]] && BASE_IMAGE=${DEFAULT_64_IMAGE}
#
# Warn user about "proxy"
#
if [[ -n ${http_proxy} ]]
then
warn "You have a proxy defined please make sure docker deamon is configured to use this proxy"
fi
main

182
package.sh Executable file
View File

@ -0,0 +1,182 @@
#!/usr/bin/env bash
set -e
TAMARIN_VERSION=0.0.1
TAMARIN_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
BASE_DIR="$TAMARIN_DIR" source "$TAMARIN_DIR/lib/util.sh"
DEFAULT_64_IMAGE="debian:jessie"
DEFAULT_32_IMAGE="32bit/debian:jessie"
function show_usage {
echo
echo "Usage: $0 -p project_path [-a arch] [-d destination] [-i image] [-k]"
echo
echo "Parameters: "
echo
echo " -p Path to the project to build"
echo " -v Optional : Show Tamarin version"
echo " -a Optional : Target architecture (default amd64)"
echo " -d Optional : Destination of the builed packages (default ./packages)"
echo " -i Optional : Name of the Docker image to use for build (default: debian:jessie)"
echo " -k Optional : Keep the Docker container after build "
echo " -B Optional : Build branch (for git projects only) (default dist/ubuntu/precise/master)"
echo
exit 2
}
# Create a build container based on the $BASE_IMAGE argument
function create_container {
# Escape image name
local escaped_basename=$(echo "$BASE_IMAGE" | sed 's/[^a-z0-9\-\_\.]/\_/gi')
# Generate container tag
container_tag="tamarin:${escaped_basename}_$(date +%s)"
# Create temporary dir for the Dockerfile
local temp_dir="$(mktemp -d)"
local projectName=${1}
# Link lib & hooks folders
ln -s $(readlink -f "$TAMARIN_DIR/lib") "$temp_dir/lib"
ln -s $(readlink -f "$TAMARIN_DIR/hooks") "$temp_dir/hooks"
# Create Dockerfile
cat << EOF > "$temp_dir/Dockerfile"
FROM ${BASE_IMAGE}
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update &&\
apt-get install --yes --no-install-recommends build-essential devscripts equivs
RUN mkdir /root/.tamarin
RUN mkdir /project
ADD ./lib /root/.tamarin/lib
ADD ./hooks /hooks
RUN chmod +x /root/.tamarin/lib/build.sh
VOLUME /src
VOLUME /dist
EOF
exec_hooks "containerbuild" "$temp_dir"
echo "CMD /root/.tamarin/lib/build.sh ${projectName} ${BUILD_BRANCH} /tmp ${BUILD_TAG}" >> "$temp_dir/Dockerfile"
# Build image
tar -C "$temp_dir" -czh . | docker build -t "$container_tag" - 2> >(stderr) 1> >(stdout)
# Delete temporary folder
rm -rf "$temp_dir"
}
# Main function
function main {
info "Building container from $BASE_IMAGE..."
local project_name=$(basename ${PROJECT_PATH})
# Create container & "$container_tag" variable
create_container ${project_name}
local docker_opt="run -e \"DISTRIB=$BASE_IMAGE\" -e \"PROJECT_NAME=$project_name\""
# Expose host uid & gid
docker_opt="${docker_opt} -e HOST_UID=$(id -u) -e HOST_GID=$(id -g)"
# Expose host proxy variables
docker_opt="${docker_opt} -e HTTP_PROXY=${HTTP_PROXY} -e HTTPS_PROXY=${HTTPS_PROXY}"
docker_opt="${docker_opt} -e http_proxy=${http_proxy} -e https_proxy=${https_proxy}"
# Target architecture
docker_opt="${docker_opt} -e TARGET_ARCH=${TARGET_ARCH}"
# If running in terminal, set docker to interactive
if [[ -t 1 ]]; then
docker_opt="${docker_opt} -it"
fi
if [[ ${PERSIST_CONTAINER} -eq 0 ]]
then
docker_opt="${docker_opt} --rm "
else
docker_opt="${docker_opt}"
fi
docker_opt="${docker_opt} -v $PROJECT_PATH:/src:ro -v $PROJECT_DEST:/dist:rw $container_tag"
info "Switching to container..."
debug "docker ${docker_opt}"
docker ${docker_opt}
res=${?}
success "Done"
return ${res}
}
#
# Parsing options
#
while getopts "vkp:d:i:B:t:a:o:" option
do
case $option in
k)
PERSIST_CONTAINER=1
;;
p)
PROJECT_PATH=$(readlink -f ${OPTARG})
;;
d)
PROJECT_DEST=$(readlink -f ${OPTARG})
;;
i)
BASE_IMAGE="${OPTARG}"
;;
B)
BUILD_BRANCH=${OPTARG}
;;
t)
BUILD_TAG=${OPTARG}
;;
a)
TARGET_ARCH=${OPTARG}
;;
v)
echo "Tamarin v${TAMARIN_VERSION}"
exit
;;
*)
show_usage
;;
esac
done
[[ -z ${PROJECT_PATH} ]] && show_usage
[[ -z ${PROJECT_DEST} ]] && PROJECT_DEST=$(readlink -f "./packages")
[[ -z ${BUILD_BRANCH} ]] && BUILD_BRANCH="dist/ubuntu/precise/master"
[[ -z ${PERSIST_CONTAINER} ]] && PERSIST_CONTAINER=0
[[ -z ${TARGET_ARCH} ]] && TARGET_ARCH=amd64
if [[ "${TARGET_ARCH}" =~ ^i[0-9]86$ ]] && [ -z "${BASE_IMAGE}" ]; then
info "32bit architecture specified and no specific image given, switching to default 32bit image..."
BASE_IMAGE=${DEFAULT_32_IMAGE}
fi
[[ -z ${BASE_IMAGE} ]] && BASE_IMAGE=${DEFAULT_64_IMAGE}
#
# Warn user about "proxy"
#
if [[ -n ${http_proxy} ]]
then
warn "You have a proxy defined please make sure docker deamon is configured to use this proxy"
fi
main