forked from patrikarlos/NSO_A2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install
executable file
·117 lines (110 loc) · 3.8 KB
/
install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/bash
#private key is to be given as 3rd argument.
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <openrc> <tag> <ssh_key>"
exit 1
fi
OPENRC=$1
TAG=$2
SSH_KEY=$3
SERVERS_FIP="servers_fip"
# Function to check and install necessary dependencies
install_dependencies() {
touch install.log
chmod 777 install.log
echo "Checking and installing necessary dependencies..."
if ! command -v python3 &> install.log; then
sudo apt update -y > install.log
sudo apt install -y python3 > install.log
fi
if ! command -v pip3 &> install.log; then
sudo apt install -y python3-pip > install.log
fi
if ! command -v openstack &> install.log; then
sudo apt install -y python3-openstackclient > install.log
fi
if ! command -v ansible &> install.log; then
sudo add-apt-repository --yes --update ppa:ansible/ansible > install.log
sudo apt install -y ansible > install.log
fi
if ! dpkg-query -W -f='${Status}' software-properties-common 2>install.log | grep -q "ok installed"; then
sudo apt install -y software-properties-common > install.log
fi
pip3 install python-openstackclient argparse subprocess32 python-openstacksdk > install.log
}
# Function to set up permissions
setup_permissions() {
chmod 777 scripts/Deploy.py
chmod 777 scripts/gen_config.py
chmod 777 scripts/site.yaml
chmod 777 operate
chmod 777 cleanup
}
# Function to invoke the Python script
invoke_python_script() {
source $OPENRC
echo "sourced $OPENRC"
python3 scripts/Deploy.py $OPENRC $TAG $SSH_KEY || exit 1
echo "executing next script"
python3 scripts/gen_config.py $TAG $SSH_KEY || exit 1
echo "python script executed"
}
function ssh_to_servers {
echo "Setting up SSH keys on servers..."
local servers_file=$1
local ssh_key=$2
if [ ! -f "$servers_file" ]; then
echo "File $servers_file not found!"
exit 1
fi
# Read the file and store IP addresses in variables
local count=1
while IFS=: read -r host ip; do
ip=$(echo "$ip" | xargs) # Trim any potential whitespace
if [ $count -eq 1 ]; then
host1=$ip
elif [ $count -eq 2 ]; then
host2=$ip
elif [ $count -eq 3 ]; then
host3=$ip
fi
count=$((count + 1))
done < "$servers_file"
# Perform SSH connections
if [ -n "$host1" ]; then
#echo "Connecting to Host1 at $host1"
ssh -i "$ssh_key" -o StrictHostKeyChecking=no -t ubuntu@"$host1" "echo '$(cat $ssh_key)' > ~/.ssh/id_rsa && chmod 600 ~/.ssh/id_rsa && exit" &>install.log
fi
if [ -n "$host2" ]; then
#echo "Connecting to Host2 at $host2"
ssh -i "$ssh_key" -o StrictHostKeyChecking=no -t ubuntu@"$host2" "echo '$(cat $ssh_key)' > ~/.ssh/id_rsa && chmod 600 ~/.ssh/id_rsa && exit" &>install.log
fi
if [ -n "$host3" ]; then
#echo "Connecting to Host3 at $host3"
ssh -i "$ssh_key" -o StrictHostKeyChecking=no -t ubuntu@"$host3" "echo '$(cat $ssh_key)' > ~/.ssh/id_rsa && chmod 600 ~/.ssh/id_rsa && exit" &>install.log
fi
}
ansible_playbook() {
echo "Executing ansible-playbook.."
# Set the ANSIBLE_CONFIG environment variable
export ANSIBLE_CONFIG=ansible.cfg
echo "Ansible config set to $ANSIBLE_CONFIG"
sleep 40
echo "Checking ping to hosts..."
ansible all -m ping -i hosts || exit 1
if [ $? -eq 0 ]; then
echo "Ping successful to hosts."
echo "Executing ansible-playbook.."
ansible-playbook -i hosts scripts/site.yaml || exit 1
else
echo "Ping not successful to hosts."
fi
}
# Main script execution
install_dependencies
setup_permissions
invoke_python_script
ssh_to_servers $SERVERS_FIP $SSH_KEY
ansible_playbook
exit 1
echo "Installation and configuration completed successfully."