#!/usr/bin/env bash

_ensure_swarm() {
	if [ "$(docker info | grep Swarm | sed 's/Swarm: //g' | tr -d ' ')" == "inactive" ]; then
   	run docker swarm init --advertise-addr 127.0.0.1:2377
   	assert_success
  fi

  if ! docker network ls | grep -q 'proxy'; then
   	run docker network create -d overlay proxy
   	assert_success
  fi

	if ! _is_local; 
	then 
		if [ "$(ssh "$TEST_SERVER" docker info | grep Swarm | sed 's/Swarm: //g' | tr -d ' ')" == "inactive" ]; then
    	run ssh "$TEST_SERVER" docker swarm init --advertise-addr 127.0.0.1:2377
    	assert_success
  	fi

  	if ! ssh "$TEST_SERVER" docker network ls | grep -q 'proxy'; then
    	run ssh "$TEST_SERVER" docker network create -d overlay proxy
    	assert_success
  	fi
	fi
}

_inspect_env_test_app() {
	if _is_local; 
	then 
		containerId="$(docker ps -f name="$TEST_APP_DOMAIN_$TEST_SERVER" -q)" 
		run docker inspect --format='{{range .Config.Env}}{{println .}}{{end}}' "$containerId"
	else 
		containerId="$(ssh "$TEST_SERVER" docker ps -f name="$TEST_APP_DOMAIN" -q)"
		run ssh "$TEST_SERVER" "docker inspect --format='{{range .Config.Env}}{{println .}}{{end}}' '$containerId'"
	fi
}
