#!/bin/sh

set -e


# Setup the SSH directory
mkdir -p $HOME/.ssh
chmod 0700 $HOME/.ssh

# Create an ssh key to ssh to localhost
ssh-keygen -N "" -f $HOME/.ssh/cumin_key

# Authorize the key for the current user
cat $HOME/.ssh/cumin_key.pub >> $HOME/.ssh/authorized_keys

# Create an ssh_config file for cumin
cat << EOF > "$AUTOPKGTEST_TMP/ssh_config"
Host localhost
  ConnectTimeout 2
  GlobalKnownHostsFile /dev/null
  IdentitiesOnly yes
  IdentityFile $HOME/.ssh/cumin_key
  LogLevel ERROR
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
EOF

# Create a configuration file for cumin
cat > "$AUTOPKGTEST_TMP/config.yaml" << EOF
transport: clustershell
log_file: $AUTOPKGTEST_TMP/cumin.log
default_backend: direct

clustershell:
    ssh_options:
        - '-F $AUTOPKGTEST_TMP/ssh_config'
EOF

# Run a simple full cumin command against localhost
cumin --force --config "$AUTOPKGTEST_TMP/config.yaml" "localhost" "id"
