HEX
Server: Apache/2.2.34 (Unix) mod_fastcgi/mod_fastcgi-SNAP-0910052141
System: Linux Kou-Etsu-Dou 4.4.59+ #25556 SMP PREEMPT Thu Mar 4 18:03:46 CST 2021 x86_64
User: hosam (1026)
PHP: 7.2.29
Disabled: NONE
Upload Files
File: /volume1/@appstore/Contacts/scripts/sdk_plugin/contacts_common.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2000-2019 Synology Inc. All rights reserved.

import re
import os
import syslog
import json
import sys
import subprocess
from argparse import ArgumentParser

DB_TOOL = '/var/packages/Contacts/target/tool/contacts-db'
SERVICE_CTL_DSM6 = '/sbin/initctl'
SERVICE_CTL_DSM7 = '/usr/syno/bin/synosystemctl'
TASK_CENTER_JOB = 'pkg-Contacts-task-center'
APID_JOB = 'pkg-Contacts-apid'
RADICALE_JOB = 'pkg-Contacts-radicale'


def log(msg):
    syslog.syslog(syslog.LOG_INFO, str(msg))


def service_ctl(operation, job):
    if os.path.exists(SERVICE_CTL_DSM6):
        subprocess.call([SERVICE_CTL_DSM6, operation, job])
    else:
        subprocess.call([SERVICE_CTL_DSM7, operation, job])


'''
in DSM7 all sdk hook is blocking, so we should make it asynchronously
'''


def update_principal():
    log('Contacts sdk hook update_principal')
    subprocess.Popen([DB_TOOL, '--sync-update-principal'])


def update_directory_object():
    log('Contacts sdk hook update_directory_object')
    subprocess.Popen([DB_TOOL, '--update-all-directory-object'])


def update_principal_and_directory_object():
    log('Contacts sdk hook update_principal_and_directory_object')
    subprocess.Popen([DB_TOOL, '--all'])


def sync_dsm_domain():
    log('Contacts sdk hook sync_dsm_domain')
    # <Contacts> #322 - in DSM7, the DSM waits for the post dirsvs_join hook, which is waiting for the domain info of DSM
    # this causes curcular waiting, thus we'll use popen to make 'sync-dsm-domain' running asynchronously
    subprocess.Popen([DB_TOOL, '--sync-dsm-domain'])


def restart_daemon():
    log('Contacts sdk hook restart_daemon')
    service_ctl('restart', TASK_CENTER_JOB)
    service_ctl('restart', APID_JOB)
    service_ctl('restart', RADICALE_JOB)


class Hook:
    version = '1.0'
    skip_failed_event = True

    def pre(self):
        pass

    def post(self):
        pass


def Main(hook):
    def print_pkg_info(key):
        print(os.popen('/bin/get_key_value /var/packages/Contacts/INFO ' + key, 'r').read())

    parser = ArgumentParser(description='Contacts SDK Hooks')
    parser.add_argument("--sdk-mod-ver", action="store_true")
    parser.add_argument("--name", action="store_true")
    parser.add_argument("--pkg-ver", action="store_true")
    parser.add_argument("--vendor", action="store_true")
    parser.add_argument("--pre", action="store_true")
    parser.add_argument("--post", action="store_true")
    options = parser.parse_args()

    if options.sdk_mod_ver:
        print(hook.version)
    elif options.name:
        print_pkg_info('package')
    elif options.pkg_ver:
        print_pkg_info('version')
    elif options.vendor:
        print_pkg_info('maintainer')
    elif options.pre:
        hook.pre()
    elif options.post:
        log(hook.__class__.__name__ + ' post hook is triggered')
        log(str(os.environ))  # print env for debugging
        if os.environ.get('RESULT') == '0':
            hook.post()
        else:
            log('the RESULT of this sdk hook is not 0')
            if not hook.skip_failed_event:
                hook.post()