#!/bin/bash
echo $0 started at $(date)

. ./common-vars
. ./main_credentials.rc

for PARTICIPANT in `seq ${START} ${MAX}` ; do
    VM_NAME=`expr charmedosm-${PARTICIPANT}`
    IP=`openstack server list --name $VM_NAME --column Networks -f yaml | head -3 | tail -1 | grep -o '[0-9]\+[.][0-9]\+[.][0-9]\+[.][0-9]\+'`
    OS_USERNAME=hackfest15group${PARTICIPANT}
    OS_PASSWORD=hackfest15group-${PARTICIPANT}!
    OS_PROJECT_NAME=hackfest15group${PARTICIPANT}

    cat << EOF > ${OS_USERNAME}.rc
export OS_AUTH_TYPE=password
export OS_AUTH_URL=${OS_AUTH_URL}
export OS_DOMAIN_NAME=${OS_DOMAIN_NAME}
export OS_IDENTITY_API_VERSION=3
export OS_INTERFACE=public
export OS_PASSWORD=${OS_PASSWORD}
export OS_PROJECT_DOMAIN_NAME=${OS_DOMAIN_NAME}
export OS_PROJECT_NAME=${OS_PROJECT_NAME}
export OS_REGION_NAME=${OS_REGION_NAME}
export OS_TENANT_NAME=${OS_USERNAME}
export OS_USERNAME=${OS_USERNAME}
export OS_USER_DOMAIN_NAME=${OS_DOMAIN_NAME}
EOF

    ssh -o StrictHostKeyChecking=no -i hackfest_rsa ubuntu@${IP} ". .profile;osm version"
    while [ $? -ne -0 ] ; do
        ssh -o StrictHostKeyChecking=no -i hackfest_rsa ubuntu@${IP} ". .profile;osm version"
    done
    scp -o StrictHostKeyChecking=no -i hackfest_rsa ${OS_USERNAME}.rc ubuntu@${IP}: &
    scp -o StrictHostKeyChecking=no -i hackfest_rsa hackfest-k8s.yaml ubuntu@${IP}:~/kubeconfig.yaml &
    sleep 10
    ssh -o StrictHostKeyChecking=no -i hackfest_rsa ubuntu@${IP} ". .profile;osm vim-create --name openstack --user ${OS_USERNAME} --password ${OS_PASSWORD} --auth_url ${OS_AUTH_URL} --tenant ${OS_PROJECT_NAME} --account_type openstack --config=\"{ management_network_name: osm-ext, security_groups: default, insecure: true, project_domain_name: ${OS_PROJECT_DOMAIN_NAME}, user_domain_name: ${OS_USER_DOMAIN_NAME} }\"" 2>&1 | tee -a logs/osm-vim-k8scluster-add-${PARTICIPANT}.log
    ssh -o StrictHostKeyChecking=no -i hackfest_rsa ubuntu@${IP} ". .profile;osm k8scluster-add --creds ~/kubeconfig.yaml --vim openstack --k8s-nets '{'net1': 'osm-ext'}' --version 1.26 --namespace hackfest --description 'Microk8s cluster' hackfest" 2>&1 | tee -a logs/osm-vim-k8scluster-add-${PARTICIPANT}.log
    ssh -o StrictHostKeyChecking=no -i hackfest_rsa ubuntu@${IP} "echo . ~/${OS_USERNAME}.rc >> .bashrc"
done

echo $0 $@ complete at $(date)