-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.py
executable file
·150 lines (127 loc) · 4.75 KB
/
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
#!/usr/bin/env python
from __future__ import unicode_literals, print_function, division
import argparse
import io
import os
import stat
import subprocess
import sys
DIR = os.path.dirname(os.path.abspath(__file__))
VENV = os.path.join(DIR, ".dbmp")
PYTHON = os.path.join(VENV, "bin", "python")
PIP = os.path.join(VENV, "bin", "pip")
REQUIREMENTS = os.path.join(DIR, "requirements.txt")
DBMP = os.path.join(DIR, "dbmp")
INTERACTIVEPY = os.path.join(DIR, "src", "dbmp", "interactive.py")
DBMPPY = os.path.join(DIR, "src", "dbmp", "main.py")
CONFIG = os.path.join(DIR, "datera-config.json")
DBMP_TEMPLATE = """
#!/bin/bash
if [[ $1 =~ .*interactive.* ]]
then
# dbmp interactive needs to know which python
# virtualenv to use
shift
{python} {interactive} "{python} {dbmp}" $@
else
{python} {dbmp} $@
fi
"""
VERBOSE = False
def vprint(*args, **kwargs):
if VERBOSE:
print(*args, **kwargs)
def exe(cmd):
vprint("Running cmd:", cmd)
return subprocess.check_output(cmd, shell=True)
def exe_pip(cmd):
vprint("Running pip cmd:", cmd)
cmd = " ".join((PIP, cmd))
return subprocess.check_output(cmd, shell=True)
def exe_python(cmd):
vprint("Running python cmd:", cmd)
cmd = " ".join((PYTHON, cmd))
return subprocess.check_output(cmd, shell=True)
def install_packages():
# Install prereqs Ubuntu
try:
exe("sudo apt-get install python-virtualenv python-dev "
"libffi-dev libssl-dev gcc -y")
# Install prereqs Centos
except subprocess.CalledProcessError as e:
vprint(e)
print("Ubuntu packages failed, trying RHEL packages")
try:
exe("sudo yum install python-virtualenv python-devel "
"libffi-devel openssl-devel gcc -y")
except subprocess.CalledProcessError as e:
vprint(e)
print("RHEL packages failed, trying SUSE packages")
try:
exe("sudo zypper install -y python-setuptools libffi-devel "
"python-curses gcc")
# For some reason this has to be a separate call
exe("sudo zypper install -y python-devel")
except subprocess.CalledProcessError as e:
vprint(e)
print("SUSE packages failed")
print("Could not install prereqs")
return 1
def install_virtualenv_from_source():
if not os.path.exists("pypa-virtualenv-3272f7b"):
exe("curl --location --output virtualenv-16.4.3.tar.gz "
"https://github.com/pypa/virtualenv/tarball/16.4.3")
exe("tar zxvf virtualenv-16.4.3.tar.gz")
exe("python pypa-virtualenv-3272f7b/virtualenv.py .dbmp")
def main(args):
global VERBOSE
VERBOSE = not args.quiet
try:
exe("which virtualenv")
except subprocess.CalledProcessError:
if install_packages() == 1:
return 1
if not os.path.isdir(VENV):
try:
exe("virtualenv {}".format(VENV))
except subprocess.CalledProcessError:
# Sometimes this fails because python-setuptools isn't installed
# this almost always happens on SUSE, but we'll install all
# necessary packages just to be safe.
if install_packages() == 1:
return 1
install_virtualenv_from_source()
exe_pip("install -U pip")
exe_pip("install -U -r {}".format(REQUIREMENTS))
exe_pip("install -e {}".format(DIR))
if not os.path.isfile(DBMP):
# Create dbmp executable
with io.open(DBMP, 'w+') as f:
f.write(DBMP_TEMPLATE.format(
python=PYTHON,
dbmp=DBMPPY,
interactive=INTERACTIVEPY))
# Ensure it is executable
st = os.stat(DBMP)
os.chmod(DBMP, st.st_mode | stat.S_IEXEC)
if not os.path.isfile(CONFIG) and args.gen_config:
exe("cd {} && {} --gen-config json".format(DIR, DBMP))
print("DBMP is now installed. Use '{}' to run DBMP."
"\nThe generated config file is located at '{}'. "
"\nIf an existing universal datera config file should be "
"used, remove the generated config file".format(
DBMP, CONFIG))
else:
print("DBMP is now installed. Use '{dbmp}' to run DBMP."
"If you would like to run DBMP in 'interactive' mode, use"
"'{dbmp} --interactive' with no other arguments".format(
dbmp=DBMP))
return 0
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-g", "--gen-config",
help="Generate Datera Universal Config file after "
"install")
parser.add_argument("-q", "--quiet", action="store_true")
args = parser.parse_args()
sys.exit(main(args))