-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
executable file
·237 lines (207 loc) · 5.98 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/bin/bash
# color codes
RESTORE='\033[0m'
NC='\033[0m'
BLACK='\033[00;30m'
RED='\033[00;31m'
GREEN='\033[00;32m'
YELLOW='\033[00;33m'
BLUE='\033[00;34m'
PURPLE='\033[00;35m'
CYAN='\033[00;36m'
SEA="\\033[38;5;49m"
LIGHTGRAY='\033[00;37m'
LBLACK='\033[01;30m'
LRED='\033[01;31m'
LGREEN='\033[01;32m'
LYELLOW='\033[01;33m'
LBLUE='\033[01;34m'
LPURPLE='\033[01;35m'
LCYAN='\033[01;36m'
WHITE='\033[01;37m'
OVERWRITE='\e[1A\e[K'
#emoji codes
CHECK_MARK="${GREEN}\xE2\x9C\x94${NC}"
X_MARK="${RED}\xE2\x9C\x96${NC}"
PIN="${RED}\xF0\x9F\x93\x8C${NC}"
CLOCK="${GREEN}\xE2\x8C\x9B${NC}"
ARROW="${SEA}\xE2\x96\xB6${NC}"
BOOK="${RED}\xF0\x9F\x93\x8B${NC}"
HOT="${ORANGE}\xF0\x9F\x94\xA5${NC}"
WARNING="${RED}\xF0\x9F\x9A\xA8${NC}"
RIGHT_ANGLE="${GREEN}\xE2\x88\x9F${NC}"
DOTFILES_LOG="$HOME/.dotfiles.log"
# export ANSIBLE_LOG_PATH=/tmp/ansible_$(date "+%Y%m%d%H%M").log
# touch $ANSIBLE_LOG_PATH
set -e
# Paths
CONFIG_DIR="$HOME/.config/dotfiles"
VAULT_SECRET="$HOME/.ansible-vault/vault.secret"
DOTFILES_DIR="$HOME/.ansible_dotfiles"
SSH_DIR="$HOME/.ssh"
IS_FIRST_RUN="$HOME/.dotfiles_run"
function __task {
# if _task is called while a task was set, complete the previous
if [[ $TASK != "" ]]; then
printf "${OVERWRITE}${LGREEN} [✓] ${LGREEN}${TASK}\n"
fi
# set new task title and print
TASK=$1
printf "${LBLACK} [ ] ${TASK} \n${LRED}"
}
# _cmd performs commands with error checking
function _cmd {
#create log if it doesn't exist
if ! [[ -f $DOTFILES_LOG ]]; then
touch $DOTFILES_LOG
fi
# empty conduro.log
> $DOTFILES_LOG
# hide stdout, on error we print and exit
if eval "$1" 1> /dev/null 2> $DOTFILES_LOG; then
return 0 # success
fi
# read error from log and add spacing
printf "${OVERWRITE}${LRED} [X] ${TASK}${LRED}\n"
while read line; do
printf " ${line}\n"
done < $DOTFILES_LOG
printf "\n"
# remove log file
# rm $DOTFILES_LOG
# exit installation
exit 1
}
function _clear_task {
TASK=""
}
function _task_done {
printf "${OVERWRITE}${LGREEN} [✓] ${LGREEN}${TASK}\n"
_clear_task
}
function ubuntu_setup() {
_cmd "sudo apt-get update"
if ! dpkg -s ansible >/dev/null 2>&1; then
__task "Installing Ansible"
_cmd "sudo apt-get install -y software-properties-common"
_cmd "sudo apt-add-repository -y ppa:ansible/ansible"
_cmd "sudo apt-get update"
_cmd "sudo apt-get install -y ansible"
_cmd "sudo apt-get install python3-argcomplete"
_cmd "sudo activate-global-python-argcomplete3"
fi
if ! dpkg -s python3 >/dev/null 2>&1; then
__task "Installing Python3"
_cmd "sudo apt-get install -y python3 git"
fi
if ! dpkg -s python3-pip >/dev/null 2>&1; then
__task "Installing Python3 Pip"
_cmd "sudo apt-get install -y python3-pip"
fi
if ! pip3 list | grep watchdog >/dev/null 2>&1; then
__task "Installing Python3 Watchdog"
_cmd "sudo apt-get install -y python3-watchdog"
fi
if ! dpkg -s virt-what >/dev/null 2>&1; then
sudo apt install -y virt-what
fi
}
function arch_setup() {
__task "Archlinux install prereqiset"
_cmd "sudo pacman -Sy --needed --noconfirm ansible git python-argcomplete python3 python-pip python-watchdog openssh"
}
function rocky_setup() {
if ! [ -x "$(command -v ansible)" ]; then
__task "Installing Ansible"
_cmd "sudo dnf install epel-release -y"
_cmd "sudo dnf update -y"
_cmd "sudo dnf install ansible-core git -y"
_cmd "sudo dnf install python-argcomplete -y"
# _cmd "sudo activate-global-python-argcomplete3"
fi
if ! [ -x "$(command -v python3)" ]; then
__task "Installing Python3"
_cmd "sudo dnf install python3 git -y"
fi
if ! [ -x "$(command -v pip)" ]; then
__task "Installing Python3 Pip"
_cmd "sudo dnf install python-pip -y"
fi
if ! pip3 list | grep watchdog >/dev/null 2>&1; then
__task "Installing Python3 Watchdog"
_cmd "pip install watchdog"
fi
_cmd "sudo dnf install virt-what -y"
if sudo /usr/sbin/virt-what | grep -q 'kvm'; then
_cmd "sudo dnf install qemu-guest-agent -y"
_cmd "sudo systemctl enable --now qemu-guest-agent"
fi
__task "Setting Locale"
_cmd "sudo localectl set-locale LANG=en_US.UTF-8"
}
# Determine the operating system
if command -v apt &>/dev/null; then
ID=ubuntu
elif command -v dnf &>/dev/null; then
ID=rocky
elif command -v pacman &>/dev/null; then
ID=arch
elif command -v zypper &>/dev/null; then
ID=suse
else
ID=unknown
fi
echo "Detected operating system: $ID"
# Execute setup based on the detected OS
case $ID in
ubuntu) ubuntu_setup ;;
arch) arch_setup ;;
rocky) rocky_setup ;;
*)
__task "Unsupported OS"
_cmd "echo 'Unsupported OS'"
;;
esac
__task "Updating Ansible Galaxy"
_cmd "ansible-galaxy collection install community.general"
_cmd "ansible-galaxy collection install ansible.posix"
_task_done
# if ! [[ -d "$DOTFILES_DIR" ]]; then
# __task "Cloning repository"
# _cmd "git clone -b Pull-Test --quiet https://github.com/Aabayoumy/ansible-pull.git $DOTFILES_DIR"
# else
# __task "Updating repository"
# _cmd "git -C $DOTFILES_DIR pull --quiet"
# fi
# ansible-galaxy install -r requirements.yml
# if ! id "abayoumy" >/dev/null 2>&1; then
# __task "Running playbook";
# ansible-playbook "$DOTFILES_DIR/local.yml" "$@"
# _task_done
# else
# __task "Running playbook";
# ansible-playbook "$DOTFILES_DIR/main.yml" "$@"
# _task_done
# fi
__task "Running playbook ($USER)";
ansible-pull --force -U "https://github.com/Aabayoumy/ansible-pull.git" "$@"
_task_done
# Rename the log file with date and time
LOG_FILE="/tmp/ansible-pull_$(date "+%Y%m%d%H%M").log"
__task "log: $LOG_FILE"
_cmd "mv /tmp/ansible-pull.log $LOG_FILE"
_cmd "/usr/bin/ls /tmp/ansible-pull_*.log -tr | head -n -5 | xargs --no-run-if-empty rm"
_task_done
__task "IP $(ip addr show | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | cut -d/ -f1 | head -1)"
_task_done
if [ -d ~/.ansible ]; then
__task "Clear old folder";
rm -rf ~/.ansible
_task_done
fi
if [ $USER == "root" ]; then
__task "Reboot";
_task_done
reboot
fi
# popd 2>&1 > /dev/null