Exemple #1
0
def get_log_directory():
    """
    Returns the directory where the Skyline log files may be found.

    :return: Path to the Skyline log directory.
    :rtype: str
    """
    if is_windows():
        return os.path.join(get_application_data_directory(), 'Logs')
    return '/var/log/systemlink'
Exemple #2
0
def get_configuration_directory():
    """
    Returns the directory where the Skyline configuration files may be found.

    :return: Path to the Skyline configuration directory.
    :rtype: str
    """
    if is_windows():
        return os.path.join(get_base_configuration_directory(), 'Config')
    return os.path.join(get_base_configuration_directory(), 'config')
Exemple #3
0
def get_base_configuration_directory():
    """
    Returns the directory where the Skyline configuration files may be found.

    :return: Path to the Skyline configuration directory.
    :rtype: str
    """
    if is_windows():
        return get_application_data_directory()
    if _IS_LEGACY_LINUX:
        return '/etc/natinst/niskyline'
    return '/etc/systemlink'
Exemple #4
0
def get_application_directory():
    """
    Returns the directory where the Skyline binaries may be found.

    :return: Path to the Skyline application directory.
    :rtype: str
    """
    if is_windows():
        # Get Program Files directory
        ni_shared_path = _get_ni_shared_dir()
        path = os.path.join(ni_shared_path, 'Skyline')
        return path
    return '/usr/local/bin'
Exemple #5
0
def get_service_descriptor_install_directory():  # pylint: disable=invalid-name
    """
    Returns the directory where the service descriptors
    are added and removed during install time.

    :return: Path to the service descriptor install directory.
    :rtype: str
    """
    if is_windows():
        return os.path.join(get_base_configuration_directory(), 'Install',
                            'Services', 'Descriptors')
    return os.path.join(get_base_configuration_directory(), 'install',
                        'services', 'descs')
Exemple #6
0
def get_application_data_directory():
    """
    Returns the directory where the Skyline data files may be found.

    :return: Path to the Skyline date file directory.
    :rtype: str
    """
    if is_windows():
        # Get APPDATA directory
        ni_appdata_path = _get_ni_common_appdata_dir()
        path = os.path.join(ni_appdata_path, 'Skyline')
        return path
    if _IS_LEGACY_LINUX:
        return '/etc/natinst/niskyline'
    return '/var/lib/systemlink'
Exemple #7
0
def get_skyline_configurations_directory():  # pylint: disable=invalid-name
    """
    Return path to the Skyline AMQP configurations directory.

    :return: Path to Skyline AMQP configurations directory.
    :rtype: str
    """
    if is_windows() or _IS_LEGACY_LINUX:
        return os.path.join(
            get_base_configuration_directory(),
            'SkylineConfigurations',
        )
    return os.path.join(
        get_base_configuration_directory(),
        'amqp_config',
    )
def stop_service_manager(silent=False):
    """
    Stop the NI Skyline Service Manager.

    :param silent: If ``False``, will print status to stdout/stderr.
        If ``True``, will not print status.
    :type silent: bool
    :return: ``True`` if the NI Skyline Service Manager was successfully
        stopped. ``False`` if the NI Skyline Service Manager was already
        stopped.
    :rtype: bool
    """
    if not utils.is_windows():
        msg = 'This operation is currently only supported in Windows'
        if not silent:
            utils.eprint(msg)
        raise NotImplementedError(msg)

    sysdir = shell.SHGetFolderPath(0, shellcon.CSIDL_SYSTEM, 0, 0)
    net_exe = os.path.join(sysdir, 'net.exe')
    if not silent:
        print('Stopping NI Skyline Service Manager')
    retcode = subprocess.call([net_exe, 'stop', WINDOWS_SERVICE_NAME, '/y'])
    if retcode == NET_EXE_RETCODE_ALREADY_STOPPED:
        if not silent:
            print('NI Skyline Service Manager already stopped')
        return False
    if retcode != 0:
        msg = ('Failed to stop the NI Skyline Service Manager '
               'with return code {0}'.format(retcode))
        if not silent:
            utils.eprint(msg)
        raise OSError(msg)
    if not silent:
        print('NI Skyline Service Manager successfully started')
    return True
# Import python libs
import os.path
import subprocess

# Import local libs
# pylint: disable=import-error
from systemlink.messagebus import service_manager_messages  # pylint: disable=no-name-in-module
from systemlink.messagebus import utils
from systemlink.messagebus.amqp_connection_manager import AmqpConnectionManager
from systemlink.messagebus.exceptions import SystemLinkException
from systemlink.messagebus.message_service import MessageService
from systemlink.messagebus.message_service_builder import MessageServiceBuilder
# pylint: enable=import-error

if utils.is_windows():
    from win32com.shell import shellcon, shell  # pylint: disable=no-name-in-module, import-error

WINDOWS_SERVICE_NAME = 'NI Skyline Service Manager'
SC_EXE_RETCODE_ALREADY_RUNNING = 1056
NET_EXE_RETCODE_ALREADY_STOPPED = 2


def start_service_manager(silent=False):
    """
    Start the NI Skyline Service Manager.

    :param silent: If ``False``, will print status to stdout/stderr.
        If ``True``, will not print status.
    :type silent: bool
    :return: ``True`` if the NI Skyline Service Manager was successfully
Exemple #10
0
try:
    from systemlink.messagebus.utils import is_windows
except ImportError:
    # Import as relative libs (for relocatability)
    # Add the current directory to the search path.
    sys.path.append(os.path.dirname(os.path.abspath(__file__)))
    try:
        from utils import is_windows
    finally:
        # Remove the extra search path that we added to sys.path
        del sys.path[-1:]
# pylint: enable=import-error

_IS_LEGACY_LINUX = False

if is_windows():
    # pylint: disable=import-error
    import winreg
    # pylint: enable=import-error

    NI_INSTALLERS_REG_PATH = 'SOFTWARE\\National Instruments\\Common\\Installer'
    NI_INSTALLERS_REG_KEY_APP_DATA = 'NIPUBAPPDATADIR'
    NI_INSTALLERS_REG_KEY_SHARED = 'NISHAREDDIR64'

    def _get_ni_shared_dir():
        """
        Return the National Instruments Shared Application Directory.
        This looks like: 'C:\\Program Files\\National Instruments\\Shared'

        :return: The National Instruments Shared Application Directory.
        :rtype: str
    def _read_configurations_from_env():  # pylint: disable=too-many-locals
        """
        Attempt to read a configuration by reading the environment variables.

        :return: A dictionary with key of the configuration ID and value
             an instance of
             :class:`systemlink.messagebus.amqp_configuration.AmqpConfiguration`.
             Will return an empty dictionary if there is no
             configuration in the environment variables.
        :rtype: dict(str, systemlink.messagebus.amqp_configuration.AmqpConfiguration)
        """
        configurations = {}
        host = os.environ.get(Environment.HOST)
        if not host:
            return configurations
        port = os.environ.get(Environment.PORT)
        exchange = os.environ.get(Environment.EXCHANGE)
        user = os.environ.get(Environment.USER)
        password = os.environ.get(Environment.PASSWORD)
        cert = os.environ.get(Environment.CERTIFICATE)
        cert_path = os.environ.get(Environment.CERTIFICATE_PATH)

        # The certificate is optional.
        if not port or not exchange or not user or password is None:
            return configurations

        actual_cert_path = None
        if cert_path:
            actual_cert_path = cert_path
        elif cert:
            if is_windows():
                raise NotImplementedError(
                    'Passing a TLS certificate via the environment is not supported on Windows'
                )
            temp_cert_dir = os.path.dirname(_TEMP_CERT_PATH)
            if not os.path.isdir(temp_cert_dir):
                os.makedirs(temp_cert_dir)
            with open(_TEMP_CERT_PATH, 'w') as fp_:
                fp_.write(cert)
            actual_cert_path = _TEMP_CERT_PATH

        try:
            port_num = int(port)
        except (TypeError, ValueError):
            raise SystemLinkException.from_name('Skyline.FailedToParse')

        configuration = {
            'Id': 'skyline_localhost',
            'DisplayName': 'Local',
            'ConnectionType': 'Local',
            'ExchangeName': exchange,
            'Host': host,
            'Port': port_num,
            'User': user,
            'Password': password,
            'UseTls' : actual_cert_path is not None,
            'TlsServerName': host if actual_cert_path is not None else None,
            'CertPath': actual_cert_path
        }

        amqp_config = AmqpConfiguration(None, configuration)
        configurations[SKYLINE_LOCALHOST_CONFIGURATION_ID] = amqp_config
        return configurations