Last active
March 19, 2019 19:11
-
-
Save joncrain/899d831b13c871eec5be85edad8e5665 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
import datetime | |
import subprocess | |
import json | |
import urllib | |
import urllib2 | |
import shutil | |
import tarfile | |
from distutils.version import LooseVersion | |
from distutils.dir_util import copy_tree | |
BACKUP_DIR = '/tmp/' | |
now = datetime.datetime.now() | |
def install_path(): | |
return os.path.dirname(os.path.realpath(__file__)).strip('build') | |
def build_version(): | |
with open(install_path() + "/app/helpers/site_helper.php", "r") as file: | |
for line in file: | |
# There is probably a more pythonic way of doing this... | |
if "$GLOBALS['version'] = '" in line: | |
return line.replace("$GLOBALS['version'] = '", "").replace("';", "") | |
def env(): | |
env_file = install_path() + '.env' | |
env_vars = {} | |
with open(env_file) as f: | |
for line in f: | |
if line.startswith('#'): | |
continue | |
key, value = line.strip().split('=', 1) | |
env_vars[key]=value | |
return env_vars | |
def database_type(): | |
try: | |
return env()['CONNECTION_DRIVER'].strip('"') | |
except: | |
return 'sqlite' | |
def set_maintenance_mode(value): | |
if value == "down": | |
open(install_path() + 'storage/framework/' + value, 'a').close() | |
else: | |
os.remove(install_path() + 'storage/framework/down') | |
# Get type of install - need to check if git or zip | |
def install_type(): | |
if not os.path.exists(install_path() + '.git'): | |
return 'zip' | |
else: | |
return 'git' | |
def munkireport_release_info(): | |
MR_API = "https://api.github.com/repos/munkireport/munkireport-php/releases/latest" | |
response = urllib.urlopen(MR_API) | |
data = json.loads(response.read()) | |
# print data['tarball_url'] | |
return data | |
def main(): | |
set_maintenance_mode("down") | |
# backup database | |
if database_type() == 'mysql': | |
# mysql backup | |
USERNAME = env()['CONNECTION_USERNAME'].strip('"') | |
PASSWORD = env()['CONNECTION_PASSWORD'].strip('"') | |
DATABASE = env()['CONNECTION_DATABASE'].strip('"') | |
BACKUP_FILE = BACKUP_DIR + DATABASE + now.strftime("%Y%m%d%H%M") + '.bak' | |
cmd = "/usr/bin/mysqldump --user=%s --password=%s %s > %s" % (USERNAME, PASSWORD, DATABASE, BACKUP_FILE) | |
print "Backing up database to %s" % BACKUP_FILE | |
subprocess.Popen(cmd, shell=True) | |
elif database_type() == 'sqlite': | |
shutil.copyfile(install_path() + 'app/db/db.sqlite', BACKUP_DIR + 'db' + now.strftime("%Y%m%d%H%M") + '.sqlite.bak') | |
# backup files | |
if install_type() == 'git': | |
FINAL_DIR = BACKUP_DIR + "munkireport" + now.strftime("%Y%m%d%H%M") | |
print "Backing up files to %s" % FINAL_DIR | |
os.mkdir(FINAL_DIR) | |
copy_tree(install_path(), FINAL_DIR) | |
elif install_type() == 'zip': | |
FINAL_DIR = BACKUP_DIR + "munkireport" + now.strftime("%Y%m%d%H%M") | |
os.mkdir(FINAL_DIR) | |
copy_tree(install_path(), FINAL_DIR) | |
# Update | |
if install_type() == 'git': | |
try: | |
# do git pull | |
print "Starting git pull" | |
process = subprocess.Popen(["git", "pull"], stdout=subprocess.PIPE) | |
output = process.communicate()[0] | |
print "Finishing git pull" | |
print "Running composer" | |
os.chdir(install_path()) | |
process = subprocess.Popen(["/usr/local/bin/composer", "update", "--no-dev"], stdout=subprocess.PIPE) | |
output = process.communicate()[0] | |
print "Composer finished" | |
os.chdir(install_path() + "/build/") | |
except: | |
print("Git stuff fail.") | |
elif install_type() == 'zip': | |
# download new munkireport | |
print "We are at version " + build_version() + "The latest master version is " + munkireport_release_info()['tag_name'].strip('v') | |
if LooseVersion(build_version()) > LooseVersion(munkireport_release_info()['tag_name'].strip('v')): | |
print "Local version is newer than the latest master release." | |
return | |
elif LooseVersion(build_version()) < LooseVersion("4.0.0"): | |
print "Local version is older than 4.0.0" | |
return | |
else: | |
print "Downloading the latest release" | |
extracted_location = "/tmp/extracted" | |
filedata = urllib2.urlopen(munkireport_release_info()['tarball_url']) | |
datatowrite = filedata.read() | |
with open('/tmp/munkireport_latest.tar.gz', 'wb') as f: | |
f.write(datatowrite) | |
# remove the old directory | |
shutil.rmtree(extracted_location) | |
try: | |
os.makedirs(extracted_location) | |
except: | |
print("Directory already exists") | |
tar = tarfile.open("/tmp/munkireport_latest.tar.gz") | |
tar.extractall(extracted_location) | |
tar.close() | |
# Run Migrations | |
print "Running migrations" | |
migration_file = install_path() + 'database/migrate.php' | |
cmd = "/usr/bin/php %s" % migration_file | |
print cmd | |
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) | |
migration_response = proc.stdout.read() | |
print "Finished migrations" | |
# turn off maintenance mode | |
set_maintenance_mode("up") | |
if __name__ == "__main__": | |
main() |
Before attempting database/migrate.php check that mysql/mariadb are fresh enough as listed in
https://github.com/munkireport/munkireport-php/wiki/MunkiReport-Database
? The errors thrown from too old sql versions are somewhat hard to get. By default in Ubuntu 16LTS or RHEL/Centos 7 (still used by many I guess?) the default version will be too old:)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Drop this in the
build
folder and run!