-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
executable file
·157 lines (146 loc) · 4.78 KB
/
run.sh
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/bash
# TODO: add mac os install
# TODO: add gitlab-runner install with ansible
# TODO: add nvim install from my repo
# TODO: add arguments for packages
############# CONSTANTS #############
BLACK='\033[30m' # Black
RED='\033[31m' # Red
GREEN='\033[32m' # Green
YELLOW='\033[33m' # Yellow
BLUE='\033[34m' # Blue
PURPLE='\033[35m' # Purple
CYAN='\033[36m' # Cyan
WHITE='\033[37m' # White
NC='\033[0m' # No color
############# END CONST ##############
############################## FUNCTIONS BLOCK ##############################
check_prerequisites() {
if ! command -v pip3 &> /dev/null
then
echo -e "Please install ${BLUE}pip3${NC} for python3"
exit 1
fi
}
proof_state() {
local yn=$(echo "$1" | tr '[:upper:]' '[:lower:]')
if [[ -z "$yn" ]]
then
yn="y"
fi
if [[ "$yn" = "y" || "$yn" = "yes" ]]
then
echo "true"
else
echo ""
fi
}
install_prerequisites() {
if ! command -v ansible &> /dev/null & ! command -v sshpass &> /dev/null
then
echo -e "${BLUE}Install prerequisites${NC}"
echo "-----------"
if [[ $(uname -s) == "Linux" ]]
then
sudo apt-get update
fi
######## Install ansible ########
if ! command -v ansible &> /dev/null
then
echo -e "${BLUE}Install ansible${NC}"
if [[ $(uname -s) == "Linux" ]]
then
sudo apt install -y ansible
fi
if [[ $(uname -s) == "Darwin" ]]
then
pip3 install ansible
else
echo -e "${RED}Can't install lib ansible${NC}"
exit 1
fi
echo -e "${CYAN}ansible ${GREEN}successfully installed${NC}"
else
echo -e "${CYAN}ansible ${GREEN}already installed${NC}"
fi
echo "-----------"
######## Install sshpass #######
if ! command -v sshpass &> /dev/null
then
echo -e "${BLUE}Install sshpass${NC}"
if [[ $(uname -s) == "Linux" ]]
then
sudo apt install -y sshpass
fi
if [[ $(uname -s) == 'Darwin' ]]
then
curl -O -L -k https://sourceforge.net/projects/sshpass/files/sshpass/1.06/sshpass-1.06.tar.gz \
&& tar xvzf sshpass-1.06.tar.gz \
&& cd sshpass-1.06 \
&& ./configure \
echo -e "${BLUE}Enter sudo password for sshpass install${NC}"
sudo make install \
&& cd ../ \
&& rm -rf sshpass-1.06 sshpass-1.06.tar.gz
else
echo -e "${RED}Can't install lib sshpass${NC}"
exit 1
fi
echo -e "${CYAN}sshpass ${GREEN}successfully installed${NC}"
else
echo -e "${CYAN}sshpass ${GREEN}already installed${NC}"
fi
echo "-----------"
fi
}
create_ssh_key_pair() {
echo -e "${BLUE}Generate ssh-keys${NC}"
read -p "Enter file in which to save the key (default: id_rsa): " key_name
if [[ -z "$key_name" ]]
then
key_name="id_rsa"
fi
echo "y" | ssh-keygen -t rsa -b 4096 -C "key for host" -f "$HOME/.ssh/${key_name}"
export CURRENT_KEY_NAME="${HOME}/.ssh/${key_name}.pub"
echo -e "${CYAN}SSH keys ${GREEN}successfully created${NC}"
echo "-----------"
}
run_ansible() {
file="./vars/default.yml"
echo -e "${BLUE}Start remote host(s) setup${NC}"
read -p "Enter username: " remote_user_name
pip3 install passlib &> /dev/null
user_password=$(python3 -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass('Enter user password: ')))")
export REMOTE_USER_PASSWORD="${user_password}"
echo "---" > "$file"
echo "user: ${remote_user_name}" >> "$file"
echo "sys_packages: ['vim', 'curl', 'git', 'tmux', 'htop']" >> "$file"
echo ""
read -p "Dow you want to rewrite ./hosts file? [Y/n]: " yn
if [[ ! -z "$(proof_state $yn)" ]]
then
echo -e "Enter all hosts. Type enter after ending"
echo "[all]" > hosts
read host
while [[ ! -z "$host" ]]
do
echo "$host" >> hosts
read host
done
fi
echo -e "${CYAN}Presetup ${GREEN}successfully ended${NC}"
echo "-----------"
echo -e "${BLUE}Start configure remote user${NC}"
read -p "Enter existing remote user name with sudo root's: " existing_remote_user
read -p "Enter password: " -s pass
echo ""
echo "Enter password again"
ansible-playbook playbook.yml --extra-vars "ansible_user=$existing_remote_user ansible_password=$pass" --ask-become-pass
}
############################ END FUNCTIONS BLOCK ##########################
############################### MAIN BLOCK ################################
set -e
check_prerequisites
install_prerequisites
create_ssh_key_pair
run_ansible