-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathconfigure_fabsim.py
297 lines (247 loc) · 9.68 KB
/
configure_fabsim.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import os
import sys
import subprocess
import platform
import getpass
from shutil import which
from pprint import pprint
import importlib
def install_required_modules(pkg_name: str, pip_pkg_name: str = None) -> bool:
"""
Import a Module,
if that fails, try to use the command pip to install it.
"""
already_installed = False
if pip_pkg_name is None:
pip_pkg_name = pkg_name
try:
# If Module it is already installed, try to Import it
importlib.import_module(pkg_name)
already_installed = True
except ImportError:
# Error if Module is not installed Yet, then install it
check_call_list = ["python3", "-m", "pip", "install", pip_pkg_name]
if pip_pkg_name == "fabric2":
check_call_list.append("invoke==2.0.0")
print("Installing {} package ...".format(pkg_name))
if bool(os.environ.get("VIRTUAL_ENV")) is True:
print("Executing : python3 -m pip install {}".format(pip_pkg_name))
subprocess.check_call(check_call_list)
else:
print(
"Executing : python3 -m pip install --user {}".format(
pip_pkg_name
)
)
subprocess.check_call(check_call_list)
return already_installed
# rich emojis list
# https://github.com/willmcgugan/rich/blob/master/rich/_emoji_codes.py
# rich colors list
# https://github.com/willmcgugan/rich/blob/master/docs/source/appendix/colors.rst
console = None
yaml = None
required = {
"rich": None,
"ruamel.yaml": None,
"numpy": None,
"yaml": "pyyaml",
"fabric2": "fabric2",
"beartype": "beartype",
}
for pkg_name, pip_pkg_name in required.items():
already_installed = install_required_modules(pkg_name, pip_pkg_name)
if pkg_name == "rich":
from rich.console import Console
from rich import print
from rich.panel import Panel
from rich.syntax import Syntax
console = Console()
if pkg_name == "ruamel.yaml":
import ruamel.yaml
yaml = ruamel.yaml.YAML()
if already_installed:
console.print(
"Package {} is already installed :thumbs_up:".format(pkg_name),
style="green",
)
else:
console.print(
"Package {} is installed in your system :party_popper:".format(
pkg_name
),
style="yellow",
)
yaml.allow_duplicate_keys = None
yaml.preserve_quotes = True
# to Prevent long lines getting wrapped in ruamel.yaml
yaml.width = 4096 # or some other big enough value to prevent line-wrap
def get_platform():
platforms = {
"linux": "Linux",
"linux1": "Linux",
"linux2": "Linux",
"linux3": "Linux",
"darwin": "OSX",
"win32": "Windows",
}
try:
return platforms[sys.platform]
except Exception:
print("[{}] Unidentified system !!!".format(sys.platform))
exit()
class AttributeDict(dict):
def __getattr__(self, key):
try:
return self[key]
except KeyError:
# to conform with __getattr__ spec
raise AttributeError(key)
def __setattr__(self, key, value):
self[key] = value
def __delattr__(self, key):
del self[key]
def linux_distribution():
try:
return platform.linux_distribution()
except Exception:
return "N/A"
FS3_env = AttributeDict(
{
"OS_system": get_platform(),
"OS_release": platform.release(),
"OS_version": platform.version(),
# os.getcwd() : not working if your call is outside of FabSim3 folder
"FabSim3_PATH": os.path.dirname(os.path.realpath(__file__)),
"user_name": getpass.getuser(),
"machines_user_yml": None,
}
)
def config_yml_files():
# Load and invoke the default non-machine specific config JSON
# dictionaries.
FS3_env.machines_user_yml = yaml.load(
open(
os.path.join(
FS3_env.FabSim3_PATH,
"fabsim",
"deploy",
"machines_user_example.yml",
)
)
)
# setup machines_user.yml
S = ruamel.yaml.scalarstring.DoubleQuotedScalarString
FS3_env.machines_user_yml["localhost"]["home_path_template"] = S(
os.path.join(FS3_env.FabSim3_PATH, "localhost_exe")
)
FS3_env.machines_user_yml["default"]["home_path_template"] = S(
os.path.join(FS3_env.FabSim3_PATH, "localhost_exe")
)
FS3_env.machines_user_yml["default"]["local_results"] = os.path.join(
FS3_env.FabSim3_PATH, "results"
)
FS3_env.machines_user_yml["default"]["local_configs"] = os.path.join(
FS3_env.FabSim3_PATH, "config_files"
)
FS3_env.machines_user_yml["default"]["username"] = FS3_env.user_name
FS3_env.machines_user_yml["localhost"]["username"] = FS3_env.user_name
machines_user_PATH = os.path.join(
FS3_env.FabSim3_PATH, "fabsim", "deploy", "machines_user.yml"
)
# backup the machines_user.yml if it exits
if os.path.isfile(machines_user_PATH):
os.rename(
machines_user_PATH,
os.path.join(
FS3_env.FabSim3_PATH,
"fabsim",
"deploy",
"machines_user_backup.yml",
),
)
# save machines_user.yml
with open(machines_user_PATH, "w") as yaml_file:
yaml.dump(FS3_env.machines_user_yml, yaml_file)
# create localhost execution folder if it is not exists
if not os.path.exists(os.path.join(FS3_env.FabSim3_PATH, "localhost_exe")):
os.makedirs(os.path.join(FS3_env.FabSim3_PATH, "localhost_exe"))
def main():
config_yml_files()
# setup ssh localhost
if os.path.isfile("{}/.ssh/id_rsa.pub".format(os.path.expanduser("~"))):
print("local id_rsa key already exists.")
else:
os.system('ssh-keygen -q -f ~/.ssh/id_rsa -t rsa -b 4096 -N ""')
os.system("cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys")
os.system("chmod og-wx ~/.ssh/authorized_keys")
os.system("ssh-keyscan -H localhost >> ~/.ssh/known_hosts")
# use ssh-add instead of ssh-copy-id for MacOSX
if FS3_env.OS_system == "OSX":
os.system("ssh-add /Users/{}/.ssh/id_rsa".format(FS3_env.user_name))
bash_scripts = [
"~/.bashrc",
"~/.bash_profile",
"~/.zshrc",
"~/.bash_aliases",
]
title = "[orange_red1]Congratulation :clinking_beer_mugs:[/orange_red1]\n"
msg = "[dark_cyan]FabSim3 installation was successful :heavy_check_mark:[/dark_cyan]\n\n"
msg += "In order to use fabsim command anywhere in your PC, you need to "
msg += "update the [blue]PATH[/blue] and [blue]PYTHONPATH[/blue] environmental variables.\n\n"
msg += "\t[red1]export[/red1] [blue]PATH[/blue]={}/fabsim/bin:[blue]$PATH[/blue]\n".format(
FS3_env.FabSim3_PATH
)
msg += "\t[red1]export[/red1] [blue]PYTHONPATH[/blue]={}:[blue]$PYTHONPATH[/blue]\n\n".format(
FS3_env.FabSim3_PATH
)
# Check if ~/.local/bin is already in PATH
local_bin_path = os.path.expanduser("~/.local/bin")
if local_bin_path not in os.environ["PATH"]:
msg += "\t[red1]export[/red1] [blue]PATH[/blue]=~/.local/bin:[blue]$PATH[/blue]\n"
msg += "\nThe last list is added because the required packages are "
msg += 'installed with flag "--user" which makes pip install packages '
msg += "in your home directory instead of system directory."
print(Panel.fit(msg, title=title, border_style="orange_red1"))
title = "[dark_green]Tip[/dark_green]"
msg = "\nTo make these updates permanent, you can add the export commands "
msg += "above at the end of your bash shell script which could be one of "
msg += "[{}] files, depends on your OS System.\n\n".format(
", ".join(
[
"[light_sea_green]{}[/light_sea_green]".format(name)
for name in bash_scripts
]
)
)
msg += ":bellhop_bell: To load the new updates in [blue]PATH[/blue] and [blue]PYTHONPATH[/blue] "
msg += "you need to reload your bash shell script, e.g., "
msg += (
"[red1]source[/red1] [light_sea_green]~/.bashrc[/light_sea_green], or "
)
msg += "lunch a new terminal."
print(Panel.fit(msg, title=title, border_style="dark_green"))
# check if fabsim command is already available or not
if which("fabsim") is not None:
print("\n")
print(
Panel.fit(
"fabsim [red1]command is already added to the [/red1][blue]PATH[/blue] "
"[red1]variable. Please make sure you remove the old FabSim3 directory "
"from your bash shell script.",
title="[orange_red1]WARNING[/orange_red1]",
)
)
# Set the FABSIM3_HOME environment variable
os.environ["FABSIM3_HOME"] = FS3_env.FabSim3_PATH
export_cmd = f'export FABSIM3_HOME="{FS3_env.FabSim3_PATH}"'
# Check if the export command already exists in the shell configuration file
shell_config_file = os.path.expanduser("~/.bashrc") # Modify the file name if needed
with open(shell_config_file, "r") as f:
shell_config_content = f.read()
if export_cmd not in shell_config_content:
# Add the export command to the shell configuration file if it doesn't exist
with open(shell_config_file, "a") as f:
f.write("\n" + export_cmd + "\n")
if __name__ == "__main__":
main()