<commit_before>#!/bin/bash

# Works with singularity 2.3 / su
# Builds a base container image for justus

# Parameters
#  --local  ; local image only, requires root password
#  --remote ; triggers remote builds on shub
#  --all    ; local image plus triggering of remote builds
#
# TODO add dockerfile support
# 
# Default is: --local
BUILDMODE="${1:-'--local'}"
CALLINGDIR=`dirname $(readlink -f $0)`
GIT_URL="<EMAIL>:c1t4r/CiTAR-Containers.git"

SHAREDWORKDIR="$CALLINGDIR/$BASHPID" && mkdir -p $SHAREDWORKDIR && cd $SHAREDWORKDIR
IMGDIR="$CALLINGDIR/images"
IMPORTS="$CALLINGDIR/rootfs"

VNFSBaseImage="rhel7.stateless.CiTAR"
CITARROOTFS="/var/lib/perceus/vnfs/$VNFSBaseImage/rootfs"
DATESTR="+%Y-%m-%d-%H-%M"

echo "Building JUSTUS Base Image"

rpm --root $CITARROOTFS -q -a --queryformat '%{NAME} ' | sort > $SHAREDWORKDIR/packagelist.txt

SRCSUM="$(sha256sum $SHAREDWORKDIR/packagelist.txt | awk '{print $1}')"
SUM=$( [[ -f $IMGDIR/$SRCSUM/sha256 ]] && cat $IMGDIR/$SRCSUM/sha256 )

if [[ -d $IMGDIR/$SRCSUM ]]; then
    echo "An up-to-date image for $SRCSUM exists already, no changes detected..."
else
    echo "Building image for $SRCSUM from $VNFSBaseImage..."
    rpm --root $CITARROOTFS -q -a --queryformat '%{NAME} ' | sort > packagelist.txt
#    tar -C $CITARROOTFS -cpf addons.tar usr/local
    cat << EOF_DEFFILE > docker.def
BootStrap: docker
From: centos:7
IncludeCmd: yes

%runscript
    echo "This is what happens when you run the container..."

%environment
    export PS1='\[\033[01;32m\]\u@\${SINGULARITY_CONTAINER}@\h\[\033[01;34m\] \w \\$\[\033[00m\] '

%files
    /usr/local/   /usr/local/
    $IMPORTS/*    /
    $IMPORTS/.singularity.d/ /

%post
    echo "Installing JUSTUS software package list"
    yum -y install deltarpm
    yum -y --skip-broken install \\
EOF_DEFFILE
    echo "Retrieving package list..."
    cat packagelist.txt >> docker.def
    echo "" >> docker.def
    echo "    yum clean all" >> docker.def
    echo "    mv /usr/bin/ssh /usr/bin/ssh_orig" >> docker.def

    cd $SHAREDWORKDIR 

    git clone $GIT_URL containerspec
    git config credential.helper store
    git config --global credential.helper 'cache --timeout 150000'

    ### begin - su part

    echo "Please enter root password for 'su' "
    stty -echo
    read -p "Password: " PASSWD; echo
    stty echo

    echo -n "Creating container image..." && echo "[$CONTAINER_COMMAND]"
    CONTAINER_COMMAND="singularity create -s 3000 centos7-justus.img" && echo "[$CONTAINER_COMMAND]"
    echo "$PASSWD" | su -c "$CONTAINER_COMMAND"

    echo -n "Bootstrapping CentOS7 base system... "
    CONTAINER_COMMAND="singularity bootstrap centos7-justus.img docker.def" && echo "[$CONTAINER_COMMAND]"
    echo "$PASSWD" | su -c "$CONTAINER_COMMAND"

    sync

    SUM="$(sha256sum centos7-justus.img | awk '{print $1}')"

    echo "Copying image into local archive..."
    mkdir -p $IMGDIR/$SRCSUM<commit_msg>fixing shub builds...wip<commit_after>#!/bin/bash

# Works with singularity 2.3 / su
# Builds a base container image for justus

# Parameters
#  --local  ; local image only, requires root password
#  --remote ; triggers remote builds on shub
#  --all    ; local image plus triggering of remote builds
#
# FIXME .gitignore workaround will not do! Rethink how to do it :)
# TODO add dockerfile support
# 
# Default is: --local
BUILDMODE="${1:-'--local'}"
CALLINGDIR=`dirname $(readlink -f $0)`
GIT_URL="<EMAIL>:c1t4r/CiTAR-Containers.git"

SHAREDWORKDIR="$CALLINGDIR/$BASHPID" && mkdir -p $SHAREDWORKDIR && cd $SHAREDWORKDIR
IMGDIR="$CALLINGDIR/images"
IMPORTS="$CALLINGDIR/rootfs"

VNFSBaseImage="rhel7.stateless.CiTAR"
CITARROOTFS="/var/lib/perceus/vnfs/$VNFSBaseImage/rootfs"
DATESTR="+%Y-%m-%d-%H-%M"

echo "Building JUSTUS Base Image"

rpm --root $CITARROOTFS -q -a --queryformat '%{NAME} ' | sort > $SHAREDWORKDIR/packagelist.txt

SRCSUM="$(sha256sum $SHAREDWORKDIR/packagelist.txt | awk '{print $1}')"
SUM=$( [[ -f $IMGDIR/$SRCSUM/sha256 ]] && cat $IMGDIR/$SRCSUM/sha256 )

if [[ -d $IMGDIR/$SRCSUM ]]; then
    echo "An up-to-date image for $SRCSUM exists already, no changes detected..."
else
    echo "Building image for $SRCSUM from $VNFSBaseImage..."
    rpm --root $CITARROOTFS -q -a --queryformat '%{NAME} ' | sort > packagelist.txt
    cat << EOF_DEFFILE > docker.def
BootStrap: docker
From: centos:7
IncludeCmd: yes

%runscript
    echo "This is what happens when you run the container..."

%environment
    export PS1='\[\033[01;32m\]\u@\${SINGULARITY_CONTAINER}@\h\[\033[01;34m\] \w \\$\[\033[00m\] '

%files
    /usr/local/   /usr/local/
    $IMPORTS/*    /
    $IMPORTS/.singularity.d /

%post
    echo "Installing JUSTUS software package list"
    yum -y install deltarpm
    yum -y --skip-broken install \\
EOF_DEFFILE
    echo "Retrieving package list..."
    cat packagelist.txt >> docker.def
    echo "" >> docker.def
    echo "    yum clean all" >> docker.def
    echo "    mv /usr/bin/ssh /usr/bin/ssh_orig" >> docker.def

    cd $SHAREDWORKDIR 

    git clone $GIT_URL containerspec
    cp docker.def containerspec
    cd containerspec

    cat docker.def | awk '/^%files/{begin=1;next}/^%/{begin=0}{if ((begin)&&$0!="") printf("mkdir -p ./imports/%s && rsync -raphvz %s ./imports/%s\n", $2, $1, $2)}' >> imports.sh
    chmod +x imports.sh
    ./imports.sh
    # fix empty dirs stupid git doesn't add them otherwise!
    find imports -type d -empty -execdir touch {}/.gitignore \;
    
    sed -i 's#'$IMPORTS'#/imports#' docker.def
    mv docker.def Singularity
    echo "" >> README.md
    echo "* updated build: $(date $DATESTR)" >> README.md
    git add Singularity imports.sh imports/ README.md
    git commit -am  'updated build'
    git push

    cd ..

    ### begin - su part

    echo "Please enter root password for 'su' "
    stty -echo
    read -p "Password: " PASSWD; echo
    stty echo

    echo -n "Creating container image..." && echo "[$CONTAINER_COMMAND]"
    CONTAINER_COMMAND="singularity create -s 3000 centos7-justus.img" && echo "[$CONTAINER_COMMAND]"
    echo "$PASSWD" | su -c "$CONTAINER_COMMAND"

    echo -n "Bootstrapping CentOS7 base system... "
    CONTAINER_COMMAND="singularity bootstrap centos7-justus.img docker.def" && echo "[$CONTAINER_COMMAND]"
    echo "$PASSWD" | su -c "$CONTAINER_COMMAND"

    sync

    SUM="$(sha256sum centos7-justus.img | awk '{print $1}')"

    echo "Copying image into local archive..."
    mkdir -p $IMGDIR/$SRCSUM
{'fraction_non_alphanumeric': 0.14933749025720966, 'fraction_numerical': 0.01745908028059236, 'mean_word_length': 3.257465162574652, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '324317', 'n_tokens_mistral': 2457, 'n_tokens_neox': 2291, 'n_words': 612}