summaryrefslogtreecommitdiff
path: root/dactyl.sh
blob: 7ccc387a56b2cc067aa896cb598fab547cdfa983 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
#!/bin/bash

#                   *******************                   #
# *******************      setup      ******************* #
#                   *******************                   #

# exit if any errors are thrown
set -e

container=""
# bad practice:
    # container variable names MUST match
    # the positional name {positional}Container
shellContainer=DM-shell
runContainer=DM-run
configContainer=DM-config
releaseBuildContainer=DM-release-build
containers=("$shellContainer" "$configContainer" "$runContainer" "$releaseBuildContainer")

imageName=dactyl-keyboard
srcBind="$(pwd)/src:/app/src"
thingsBind="$(pwd)/things:/app/things"

# force exit on interrupt in case we are in a menu
function catch_interrupt() { exit 1; }
trap catch_interrupt SIGINT
trap catch_interrupt SIGTSTP

#                   *******************                   #
# *******************    functions    ******************* #
#                   *******************                   #

################################
# General Helpers
################################

function inform() {
  echo -e "\n[INFO] $@\n"
}

function warn() {
  echo -e "\n[WARN] $@\n"
}

function error() {
  echo -e "\n[ERROR] $@\n"
}

function exitUnexpectedPositionalArgs() {
  error "Unexpected positionnal argument.\n\n\tAlready had: $positional\n\n\tAnd then got: $1"
  exit 1
}

function exitUnexpectedFlags() {
  error "One or more flags are invalid:\n\n\tPositional: $positional\n\n\tFlags: $flags"
  exit 1
}

################################
# Interactive Menu
################################

# https://unix.stackexchange.com/questions/146570/arrow-key-enter-menu
#  Arguments:
#    array of options
#
#  Return value:
#    selected index (0 for opt1, 1 for opt2 ...)

function menu {
  local header="\n[Dactyl Manuform]"
  if [ $container ]; then
    header+=" -- $container"
  fi
  header+="\n\nPlease choose an option:\n\n"
  printf "$header"
	options=("$@")

	# helpers for terminal print control and key input
	ESC=$(printf "\033")
	cursor_blink_on()	{ printf "$ESC[?25h"; }
	cursor_blink_off()	{ printf "$ESC[?25l"; }
	cursor_to()			{ printf "$ESC[$1;${2:-1}H"; }
	print_option()		{ printf "\t   $1 "; }
	print_selected()	{ printf "\t${COLOR_GREEN}  $ESC[7m $1 $ESC[27m${NC}"; }
	get_cursor_row()	{ IFS=';' read -sdR -p $'\E[6n' ROW COL; echo ${ROW#*[}; }
  key_input() {
    local key
    # read 3 characters, 1 at a time
    for (( i=0; i < 3; ++i)); do
      read -s -n1 input 2>/dev/null >&2
      # concatenate chars together
      key+="$input"
      # if a number is encountered, echo it back
      if [[ $input =~ ^[1-9]$ ]]; then
        echo $input; return;
      # if enter, early return
      elif [[ $input = "" ]]; then
        echo enter; return;
      # if we encounter something other than [1-9] or "" or the escape sequence
      # then consider it an invalid input and exit without echoing back
      elif [[ ! $input = $ESC && i -eq 0 ]]; then
        return
      fi
    done

    if [[ $key = $ESC[A ]]; then echo up; fi;
    if [[ $key = $ESC[B ]]; then echo down; fi;
  }
  function cursorUp() { printf "$ESC[A"; }
  function clearRow() { printf "$ESC[2K\r"; }
  function eraseMenu() {
    cursor_to $lastrow
    clearRow
    numHeaderRows=$(printf "$header" | wc -l)
    numOptions=${#options[@]}
    numRows=$(($numHeaderRows + $numOptions))
    for ((i=0; i<$numRows; ++i)); do
      cursorUp; clearRow;
    done
  }

	# initially print empty new lines (scroll down if at bottom of screen)
	for opt in "${options[@]}"; do printf "\n"; done

	# determine current screen position for overwriting the options
	local lastrow=`get_cursor_row`
	local startrow=$(($lastrow - $#))
  local selected=0

	# ensure cursor and input echoing back on upon a ctrl+c during read -s
	trap "cursor_blink_on; stty echo; printf '\n'; exit" 2
	cursor_blink_off

	while true; do
    # print options by overwriting the last lines
		local idx=0
    for opt in "${options[@]}"; do
      cursor_to $(($startrow + $idx))
      # add an index to the option
      local label="$(($idx + 1)). $opt"
      if [ $idx -eq $selected ]; then
        print_selected "$label"
      else
        print_option "$label"
      fi
      ((idx++))
    done

		# user key control
    input=$(key_input)

		case $input in
			enter) break;;
      [1-9])
        # If a digit is encountered, consider it a selection (if within range)
        if [ $input -lt $(($# + 1)) ]; then
          selected=$(($input - 1))
          break
        fi
        ;;
			up)	((selected--));
					if [ $selected -lt 0 ]; then selected=$(($# - 1)); fi;;
			down)  ((selected++));
					if [ $selected -ge $# ]; then selected=0; fi;;
		esac
	done

  eraseMenu
	cursor_blink_on

	return $selected
}


################################
# Setup helpers
################################

function showHelpAndExit() {
cat << _end_of_text
[Dactyl Manuform]

A bash CLI to manage Docker artifacts for the Dactyl Keyboard project.

Run the script without any flags to use the interactive menu.

Usage:
  ./dactyl.sh 
  ./dactyl.sh [-h | --help | --uninstall]
  ./dactyl.sh image [--build | --inspect | --remove]
  ./dactyl.sh (config|run|releaseBuild) [--build | --inspect | --start | --stop | --remove]
  ./dactyl.sh shell [--build | --inspect | --session | --start | --stop | --remove]

Options:
  positional    Target the image or a particular container (shell | config | run | releaseBuild)
  -h --help     Show this screen.
  --uninstall   Remove all Docker artifacts.
  --build       Build (or rebuild) and run the target container.
  --inspect     Show "docker inspect" results for the target container.
  --start       Start or restart the target container.
  --stop        Stop the target container.
  --remove      Remove the target container.
  --session     Start a shell session in the shell container.
_end_of_text
exit
}

# error on any unexpected flags or more than one positional argument
function processArgs() {
  while [[ $# -gt 0 ]]
    do
      key="$1"

      case $key in
        --build|--remove|--inspect|--session|--start|--stop)
          if [[ $flags ]]; then
            flags+=" $key"
          else
            flags=$key
          fi
          shift;;
        -h|--help) showHelpAndExit;;
        --uninstall) handleUninstall;;
        *)
          # all valid flags should have already been captured above
          if [[ $key == -* ]]; then
            error "Unknwon flag: $key"
            exit 1
          # if we already have a positional argument we shouldn't get another
          elif [[ "$positional" ]]; then
            exitUnexpectedPositionalArgs $key
            exit 1
          # the totality of accepted positional arguments
          elif [[ "$key" =~ ^(image|shell|config|run|releaseBuild)$ ]]; then
            positional="$key"
            if [[ ! $key = image ]]; then
              key+="Container"
              container=$(echo "${!key}")
            fi
            shift
          else
            error "Unknown positional arg: \"$key\""
            exit 1
          fi
          ;;
      esac
    done
}

# installing docker is out of scope
# so if it isn't found, inform user and exit
function checkDocker() {
  if ! which docker &> /dev/null; then
    error "Docker is not installed.\n\n\tPlease visit https://www.docker.com/products/docker-desktop for more information."
    exit 1
  fi
}

# exit unless user responds with yes
function confirmContinue() {
  while true; do
    read -p "$@ [y/n]" yn
    case $yn in
        [Yy]* ) break;;
        [Nn]* ) exit 0;;
        * ) error "Please answer yes or no.";;
    esac
  done
}

################################
# Image Logic
################################

function imageExists() {
  docker image list | grep "$imageName" &> /dev/null
}

function buildImage() {
  inform "Building docker image: $imageName..."
  docker build -t dactyl-keyboard -f docker/Dockerfile .
}

function promptBuildImageIfNotExists() {
  if ! imageExists; then
    inform "Docker image not found: $imageName"
    confirmContinue "Would you like to build it now?"
    buildImage
  fi
}

# image will always exist if we are here
function handleRebuildImage() {
  warn "Docker image already exists: $imageName"
  confirmContinue "Would you like to overwrite it?"
  buildImage
}

function removeImage() {
  inform "Removing docker image: $imageName..."
  docker image rm $imageName
}

function handleRemoveImage() {
  warn "This will remove docker image: $imageName"
  confirmContinue "Would you like to continue?"
  removeImage
}

function handleInspectImage() {
  inform "Checking status of image: $imageName"
  docker image inspect $imageName
}

function handleImageMenu() {
  local check="Check Image Status"
  local build="Rebuild Image"
  local remove="Remove Image"
  local mainMenu="Main Menu"
  local end="Exit"
  options=("$check" "$build" "$remove" "$mainMenu" "$end")
  # execute in subshell so exit code doesn't exit the script
  (menu "${options[@]}") && true
  result="${options[$?]}"

  case $result in
    $check) handleInspectImage;;
    $build) handleRebuildImage;;
    $remove) handleRemoveImage;;
    $mainMenu) handleMainMenu;;
    *) exit;;
  esac
}

# if we made it this far, image is confirmed to exist
function handleImageCLI() {
  if [[ ! "$flags" ]]; then
    handleImageMenu
  elif [[ "$flags" =~ ^.*(--inspect).*$ ]]; then
    handleInspectImage
  elif [[ "$flags" =~ ^.*(--build).*$ ]]; then
    handleRebuildImage
  elif [[ "$flags" =~ ^.*(--remove).*$ ]]; then
    handleRemoveImage
  else
    exitUnexpectedFlags
  fi
}

################################
# Container Helpers
################################

function containerExists() {
  docker container list -a | grep "$container" &> /dev/null
}

function containerIsRunning() {
  if ! containerExists "$container"; then
    return 1
  fi

  docker container inspect $container | grep '"Status": "running",' &> /dev/null
}

function isShell() {
  test $container = $shellContainer
}

function promptIfShell() {
  if isShell; then
    promptStartShellSession
  fi
}

function buildContainerAndExecutePythonScript() {
  docker run --name $container -d -v "$srcBind" -v "$thingsBind" $imageName python3 -i $1
}

function buildContainer() {
  inform "Building docker container: $container..."
  case $container in
    $shellContainer)
      docker run --name $shellContainer -d -it -v "$srcBind" -v "$thingsBind" $imageName
      ;;
    $runContainer)
      buildContainerAndExecutePythonScript dactyl_manuform.py
      ;;
    $configContainer)
      buildContainerAndExecutePythonScript generate_configuration.py
      ;;
    $releaseBuildContainer)
      buildContainerAndExecutePythonScript model_builder.py
      ;;
    *)
      error "Unexpected exception. Containier: $container"
      exit 1;;
  esac
  echo
}

function buildContainerIfNotExists() {
  if ! containerExists; then
    warn "Container not found: $container"
    confirmContinue "Would you like to build it now?"
    buildContainer
  fi
}

function startContainer() {
  docker container start $container &> /dev/null
}

function promptStartContainerIfNotRunning() {
  buildContainerIfNotExists
  if ! containerIsRunning; then
    warn "Container is not running: $container"
    confirmContinue "Would you like to start it now?"
    startContainer
  fi
}

function startContainerIfNotRunning() {
  buildContainerIfNotExists
  if ! containerIsRunning; then
    inform "Starting docker container: $container"
    startContainer
  fi
}

function startContainerOrAlert() {
  if containerIsRunning; then
    inform "Container is already running: $shellContainer"
  else
    startContainerIfNotRunning
  fi

  if isShell; then
    promptStartShellSession
  fi
}

function stopContainer() {
  if containerIsRunning; then
    inform "Stopping docker container: $container..."
    docker container stop $container &> /dev/null
    docker container wait $container &> /dev/null
  fi
}

function handleStopContainer() {
  if ! containerExists; then
    warn "Docker container does not exist: $container"
  elif ! containerIsRunning; then
    inform "Container is already stopped: $container"
  else
    stopContainer
  fi
}

function removeContainer() {
  if containerExists; then
    stopContainer
    inform "Removing docker container: $container..."
    docker container rm $container &> /dev/null
  fi
}

function inspectContainer() {
  if ! containerExists; then
    inform "Container \"$container\" does not exist."
    confirmContinue "Would you like to build it?"
    buildContainer
  fi

  docker container inspect $container
}

function handleBuildContainer() {
  if containerExists; then
    warn "Container already exists: $container"
    confirmContinue "Would you like to overwrite it?"
    removeContainer
  fi

  buildContainer
  promptIfShell
}

function handleContainerMenu() {
  local build="Rebuild $container Container"
  local start="Start $container Container"
  local stop="Stop $container Container"
  local remove="Remove $container Container"
  local inspect="Inspect $container Container"
  local session="Start $container Session"
  local main="Main Menu"
  local end="Exit"

  if ! containerExists; then
    build="Build and run $container Container"
    options=("$build" "$main" "$end")
  elif containerIsRunning; then
    options=("$session" "$inspect" "$build" "$stop" "$remove" "$main" "$end")
    if ! isShell; then
      unset options[0]
    fi
  else
    options=("$inspect" "$build" "$start" "$remove" "$main" "$end")
  fi
  
  # execute in subshell so exit code doesn't exit the script
  (menu "${options[@]}") && true
  result="${options[$?]}"

  case $result in
    $build) handleBuildContainer;;
    $start) startContainerOrAlert;;
    $stop) handleStopContainer;;
    $remove) removeContainer;;
    $inspect) inspectContainer;;
    $main) handleMainMenu;;
    *) 
      if isShell && [[ $session = $result ]]; then
        startShellSession
      fi
      exit
      ;;
  esac
}

function handleContainerCLI() {
  if [[ ! "$flags" ]]; then
    handleContainerMenu
  elif [[ "$flags" =~ ^.*(--inspect).*$ ]]; then
    inspectContainer
  elif [[ "$flags" =~ ^.*(--build).*$ ]]; then
    handleBuildContainer
  elif [[ "$flags" =~ ^.*(--session).*$ ]] && isShell; then
    startShellSession
  elif [[ "$flags" =~ ^.*(--start).*$ ]]; then
    startContainerOrAlert
  elif [[ "$flags" =~ ^.*(--stop).*$ ]]; then
    handleStopContainer
  elif [[ "$flags" =~ ^.*(--remove).*$ ]]; then
    removeContainer
  else
    exitUnexpectedFlags
  fi
}

################################
# Shell Specific Logic
################################

function startShellSession() {
  promptStartContainerIfNotRunning
  inform "Starting session in container: $shellContainer\n\n\tType \"exit\" to terminate the session."
  docker exec -it $shellContainer /bin/bash
}

function promptStartShellSession() {
  confirmContinue "Would you like to start a shell session?"
  startShellSession
}

################################
# Uninstaller
################################

function handleUninstall() {
  warn "This will remove all containers and images."
  confirmContinue "Are you sure you want to continue?"
  for currentContainer in "${containers[@]}"; do
    container="$currentContainer"
    removeContainer
  done

  removeImage
  exit
}

################################
# Main Menu Logic
################################

function handleMainMenu() {
  container=""

  local imageOpt="Manage Docker Image"
  local shellOpt="$shellContainer Container"
  local configOpt="$configContainer Container"
  local releaseOpt="$releaseBuildContainer Container"
  local runOpt="$runContainer Container"
  local uninstallOpt="Uninstall"
  local help="Show Help"
  local end="Exit"

  options=("$imageOpt" "$shellOpt" "$configOpt" "$runOpt" "$releaseOpt" "$help" "$uninstallOpt" "$end")
  
  # execute in subshell so exit code doesn't exit the script
  (menu "${options[@]}") && true
  result="${options[$?]}"

  case $result in
    $help) showHelpAndExit;;
    $imageOpt) handleImageMenu;;
    $uninstallOpt) handleUninstall;;
    $shellOpt|$configOpt|$runOpt|$releaseOpt)
      # remove " Container" and set as currentn container
      container=$(echo "${result/ Container/}")
      handleContainerMenu
      ;;
    * ) exit;;
  esac
}

#                   *******************                   #
# *******************      main       ******************* #
#                   *******************                   #

# figure out why we're running the script
processArgs $@

# exit if `docker` command not available
checkDocker

# make sure the base image has been built
promptBuildImageIfNotExists

# main switchboard to act depending on which positionl arg was passed

if [[ "$positional" ]]; then
  case $positional in
    image) handleImageCLI;;
    shell|config|run|releaseBuild) handleContainerCLI;;
    *) exitUnexpectedPositionalArgs;;
  esac
else
  handleMainMenu
fi