1 | import os |
---|
2 | import subprocess |
---|
3 | #!/usr/bin/env python3 |
---|
4 | |
---|
5 | print("=============== START LOAD ENVIRONMENT =================") |
---|
6 | # Idioma por defecto. |
---|
7 | os.environ["LANG"] = os.getenv("LANG", "es_ES") |
---|
8 | |
---|
9 | print("-- step 2") |
---|
10 | os.environ["LC_ALL"] = os.getenv("LC_ALL", os.environ["LANG"]) |
---|
11 | |
---|
12 | print("-- step 3") |
---|
13 | result = subprocess.run(["locale-gen", os.environ["LANG"]], capture_output=True, text=True) |
---|
14 | if result.returncode != 0: |
---|
15 | print(f"Error generating locale: {result.stderr}") |
---|
16 | |
---|
17 | print("-- step 4") |
---|
18 | |
---|
19 | opengnsys_path = os.environ['OPENGNSYS'] |
---|
20 | |
---|
21 | print(f"OPENGNSYS Directory: {opengnsys_path}") |
---|
22 | |
---|
23 | print("-- step 5") |
---|
24 | |
---|
25 | # Directorios del proyecto OpenGnsys. |
---|
26 | os.environ["OPENGNSYS"] = os.getenv("OPENGNSYS", "/opt/opengnsys") |
---|
27 | |
---|
28 | print("-- step 6") |
---|
29 | |
---|
30 | print (f"OPENGNSYS: {os.environ['OPENGNSYS']}") |
---|
31 | |
---|
32 | if os.path.isdir(os.environ["OPENGNSYS"]): |
---|
33 | print("OPENGNSYS directory found") |
---|
34 | os.environ["OGBIN"] = os.path.join(os.environ["OPENGNSYS"], "bin") |
---|
35 | os.environ["OGETC"] = os.path.join(os.environ["OPENGNSYS"], "etc") |
---|
36 | os.environ["OGLIB"] = os.path.join(os.environ["OPENGNSYS"], "lib") |
---|
37 | os.environ["OGAPI"] = os.path.join(os.environ["OGLIB"], "engine", "bin") |
---|
38 | os.environ["OGSCRIPTS"] = os.path.join(os.environ["OPENGNSYS"], "scripts") |
---|
39 | os.environ["OGIMG"] = os.path.join(os.environ["OPENGNSYS"], "images") |
---|
40 | os.environ["OGCAC"] = os.path.join(os.environ["OPENGNSYS"], "cache") |
---|
41 | os.environ["OGLOG"] = os.path.join(os.environ["OPENGNSYS"], "log") |
---|
42 | |
---|
43 | os.environ["PATH"] = os.pathsep.join([ |
---|
44 | os.environ["PATH"], |
---|
45 | "/sbin", |
---|
46 | "/usr/sbin", |
---|
47 | "/usr/local/sbin", |
---|
48 | "/bin", |
---|
49 | "/usr/bin", |
---|
50 | "/usr/local/bin", |
---|
51 | "/opt/oglive/rootfs/opt/drbl/sbin", |
---|
52 | os.environ["OGSCRIPTS"], |
---|
53 | os.environ["OGAPI"], |
---|
54 | os.environ["OGBIN"] |
---|
55 | ]) |
---|
56 | print("-- step 7") |
---|
57 | # Exportar parámetros del kernel. |
---|
58 | with open("/proc/cmdline") as f: |
---|
59 | for i in f.read().split(): |
---|
60 | if "=" in i: |
---|
61 | key, value = i.split("=", 1) |
---|
62 | os.environ[key] = value |
---|
63 | |
---|
64 | print("-- step 8") |
---|
65 | # Cargar fichero de idioma. |
---|
66 | lang_file = os.path.join(os.environ["OGETC"], f"lang.{os.environ['LANG'].split('@')[0]}.conf") |
---|
67 | if os.path.isfile(lang_file): |
---|
68 | with open(lang_file) as f: |
---|
69 | for line in f: |
---|
70 | if "=" in line: |
---|
71 | key, value = line.strip().split("=", 1) |
---|
72 | os.environ[key] = value |
---|
73 | print("-- step 9") |
---|
74 | # Mensaje de carga del entorno. |
---|
75 | print(os.getenv("MSG_LOADAPI", ".")) |
---|
76 | |
---|
77 | print("-- step 10") |
---|
78 | # Cargar mapa de teclado. |
---|
79 | subprocess.run(["loadkeys", os.environ["LANG"].split("_")[0]], stdout=subprocess.DEVNULL) |
---|
80 | |
---|
81 | |
---|
82 | print("-- step 10.1") |
---|
83 | # Imprimir todas las variables de entorno declaradas hasta el momento. |
---|
84 | for key, value in os.environ.items(): |
---|
85 | print(f"{key}: {value}") |
---|
86 | |
---|
87 | |
---|
88 | print("-- step 11") |
---|
89 | # Cargar API de funciones. |
---|
90 | def execute_lib_file(filepath): |
---|
91 | with open(filepath) as f: |
---|
92 | code = compile(f.read(), filepath, 'exec') |
---|
93 | exec(code, globals()) |
---|
94 | |
---|
95 | for lib_file in os.listdir(os.environ["OGAPI"]): |
---|
96 | if lib_file.endswith(".lib"): |
---|
97 | execute_lib_file(os.path.join(os.environ["OGAPI"], lib_file)) |
---|
98 | |
---|
99 | |
---|
100 | |
---|
101 | # for lib_file in os.listdir(os.environ["OGAPI"]): |
---|
102 | # if lib_file.endswith(".lib"): |
---|
103 | # exec(open(os.path.join(os.environ["OGAPI"], lib_file)).read()) |
---|
104 | |
---|
105 | print("-- step 12") |
---|
106 | # Cargar configuración del engine. |
---|
107 | engine_cfg = os.path.join(os.environ["OGETC"], "engine.cfg") |
---|
108 | if os.path.isfile(engine_cfg): |
---|
109 | exec(open(engine_cfg).read()) |
---|
110 | os.environ["OGLOGCOMMAND"] = os.getenv("OGLOGCOMMAND", "/tmp/command.log") |
---|
111 | os.environ["OGLOGSESSION"] = os.getenv("OGLOGSESSION", "/tmp/session.log") |
---|
112 | |
---|
113 | |
---|
114 | print("-- step 13") |
---|
115 | # Cargar las APIs según engine. |
---|
116 | ogengine = os.getenv("ogengine") |
---|
117 | if ogengine: |
---|
118 | for api_file in os.listdir(os.environ["OGAPI"]): |
---|
119 | if api_file.endswith(f".{ogengine}"): |
---|
120 | exec(open(os.path.join(os.environ["OGAPI"], api_file)).read()) |
---|
121 | |
---|
122 | print("-- step 14") |
---|
123 | # Configuración de la red (modo offline). |
---|
124 | initrd_cfg = "/tmp/initrd.cfg" |
---|
125 | if os.path.isfile(initrd_cfg): |
---|
126 | with open(initrd_cfg) as f: |
---|
127 | for line in f: |
---|
128 | if line.startswith("DEVICECFG="): |
---|
129 | device_cfg = line.strip().split("=", 1)[1] |
---|
130 | os.environ["DEVICECFG"] = device_cfg |
---|
131 | if os.path.isfile(device_cfg): |
---|
132 | exec(open(device_cfg).read()) |
---|
133 | |
---|
134 | print("-- step 15") |
---|
135 | # FIXME Pruebas para grupos de ordenadores |
---|
136 | os.environ["OGGROUP"] = os.getenv("group", "") |
---|
137 | |
---|
138 | root_repo = os.getenv("ROOTREPO", os.getenv("OGSERVERIMAGES")) |
---|
139 | |
---|
140 | # Fichero de registros. |
---|
141 | og_log_file = os.path.join(os.environ["OGLOG"], f"{ogGetIpAddress()}.log") |
---|
142 | os.environ["OGLOGFILE"] = og_log_file |
---|
143 | else: |
---|
144 | print("ERROR: OPENGNSYS directory not found") |
---|
145 | |
---|
146 | # Compatibilidad para usar proxy en clientes ogLive. |
---|
147 | if not os.getenv("http_proxy") and os.getenv("ogproxy"): |
---|
148 | os.environ["http_proxy"] = os.getenv("ogproxy") |
---|
149 | |
---|
150 | # Compatibilidad para usar servidor DNS en clientes ogLive. |
---|
151 | if not os.path.isfile("/run/resolvconf/resolv.conf") and os.getenv("ogdns"): |
---|
152 | os.makedirs("/run/resolvconf", exist_ok=True) |
---|
153 | with open("/run/resolvconf/resolv.conf", "w") as f: |
---|
154 | f.write(f"nameserver {os.getenv('ogdns')}\n") |
---|
155 | |
---|
156 | # Declaración de códigos de error. |
---|
157 | error_codes = { |
---|
158 | "OG_ERR_FORMAT": 1, |
---|
159 | "OG_ERR_NOTFOUND": 2, |
---|
160 | "OG_ERR_PARTITION": 3, |
---|
161 | "OG_ERR_LOCKED": 4, |
---|
162 | "OG_ERR_IMAGE": 5, |
---|
163 | "OG_ERR_NOTOS": 6, |
---|
164 | "OG_ERR_NOTEXEC": 7, |
---|
165 | "OG_ERR_NOTWRITE": 14, |
---|
166 | "OG_ERR_NOTCACHE": 15, |
---|
167 | "OG_ERR_CACHESIZE": 16, |
---|
168 | "OG_ERR_REDUCEFS": 17, |
---|
169 | "OG_ERR_EXTENDFS": 18, |
---|
170 | "OG_ERR_OUTOFLIMIT": 19, |
---|
171 | "OG_ERR_FILESYS": 20, |
---|
172 | "OG_ERR_CACHE": 21, |
---|
173 | "OG_ERR_NOGPT": 22, |
---|
174 | "OG_ERR_REPO": 23, |
---|
175 | "OG_ERR_NOMSDOS": 24, |
---|
176 | "OG_ERR_IMGSIZEPARTITION": 30, |
---|
177 | "OG_ERR_UPDATECACHE": 31, |
---|
178 | "OG_ERR_DONTFORMAT": 32, |
---|
179 | "OG_ERR_IMAGEFILE": 33, |
---|
180 | "OG_ERR_GENERIC": 40, |
---|
181 | "OG_ERR_UCASTSYNTAXT": 50, |
---|
182 | "OG_ERR_UCASTSENDPARTITION": 51, |
---|
183 | "OG_ERR_UCASTSENDFILE": 52, |
---|
184 | "OG_ERR_UCASTRECEIVERPARTITION": 53, |
---|
185 | "OG_ERR_UCASTRECEIVERFILE": 54, |
---|
186 | "OG_ERR_MCASTSYNTAXT": 55, |
---|
187 | "OG_ERR_MCASTSENDFILE": 56, |
---|
188 | "OG_ERR_MCASTRECEIVERFILE": 57, |
---|
189 | "OG_ERR_MCASTSENDPARTITION": 58, |
---|
190 | "OG_ERR_MCASTRECEIVERPARTITION": 59, |
---|
191 | "OG_ERR_PROTOCOLJOINMASTER": 60, |
---|
192 | "OG_ERR_DONTMOUNT_IMAGE": 70, |
---|
193 | "OG_ERR_DONTSYNC_IMAGE": 71, |
---|
194 | "OG_ERR_DONTUNMOUNT_IMAGE": 72, |
---|
195 | "OG_ERR_NOTDIFFERENT": 73, |
---|
196 | "OG_ERR_SYNCHRONIZING": 74, |
---|
197 | "OG_ERR_NOTUEFI": 80, |
---|
198 | "OG_ERR_NOTBIOS": 81 |
---|
199 | } |
---|
200 | |
---|
201 | for key, value in error_codes.items(): |
---|
202 | os.environ[key] = str(value) |
---|