Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
# Copyright 2015 TrilioData Inc.
# All Rights Reserved.

import os
import errno
import socket
import fcntl
import struct
import subprocess
import errno
from time import sleep
from multiprocessing import Process

try:
    import ConfigParser
except ImportError:
    import configparser as ConfigParser

try:
    from oslo_log import log as logging
except ImportError:
    from .nova.openstack.common import log as logging

LOG = logging.getLogger(__name__)


def run_process(method, progress_tracking_file_path, *args):
    try:
        process = Process(target=method, args=args)
        process.start()
        if process.is_alive():
            sleep(10)
            # update the timestamp so the workloadmgr knows contego
            # is alive and kicking
            os.utime(progress_tracking_file_path, None)
    except Exception as ex:
        raise ex


def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(
        fcntl.ioctl(
            s.fileno(), 0x8915, struct.pack("256s", ifname[:15])  # SIOCGIFADDR
        )[20:24]
    )


def get_lan_ip(config_file):
    def get_config_value(cfg, Section, Option, Key=None):
        if cfg.has_option(Section, Option):
            return cfg.get(Section, Option, Key)
        else:
            return None

    cfg = ConfigParser.ConfigParser()
    cfg.read(config_file)
    ip = get_config_value(cfg, "DEFAULT", "my_ip")

    if ip is not None:
        return ip
    return socket.gethostname()


def ensure_tree(path):
    """Create a directory (and any ancestor directories required)

    :param path: Directory to create
    """
    try:
        os.makedirs(path)
    except OSError as exc:
        if exc.errno == errno.EEXIST:
            if not os.path.isdir(path):
                raise
        else:
            raise


def touch_file(file_path):
    """ Utility to simulate the filesystem "touch" command.

    Args:
        file_path (str): Path to file to be touched.
    """
    # In order to support both local filesystem and Fuse mounts we
    # need to open the file and call os.utime. Opening the file updates
    # the time stamp for Fuse mounts, but os.utime() updates for local/nfs
    # file systems.
    with open(file_path, "a"):
        os.utime(file_path, None)


def update_progress(progress_tracking_file_path, progress_msg):
    LOG.info("update_progress %s: %s" % (progress_tracking_file_path, progress_msg))
    try:
        with open(progress_tracking_file_path, "a") as progress_tracking_file:
            progress_tracking_file.write(progress_msg)
    except Exception as ex:
        sleep(10)
        with open(progress_tracking_file_path, "a") as progress_tracking_file:
            progress_tracking_file.write(progress_msg)


def get_progress(progress_tracking_file_path):

    try:
        with open(progress_tracking_file_path, "r") as progress_tracking_file:
            return progress_tracking_file.readlines()
    except Exception as ex:
        sleep(10)
        with open(progress_tracking_file_path, "r") as progress_tracking_file:
            return progress_tracking_file.readlines()


def run_cmd(
    cmd,
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
    shell=False,
):
    process = subprocess.Popen(
        cmd, stdin=stdin, stdout=stdout, stderr=stderr, shell=shell
    )
    return process


def poll_process(process, cmd, sleep_time=5, no_of_count=0, progress_file=None):
    try:
        count = 0
        while process.poll() is None:
            if no_of_count != 0 and count > no_of_count:
                break
            sleep(sleep_time)
            count += 1
            if progress_file is not None:
                # update the timestamp so the workloadmgr knows contego
                # is alive and kicking
                touch_file(progress_file)

        process.stdin.close()
        _returncode = process.returncode
        if _returncode:
            LOG.error(("Result was %s" % _returncode))
            LOG.exception(
                "Execution error %(exit_code)d (%(stderr)s). "
                "cmd %(cmd)s"
                % {
                    "exit_code": _returncode,
                    "stderr": str(process.stderr.read(), "utf-8"),
                    "cmd": cmd,
                }
            )
        return _returncode
    except Exception as ex:
        raise ex


def get_cmd_specs(cmd, **kwargs):
    def _key_check(locals, keys):
        for key in keys:
            if locals[key] is None:
                raise Exception(
                    "Required parameter: %s for %s connection can not be None"
                    % (key, locals["cmd"])
                )

    try:
        authorized_key = kwargs.get("authorized_key", None)
        user = kwargs.get("user", None)
        host = kwargs.get("host", None)
        if cmd == "ssh":
            _key_check(locals(), ["authorized_key", "user", "host"])
            cmdspec = [
                "ssh",
                "-o",
                "StrictHostKeyChecking=no",
                "-i",
                authorized_key,
                user + "@" + host,
            ]
        elif cmd == "scp":
            source_path = kwargs.get("source_path", None)
            dest_path = kwargs.get("dest_path", None)
            _key_check(
                locals(),
                ["authorized_key", "user", "host", "source_path", "dest_path"],
            )
            cmdspec = [
                "scp",
                "-o",
                "StrictHostKeyChecking=no",
                "-i",
                authorized_key,
                "-r",
                user + "@" + host + ":" + source_path + "/*",
                dest_path,
            ]
        return cmdspec
    except Exception as ex:
        raise ex


class get_dummy_object(object):
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)