This repository was archived by the owner on Oct 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdp-setup.py
More file actions
executable file
·164 lines (132 loc) · 5.72 KB
/
dp-setup.py
File metadata and controls
executable file
·164 lines (132 loc) · 5.72 KB
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
#!/usr/bin/env python3
"""
Datapane Server script
"""
import argparse
import datetime
import logging
import secrets
import shutil
import subprocess
import sys
from contextlib import suppress
from distutils.util import strtobool
from pathlib import Path
from typing import List, Optional
log = logging.getLogger("dp-setup")
################################################################################
# commands
# TODO - start/stop local trial
def check(args):
"""Check all (docker) dependencies are installed"""
docker_exe = shutil.which("docker")
if docker_exe:
log.debug(f"Found docker at {docker_exe}")
subprocess.run([docker_exe, "--version"], check=True)
subprocess.run([docker_exe, "info"], check=True, capture_output=True)
else:
sys.exit("docker not found, please install")
docker_compose_exe = shutil.which("docker-compose")
if docker_compose_exe:
log.debug(f"Found docker-compose at {docker_compose_exe}")
subprocess.run(["docker-compose", "--version"], check=True)
else:
sys.exit("docker-compose not found, please install")
print("Dependencies all provided, please run `configure` to generate your config file")
def configure(args):
"""
Configure the on-prem instance (for docker-compose only at present),
generates a docker-compose.yml and datapane.env suitable for running
# TODO - future questions
# - cloud provider? not right now, S3 only - see Provider enum in django config
# - optional redis
# - domain - allowed hosts config
# - https
"""
print("👋 Hi! I'm here to help you set up a self-hosted Datapane Server.\n")
print(
"Datapane can run with in `dev` mode with all dependencies, or in `prod` mode using managed cloud services, such as AWS RDS. " +
"We recommend dev mode for trying out, and prod mode for longer term deployments.")
if args.mode:
prod_mode = args.mode == "prod"
else:
while True:
with suppress(ValueError):
prod_mode = strtobool(input("Run in prod mode (Yes/No)? "))
break
template = Path("docker/datapane.env").read_text()
# common template params
params = dict(
django_secret_key=secrets.token_urlsafe(50)
)
if prod_mode:
print("Building prod docker-compose configuration")
shutil.copyfile("docker/docker-compose.prod.yml", "docker-compose.yml")
params.update(
aws_access_key="",
aws_secret_key="",
aws_endpoint_url=""
)
else:
print("Building dev docker-compose configuration")
shutil.copyfile("docker/docker-compose.dev.yml", "docker-compose.yml")
shutil.copyfile("docker/nginx.conf", "nginx.conf")
params.update(
aws_access_key="minio",
aws_secret_key="minio123",
aws_endpoint_url="AWS_S3_ENDPOINT_URL=http://minio:9000"
)
output = template.format(**params)
log.debug(params)
out_env = Path("datapane.env")
if out_env.exists():
backup_env = f"datapane.env.{datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}"
log.debug(f"Found old env, saving as {backup_env}")
shutil.copyfile(out_env, backup_env)
out_env.write_text(output)
print(
"\n👍 Thanks! Now edit the datapane.env as needed, add your license key, and run `docker-compose up` to launch Datapane! 🍀")
def update(args):
"""Update the docker containers"""
subprocess.run(["sudo", "docker", "image", "prune", "-a", "-f"], check=True)
subprocess.run(["sudo docker-compose pull && sudo docker-compose up -d"], shell=True, check=True)
# def start(args):
# subprocess.run(["sudo docker-compose up -d"], shell=True, check=True)
#
#
# def stop(args):
# subprocess.run(["sudo docker-compose down"], shell=True, check=True)
def parse_args(argv: Optional[List[str]] = None) -> argparse.Namespace:
"""Configure the arg parser"""
parser = argparse.ArgumentParser(description="Setup and configure an on-premise Datapane Server",
epilog="For more info see https://github.com/datapane/datapane-onpremise/ and https://docs.datapane.com/deployment/")
# main args
parser.add_argument("--debug", action="store_true", default=False, help="Enable debug output")
subparsers = parser.add_subparsers(help='Available commands', dest="command")
# subcommands
parser_check = subparsers.add_parser("check", help="Check all dependencies are installed")
parser_check.set_defaults(command=check)
parser_configure = subparsers.add_parser("configure", help="Configure the datapane environment file")
parser_configure.add_argument('--mode', choices=["prod", "dev"],
help="Configure Datapane Server headlessly in prod/dev mode")
parser_configure.set_defaults(command=configure)
parser_update = subparsers.add_parser("update", help="Update Datapane Server to latest version")
parser_update.set_defaults(command=update)
# parser_start = subparsers.add_parser("start", help="Start Datapane Server (via docker-compose)")
# parser_start.set_defaults(command=start)
#
# parser_stop = subparsers.add_parser("stop", help="Stop Datapane Server (via docker-compose)")
# parser_stop.set_defaults(command=stop)
return parser.parse_args(argv)
def main():
args = parse_args()
logging.basicConfig(level=logging.DEBUG if args.debug else logging.INFO)
log.debug(args)
if args.command:
args.command(args)
else:
sys.exit("No command entered, use --help")
if __name__ == "__main__":
# check deps
assert sys.version_info.major == 3 and sys.version_info.minor >= 6, "Requires Python 3.6 or above"
main()