init
This commit is contained in:
99
scripts/upgrade.py
Executable file
99
scripts/upgrade.py
Executable file
@ -0,0 +1,99 @@
|
||||
#! /usr/bin/env python
|
||||
|
||||
import requests, platform, re, collections, tempfile, os, sys, shutil
|
||||
from subprocess import call
|
||||
|
||||
version_re = "seafile-server-?_?([\d\.]*)"
|
||||
|
||||
seaf_home = os.environ["SEAF"] or "/opt/seafile"
|
||||
|
||||
def available(page):
|
||||
(arch, _) = platform.architecture()
|
||||
if arch == "64bit":
|
||||
arch = "x86-64"
|
||||
else: arch = "i386"
|
||||
link = "http(s?):\/\/[^ \"\(\)\<\>]*%s_?%s.tar.gz" % (version_re ,arch)
|
||||
resp = requests.get(page)
|
||||
available = {}
|
||||
for m in re.finditer(link, resp.text):
|
||||
available[m.group(2)] = m.group(0) #{version: link}
|
||||
return collections.OrderedDict(sorted(available.items(), reverse=True))
|
||||
|
||||
def list_command(page="https://www.seafile.com/en/download/"):
|
||||
for (version, link) in available(page).items():
|
||||
print("- %s: %s" % (version, link))
|
||||
return 0
|
||||
|
||||
def current_version_command():
|
||||
print(current_version())
|
||||
|
||||
def current_version():
|
||||
current = os.path.realpath(os.path.join(seaf_home, "seafile-server-latest"))
|
||||
res = re.findall(version_re,current)
|
||||
res[0] = "0" + res[0]
|
||||
return '.'.join(res)
|
||||
|
||||
def perform_upgrade_command(version,mysql=False,yes=True):
|
||||
scripts = os.path.join(seaf_home,"seafile-server-%s" % version, "upgrade")
|
||||
current = map(lambda x: int(x), current_version().split("."))
|
||||
target = map(lambda x: int(x), version.split("."))
|
||||
print len(current) == 1 and current[0] == 0
|
||||
if len(current) == 1 and current[0] == 0: #Not installed
|
||||
installer = os.path.join(seaf_home,"seafile-server-%s" % version,"setup-seafile%s.sh" % ("-mysql" if mysql else ""))
|
||||
call(["sh","-c",installer],stdout=sys.stdout, stdin=sys.stdin)
|
||||
return 0
|
||||
script_re = "^upgrade_(\d+).(\d+)_(\d+).(\d+)\.sh$"
|
||||
run = []
|
||||
last = None
|
||||
for s in os.listdir(scripts):
|
||||
m = re.match(script_re, s)
|
||||
if m:
|
||||
comp = map(lambda x: int(x), m.groups())
|
||||
last = (s, comp)
|
||||
(major, minor, minorsub, rev) = comp
|
||||
if current[0] <= major and minor > current[1]:
|
||||
run.append(s)
|
||||
for script in run:
|
||||
cmd = []
|
||||
if yes: cmd.append("echo -ne '\n'")
|
||||
cmd.append(script)
|
||||
call(["/bin/sh", "-c", ''.join(cmd)], stdout=sys.stdout, stdin=(None if yes else sys.stdin))
|
||||
|
||||
def install_command(url):
|
||||
tmp = tempfile.mkdtemp()
|
||||
if call(["/bin/sh", "-c", 'cd %s; curl %s | tar xzv' % (tmp, url)]) == 0:
|
||||
for f in os.listdir(tmp):
|
||||
if len(re.findall(version_re, f)) > 0:
|
||||
dest = os.path.join(seaf_home, os.path.basename(f))
|
||||
shutil.move(os.path.join(tmp,f), dest)
|
||||
current = current_version()
|
||||
target_version = ''.join(re.findall(version_re,dest))
|
||||
if len(current) == 1: current = current[0]
|
||||
else: current = "0.0.0"
|
||||
print("Upgrading from: %s -> %s" % (current, target_version))
|
||||
perform_upgrade_command(target_version)
|
||||
if len(os.listdir(tmp)) != 0:
|
||||
print("Failed to unpack update")
|
||||
os.rmdir(tmp)
|
||||
return 1
|
||||
os.rmdir(tmp)
|
||||
return 0
|
||||
|
||||
def main_command(version="latest",page="https://www.seafile.com/en/download/"):
|
||||
avail = available(page)
|
||||
install = None
|
||||
if version == "latest":
|
||||
install = avail.items()[0]
|
||||
else:
|
||||
if version in dict(avail.items()):
|
||||
install = dict(avail.items())[version]
|
||||
if install:
|
||||
print("Installing: %s" % install[1])
|
||||
return install_command(install[1])
|
||||
else:
|
||||
print("Couldn't determine download for %s" % version)
|
||||
return 1
|
||||
|
||||
if __name__ == '__main__':
|
||||
import scriptine
|
||||
scriptine.run()
|
Reference in New Issue
Block a user