#!/bin/bash

# Copyright 2014,2015,2016,2017,2018,2019,2020 Security Onion Solutions, LLC
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

. /usr/sbin/so-common

manager_check() {
  # Check to see if this is a manager
  MANAGERCHECK=$(cat /etc/salt/grains | grep role | awk '{print $2}')
  if [ $MANAGERCHECK == 'so-eval' ] || [ $MANAGERCHECK == 'so-manager' ] || [ $MANAGERCHECK == 'so-managersearch' ] || [ $MANAGERCHECK == 'so-standalone' ] || [ $MANAGERCHECK == 'so-helix' ]; then
    echo "This is a manager. We can proceed"
  else
    echo "Please run soup on the manager. The manager controls all updates."
    exit 1
  fi
}

update_docker_containers() {
  
  # Download the containers from the interwebs
  for i in "${TRUSTED_CONTAINERS[@]}"
  do
    # Pull down the trusted docker image
    echo "Downloading $i"
    docker pull --disable-content-trust=false docker.io/$IMAGEREPO/$i
    # Tag it with the new registry destination
    docker tag $IMAGEREPO/$i $HOSTNAME:5000/$IMAGEREPO/$i
    docker push $HOSTNAME:5000/$IMAGEREPO/$i
  done
  
}

version_check() {
  if [ -f /etc/soversion ]; then
    VERSION=$(cat /etc/soversion)
  else
    echo "Unable to detect version. I will now terminate."
    exit 1
  fi
}

manager_check
version_check

# Use the hostname
HOSTNAME=$(hostname)
# List all the containers
if [ $MANAGERCHECK != 'so-helix' ]; then
    TRUSTED_CONTAINERS=( \
    "so-acng:$VERSION" \
    "so-thehive-cortex:$VERSION" \
    "so-curator:$VERSION" \
    "so-domainstats:$VERSION" \
    "so-elastalert:$VERSION" \
    "so-elasticsearch:$VERSION" \
    "so-filebeat:$VERSION" \
    "so-fleet:$VERSION" \
    "so-fleet-launcher:$VERSION" \
    "so-freqserver:$VERSION" \
    "so-grafana:$VERSION" \
    "so-idstools:$VERSION" \
    "so-influxdb:$VERSION" \
    "so-kibana:$VERSION" \
    "so-kratos:$VERSION" \
    "so-logstash:$VERSION" \
    "so-minio:$VERSION" \
    "so-mysql:$VERSION" \
    "so-nginx:$VERSION" \
    "so-pcaptools:$VERSION" \
    "so-playbook:$VERSION" \
    "so-redis:$VERSION" \
    "so-soc:$VERSION" \
    "so-soctopus:$VERSION" \
    "so-steno:$VERSION" \
    "so-strelka-frontend:$VERSION" \
		"so-strelka-manager:$VERSION" \
		"so-strelka-backend:$VERSION" \
		"so-strelka-filestream:$VERSION" \
    "so-suricata:$VERSION" \
    "so-telegraf:$VERSION" \
    "so-thehive:$VERSION" \
    "so-thehive-es:$VERSION" \
    "so-wazuh:$VERSION" \
    "so-zeek:$VERSION" )
  else
    TRUSTED_CONTAINERS=( \
    "so-filebeat:$VERSION" \
    "so-idstools:$VERSION" \
    "so-logstash:$VERSION" \
    "so-nginx:$VERSION" \
    "so-redis:$VERSION" \
    "so-steno:$VERSION" \
    "so-suricata:$VERSION" \
    "so-telegraf:$VERSION" \
    "so-zeek:$VERSION" )
  fi

update_docker_containers
