172 lines
4.5 KiB
Python
Executable File
172 lines
4.5 KiB
Python
Executable File
#!/usr/bin/python3
|
|
|
|
import sys
|
|
import dotenv
|
|
import os
|
|
import subprocess
|
|
import docker
|
|
import string
|
|
import random
|
|
|
|
def main():
|
|
INSENV = ".env-nextc"
|
|
DOCOMP = "docker-compose"
|
|
USRCMP = ("sudo", DOCOMP, "exec", "-u", "1000")
|
|
|
|
if not os.path.exists("docker-compose.yml"):
|
|
raise ProgramError("Needs to be run from compose directory\n")
|
|
|
|
if not os.path.exists(".env"):
|
|
raise ProgramError("Missing .env file")
|
|
|
|
dotenv.load_dotenv(os.environ["PWD"] + "/.env")
|
|
try:
|
|
CONF = os.getenv("CONF")
|
|
except KeyError:
|
|
raise ProgramError("Missing CONF in .env file")
|
|
try:
|
|
DATA = os.getenv("DATA")
|
|
except KeyError:
|
|
raise ProgramError("Missing DATA in .env file")
|
|
|
|
try:
|
|
out = open (INSENV, 'a')
|
|
except PermissionError:
|
|
raise ProgramError("Error: cannot open " + INSENV + " file")
|
|
finally:
|
|
out.close()
|
|
|
|
print("Checking for environment file...")
|
|
if os.path.exists(INSENV):
|
|
print("...found env file")
|
|
env = dotenv.dotenv_values(os.environ["PWD"] + "/" + INSENV)
|
|
else:
|
|
print("none\n")
|
|
env = {}
|
|
|
|
print("Set DB password attempt...")
|
|
try:
|
|
ROOTPASS = env["ROOTPASS"]
|
|
print("...DB password already applied")
|
|
except KeyError:
|
|
print ("...making password")
|
|
ROOTPASS = "".join(random.choices(string.ascii_uppercase + string.digits, k=10))
|
|
print ("...setting")
|
|
cmd = USRCMP + ("nextc-db", "mysqladmin", "-u", "root", "password", ROOTPASS)
|
|
print (cmd)
|
|
try:
|
|
out = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True, text=True)
|
|
except subprocess.CalledProcessError as err:
|
|
print (err.stderr)
|
|
print (err.stdout)
|
|
raise ProgramError("Failure to create DB password")
|
|
with open (INSENV, 'a') as out:
|
|
print("ROOTPASS=" + ROOTPASS, file=out)
|
|
print("...success\n")
|
|
|
|
try:
|
|
USERNAME = env["USERNAME"]
|
|
except KeyError:
|
|
USERNAME = input("Enter admin name: ")
|
|
if USERNAME == "":
|
|
raise ProgramError("Empty admin name")
|
|
if os.path.exists(DATA + "/cndata/ncdata/" + USERNAME):
|
|
raise ProgramError("Data exists for user (please move or delete)")
|
|
with open (INSENV, 'a') as out:
|
|
print ("USERNAME=" + USERNAME, file=out)
|
|
try:
|
|
USERPASS = env["USERPASS"]
|
|
except KeyError:
|
|
USERPASS = "".join(random.choices(string.ascii_uppercase + string.digits, k=4))
|
|
with open (INSENV, 'a') as out:
|
|
print("USERPASS=" + USERPASS, file=out)
|
|
|
|
print("Checking installed status...")
|
|
cmd = USRCMP + ("-w", "/config/www/nextcloud", "nextc", "php", "occ", "status")
|
|
try:
|
|
out = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True, text=True)
|
|
except subprocess.CalledProcessError:
|
|
print(out.stderr)
|
|
raise ProgramError("Failure to check install status (" + out.stderr + ")")
|
|
if "installed:" not in out.stdout:
|
|
raise ProgramError("No result from install status test")
|
|
if "installed: true" in out.stdout:
|
|
raise ProgramError("Nextcloud is installed")
|
|
print("...not installed yet")
|
|
|
|
print("Attempting install...")
|
|
cmd = USRCMP + ("-w", "/config/www/nextcloud", "nextc", "php", "occ", "maintenance:install",
|
|
"--database", "mysql", "--database-host", "nextc-db", "--database-name", "ncdb",
|
|
"--database-user", "root", "--database-pass", ROOTPASS, "--admin-user", USERNAME,
|
|
"--admin-pass", USERPASS, "--data-dir", "/ncdata")
|
|
print (cmd)
|
|
try:
|
|
out = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True, text=True)
|
|
except subprocess.CalledProcessError as err:
|
|
print(err.stderr)
|
|
print(err.stdout)
|
|
raise ProgramError("Failure to install")
|
|
print("...success")
|
|
|
|
|
|
class ProgramError(Exception):
|
|
""" Program Exception """
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except ProgramError as err:
|
|
print("Setup of Database and/or Nextcloud failed: " + err.args[0])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
# read -p "Enter Trusted domain: " DOMAIN
|
|
#
|
|
# RE=$($P1 nextc bash -c "$P2 config:system:set trusted_domains 1 --value=nextc")
|
|
# if [[ $? -eq 0 ]]; then
|
|
# printf "Trusted Domain nextc set\n"
|
|
# else
|
|
# exit 1;
|
|
# fi
|
|
# RE=$($P1 nextc bash -c "$P2 config:system:set trusted_domains 2 --value=$DOMAIN")
|
|
# if [[ $? -eq 0 ]]; then
|
|
# printf "Trusted Domain $DOMAIN set\n"
|
|
# else
|
|
# exit 1;
|
|
# fi
|
|
#
|
|
# set +o xtrace
|
|
#
|
|
#print("%s%s" % (os.environ, "\n"))
|
|
#globals_ = set(globals());
|
|
#for name in globals_:
|
|
# val = eval(name)
|
|
# print(name, "is", type(val), "and is equal to ", val)
|
|
#parent_dir = os.path.dirname(__file__)
|
|
#dotenv.load_dotenv(os.environ["PWD"] + "/.env")
|
|
#CONF = os.getenv("CONF")
|
|
#print (CONF, "\n")
|