Skip to content

Commit 1514a1c

Browse files
committed
adding secbuild script
Signed-off-by: Vanessa Sochat <[email protected]>
1 parent 2222639 commit 1514a1c

File tree

5 files changed

+178
-32
lines changed

5 files changed

+178
-32
lines changed

singularity/build/google/instances.py

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,37 @@ def run_build(logfile='/tmp/.shub-log'):
4343

4444
'''run_build will generate the Singularity build from a spec_file from a repo_url.
4545
46-
If no arguments are required, the metadata api is queried for the values.
47-
48-
:param build_dir: directory to do the build in. If not specified, will use temporary.
49-
:param spec_file: the spec_file name to use, assumed to be in git repo
50-
:param repo_url: the url to download the repo from
51-
:param repo_id: the repo_id to uniquely identify the repo (in case name changes)
52-
:param commit: the commit to checkout. If none provided, will use most recent.
53-
:param bucket_name: the name of the bucket to send files to
54-
:param verbose: print out extra details as we go (default True)
55-
:param token: a token to send back to the server to authenticate the collection
56-
:param secret: a secret to match to the correct container
57-
:param response_url: the build url to send the response back to. Should also come
58-
from metadata. If not specified, no response is sent
59-
:param branch: the branch to checkout for the build.
60-
61-
:: note: this function is currently configured to work with Google Compute
62-
Engine metadata api, and should (will) be customized if needed to work elsewhere
46+
If no arguments are required, the metadata api is queried for the values.
47+
48+
Parameters
49+
==========
50+
build_dir: directory to do the build in. If not specified, will use temporary.
51+
spec_file: the spec_file name to use, assumed to be in git repo
52+
repo_url: the url to download the repo from
53+
repo_id: the repo_id to uniquely identify the repo (in case name changes)
54+
commit: the commit to checkout. If none provided, will use most recent.
55+
bucket_name: the name of the bucket to send files to
56+
verbose: print out extra details as we go (default True)
57+
token: a token to send back to the server to authenticate the collection
58+
secret: a secret to match to the correct container
59+
response_url: the build url to send the response back to. Should also come
60+
from metadata. If not specified, no response is sent
61+
branch: the branch to checkout for the build.
62+
63+
:: note: this function is currently configured to work with Google Compute
64+
Engine metadata api, and should (will) be customized if needed to work elsewhere
6365
6466
'''
6567

6668
# If we are building the image, this will not be set
6769
go = get_build_metadata(key='dobuild')
68-
if go == None:
70+
if go is None:
6971
sys.exit(0)
7072

7173
# If the user wants debug, this will be set
7274
debug = True
7375
enable_debug = get_build_metadata(key='debug')
74-
if enable_debug == None:
76+
if enable_debug is None:
7577
debug = False
7678
bot.info('DEBUG %s' %debug)
7779

@@ -117,7 +119,7 @@ def run_build(logfile='/tmp/.shub-log'):
117119

118120
# Upload image package files to Google Storage
119121
if os.path.exists(finished_image):
120-
bot.info("%s successfully built" %finished_image)
122+
bot.info("%s successfully built" % finished_image)
121123
dest_dir = tempfile.mkdtemp(prefix='build')
122124

123125
# The path to the images on google drive will be the github url/commit folder
@@ -175,10 +177,11 @@ def run_build(logfile='/tmp/.shub-log'):
175177

176178
def finish_build(verbose=True):
177179
'''finish_build will finish the build by way of sending the log to the same bucket.
178-
the params are loaded from the previous function that built the image, expected in
179-
$HOME/params.pkl
180-
:: note: this function is currently configured to work with Google Compute
181-
Engine metadata api, and should (will) be customized if needed to work elsewhere
180+
the params are loaded from the previous function that built the image, expected in
181+
$HOME/params.pkl
182+
183+
:: note: this function is currently configured to work with Google Compute
184+
Engine metadata api, and should (will) be customized if needed to work elsewhere
182185
'''
183186
# If we are building the image, this will not be set
184187
go = get_build_metadata(key='dobuild')
@@ -218,7 +221,10 @@ def finish_build(verbose=True):
218221

219222
def get_build_metadata(key):
220223
'''get_build_metadata will return metadata about an instance from within it.
221-
:param key: the key to look up
224+
225+
Parameters
226+
==========
227+
key: the key to look up
222228
'''
223229
headers = {"Metadata-Flavor":"Google"}
224230
url = "http://metadata.google.internal/computeMetadata/v1/instance/attributes/%s" % key

singularity/build/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def run_build(build_dir, params, verbose=True):
5656
- returns a dictionary with:
5757
image (path), metadata (dict)
5858
59-
The following must be included in params:
59+
The following must be included in params:
6060
spec_file, repo_url, branch, commit
6161
6262
'''

singularity/build/scripts/bundle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func NewBundle(bundleDir, bundlePrefix string) (b *Bundle, err error) {
7474

7575
// Bundle path must be predictable
7676
b.Path = "/tmp/sbuild"
77-
err = os.Mkdir(b.Path)
77+
err = os.Mkdir(b.Path, 0755)
7878
if err != nil {
7979
return nil, err
8080
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#!/bin/bash
2+
3+
# The script takes the definition file as first argument. and
4+
# desired final image page as second.
5+
SINGULARITY_BUILDDEF="${1}"
6+
SINGULARITY_FINAL="${2}"
7+
8+
if [ ! -f ${SINGULARITY_BUILDDEF} ]; then
9+
echo "${SINGULARITY_BUILDDEF} does not exist";
10+
exit 1;
11+
fi
12+
13+
SINGULARITY_confdir="/usr/local/etc/singularity"
14+
SINGULARITY_bindir="/usr/local/bin"
15+
SINGULARITY_libexecdir="/usr/local/libexec/singularity"
16+
SINGULARITY_PATH="/usr/local/bin"
17+
SECBUILD_IMAGE="$SINGULARITY_libexecdir/secure-build/secbuild.sif"
18+
19+
# Set the isolated root
20+
if [ -z "${SINGULARITY_ISOLATED_ROOT:-}" ]; then
21+
BUILDDEF_DIR_NAME=$(dirname ${SINGULARITY_BUILDDEF:-})
22+
else
23+
BUILDDEF_DIR_NAME=$(readlink -f ${SINGULARITY_ISOLATED_ROOT:-})
24+
fi
25+
BUILDDEF_DIR=$(readlink -f ${BUILDDEF_DIR_NAME:-})
26+
27+
if [ -z "${BUILDDEF_DIR:-}" ]; then
28+
echo "Can't find parent directory of $SINGULARITY_BUILDDEF"
29+
exit 1
30+
fi
31+
32+
BUILDDEF=$(basename ${SINGULARITY_BUILDDEF:-})
33+
34+
# create a temporary dir per build instance
35+
export SINGULARITY_WORKDIR=$(mktemp -d)
36+
37+
# create /tmp and /var/tmp into WORKDIR
38+
mkdir -p $SINGULARITY_WORKDIR/tmp $SINGULARITY_WORKDIR/var_tmp
39+
40+
# set sticky bit for these directories
41+
chmod 1777 $SINGULARITY_WORKDIR/tmp
42+
chmod 1777 $SINGULARITY_WORKDIR/var_tmp
43+
44+
# setup a fake root directory
45+
cp -a /etc/skel $SINGULARITY_WORKDIR/root
46+
47+
cat > "$SINGULARITY_WORKDIR/root/.rpmmacros" << RPMMAC
48+
%_var /var
49+
%_dbpath %{_var}/lib/rpm
50+
RPMMAC
51+
52+
REPO_DIR="/root/repo"
53+
STAGED_BUILD_IMAGE="/root/build"
54+
55+
# Move the repo to be the REPO_DIR
56+
mv $BUILDDEF_DIR $REPO_DIR
57+
58+
mkdir ${SINGULARITY_WORKDIR}${REPO_DIR}
59+
mkdir ${SINGULARITY_WORKDIR}${STAGED_BUILD_IMAGE}
60+
61+
BUILD_SCRIPT="$SINGULARITY_WORKDIR/tmp/build-script"
62+
TMP_CONF_FILE="$SINGULARITY_WORKDIR/tmp.conf"
63+
FSTAB_FILE="$SINGULARITY_WORKDIR/fstab"
64+
RESOLV_CONF="$SINGULARITY_WORKDIR/resolv.conf"
65+
HOSTS_FILE="$SINGULARITY_WORKDIR/hosts"
66+
67+
cp /etc/resolv.conf $RESOLV_CONF
68+
cp /etc/hosts $HOSTS_FILE
69+
70+
cat > "$FSTAB_FILE" << FSTAB
71+
none $STAGED_BUILD_IMAGE bind dev 0 0
72+
FSTAB
73+
74+
cat > "$TMP_CONF_FILE" << CONF
75+
config passwd = yes
76+
config group = no
77+
config resolv_conf = no
78+
mount proc = no
79+
mount sys = no
80+
mount home = no
81+
mount dev = minimal
82+
mount devpts = no
83+
mount tmp = no
84+
mount slave = no
85+
enable overlay = no
86+
enable underlay = no
87+
user bind control = no
88+
bind path = $SINGULARITY_WORKDIR/root:/root
89+
bind path = $SINGULARITY_WORKDIR/tmp:/tmp
90+
bind path = $SINGULARITY_WORKDIR/var_tmp:/var/tmp
91+
bind path = /tmp/sbuild/fs:$STAGED_BUILD_IMAGE
92+
bind path = $FSTAB_FILE:/etc/fstab
93+
bind path = $RESOLV_CONF:/etc/resolv.conf
94+
bind path = $HOSTS_FILE:/etc/hosts
95+
root default capabilities = full
96+
allow user capabilities = no
97+
allow setuid = yes
98+
CONF
99+
100+
# We only use the builder once, make default config
101+
sudo cp "$TMP_CONF_FILE" "${SINGULARITY_confdir}/singularity.conf"
102+
103+
# here build pre-stage
104+
cat > "$BUILD_SCRIPT" << SCRIPT
105+
#!/bin/sh
106+
mount -r --no-mtab -t proc proc /proc
107+
if [ \$? != 0 ]; then
108+
echo "Can't mount /proc directory"
109+
exit 1
110+
fi
111+
mount -r --no-mtab -t sysfs sysfs /sys
112+
if [ \$? != 0 ]; then
113+
echo "Can't mount /sys directory"
114+
exit 1
115+
fi
116+
cd $REPO_DIR
117+
singularity build $STAGED_BUILD_IMAGE/container.sif $BUILDDEF
118+
exit \$?
119+
SCRIPT
120+
121+
chmod +x $BUILD_SCRIPT
122+
123+
unset SINGULARITY_IMAGE
124+
unset SINGULARITY_NO_PRIVS
125+
unset SINGULARITY_KEEP_PRIVS
126+
unset SINGULARITY_ADD_CAPS
127+
unset SINGULARITY_DROP_CAPS
128+
129+
${SINGULARITY_bindir}/singularity exec -e -i -p $SECBUILD_IMAGE /tmp/build-script
130+
if [ $? != 0 ]; then
131+
rm -rf $SINGULARITY_WORKDIR
132+
exit 1
133+
fi
134+
135+
if [ ! -f "${SINGULARITY_WORKDIR}${STAGED_BUILD_IMAGE}/container.sif" ]; then
136+
echo "Container was not built.";
137+
exit 1;
138+
fi
139+
140+
sudo mv "${SINGULARITY_WORKDIR}${STAGED_BUILD_IMAGE}/container.sif" "${SINGULARITY_FINAL}"
141+
sudo rm -rf $SINGULARITY_WORKDIR

singularity/build/scripts/singularity-prepare-instance.sh

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,16 @@ sudo apt-get -y install git \
2828
build-essential \
2929
libssl-dev \
3030
uuid-dev \
31-
libgpgme11-dev \
31+
libgpgme-dev \
3232
libseccomp-dev \
33-
pkg-config
33+
pkg-config \
3434
squashfs-tools \
3535
debootstrap \
3636
yum \
37-
zypper \
3837
python3-pip
3938

4039
# Pip3 installs
41-
sudo -H pip3 install -H --upgrade pip
40+
sudo -H pip3 install --upgrade pip
4241
sudo -H pip3 install pyasn1-modules -U
4342
sudo -H pip3 install --upgrade google-api-python-client
4443
sudo -H pip3 install --upgrade google
@@ -91,7 +90,7 @@ From: ubuntu:18.04
9190
%post
9291
export LC_LANG=C
9392
export VERSION=1.12.6 OS=linux ARCH=amd64
94-
export SINGULARITY_VERSION=${SINGULARITY_VERSION}
93+
export SINGULARITY_VERSION=3.2.1
9594
apt-get update -y
9695
apt-get -y install git build-essential libssl-dev uuid-dev pkg-config curl gcc
9796
apt-get -y install libgpgme11-dev libseccomp-dev squashfs-tools libc6-dev-i386

0 commit comments

Comments
 (0)