-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
easy-install.py
executable file
·854 lines (752 loc) · 23.8 KB
/
easy-install.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
#!/usr/bin/env python3
import argparse
import base64
import logging
import os
import platform
import shutil
import subprocess
import sys
import time
import urllib.request
from shutil import move, unpack_archive, which
from typing import Dict, List
logging.basicConfig(
filename="easy-install.log",
filemode="w",
format="%(asctime)s - %(levelname)s - %(message)s",
level=logging.INFO,
)
def cprint(*args, level: int = 1):
"""
logs colorful messages
level = 1 : RED
level = 2 : GREEN
level = 3 : YELLOW
default level = 1
"""
CRED = "\033[31m"
CGRN = "\33[92m"
CYLW = "\33[93m"
reset = "\033[0m"
message = " ".join(map(str, args))
if level == 1:
print(CRED, message, reset)
if level == 2:
print(CGRN, message, reset)
if level == 3:
print(CYLW, message, reset)
def clone_frappe_docker_repo() -> None:
try:
urllib.request.urlretrieve(
"https://github.com/frappe/frappe_docker/archive/refs/heads/main.zip",
"frappe_docker.zip",
)
logging.info("Downloaded frappe_docker zip file from GitHub")
unpack_archive("frappe_docker.zip", ".")
# Unzipping the frappe_docker.zip creates a folder "frappe_docker-main"
move("frappe_docker-main", "frappe_docker")
logging.info("Unzipped and Renamed frappe_docker")
os.remove("frappe_docker.zip")
logging.info("Removed the downloaded zip file")
except Exception as e:
logging.error("Download and unzip failed", exc_info=True)
cprint("\nCloning frappe_docker Failed\n\n", "[ERROR]: ", e, level=1)
def get_from_env(dir, file) -> Dict:
env_vars = {}
with open(os.path.join(dir, file)) as f:
for line in f:
if line.startswith("#") or not line.strip():
continue
key, value = line.strip().split("=", 1)
env_vars[key] = value
return env_vars
def write_to_env(
frappe_docker_dir: str,
out_file: str,
sites: List[str],
db_pass: str,
admin_pass: str,
email: str,
cronstring: str,
erpnext_version: str = None,
http_port: str = None,
custom_image: str = None,
custom_tag: str = None,
) -> None:
quoted_sites = ",".join([f"`{site}`" for site in sites]).strip(",")
example_env = get_from_env(frappe_docker_dir, "example.env")
erpnext_version = erpnext_version or example_env["ERPNEXT_VERSION"]
env_file_lines = [
# defaults to latest version of ERPNext
f"ERPNEXT_VERSION={erpnext_version}\n",
f"DB_PASSWORD={db_pass}\n",
"DB_HOST=db\n",
"DB_PORT=3306\n",
"REDIS_CACHE=redis-cache:6379\n",
"REDIS_QUEUE=redis-queue:6379\n",
"REDIS_SOCKETIO=redis-socketio:6379\n",
f"LETSENCRYPT_EMAIL={email}\n",
f"SITE_ADMIN_PASS={admin_pass}\n",
f"SITES={quoted_sites}\n",
"PULL_POLICY=missing\n",
f'BACKUP_CRONSTRING="{cronstring}"\n',
]
if http_port:
env_file_lines.append(f"HTTP_PUBLISH_PORT={http_port}\n")
if custom_image:
env_file_lines.append(f"CUSTOM_IMAGE={custom_image}\n")
if custom_tag:
env_file_lines.append(f"CUSTOM_TAG={custom_tag}\n")
with open(os.path.join(out_file), "w") as f:
f.writelines(env_file_lines)
def generate_pass(length: int = 12) -> str:
"""Generate random hash using best available randomness source."""
import math
import secrets
if not length:
length = 56
return secrets.token_hex(math.ceil(length / 2))[:length]
def get_frappe_docker_path():
return os.path.join(os.getcwd(), "frappe_docker")
def check_repo_exists() -> bool:
return os.path.exists(get_frappe_docker_path())
def start_prod(
project: str,
sites: List[str] = [],
email: str = None,
cronstring: str = None,
version: str = None,
image: str = None,
is_https: bool = True,
http_port: str = None,
):
if not check_repo_exists():
clone_frappe_docker_repo()
install_container_runtime()
compose_file_name = os.path.join(
os.path.expanduser("~"),
f"{project}-compose.yml",
)
env_file_dir = os.path.expanduser("~")
env_file_name = f"{project}.env"
env_file_path = os.path.join(
os.path.expanduser("~"),
env_file_name,
)
frappe_docker_dir = get_frappe_docker_path()
cprint(
f"\nPlease refer to {env_file_path} to know which keys to set\n\n",
level=3,
)
admin_pass = ""
db_pass = ""
custom_image = None
custom_tag = None
if image:
custom_image = image
custom_tag = version
with open(compose_file_name, "w") as f:
# Writing to compose file
if not os.path.exists(env_file_path):
admin_pass = generate_pass()
db_pass = generate_pass(9)
write_to_env(
frappe_docker_dir=frappe_docker_dir,
out_file=env_file_path,
sites=sites,
db_pass=db_pass,
admin_pass=admin_pass,
email=email,
cronstring=cronstring,
erpnext_version=version,
http_port=http_port if not is_https and http_port else None,
custom_image=custom_image,
custom_tag=custom_tag,
)
cprint(
"\nA .env file is generated with basic configs. Please edit it to fit to your needs \n",
level=3,
)
with open(
os.path.join(os.path.expanduser("~"), f"{project}-passwords.txt"), "w"
) as en:
en.writelines(f"ADMINISTRATOR_PASSWORD={admin_pass}\n")
en.writelines(f"MARIADB_ROOT_PASSWORD={db_pass}\n")
else:
env = get_from_env(env_file_dir, env_file_name)
sites = env["SITES"].replace("`", "").split(",") if env["SITES"] else []
db_pass = env["DB_PASSWORD"]
admin_pass = env["SITE_ADMIN_PASS"]
email = env["LETSENCRYPT_EMAIL"]
custom_image = env.get("CUSTOM_IMAGE")
custom_tag = env.get("CUSTOM_TAG")
version = env.get("ERPNEXT_VERSION", version)
write_to_env(
frappe_docker_dir=frappe_docker_dir,
out_file=env_file_path,
sites=sites,
db_pass=db_pass,
admin_pass=admin_pass,
email=email,
cronstring=cronstring,
erpnext_version=version,
http_port=http_port if not is_https and http_port else None,
custom_image=custom_image,
custom_tag=custom_tag,
)
try:
command = [
"docker",
"compose",
"--project-name",
project,
"-f",
"compose.yaml",
"-f",
"overrides/compose.mariadb.yaml",
"-f",
"overrides/compose.redis.yaml",
"-f",
(
"overrides/compose.https.yaml"
if is_https
else "overrides/compose.noproxy.yaml"
),
"-f",
"overrides/compose.backup-cron.yaml",
"--env-file",
env_file_path,
"config",
]
subprocess.run(
command,
cwd=frappe_docker_dir,
stdout=f,
check=True,
)
except Exception:
logging.error("Docker Compose generation failed", exc_info=True)
cprint("\nGenerating Compose File failed\n")
sys.exit(1)
try:
# Starting with generated compose file
command = [
"docker",
"compose",
"-p",
project,
"-f",
compose_file_name,
"up",
"--force-recreate",
"--remove-orphans",
"-d",
]
subprocess.run(
command,
check=True,
)
logging.info(f"Docker Compose file generated at ~/{project}-compose.yml")
except Exception as e:
logging.error("Prod docker-compose failed", exc_info=True)
cprint(" Docker Compose failed, please check the container logs\n", e)
sys.exit(1)
return db_pass, admin_pass
def setup_prod(
project: str,
sites: List[str],
email: str,
cronstring: str,
version: str = None,
image: str = None,
apps: List[str] = [],
is_https: bool = False,
http_port: str = None,
) -> None:
if len(sites) == 0:
sites = ["site1.localhost"]
db_pass, admin_pass = start_prod(
project=project,
sites=sites,
email=email,
cronstring=cronstring,
version=version,
image=image,
is_https=is_https,
http_port=http_port,
)
for sitename in sites:
create_site(sitename, project, db_pass, admin_pass, apps)
cprint(
f"MariaDB root password is {db_pass}",
level=2,
)
cprint(
f"Site administrator password is {admin_pass}",
level=2,
)
passwords_file_path = os.path.join(
os.path.expanduser("~"),
f"{project}-passwords.txt",
)
cprint(f"Passwords are stored in {passwords_file_path}", level=3)
def update_prod(
project: str,
version: str = None,
image: str = None,
cronstring: str = None,
is_https: bool = False,
http_port: str = None,
) -> None:
start_prod(
project=project,
version=version,
image=image,
cronstring=cronstring,
is_https=is_https,
http_port=http_port,
)
migrate_site(project=project)
def setup_dev_instance(project: str):
if not check_repo_exists():
clone_frappe_docker_repo()
install_container_runtime()
try:
command = [
"docker",
"compose",
"-f",
"devcontainer-example/docker-compose.yml",
"--project-name",
project,
"up",
"-d",
]
subprocess.run(
command,
cwd=get_frappe_docker_path(),
check=True,
)
cprint(
"Please go through the Development Documentation: https://github.com/frappe/frappe_docker/tree/main/docs/development.md to fully complete the setup.",
level=2,
)
logging.info("Development Setup completed")
except Exception as e:
logging.error("Dev Environment setup failed", exc_info=True)
cprint("Setting Up Development Environment Failed\n", e)
def install_docker():
cprint("Docker is not installed, Installing Docker...", level=3)
logging.info("Docker not found, installing Docker")
if platform.system() == "Darwin" or platform.system() == "Windows":
cprint(
f"""
This script doesn't install Docker on {"Mac" if platform.system()=="Darwin" else "Windows"}.
Please go through the Docker Installation docs for your system and run this script again"""
)
logging.debug("Docker setup failed due to platform is not Linux")
sys.exit(1)
try:
ps = subprocess.run(
["curl", "-fsSL", "https://get.docker.com"],
capture_output=True,
check=True,
)
subprocess.run(["/bin/bash"], input=ps.stdout, capture_output=True)
subprocess.run(
[
"sudo",
"usermod",
"-aG",
"docker",
str(os.getenv("USER")),
],
check=True,
)
cprint("Waiting Docker to start", level=3)
time.sleep(10)
subprocess.run(
[
"sudo",
"systemctl",
"restart",
"docker.service",
],
check=True,
)
except Exception as e:
logging.error("Installing Docker failed", exc_info=True)
cprint("Failed to Install Docker\n", e)
cprint("\n Try Installing Docker Manually and re-run this script again\n")
sys.exit(1)
def install_container_runtime(runtime="docker"):
if which(runtime) is not None:
cprint(runtime.title() + " is already installed", level=2)
return
if runtime == "docker":
install_docker()
def create_site(
sitename: str,
project: str,
db_pass: str,
admin_pass: str,
apps: List[str] = [],
):
apps = apps or []
cprint(f"\nCreating site: {sitename} \n", level=3)
command = [
"docker",
"compose",
"-p",
project,
"exec",
"backend",
"bench",
"new-site",
"--no-mariadb-socket",
f"--db-root-password={db_pass}",
f"--admin-password={admin_pass}",
]
for app in apps:
command.append("--install-app")
command.append(app)
command.append(sitename)
try:
subprocess.run(
command,
check=True,
)
logging.info("New site creation completed")
except Exception as e:
logging.error(f"Bench site creation failed for {sitename}", exc_info=True)
cprint(f"Bench Site creation failed for {sitename}\n", e)
def migrate_site(project: str):
cprint(f"\nMigrating sites for {project}", level=3)
exec_command(
project=project,
command=[
"bench",
"--site",
"all",
"migrate",
],
)
def exec_command(project: str, command: List[str] = [], interactive_terminal=False):
if not command:
command = ["echo", '"Please execute a command"']
cprint(f"\nExecuting Command:\n{' '.join(command)}", level=3)
exec_command = [
"docker",
"compose",
"-p",
project,
"exec",
]
if interactive_terminal:
exec_command.append("-it")
exec_command.append("backend")
exec_command += command
try:
subprocess.run(
exec_command,
check=True,
)
logging.info("New site creation completed")
except Exception as e:
logging.error(f"Exec command failed for {project}", exc_info=True)
cprint(f"Exec command failed for {project}\n", e)
def add_project_option(parser: argparse.ArgumentParser):
parser.add_argument(
"-n",
"--project",
help="Project Name",
default="frappe",
)
return parser
def add_setup_options(parser: argparse.ArgumentParser):
parser.add_argument(
"-a",
"--app",
dest="apps",
default=[],
help="list of app(s) to be installed",
action="append",
)
parser.add_argument(
"-s",
"--sitename",
help="Site Name(s) for your production bench",
default=[],
action="append",
dest="sites",
)
parser.add_argument("-e", "--email", help="Add email for the SSL.")
return parser
def add_common_parser(parser: argparse.ArgumentParser):
parser = add_project_option(parser)
parser.add_argument(
"-g",
"--backup-schedule",
help='Backup schedule cronstring, default: "@every 6h"',
default="@every 6h",
)
parser.add_argument("-i", "--image", help="Full Image Name")
parser.add_argument(
"-m", "--http-port", help="Http port in case of no-ssl", default="8080"
)
parser.add_argument("-q", "--no-ssl", action="store_true", help="No https")
parser.add_argument(
"-v",
"--version",
help="ERPNext or image version to install, defaults to latest stable",
)
parser.add_argument(
"-l",
"--force-pull",
action="store_true",
help="Force pull frappe_docker",
)
return parser
def add_build_parser(subparsers: argparse.ArgumentParser):
parser = subparsers.add_parser("build", help="Build custom images")
parser = add_common_parser(parser)
parser = add_setup_options(parser)
parser.add_argument(
"-p",
"--push",
help="Push the built image to registry",
action="store_true",
)
parser.add_argument(
"-r",
"--frappe-path",
help="Frappe Repository to use, default: https://github.com/frappe/frappe",
default="https://github.com/frappe/frappe",
)
parser.add_argument(
"-b",
"--frappe-branch",
help="Frappe branch to use, default: version-15",
default="version-15",
)
parser.add_argument(
"-j",
"--apps-json",
help="Path to apps json, default: frappe_docker/development/apps-example.json",
default="frappe_docker/development/apps-example.json",
)
parser.add_argument(
"-t",
"--tag",
dest="tags",
help="Full Image Name(s), default: custom-apps:latest",
action="append",
)
parser.add_argument(
"-c",
"--containerfile",
help="Path to Containerfile: images/custom/Containerfile",
default="images/custom/Containerfile",
)
parser.add_argument(
"-y",
"--python-version",
help="Python Version, default: 3.11.6",
default="3.11.6",
)
parser.add_argument(
"-d",
"--node-version",
help="NodeJS Version, default: 18.18.2",
default="18.18.2",
)
parser.add_argument(
"-x",
"--deploy",
help="Deploy after build",
action="store_true",
)
parser.add_argument(
"-u",
"--upgrade",
help="Upgrade after build",
action="store_true",
)
def add_deploy_parser(subparsers: argparse.ArgumentParser):
parser = subparsers.add_parser("deploy", help="Deploy using compose")
parser = add_common_parser(parser)
parser = add_setup_options(parser)
def add_develop_parser(subparsers: argparse.ArgumentParser):
parser = subparsers.add_parser("develop", help="Development setup using compose")
parser.add_argument(
"-n", "--project", default="frappe", help="Compose project name"
)
def add_upgrade_parser(subparsers: argparse.ArgumentParser):
parser = subparsers.add_parser("upgrade", help="Upgrade existing project")
parser = add_common_parser(parser)
def add_exec_parser(subparsers: argparse.ArgumentParser):
parser = subparsers.add_parser("exec", help="Exec into existing project")
parser = add_project_option(parser)
def build_image(
push: bool,
frappe_path: str,
frappe_branch: str,
containerfile_path: str,
apps_json_path: str,
tags: List[str],
python_version: str,
node_version: str,
):
if not check_repo_exists():
clone_frappe_docker_repo()
install_container_runtime()
if not tags:
tags = ["custom-apps:latest"]
apps_json_base64 = None
try:
with open(apps_json_path, "rb") as file_text:
file_read = file_text.read()
apps_json_base64 = (
base64.encodebytes(file_read).decode("utf-8").replace("\n", "")
)
except Exception as e:
logging.error("Unable to base64 encode apps.json", exc_info=True)
cprint("\nUnable to base64 encode apps.json\n\n", "[ERROR]: ", e, level=1)
command = [
which("docker"),
"build",
"--progress=plain",
]
for tag in tags:
command.append(f"--tag={tag}")
command += [
f"--file={containerfile_path}",
f"--build-arg=FRAPPE_PATH={frappe_path}",
f"--build-arg=FRAPPE_BRANCH={frappe_branch}",
f"--build-arg=PYTHON_VERSION={python_version}",
f"--build-arg=NODE_VERSION={node_version}",
f"--build-arg=APPS_JSON_BASE64={apps_json_base64}",
".",
]
try:
subprocess.run(
command,
check=True,
cwd="frappe_docker",
)
except Exception as e:
logging.error("Image build failed", exc_info=True)
cprint("\nImage build failed\n\n", "[ERROR]: ", e, level=1)
if push:
try:
for tag in tags:
subprocess.run(
[which("docker"), "push", tag],
check=True,
)
except Exception as e:
logging.error("Image push failed", exc_info=True)
cprint("\nImage push failed\n\n", "[ERROR]: ", e, level=1)
def get_args_parser():
parser = argparse.ArgumentParser(
description="Easy install script for Frappe Framework"
)
# Setup sub-commands
subparsers = parser.add_subparsers(dest="subcommand")
# Build command
add_build_parser(subparsers)
# Deploy command
add_deploy_parser(subparsers)
# Upgrade command
add_upgrade_parser(subparsers)
# Develop command
add_develop_parser(subparsers)
# Exec command
add_exec_parser(subparsers)
return parser
if __name__ == "__main__":
parser = get_args_parser()
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(1)
args = parser.parse_args()
if (
args.subcommand != "exec"
and args.force_pull
and os.path.exists(get_frappe_docker_path())
):
cprint("\nForce pull frappe_docker again\n", level=2)
shutil.rmtree(get_frappe_docker_path(), ignore_errors=True)
if args.subcommand == "build":
build_image(
push=args.push,
frappe_path=args.frappe_path,
frappe_branch=args.frappe_branch,
apps_json_path=args.apps_json,
tags=args.tags,
containerfile_path=args.containerfile,
python_version=args.python_version,
node_version=args.node_version,
)
if args.deploy:
setup_prod(
project=args.project,
sites=args.sites,
email=args.email,
cronstring=args.backup_schedule,
version=args.version,
image=args.image,
apps=args.apps,
is_https=not args.no_ssl,
http_port=args.http_port,
)
elif args.upgrade:
update_prod(
project=args.project,
version=args.version,
image=args.image,
cronstring=args.backup_schedule,
is_https=not args.no_ssl,
http_port=args.http_port,
)
elif args.subcommand == "deploy":
cprint("\nSetting Up Production Instance\n", level=2)
logging.info("Running Production Setup")
if args.email and "example.com" in args.email:
cprint("Emails with example.com not acceptable", level=1)
sys.exit(1)
setup_prod(
project=args.project,
sites=args.sites,
email=args.email,
version=args.version,
cronstring=args.backup_schedule,
image=args.image,
apps=args.apps,
is_https=not args.no_ssl,
http_port=args.http_port,
)
elif args.subcommand == "develop":
cprint("\nSetting Up Development Instance\n", level=2)
logging.info("Running Development Setup")
setup_dev_instance(args.project)
elif args.subcommand == "upgrade":
cprint("\nUpgrading Production Instance\n", level=2)
logging.info("Upgrading Development Setup")
update_prod(
project=args.project,
version=args.version,
image=args.image,
is_https=not args.no_ssl,
cronstring=args.backup_schedule,
http_port=args.http_port,
)
elif args.subcommand == "exec":
cprint(f"\nExec into {args.project} backend\n", level=2)
logging.info(f"Exec into {args.project} backend")
exec_command(
project=args.project,
command=["bash"],
interactive_terminal=True,
)