Project

General

Profile

dbash.sh

Roger Borrello, 06/05/2025 11:33 AM

Download (3.59 KB)

 
1
#!/bin/bash
2

    
3
#set -x
4

    
5
function usage()
6
{
7
   i=0;       usage[$i]="Usage: $0 [-d] [--jdk8] [-c <cmd_line>] [-f [tag]]"
8
   i=$((i+1));usage[$i]="\nCreate a container from the base_ubuntu_22.04 or fwd_4.0_ubuntu_22.04 Docker image"
9
   i=$((i+1));usage[$i]="\nWhere:"
10
   i=$((i+1));usage[$i]="\t-d = dryrun (just display commands)"
11
   i=$((i+1));usage[$i]="\t--jdk8 = Include JDK8 in the container. The default is JDK17."
12
   i=$((i+1));usage[$i]="\t-c <cmd_line> = Pass a command line to the Docker container."
13
   i=$((i+1));usage[$i]="\t-f [tag] = Include FWD in the container. Specify a tag, if desired. (default=${tag}). *MUST be last parameter*"
14
   i=$((i+1));usage[$i]="\n -? or -h or --help = show usage."
15

    
16
   for i in "${usage[@]}"; do
17
      echo -e $i
18
   done
19
}
20

    
21
# Function to determine the next available container name,
22
# if the given container is already running
23
get_container()
24
{
25
   local base_name=$1
26
   local max=$2
27
   local candidate_name="$base_name"
28
   local cid
29

    
30
   for ((i = 0; i < max; i++)); do
31
      # Only bump the name if there is a running container of the initial name
32
      if [ $i -gt 0 ]; then
33
         candidate_name="${base_name}${i}"
34
      fi
35

    
36
      cid=$(docker ps -q -f status=running -f name=^/${candidate_name}$)
37
      if [ -z "$cid" ]; then
38
         echo "$candidate_name"
39
         return
40
      fi
41
   done
42

    
43
   echo "0"
44
}
45

    
46
# This has limited potential. It has 2 limits:
47
# 1) We can only have 1 argument with options
48
# 2) It must be at the end of the list of arguments
49
# Other than that, it works great.
50
getopts_get_optional_argument()
51
{
52
  next_token="${!OPTIND}"
53
  #${parameter:offset:length}
54
  if [[ "${next_token:0:1}" = "-" ]]; then
55
     next_token=${next_token:2}
56
  fi
57
  if [[ -n $next_token && $next_token != -* ]]; then
58
    OPTIND=$((OPTIND + 1))
59
    OPTARG=$next_token
60
  else
61
    unset OPTARG
62
  fi
63
}
64

    
65
# Defaults
66
jdk_version="jdk17"
67
docker_image="goldencode/base_ubuntu_22.04"
68
tag="latest"
69

    
70
while getopts ":h?dc:-:f" opt; do
71
   case $opt in
72
      d  ) mycmd="echo"
73
           ;;
74
      c  ) cmd_line="${OPTARG}"
75
           ;;
76
      -  ) case ${OPTARG} in
77
             "jdk8" ) jdk_version="jdk8" ;;
78
             "help" ) usage
79
                      exit 1 ;;
80
              * ) echo "Unknown option: --${OPTARG}"
81
                  usage
82
                  exit 1 ;;
83
           esac
84
           ;;
85
      f  ) getopts_get_optional_argument "$@"
86
           [ ! -z "${OPTARG}" ] && tag="${OPTARG}"
87
           docker_image="goldencode/fwd_4.0_ubuntu_22.04"
88
           fwd="_fwd"
89
           ;;
90
      h | \? ) if [[ "$OPTARG" == "?" || "$opt" == "h" ]]; then
91
                  usage
92
                  exit 1
93
               else
94
                  echo "Unknown option: -${OPTARG}"
95
                  usage
96
                  exit 1
97
               fi
98
               ;;
99
   esac
100
done
101
shift $(($OPTIND - 1))
102

    
103
max_containers=10  # Maximum builds we allow concurrently
104
container_name="${jdk_version}${fwd}_container_${tag}"
105
[ "$jdk_version" != "jdk8" ] && docker_image+="_${jdk_version}"
106

    
107
check_container_name=$(get_container $container_name $max_containers)
108
if [ "$check_container_name" == "0" ]; then
109
   echo "ERROR: Too many ${container_name} containers (max=${max_containers}) running. "
110
   exit
111
else
112
   container_name=$check_container_name
113
fi
114

    
115
if [ -z "$cmd_line" ]; then
116
   cmd_line="bash"
117
else
118
   cmd_line="sh -c \"${cmd_line}\""
119
fi
120

    
121
volumes="-v ${HOME}:/home/${USER} -v /etc/passwd:/etc/passwd:ro -v /etc/group:/etc/group:ro"
122
dcmd="$mycmd docker run --name $container_name -h $container_name --rm -it $volumes --pull always -u $(id -u):$(id -g) -e HOME=${HOME} -e USER=${USER} -w $(pwd) ${docker_image}:${tag} $cmd_line"
123
echo "Running: $dcmd"
124
eval $dcmd
125