Skip to content

Commit 0fb3670

Browse files
Merge pull request #15 from derickson2402/dev
Merge changes from dev for release v0.5 - Add functionality and error checking to wrapper script (closes #8) - Add support for input redirection (closes #2) - Add support for running Perf command with elevated Docker permissions (closes #6) - Add cppchecker to C++ container (closes #3)
2 parents 7319d05 + 49a7470 commit 0fb3670

File tree

5 files changed

+188
-50
lines changed

5 files changed

+188
-50
lines changed

.github/workflows/publish.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ jobs:
66
build:
77
runs-on: ubuntu-latest
88
steps:
9+
- name: Set up Docker Buildx
10+
uses: docker/setup-buildx-action@v1
11+
912
- name: Check out repo
1013
uses: actions/checkout@v2
1114

@@ -29,6 +32,7 @@ jobs:
2932
with:
3033
context: ./
3134
file: ./Dockerfile
35+
pull: true
3236
push: true
3337
tags: ${{ env.CONTAINER_NAME }}:latest,${{ env.CONTAINER_NAME }}:${{ steps.tag.outputs.lowercase }}
3438

.github/workflows/testing.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ jobs:
88
build:
99
runs-on: ubuntu-latest
1010
steps:
11+
- name: Set up Docker Buildx
12+
uses: docker/setup-buildx-action@v1
13+
1114
- name: Check out repo
1215
uses: actions/checkout@v2
1316

@@ -31,6 +34,7 @@ jobs:
3134
with:
3235
context: ./
3336
file: ./Dockerfile
37+
pull: true
3438
push: true
3539
tags: ${{ env.CONTAINER_NAME }}:${{ steps.branch.outputs.lowercase }}
3640

Dockerfile

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,38 @@
55
FROM centos:8 AS caen-base
66

77
LABEL maintainer = "Dan Erickson ([email protected])"
8-
LABEL version = "v0.3"
9-
LABEL release-date = "2022-02-02"
8+
LABEL version = "v0.5"
9+
LABEL release-date = "2022-02-06"
1010
LABEL org.opencontainers.image.source = "https://github.com/derickson2402/Dockerized-CAEN"
1111

12+
# Prep base environment
1213
ENV USER=1000 \
13-
GROUP=1000
14+
GROUP=1000 \
15+
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/code
1416
VOLUME /code
1517
RUN mkdir -p /usr/um
1618

17-
# CentOS has been deprecated in favor of CentOS stream, so update repo list to search archives
18-
#
19+
# CentOS has been deprecated in favor of CentOS stream, so update repo list to
20+
# search archives:
1921
# https://forums.centos.org/viewtopic.php?f=54&t=78708
20-
RUN rm -f /etc/yum.repos.d/CentOS-Linux-AppStream.repo \
21-
&& sed -i \
22-
-e 's/mirrorlist.centos.org/vault.centos.org/' \
23-
-e 's/mirror.centos.org/vault.centos.org/' \
24-
-e 's/#baseurl/baseurl/' /etc/yum.repos.d/CentOS-Linux-BaseOS.repo \
22+
# https://www.getpagespeed.com/server-setup/how-to-fix-dnf-after-centos-8-went-eol
23+
RUN sed -i \
24+
-e 's/#baseurl/baseurl/g' \
25+
-e 's/mirror.centos.org/vault.epel.cloud/g' \
26+
-e 's/mirrorlist/#mirrorlist/g' \
27+
/etc/yum.repos.d/CentOS-Linux-* \
2528
&& dnf clean all \
2629
&& dnf swap -y centos-linux-repos centos-stream-repos \
27-
&& dnf install -y --nodocs wget bzip2 tar which
30+
&& dnf install -y --nodocs wget bzip2 tar make
2831

2932
# Set bash as default
3033
SHELL ["/bin/bash", "-c"]
3134

3235

3336
################################################################################
3437
#
35-
# Development environment with basic tools installed, for testing new layers and
36-
# configurations
38+
# Development environment with basic tools installed, used for builder layers
39+
# and for testing
3740

3841
FROM caen-base AS caen-dev
3942

@@ -47,18 +50,32 @@ CMD ["/bin/bash"]
4750

4851
################################################################################
4952
#
50-
# Experimental golang environment
53+
# Builder container for compiling cppcheck
5154

52-
FROM caen-base AS caen-golang
55+
FROM caen-dev AS builder-cppcheck
5356

54-
RUN wget https://dl.google.com/go/go1.16.12.linux-amd64.tar.gz -O /tmp/go.tar.gz \
55-
&& tar -C /usr/um -xzf /tmp/go.tar.gz \
56-
&& rm -rf /tmp/go.tar.gz /usr/local/go /usr/go /usr/bin/go \
57-
&& ln -s /usr/um/go/bin/go /usr/bin/go
57+
# Download and compile cppcheck. Note that /usr/share/Cppcheck has an
58+
# uppercase 'C', as this is what CAEN has for some reason
59+
RUN wget https://github.com/danmar/cppcheck/archive/2.4.tar.gz \
60+
-O /tmp/cppcheck-2.4.tar.gz \
61+
&& tar -C /tmp -xzf /tmp/cppcheck-2.4.tar.gz \
62+
&& rm -rf /tmp/cppcheck-2.4.tar.gz \
63+
&& cd /tmp/cppcheck-2.4 \
64+
&& FILESDIR=/usr/share/Cppcheck make install \
65+
&& rm -rf /tmp/cppcheck-2.4
5866

59-
# Run the container in the user's project folder
60-
WORKDIR /code
61-
CMD ["/bin/bash"]
67+
68+
################################################################################
69+
#
70+
# Builder container for compiling cppcheck
71+
72+
FROM caen-dev AS builder-golang
73+
74+
# Download and install golang
75+
RUN wget https://dl.google.com/go/go1.16.12.linux-amd64.tar.gz \
76+
-O /tmp/go.tar.gz \
77+
&& tar -C /usr/um -xzf /tmp/go.tar.gz \
78+
&& rm -rf /tmp/go.tar.gz /usr/local/go /usr/go /usr/bin/go
6279

6380

6481
################################################################################
@@ -68,19 +85,26 @@ CMD ["/bin/bash"]
6885
FROM caen-base
6986

7087
# Install dev packages and tools
71-
RUN yum --setopt=group_package_types=mandatory groupinstall --nodocs -y "Development Tools" \
72-
&& yum install --nodocs -y perf valgrind \
73-
&& yum clean all \
88+
RUN dnf --setopt=group_package_types=mandatory \
89+
groupinstall --nodocs -y "Development Tools" \
90+
&& dnf install --nodocs -y perf valgrind \
91+
&& dnf clean all \
7492
&& rm -rf /var/cache/yum \
7593
&& rm -rf /var/lib/rpm/Packages
7694

7795
# Sym link expected location of CAEN compiler just in case
7896
RUN mkdir -p /usr/um/gcc-6.2.0/bin/ \
7997
&& ln -s /usr/bin/gcc /usr/um/gcc-6.2.0/bin/gcc \
80-
&& ln -s /usr/bin/g++ /usr/um/gcc-6.2.0/bin/g++ \
81-
&& echo "export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/code" >> /root/.bashrc
98+
&& ln -s /usr/bin/g++ /usr/um/gcc-6.2.0/bin/g++
99+
100+
# Set up cppcheck
101+
COPY --from=builder-cppcheck /usr/bin/cppcheck /usr/bin/cppcheck
102+
COPY --from=builder-cppcheck /usr/share/Cppcheck /usr/share/Cppcheck
103+
104+
# Set up golang
105+
COPY --from=builder-golang /usr/um/go /usr/um/go
106+
RUN ln -s /usr/um/go/bin/go /usr/bin/go
82107

83108
# Run the container in the user's project folder
84109
WORKDIR /code
85110
CMD ["/bin/bash"]
86-

README.md

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,31 @@
55
Tired of using ssh and Duo mobile when testing your code with CAEN? With this script, all you have to do is run:
66

77
```bash
8-
./caen <program> [args]
8+
caen <program> [args]
99
```
1010

11-
This will run your command in an environment identical to CAEN Linux. For example, if you are working on a c++ project for EECS 281, you could use:
11+
This will run your command in an environment identical to CAEN Linux. For example, if you are working on a C++ project for EECS 281, you could use:
1212

1313
```bash
14-
./caen make clean
15-
./caen make my_program.cpp
16-
./caen valgrind my_program.cpp
17-
./caen perf my_program.cpp
14+
caen make clean
15+
caen make my_program.cpp < input.txt
16+
caen valgrind my_program.cpp
17+
caen perf record my_program.cpp
18+
```
19+
20+
Or maybe you are writing a program in Go, you might want to use:
21+
22+
```bash
23+
caen go build my_program.go
24+
caen ./my_program
1825
```
1926

2027
## Installation
2128

22-
To use this script, you need to have Docker installed on your [macOS](https://docs.docker.com/desktop/mac/install/), [Windows](https://docs.docker.com/desktop/windows/install/), or [Linux](https://docs.docker.com/engine/install/) computer. With Docker installed and running, simply the ```caen``` script in your project folder and preface any of your commands with it. Run the following in your project folder to automatically grab the script:
29+
To use this script, you need to have Docker installed on your [macOS](https://docs.docker.com/desktop/mac/install/), [Windows](https://docs.docker.com/desktop/windows/install/), or [Linux](https://docs.docker.com/engine/install/) computer. With Docker installed, simply run the following command in a shell:
2330

2431
```bash
25-
wget https://raw.githubusercontent.com/derickson2402/Dockerized-CAEN/main/caen && chmod +x ./caen
32+
wget https://raw.githubusercontent.com/derickson2402/Dockerized-CAEN/main/caen -O /usr/local/bin/caen && chmod +x /usr/local/bin/caen
2633
```
2734

2835
## How Does This Work?
@@ -35,10 +42,16 @@ Oops! Sorry about that! Please log an issue [here](https://github.com/derickson2
3542

3643
## Useful Tips
3744

38-
This container is currently under development, but the script does not check for updates automatically. To get the newest container version, run the following in a terminal with Docker running:
45+
If you want to use a different version of the container other than the default, you can specify the ```CAEN_VERSION``` environment variable before running the script like such:
3946

4047
```bash
41-
docker pull ghcr.io/derickson2402/dockerized-caen:latest
48+
CAEN_VERSION=dev caen my-program
49+
```
50+
51+
This also works for optional arguements to the Docker engine, but this is not recommended as it could conflict with the other options used:
52+
53+
```bash
54+
CAEN_ARGS="-e UID=1001" caen my-program
4255
```
4356

4457
Executables generated with this container are compiled for CAEN servers and won't work on your host system. You should run your ```make clean``` script before switching back and forth, and then run ```make``` from the environment you want to use.
@@ -57,13 +70,19 @@ docker run --rm -it -v "$(pwd):/code" ghcr.io/derickson2402/dockerized-caen:late
5770

5871
## Hackery
5972

60-
If the container environment is not suiting your needs, you can always run the container manually and hack it into working. The problem is that the update won't survive a container restart, so change the normal script like so:
73+
You can specify the name of the container to use just like you can specify the tag:
74+
75+
```bash
76+
CAEN_REPO_NAME=my-container-name caen my-program
77+
```
78+
79+
If the container environment is not suiting your needs, you can always run the container manually and hack it into working. This is not recommended, and will not necessarily work if you don't know how docker works. However, the following should be what you want:
6180

6281
```bash
6382
docker run -it --name caen-tainer -v "$(pwd):/code" ghcr.io/derickson2402/dockerized-caen:latest bash
6483
```
6584

66-
The important part is to get rid of the ```--rm``` tag so the container isn't destroyed when it exits, and to give it a name to easily reference it with (you don't have to use ```caen-tainer```, but I thought it was funny :smile:). You should be able to jump back into the container with either of:
85+
The important part is to mount your local directory correctly, and to get rid of the ```--rm``` tag so the container isn't destroyed when it exits. If you give the container a name to easily reference it with (you don't have to use ```caen-tainer```, but I thought it was funny :smile:), you should be able to jump back into the container with either of:
6786

6887
```bash
6988
docker start -ai caen-tainer
@@ -75,3 +94,4 @@ docker exec -it caen-tainer <command>
7594
I started working on this project while taking EECS-281, in order to make debugging my programs easier. I am sharing this project online in hopes that others will find it useful, but note that I don't have much free time to develop this project.
7695

7796
With that said, if you have an idea that would make this project even better, feel free to log an issue or submit a Pull Request. I greatly appreciate any help on developing this project!
97+

caen

Lines changed: 96 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,106 @@
11
#!/bin/bash
22

3-
# Present the help screen
4-
if [ $# -eq 0 ] || [ $1 = "help" ]; then
5-
echo -e "Helper script for Dockerized-CAEN. To use, run the following:\n"
6-
echo -e " ./caen <program> [args]\n"
7-
echo -e "For example, running the following would run Valgrind on an"
8-
echo -e "executable called 'program.exe' with a '--flag' and input from a file:\n"
9-
echo -e " ./caen valgrind program.exe --flag < input.txt\n"
3+
# Shell settings to improve safety
4+
set -f -o pipefail
5+
6+
# Define our help message, docker flags, and env vars
7+
CAEN_DEFAULT_ARGS="--rm -i --name caen-tainer -e TERM=${TERM:=linux}"
8+
CAEN_WORKING_DIR="-v '$(pwd):/code'"
9+
HELP_MESSAGE=$(cat << EOF
10+
Helper script for Dockerized-CAEN. To use, run the following:
11+
12+
$0 <program> [args]
13+
14+
For example, running the following would run Valgrind on an
15+
executable called 'program.exe' with a '--flag' and input from a file:
16+
17+
$0 valgrind program.exe --flag < input.txt
18+
19+
You can specify a specific version of the container to run with the
20+
\$CAEN_VERSION environment variable like so:
21+
22+
CAEN_VERSION=dev $0 valgrind
23+
24+
EOF
25+
)
26+
27+
# Get OS information
28+
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
29+
HOST_OS=linux
30+
elif [[ "$OSTYPE" == "darwin"* ]]; then
31+
HOST_OS=mac
32+
elif [[ "$OSTYPE" == "cygwin"* ]] | [[ "$OSTYPE" == "msys" ]]; then
33+
HOST_OS=windows
34+
else
35+
echo -e "Uh Oh!! We couldn't figure out what computer you're running..."
36+
echo -e "You can probably still run the container manually though, check"
37+
echo -e "out the GitHub page for help!"
38+
echo -e "\n\thttps://github.com/derickson2402/Dockerized-CAEN"
39+
exit
40+
fi
41+
42+
# Make sure that Docker Engine is installed and running
43+
if ( ! command -v docker &> /dev/null ); then
44+
echo -e "Heads up! Docker is not installed on your computer!"
45+
echo -e "You can download and install it here:"
46+
if [[ "$HOST_OS" == "windows" ]]; then
47+
echo -e "\n\thttps://docs.docker.com/desktop/#download-and-install"
48+
elif [[ "$HOST_OS" == "mac" ]]; then
49+
echo -e "\n\thttps://docs.docker.com/desktop/mac/install/"
50+
else
51+
echo -e "\n\thttps://docs.docker.com/engine/install/"
52+
fi
1053
exit
54+
elif ( ! docker stats --no-stream &> /dev/null ); then
55+
echo -e "Waking up Moby the Whale..."
56+
if [[ "$HOST_OS" == "mac" ]]; then
57+
open /Applications/Docker.app
58+
elif [[ "$HOST_OS" == "windows" ]]; then
59+
/mnt/c/Program\ Files/Docker/Docker/resources/dockerd.exe
60+
elif [[ "$HOST_OS" == "linux" ]]; then
61+
sudo systemctl start docker
62+
fi
63+
sleep 5
64+
declare -i counter=0
65+
while ( ! docker stats --no-stream &> /dev/null ); do
66+
(( counter += 1 ))
67+
echo -e "Waiting for Moby the Whale to wake up..."
68+
sleep 10
69+
if [[ counter -gt 2 ]]; then
70+
echo -e "Couldn't wake up Moby :("
71+
echo -e "Maybe try restarting Docker?"
72+
exit
73+
fi
74+
done
1175
fi
1276

13-
# Process the CLI args and get the specified program
77+
# Process the CLI args, set default vars, and get the specified program
78+
if [ $# -eq 0 ]; then
79+
echo -e "$HELP_MESSAGE" && exit
80+
fi
81+
if ( wget -q --spider http://github.com ); then
82+
CAEN_DEFAULT_ARGS="$CAEN_DEFAULT_ARGS --pull always"
83+
fi
1484
PROGRAM=$1
1585
shift
86+
case $PROGRAM in
87+
help)
88+
echo -e "$HELP_MESSAGE" && exit
89+
;;
90+
perf)
91+
echo -e "Heads up! Perf requires SYS_ADMIN Docker capabilities to run."
92+
echo -e "You should only do this with code that you trust!"
93+
read -p "Hit [Ctrl+c] to cancel, or enter to continue" </dev/tty
94+
CAEN_DEFAULT_ARGS="$CAEN_DEFAULT_ARGS --privileged --cap-add SYS_ADMIN"
95+
;;
96+
esac
1697

1798
# Execute the given program and its args using the CAEN container
18-
# docker run --rm -it -v "$(pwd):/code" ghcr.io/derickson2402/dockerized-caen:latest $PROGRAM $@
19-
docker run --rm -it -v "$(pwd):/code" ghcr.io/derickson2402/dockerized-caen:latest $PROGRAM $@
99+
# Also check for stdin, allowing for file redirection
100+
if [ ! -t 0 ]; then
101+
eval -- docker run "$CAEN_DEFAULT_ARGS" "$CAEN_ARGS" "$CAEN_WORKING_DIR" "${CAEN_REPO_NAME:=ghcr.io/derickson2402/dockerized-caen}":"${CAEN_VERSION:=latest}" "$PROGRAM" "$@" < /dev/stdin
102+
else
103+
CAEN_DEFAULT_ARGS="$CAEN_DEFAULT_ARGS -t"
104+
eval -- docker run "$CAEN_DEFAULT_ARGS" "$CAEN_ARGS" "$CAEN_WORKING_DIR" "${CAEN_REPO_NAME:=ghcr.io/derickson2402/dockerized-caen}":"${CAEN_VERSION:=latest}" "$PROGRAM" "$@"
105+
fi
20106

0 commit comments

Comments
 (0)