Skip to content

Instantly share code, notes, and snippets.

@mattsmithdatera
Last active November 7, 2018 22:39
Show Gist options
  • Save mattsmithdatera/7a00bded151e736d3eeaa25b6c7d5784 to your computer and use it in GitHub Desktop.
Save mattsmithdatera/7a00bded151e736d3eeaa25b6c7d5784 to your computer and use it in GitHub Desktop.
#!/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_T = os.path.join(os.path.dirname("/usr/local/bin/"), "{}")
VENV_T = os.path.join(DIR_T, ".venv")
PYTHON_T = os.path.join(VENV_T, "bin", "python")
PIP_T = os.path.join(VENV_T, "bin", "pip")
REQUIREMENTS_T = os.path.join(DIR_T, "requirements.txt")
URL_T = "http://github.com/Datera/{}"
DDCT = "ddct"
DBMP = "dbmp"
DVOT = "dvot"
DDCTPY = os.path.join(DIR_T.format(DDCT), "src", "ddct.py")
DBMPPY = os.path.join(DIR_T.format(DBMP), "src", DBMP, "main.py")
DVOTPY = os.path.join(DIR_T.format(DVOT), "src", DVOT, "main.py")
DDICT = {DDCT: DDCTPY,
DBMP: DBMPPY,
DVOT: DVOTPY}
SHELL_TEMPLATE = """
#!/bin/bash
{python} {binary} $@
"""
FAILURE = 1
SUCCESS = 0
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(pip, cmd):
vprint("Running pip cmd:", cmd)
cmd = " ".join((pip, cmd))
return subprocess.check_output(cmd, shell=True)
def exe_python(python, cmd):
vprint("Running python cmd:", cmd)
cmd = " ".join((python, cmd))
return subprocess.check_output(cmd, shell=True)
def main(args):
global VERBOSE
VERBOSE = not args.quiet
try:
exe("which virtualenv")
except subprocess.CalledProcessError:
# Install prereqs Ubuntu
try:
exe("sudo apt-get update")
exe("sudo apt-get install python-virtualenv python-dev "
"libffi-dev libssl-dev open-iscsi git -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 iscsi-initiator-utils git -y")
except subprocess.CalledProcessError as e:
print(e)
print("RHEL packages failed")
print("Could not install prereqs")
return FAILURE
for app in [DDCT, DBMP, DVOT]:
url = URL_T.format(app)
exe("git clone {} /usr/local/bin/{}".format(url, app))
venv = VENV_T.format(app)
pip = PIP_T.format(app)
req = REQUIREMENTS_T.format(app)
d = DIR_T.format(app)
py = PYTHON_T.format(app)
dmain = DDICT[app]
if not os.path.isdir(venv):
exe("virtualenv {}".format(venv))
exe_pip(pip, "install -U pip")
exe_pip(pip, "install -U -r {}".format(req))
exe_pip(pip, "install -e {}".format(d))
if not os.path.isfile(app):
# Create executable
with io.open(app, 'w+') as f:
f.write(SHELL_TEMPLATE.format(
python=py,
binary=dmain))
# Ensure it is executable
st = os.stat(app)
os.chmod(app, st.st_mode | stat.S_IEXEC)
print("{0} is now installed. Use '{0}' to run {0}.".format(app))
return SUCCESS
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-q", "--quiet", action="store_true")
args = parser.parse_args()
sys.exit(main(args))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment