<commit_before>#!/bin/bash
cd database-server-ubuntu
docker build -t cloudstack-management-database .
cd ..
cd management-server-ubuntu
docker build -t cloudstack-management-server .
cd ..
docker run -d --name cloudstack-db -p 3306:3306 cloudstack-management-database
docker run -d --link cloudstack-db:db --name cloudstack-mgmt -p 8080:8080 cloudstack-management-server<commit_msg>Refactor of build-and-run to handle versioning, proper tagging of containers, breaking processes into functions, and handling restarting containers after a reboot of the docker host.<commit_after>#!/bin/bash
CLOUDSTACK_VERSION=4.3

check_container(){
  CHECK_FOR_CONTAINER=`docker images | grep -o cloudstack-$1 | uniq`
  if [[ $CHECK_FOR_CONTAINER == "cloudstack-$1" ]]; then
    CHECK_CONTAINER_VERSION=`docker images | grep cloudstack-$1 | awk '{ print $2 }'`
    if [[ $CHECK_CONTAINER_VERSION == *"$CLOUDSTACK_VERSION"* ]]; then
      echo "CloudStack $1 Container is up-to-date. Skipping build."
    else
      echo "CloudStack $1 Container is not up-to-date. Building."
      build_container $1
    fi
  else
    echo "CloudStack $1 Container does not exist. Building."
    build_container $1
  fi
}

build_container(){
  cd $1-server-ubuntu
  docker build -t cloudstack-$1:$CLOUDSTACK_VERSION .
  cd .. 
}

check_db_container_running(){
  CHECK_DB_RUNNING=`docker ps | grep -o cloudstack-database | uniq`
  if [[ $CHECK_DB_RUNNING == "cloudstack-database" ]]; then
    echo "CloudStack Database running. Not starting."
  else
    CHECK_DB_EXISTS=`docker ps -a | grep -o cloudstack-database`
    if [[ $CHECK_DB_EXISTS == "cloudstack-database" ]]; then
      echo "CloudStack Database Container exists, but not running. Starting it now."
      # For some reason, this container won't restart from stop, but DB data exists outside the container.
      docker rm cloudstack-db
      check_db_container_running
    else
      docker run -d --name cloudstack-db -p 3306:3306 cloudstack-database:$CLOUDSTACK_VERSION
    fi
  fi
}

check_mgmt_container_running(){
  CHECK_MGMT_RUNNING=`docker ps | grep -o cloudstack-management | uniq`
  if [[ $CHECK_MGMT_RUNNING == "cloudstack-management" ]]; then
    echo "CloudStack Management Server running. Not starting."
  else
    CHECK_DB_EXISTS=`docker ps -a | grep -o cloudstack-management`
    if [[ $CHECK_DB_EXISTS == "cloudstack-management" ]]; then
      echo "CloudStack Management Server Container exists, but not running. Starting it now."
      docker rm cloudstack-mgmt
      check_mgmt_container_running
    else
      docker run -d --name cloudstack-mgmt --link cloudstack-db:db -p 8080:8080 cloudstack-management:$CLOUDSTACK_VERSION
    fi
  fi
}

check_container "database"
check_container "management"
check_db_container_running
sleep 5
check_mgmt_container_running
{'fraction_non_alphanumeric': 0.09754360982556069, 'fraction_numerical': 0.016375934496262016, 'mean_word_length': 3.9211908931698773, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5126808', 'n_tokens_mistral': 923, 'n_tokens_neox': 856, 'n_words': 256}