File: //usr/share/l.v.e-manager/utils/fix-nodejs-environments.py
#!/opt/cloudlinux/venv/bin/python3 -bb
# coding:utf-8
# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2019 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
import sys
from future.utils import iteritems
from clcommon.clpwd import ClPwd
from clselect.clselectctlnodejsuser import _create_environment
from clselect.clselectexcept import ClSelectExcept
from clselect.clselectnodejs import scan_node_versions
from clselect.clselectnodejs.apps_manager import ApplicationsManager
WRAPPERS_VERSION_FILE = '/usr/share/l.v.e-manager/utils/nodejs_selector_wrappers_version'
# Increment when introducing incompatible changes to nodejs virtualenv
LAST_WRAPPERS_VERSION = 1
def is_alt_nodejs_installed():
return scan_node_versions() != {}
def get_wrappers_version():
"""
Return the version of wrappers used in existing apps
:rtype: int or None
"""
try:
with open(WRAPPERS_VERSION_FILE, 'r') as f:
version = f.read()
except (IOError, OSError):
return None
else:
try:
return int(version)
except ValueError:
return None
def set_wrappers_version(wrappers_version=LAST_WRAPPERS_VERSION):
"""
Save the last wrappers version introducing incompatibility
:rtype: None
"""
try:
with open(WRAPPERS_VERSION_FILE, 'w') as f:
f.write("{}".format(wrappers_version))
except (IOError, OSError):
pass
def main():
if not is_alt_nodejs_installed():
sys.exit(0)
current_version = get_wrappers_version()
if current_version is not None and current_version >= LAST_WRAPPERS_VERSION:
sys.exit(0)
clpwd = ClPwd()
clpwd.get_user_dict()
users = clpwd.get_user_dict()
apps_manager = ApplicationsManager()
for user in users:
try:
user_config = apps_manager.get_user_config_data(user)
except ClSelectExcept.WrongData:
# Broken/missing per-user node-selector.json must not abort the
# whole migration: this runs as root from rpm_post.sh, so one
# poisoned user record would otherwise prevent
# set_wrappers_version() from advancing the sentinel and the
# next rpm_post run would re-hit the same record.
continue
# apps_manager.get_user_config_data() returns the raw json.loads()
# result; the user owns ~/.cl.selector/node-selector.json so the
# outer shape (must be a dict mapping approot -> {nodejs_version})
# is attacker-controlled. Skip entries that don't match.
if not isinstance(user_config, dict):
continue
for approot, app_info in iteritems(user_config):
if not isinstance(app_info, dict):
continue
nodejs_version = app_info.get('nodejs_version')
if not nodejs_version:
continue
try:
_create_environment(user, approot, nodejs_version, destroy_first=True)
except (ClSelectExcept.NoSuchAlternativeVersion,
ClSelectExcept.WrongData):
# WrongData covers approot escaping the user home
# (commons/lib/clselect/utils.py:get_abs_rel); without it,
# a single '..'-style approot would abort migration for
# every later user on the host.
continue
set_wrappers_version(LAST_WRAPPERS_VERSION)
if __name__ == "__main__":
main()
sys.exit(0)