2017-11-02 21:08:17 +00:00
|
|
|
#!/usr/bin/env python
|
2016-02-18 10:49:26 +00:00
|
|
|
from __future__ import print_function
|
2017-11-02 21:08:17 +00:00
|
|
|
import json
|
2012-11-02 12:41:41 +00:00
|
|
|
import os
|
2015-11-14 14:22:40 +00:00
|
|
|
import shutil
|
2012-11-02 12:41:41 +00:00
|
|
|
import subprocess
|
2017-11-02 21:08:17 +00:00
|
|
|
import sys
|
2017-02-16 17:31:46 +00:00
|
|
|
try:
|
|
|
|
from urllib.request import urlopen
|
|
|
|
except:
|
|
|
|
from urllib2 import urlopen
|
2012-11-02 12:41:41 +00:00
|
|
|
from os.path import join, exists
|
|
|
|
|
2017-11-02 21:08:17 +00:00
|
|
|
|
|
|
|
repos = {
|
|
|
|
"pandora": {
|
|
|
|
"url": "https://git.0x2620.org/pandora.git",
|
|
|
|
"path": ".",
|
|
|
|
},
|
|
|
|
"oxjs": {
|
|
|
|
"url": "https://git.0x2620.org/oxjs.git",
|
|
|
|
"path": "./static/oxjs",
|
|
|
|
},
|
|
|
|
"oxtimelines": {
|
|
|
|
"url": "https://git.0x2620.org/oxtimelines.git",
|
|
|
|
"path": "./src/oxtimelines",
|
|
|
|
},
|
|
|
|
"python-ox": {
|
|
|
|
"url": "https://git.0x2620.org/python-ox.git",
|
|
|
|
"path": "./src/python-ox",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def activate_venv(base):
|
|
|
|
if os.path.exists(base):
|
|
|
|
old_os_path = os.environ.get('PATH', '')
|
|
|
|
bin_path = os.path.join(base, 'bin')
|
|
|
|
if bin_path not in old_os_path:
|
|
|
|
os.environ['PATH'] = os.path.join(base, 'bin') + os.pathsep + old_os_path
|
|
|
|
site_packages = os.path.join(base, 'lib', 'python%s' % sys.version[:3], 'site-packages')
|
|
|
|
prev_sys_path = list(sys.path)
|
|
|
|
import site
|
|
|
|
site.addsitedir(site_packages)
|
|
|
|
sys.real_prefix = sys.prefix
|
|
|
|
sys.prefix = base
|
|
|
|
# Move the added items to the front of the path:
|
|
|
|
new_sys_path = []
|
|
|
|
for item in list(sys.path):
|
|
|
|
if item not in prev_sys_path:
|
|
|
|
new_sys_path.append(item)
|
|
|
|
sys.path.remove(item)
|
|
|
|
sys.path[:0] = new_sys_path
|
|
|
|
|
|
|
|
|
2012-11-02 12:41:41 +00:00
|
|
|
def run(*cmd):
|
|
|
|
p = subprocess.Popen(cmd)
|
|
|
|
p.wait()
|
|
|
|
return p.returncode
|
|
|
|
|
2017-11-02 21:08:17 +00:00
|
|
|
|
2012-11-02 12:41:41 +00:00
|
|
|
def get(*cmd):
|
|
|
|
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
stdout, error = p.communicate()
|
2017-02-16 14:22:21 +00:00
|
|
|
return stdout.decode()
|
2012-11-02 12:41:41 +00:00
|
|
|
|
2017-11-02 21:08:17 +00:00
|
|
|
|
2014-10-05 20:10:36 +00:00
|
|
|
def get_json(url):
|
2017-02-16 17:31:46 +00:00
|
|
|
return json.loads(urlopen(url).read().decode())
|
2014-10-05 20:10:36 +00:00
|
|
|
|
2017-11-02 21:08:17 +00:00
|
|
|
|
2013-04-18 11:12:42 +00:00
|
|
|
def get_release():
|
2015-11-14 14:22:40 +00:00
|
|
|
if os.path.exists('.release'):
|
|
|
|
url = open('.release').read().strip()
|
|
|
|
else:
|
|
|
|
url = 'https://pan.do/json/release-stable.json'
|
2013-04-18 11:12:42 +00:00
|
|
|
try:
|
2015-11-14 14:22:40 +00:00
|
|
|
return get_json(url)
|
2013-04-18 11:12:42 +00:00
|
|
|
except:
|
2016-02-18 10:49:26 +00:00
|
|
|
print("Failed to load %s check your internet connection." % url)
|
2013-04-18 11:12:42 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
2012-11-02 12:41:41 +00:00
|
|
|
|
2013-03-01 07:09:44 +00:00
|
|
|
def reload_notice(base):
|
2016-02-18 10:49:26 +00:00
|
|
|
print('\nPlease restart pan.do/ra to finish the update:\n\tsudo %s/ctl reload\n' % base)
|
2012-11-02 12:41:41 +00:00
|
|
|
|
2017-11-02 21:08:17 +00:00
|
|
|
|
2015-04-30 13:01:27 +00:00
|
|
|
def check_services(base):
|
|
|
|
services = "pandora pandora-tasks pandora-encoding pandora-cron pandora-websocketd".split()
|
|
|
|
for service in services:
|
2015-04-30 13:07:24 +00:00
|
|
|
cmd = ['service', service, 'status']
|
2015-11-16 13:21:43 +00:00
|
|
|
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
p.wait()
|
|
|
|
if p.returncode != 0:
|
2016-02-18 10:49:26 +00:00
|
|
|
print('Please install init script for "%s" service:' % service)
|
2016-03-08 07:48:35 +00:00
|
|
|
if os.path.exists('/etc/init') and not os.path.exists('/bin/systemctl'):
|
2016-02-18 10:49:26 +00:00
|
|
|
print('\tsudo cp %s/etc/init/%s.conf /etc/init/' % (base, service))
|
2016-03-08 14:21:19 +00:00
|
|
|
if os.path.exists('/bin/systemctl'):
|
2016-06-09 10:50:34 +00:00
|
|
|
print('\tsudo cp %s/etc/systemd/system/%s.service /etc/systemd/system/' % (base, service))
|
2016-03-08 14:21:19 +00:00
|
|
|
print('\tsudo systemctl daemon-reload')
|
2016-02-18 10:49:26 +00:00
|
|
|
print('\tsudo service %s start' % service)
|
|
|
|
print('')
|
2015-04-30 13:01:27 +00:00
|
|
|
|
2017-11-02 21:08:17 +00:00
|
|
|
|
2016-03-17 09:38:15 +00:00
|
|
|
def update_service(service):
|
|
|
|
print('Please install new init script for "%s" service:' % service)
|
2017-11-02 21:08:17 +00:00
|
|
|
if os.path.exists('/etc/init/%s.conf' % service):
|
2016-03-17 09:38:15 +00:00
|
|
|
print('\tsudo cp %s/etc/init/%s.conf /etc/init/' % (base, service))
|
|
|
|
if os.path.exists('/bin/systemctl'):
|
2016-06-09 10:50:34 +00:00
|
|
|
print('\tsudo cp %s/etc/systemd/system/%s.service /etc/systemd/system/' % (base, service))
|
2016-03-17 09:38:15 +00:00
|
|
|
print('\tsudo systemctl daemon-reload')
|
|
|
|
print('\tsudo service %s restart' % service)
|
|
|
|
|
2017-11-02 21:08:17 +00:00
|
|
|
|
2015-11-14 14:22:40 +00:00
|
|
|
def run_git(path, *args):
|
|
|
|
cmd = ['git'] + list(args)
|
|
|
|
env = {'GIT_DIR': '%s/.git' % path}
|
2017-02-16 14:22:21 +00:00
|
|
|
return subprocess.check_output(cmd, env=env).decode().strip()
|
2015-11-14 14:22:40 +00:00
|
|
|
|
2017-11-02 21:08:17 +00:00
|
|
|
|
2015-11-14 14:22:40 +00:00
|
|
|
def get_version(path):
|
|
|
|
return run_git(path, 'rev-list', 'HEAD', '--count')
|
|
|
|
|
2017-11-02 21:08:17 +00:00
|
|
|
|
2012-11-02 12:41:41 +00:00
|
|
|
if __name__ == "__main__":
|
2017-11-02 21:08:17 +00:00
|
|
|
base = os.path.normpath(os.path.abspath(os.path.dirname(__file__)))
|
|
|
|
os.chdir(base)
|
|
|
|
activate_venv(base)
|
|
|
|
|
2013-04-30 17:17:48 +00:00
|
|
|
if len(sys.argv) == 2 and sys.argv[1] in ('database', 'db'):
|
2012-11-02 12:41:41 +00:00
|
|
|
os.chdir(join(base, 'pandora'))
|
2016-02-19 18:20:18 +00:00
|
|
|
print('\nRunning "./manage.py migrate"\n')
|
|
|
|
run('./manage.py', 'migrate', '--noinput')
|
|
|
|
run('./manage.py', 'sqlfindindex')
|
|
|
|
run('./manage.py', 'sync_itemsort')
|
2017-10-24 19:28:22 +00:00
|
|
|
run('./manage.py', 'sync_documentsort')
|
2016-02-19 18:20:18 +00:00
|
|
|
reload_notice(base)
|
2013-05-29 12:41:13 +00:00
|
|
|
elif len(sys.argv) == 2 and sys.argv[1] == 'static':
|
|
|
|
os.chdir(join(base, 'pandora'))
|
|
|
|
run('./manage.py', 'update_static')
|
2013-05-30 11:17:48 +00:00
|
|
|
elif len(sys.argv) == 4 and sys.argv[1] == 'postupdate':
|
|
|
|
os.chdir(base)
|
2015-11-14 15:16:58 +00:00
|
|
|
old = sys.argv[2].strip()
|
|
|
|
new = sys.argv[3].strip()
|
2015-11-14 14:22:40 +00:00
|
|
|
if old.isdigit():
|
|
|
|
old = int(old)
|
|
|
|
if new.isdigit():
|
|
|
|
new = int(new)
|
2016-02-18 10:49:26 +00:00
|
|
|
print('Post Update from %s to %s' % (old, new))
|
2013-05-30 11:17:48 +00:00
|
|
|
if old < 3111:
|
2013-05-30 11:18:48 +00:00
|
|
|
run('bzr', 'resolved', 'pandora/monkey_patch', 'pandora/monkey_patch/migrations')
|
2013-05-30 11:17:48 +00:00
|
|
|
if os.path.exists('pandora/monkey_patch'):
|
|
|
|
run('rm', '-r', 'pandora/monkey_patch')
|
2013-08-03 08:47:58 +00:00
|
|
|
if old < 3448:
|
|
|
|
if os.path.exists('static/pandora'):
|
|
|
|
run('bzr', 'resolved', 'static/pandora')
|
2013-09-19 12:01:40 +00:00
|
|
|
if old < 3651:
|
|
|
|
if os.path.exists('src/django/.git'):
|
|
|
|
os.chdir(os.path.join(base, 'src/django'))
|
|
|
|
run('git', 'checkout', 'stable/1.4.x')
|
|
|
|
run('git', 'pull')
|
|
|
|
os.chdir(base)
|
2013-09-29 13:00:06 +00:00
|
|
|
if old < 3666:
|
|
|
|
run('./bin/pip', 'install', '-r', 'requirements.txt')
|
2013-12-31 09:22:47 +00:00
|
|
|
if old < 3770:
|
|
|
|
run('./bin/pip', 'install', '-r', 'requirements.txt')
|
2014-10-05 19:23:03 +00:00
|
|
|
if old < 4379:
|
|
|
|
run('./bin/pip', 'install', '-r', 'requirements.txt')
|
2014-11-26 20:03:27 +00:00
|
|
|
if old < 4549:
|
|
|
|
import pandora.settings
|
|
|
|
with open('pandora/local_settings.py', 'r') as f:
|
|
|
|
local_settings = f.read()
|
2017-11-02 21:08:17 +00:00
|
|
|
if 'BROKER_URL' not in local_settings:
|
2014-11-26 20:03:27 +00:00
|
|
|
broker_url = 'amqp://%s:%s@%s:%s/%s' % (
|
|
|
|
getattr(pandora.settings, 'BROKER_USER', 'pandora'),
|
|
|
|
getattr(pandora.settings, 'BROKER_PASSWORD', 'box'),
|
|
|
|
getattr(pandora.settings, 'BROKER_HOST', '127.0.0.1'),
|
|
|
|
getattr(pandora.settings, 'BROKER_PORT', 5672),
|
|
|
|
getattr(pandora.settings, 'BROKER_VHOST', '/pandora'),
|
|
|
|
)
|
|
|
|
local_settings = [
|
|
|
|
l for l in local_settings.split('\n') if not l.startswith('BROKER_')
|
|
|
|
] + [
|
|
|
|
'BROKER_URL = "%s"' % broker_url, ''
|
|
|
|
]
|
|
|
|
with open('pandora/local_settings.py', 'w') as f:
|
|
|
|
f.write('\n'.join(local_settings))
|
2015-04-28 17:35:15 +00:00
|
|
|
if old < 4947:
|
|
|
|
run('./bin/pip', 'install', 'tornado==4.1')
|
2015-04-30 13:01:27 +00:00
|
|
|
check_services(base)
|
2015-11-14 14:22:40 +00:00
|
|
|
if old < 5074:
|
|
|
|
for component in ('oxtimelines', 'python-ox'):
|
|
|
|
if not os.path.exists('./src/%s/.git' % component):
|
|
|
|
run('./bin/pip', 'install', '-e',
|
|
|
|
'git+https://git.0x2620.org/%s.git#egg=%s' % (component, component),
|
|
|
|
'--exists-action', 'w')
|
|
|
|
if not os.path.exists('./static/oxjs/.git'):
|
|
|
|
if os.path.exists('static/oxjs'):
|
|
|
|
shutil.move('static/oxjs', 'static/oxjs_bzr')
|
2016-02-12 08:32:24 +00:00
|
|
|
run('git', 'clone', '--depth', '1', 'https://git.0x2620.org/oxjs.git', 'static/oxjs')
|
2015-11-14 14:22:40 +00:00
|
|
|
run('./pandora/manage.py', 'update_static')
|
|
|
|
if os.path.exists('static/oxjs_bzr'):
|
|
|
|
shutil.rmtree('static/oxjs_bzr')
|
|
|
|
if os.path.exists('REPOSITORY_MOVED_TO_GIT'):
|
|
|
|
os.unlink('REPOSITORY_MOVED_TO_GIT')
|
|
|
|
if os.path.exists('.bzr'):
|
|
|
|
shutil.rmtree('.bzr')
|
2015-11-14 14:38:45 +00:00
|
|
|
run('git', 'checkout', 'update.py')
|
2016-02-20 05:36:47 +00:00
|
|
|
if old <= 5389:
|
2016-02-20 12:36:05 +00:00
|
|
|
for path in (
|
|
|
|
'lib/python2.7/site-packages/Django.egg-link',
|
2016-03-03 08:19:22 +00:00
|
|
|
'lib/python2.7/site-packages/django-extensions.egg-link',
|
|
|
|
'lib/python2.7/site-packages/django-devserver.egg-link'
|
2016-02-20 12:36:05 +00:00
|
|
|
):
|
|
|
|
if os.path.exists(path):
|
|
|
|
os.unlink(path)
|
2016-03-03 08:19:22 +00:00
|
|
|
for path in (
|
|
|
|
'src/django',
|
|
|
|
'src/django-devserver',
|
|
|
|
'src/django-extensions',
|
|
|
|
):
|
|
|
|
if os.path.exists(path):
|
|
|
|
shutil.rmtree(path)
|
|
|
|
if os.path.exists('contrib'):
|
|
|
|
shutil.rmtree('contrib')
|
|
|
|
run('./bin/pip', 'install', '-r', 'requirements.txt')
|
|
|
|
run('./pandora/manage.py', 'migrate', '--fake-initial', '--noinput')
|
2016-03-17 09:38:15 +00:00
|
|
|
update_service('pandora')
|
2016-03-04 07:20:44 +00:00
|
|
|
if old <= 5432:
|
|
|
|
import pandora.settings
|
|
|
|
run('./bin/pip', 'install', '-r', 'requirements.txt')
|
|
|
|
path = os.path.join(pandora.settings.GEOIP_PATH, 'GeoLite2-City.mmdb')
|
|
|
|
if not os.path.exists(path):
|
|
|
|
run('./pandora/manage.py', 'update_geoip')
|
2016-03-07 08:55:24 +00:00
|
|
|
if old <= 5443:
|
|
|
|
gunicorn_config = 'pandora/gunicorn_config.py'
|
|
|
|
if not os.path.exists(gunicorn_config):
|
2017-02-28 19:42:53 +00:00
|
|
|
shutil.copy('%s.in' % gunicorn_config, gunicorn_config)
|
2016-03-07 08:55:24 +00:00
|
|
|
if os.path.exists('/etc/init/pandora.conf'):
|
|
|
|
with open('/etc/init/pandora.conf') as fd:
|
|
|
|
data = fd.read()
|
|
|
|
if '0.0.0.0:2620' in data:
|
|
|
|
run('sed', '-i', 's/127.0.0.1:2620/0.0.0.0:2620/g', gunicorn_config)
|
|
|
|
if old > 5389:
|
2016-03-17 09:38:15 +00:00
|
|
|
update_service('pandora')
|
2016-03-17 09:47:08 +00:00
|
|
|
if old <= 5452:
|
2016-03-17 09:38:15 +00:00
|
|
|
run('./bin/pip', 'install', '-r', 'requirements.txt')
|
|
|
|
update_service('pandora-encoding')
|
|
|
|
update_service('pandora-tasks')
|
2018-07-09 13:29:06 +00:00
|
|
|
if old < 5975:
|
2018-06-19 18:48:18 +00:00
|
|
|
run('./bin/pip', 'install', '-r', 'requirements.txt')
|
2018-11-15 10:28:30 +00:00
|
|
|
if old <= 6064:
|
|
|
|
run('./bin/pip', 'install', '-r', 'requirements.txt')
|
|
|
|
run('./pandora/manage.py', 'createcachetable')
|
2012-11-02 12:41:41 +00:00
|
|
|
else:
|
2013-04-18 11:12:42 +00:00
|
|
|
if len(sys.argv) == 1:
|
|
|
|
release = get_release()
|
2013-04-18 13:32:28 +00:00
|
|
|
repos = release['repositories']
|
2013-04-18 11:12:42 +00:00
|
|
|
development = False
|
2012-11-14 17:52:26 +00:00
|
|
|
else:
|
2013-04-18 11:12:42 +00:00
|
|
|
release = {
|
|
|
|
'date': 'development'
|
|
|
|
}
|
|
|
|
development = True
|
|
|
|
os.chdir(base)
|
|
|
|
current = ''
|
|
|
|
new = ''
|
2017-02-16 13:38:29 +00:00
|
|
|
if development:
|
2017-02-16 14:22:21 +00:00
|
|
|
if get('git', 'symbolic-ref', 'HEAD').strip().split('/')[-1] != 'master':
|
2017-02-16 13:38:29 +00:00
|
|
|
print('update only possible if you are on master branch')
|
|
|
|
sys.exit(1)
|
2013-07-03 21:14:11 +00:00
|
|
|
for repo in sorted(repos, key=lambda r: repos[r]['path']):
|
2013-04-18 11:12:42 +00:00
|
|
|
path = os.path.join(base, repos[repo]['path'])
|
|
|
|
if exists(path):
|
|
|
|
os.chdir(path)
|
2015-11-14 14:22:40 +00:00
|
|
|
revno = get_version(path)
|
2013-05-30 11:17:48 +00:00
|
|
|
if repo == 'pandora':
|
|
|
|
pandora_old_revno = revno
|
2013-04-18 11:12:42 +00:00
|
|
|
current += revno
|
|
|
|
url = repos[repo]['url']
|
|
|
|
if 'revision' in repos[repo]:
|
2015-11-14 14:22:40 +00:00
|
|
|
if revno != repos[repo]['revision']:
|
|
|
|
run('git', 'fetch')
|
|
|
|
run('git', 'checkout', repos[repo]['commit'])
|
2013-04-18 11:12:42 +00:00
|
|
|
else:
|
2016-02-18 10:49:26 +00:00
|
|
|
print('Checking', repo)
|
2016-02-20 06:46:53 +00:00
|
|
|
#run('git', 'checkout', 'master', '-q')
|
2015-11-14 14:22:40 +00:00
|
|
|
run('git', 'pull')
|
|
|
|
revno = get_version(path)
|
2013-04-18 11:12:42 +00:00
|
|
|
new += revno
|
2013-05-30 11:17:48 +00:00
|
|
|
if repo == 'pandora':
|
|
|
|
pandora_new_revno = revno
|
2013-04-18 11:12:42 +00:00
|
|
|
else:
|
|
|
|
os.chdir(os.path.dirname(path))
|
2015-11-14 14:22:40 +00:00
|
|
|
if 'revision' in repos[repo]:
|
2016-02-20 07:49:06 +00:00
|
|
|
run('git', 'clone', repos[repo]['url'])
|
2015-11-14 14:22:40 +00:00
|
|
|
run_git(path, 'checkout', repos[repo]['commit'])
|
2016-02-20 07:49:06 +00:00
|
|
|
else:
|
|
|
|
run('git', 'clone', '--depth', '1', repos[repo]['url'])
|
2013-04-18 11:12:42 +00:00
|
|
|
setup = os.path.join(base, repos[repo]['path'], 'setup.py')
|
|
|
|
if repo in ('python-ox', 'oxtimelines') and os.path.exists(setup):
|
|
|
|
os.chdir(os.path.dirname(setup))
|
|
|
|
run(os.path.join(base, 'bin', 'python'), 'setup.py', 'develop')
|
|
|
|
new += '+'
|
2012-11-02 12:41:41 +00:00
|
|
|
os.chdir(join(base, 'pandora'))
|
2013-10-22 20:03:42 +00:00
|
|
|
if pandora_old_revno != pandora_new_revno:
|
|
|
|
os.chdir(base)
|
|
|
|
run('./update.py', 'postupdate', pandora_old_revno, pandora_new_revno)
|
2013-10-28 11:11:58 +00:00
|
|
|
os.chdir(join(base, 'pandora'))
|
2016-02-20 12:26:14 +00:00
|
|
|
if current != new:
|
|
|
|
run('./manage.py', 'update_static')
|
2016-03-03 09:34:54 +00:00
|
|
|
run('./manage.py', 'compile_pyc', '-p', '.')
|
2016-02-20 12:26:14 +00:00
|
|
|
os.chdir(join(base, 'pandora'))
|
2012-11-02 12:41:41 +00:00
|
|
|
diff = get('./manage.py', 'sqldiff', '-a').strip()
|
|
|
|
if diff != '-- No differences':
|
2016-02-18 10:49:26 +00:00
|
|
|
print('Database has changed, please make a backup and run %s db' % sys.argv[0])
|
2014-10-12 15:10:33 +00:00
|
|
|
elif not development:
|
2016-02-18 10:49:26 +00:00
|
|
|
print('pan.do/ra is at the latest release,\nyou can run "%s dev" to update to the development version' % sys.argv[0])
|
2013-10-22 20:03:42 +00:00
|
|
|
elif current != new:
|
2013-03-01 07:09:44 +00:00
|
|
|
reload_notice(base)
|