-
Notifications
You must be signed in to change notification settings - Fork 0
/
catsansvin.sh
executable file
·127 lines (102 loc) · 3.1 KB
/
catsansvin.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
#!/bin/bash
# Check Dump
if ! [ "$(hostname | grep '42')" ]
then
printf 'This Script is made to work with the 42 infra only, this won'"'"'t do the job on your laptop' >&2
fi
# Check USER
if [ "$USER" ]
then
echo "Welcome $USER !"
else
printf '$USER not set\nAbort..\n' >&2
exit 1
fi
# Check Sgoinfre
SGOINFRE_PERSO='/sgoinfre/Perso/'"$USER"
if ! [ -d "$SGOINFRE_PERSO" ]
then
printf "$SGOINFRE_PERSO doesn't exist\n" >&2
mkdir -pv "$SGOINFRE_PERSO/VirtualBoxVMs"
vboxmanage setproperty machinefolder "$SGOINFRE_PERSO/VirtualBoxVMs"
chmod -R 700 "$SGOINFRE_PERSO"
fi
# Check OVA File
OVA_FILE='/sgoinfre/ISO/Xubuntu_42Paris.ova'
IMAGE_URL='https://sourceforge.net/projects/linuxvmimages/files/VirtualBox/X/Xubuntu_20.04.1_VB.zip/download'
if ! [ -e "$OVA_FILE" ]
then
printf "OVA File not present, downloading\n" >&2
curl -L --progress-bar "$IMAGE_URL" > '/tmp/xubuntu.zip'
cd '/tmp'
unzip 'xubuntu.zip'
mv Xubuntu_20.04.1_VB_LinuxVMImages.COM.ova "$OVA_FILE"
cd -
fi
# Import VM
TC_RED="\033[31m"
TC_RESET="\033[0;0m"
VM_NAME="42OS"
VM_RAM="$((1024*2))"
VM_VRAM='128'
VM_CPUS_COUNT='2'
# Ensure VM exist or create it
if ! [ "$(vboxmanage list vms | grep "$VM_NAME")" ]
then
# Import downloaded file & accept VBox licence
printf "${TC_YELLOW}Import ova... ${TC_RESET}\n"
if ! vboxmanage import "$OVA_FILE" --vsys 0 --eula accept --vmname "$VM_NAME"
then
printf "${TC_RED}Failed to import the VM${TC_RESET}\n"
printf "${TC_RED}Check free space on disk${TC_RESET}\n"
exit 1
fi
# Configure Basic system settings (Network, graphics & memory)
VBoxManage modifyvm "$VM_NAME" --ioapic on
VBoxManage modifyvm "$VM_NAME" --memory "$VM_RAM" --vram "$VM_VRAM"
VBoxManage modifyvm "$VM_NAME" --graphicscontroller vmsvga
VBoxManage modifyvm "$VM_NAME" --nic1 nat
VBoxManage modifyvm "$VM_NAME" --natpf1 "ssh,tcp,,4222,,22"
VBoxManage modifyvm "$VM_NAME" --cpus "$VM_CPUS_COUNT"
old_description="$(VBoxManage showvminfo "$VM_NAME" --details | sed -n '/Description:/,/Guest:/p' | grep -Ev "(Description:|Guest:)" | head -n1)"
VBoxManage modifyvm "42OS" --description "$(printf "\t42 - ${old_description}\n\nBecause of the current crisis, you are allowed to organize remote defenses on Linux VM, on
allowed projects only, in order to progress into your curriculum.\n")"
else
printf "${TC_RED}There is already a VM with the name: ${VM_NAME}${TC_RESET}\n"
fi
# Ensure VM is up or start it
if ! [ "$(vboxmanage list runningvms | grep "$VM_NAME")" ]
then
VboxManage startvm "$VM_NAME"
else
printf "${TC_RED}VM with the name: ${VM_NAME} is already running${TC_RESET}\n"
fi
# Copy SSH-Key
SSH_PUBKEY="$HOME/.ssh/id_rsa.pub"
if ! [ -e "$SSH_PUBKEY" ]
then
printf "${TC_RED}SSH-Key not found${TC_RESET}\n"
exit 1
else
cat "$SSH_PUBKEY" >> $HOME/.ssh/authorized_keys
./ssh-connect.ex 4222 127.0.0.1 'xubuntu' 'xubuntu'
fi
# Configure VM
if [ -e '42Playbooks' ]
then
cd 42Playbooks &&
git pull
else
git clone 'https://github.com/thdelmas/42Playbooks' &&
cd 42Playbooks
fi
./configure.sh
source .venv/bin/activate
ansible -m ping all
if [ "$1" ]
then
ansible-playbook site.yml --tags="$1"
else
ansible-playbook site.yml
fi
cd -