-
Notifications
You must be signed in to change notification settings - Fork 1
/
bootstrap.py
94 lines (71 loc) · 3.54 KB
/
bootstrap.py
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
import shutil
import time
from pathlib import Path
import utils
# ------------------------------------------------------------------------------------ #
print("\nWe need to ask for the password beforehand to prettify the script output.")
utils.run("sudo echo")
print()
# ------------------------------------------------------------------------------------ #
print("Updating packages list...", end="")
utils.run("sudo apt-get update")
print(" Done.")
# ------------------------------------------------------------------------------------ #
number_of_updates = len(utils.popen("apt list --upgradable").stdout.readlines()) - 1
title = f"Updating {number_of_updates} package(s)..."
total_lines = int(number_of_updates * 7.5)
utils.show_progress(title, utils.popen("sudo apt-get upgrade -y"), total_lines)
# ------------------------------------------------------------------------------------ #
answer = utils.get_ascii_input("Do you want to change the hostname? [y/N]: ")
if utils.is_answer_positive(answer):
new_hostname = utils.get_ascii_input("Enter your new hostname: ")
print("Changing hostname...", end="")
utils.change_hostname(new_hostname)
print(" Done.")
print("You need to reboot the machine to see the changes.")
# ------------------------------------------------------------------------------------ #
print("Installing dotfiles and SSH config in home directory...", end="")
shutil.copytree("machine/ubuntu/dotfiles", Path.home(), dirs_exist_ok=True)
shutil.copytree("machine/ubuntu/ssh", f"{Path.home()}/.ssh", dirs_exist_ok=True)
utils.run("cat .bashrc_partial >> .bashrc && rm .bashrc_partial", shell=True)
print(" Done.")
# ------------------------------------------------------------------------------------ #
title = "Installing pip3 (python package manager)..."
utils.show_progress(title, utils.popen("sudo apt-get install python3-pip -y"), 307)
# ------------------------------------------------------------------------------------ #
print("Installing virtualenv (python environments manager)...", end="")
utils.run("pip3 install virtualenv")
print(" Done.")
# ------------------------------------------------------------------------------------ #
print("Installing black (python formatter)...", end="")
utils.run("pip3 install black")
print(" Done.")
# ------------------------------------------------------------------------------------ #
print("Adding 'projects' and 'sandbox' folders...", end="")
utils.run("mkdir projects")
utils.run("mkdir sandbox")
utils.run("touch sandbox/app.py")
utils.run(f"{Path.home()}/.local/bin/virtualenv sandbox/venv")
utils.run("sandbox/venv/bin/python -m pip install black")
print(" Done.")
# ------------------------------------------------------------------------------------ #
print("Cleaning up after provisioning...", end="")
shutil.rmtree("machine")
utils.run("chmod 700 ~/.ssh/", shell=True)
utils.run("chmod 600 ~/.ssh/*", shell=True)
utils.run("chmod 644 ~/.gitconfig", shell=True)
utils.run("sudo apt-get autoremove -y")
print(" Done.")
# ------------------------------------------------------------------------------------ #
answer = utils.get_ascii_input("Do you want to reboot the machine right now? [y/N]: ")
if utils.is_answer_positive(answer):
try:
print("\nThe reboot will be initiated in 5 seconds.")
print("If you want to cancel it, press 'Ctrl+C'.")
print("Thank you for choosing my script. Bye!\n")
time.sleep(5)
utils.run("sudo reboot")
except KeyboardInterrupt:
print("\rReboot aborted.")
else:
print("\nThank you for choosing my script. Bye!\n")