Exemplo n.º 1
0
 def run(self):
     try:
         runner = ansible_module_runner.AnsibleRunner(
             ANSIBLE_MODULE_PATH, **self.attributes)
     except ansible_module_runner.AnsibleModuleNotFound:
         # Backward compat ansible<=2.2
         runner = ansible_module_runner.AnsibleRunner(
             "core/" + ANSIBLE_MODULE_PATH, **self.attributes)
     try:
         result, err = runner.run()
         try:
             logger.log("debug", NS.publisher_id,
                        {"message": "Command Execution: %s" % result})
         except KeyError:
             sys.stdout.write("Command Execution: %s \n" % result)
     except ansible_module_runner.AnsibleExecutableGenerationFailed as e:
         try:
             Event(
                 ExceptionMessage(priority="debug",
                                  publisher=NS.publisher_id,
                                  payload={
                                      "message":
                                      "could not run the command %s. " %
                                      self.attributes["_raw_params"],
                                      "exception":
                                      e
                                  }))
         except KeyError:
             sys.stderr.write("could not run the command %s. Error: %s\n" %
                              (self.attributes["_raw_params"], str(e)))
         return "", str(e.message), -1
     stdout = result.get("stdout", "")
     stderr = result.get("stderr", "").encode("ascii")
     rc = result.get("rc", -1)
     return stdout, stderr, rc
Exemplo n.º 2
0
def install_gdeploy():
    # Install gdeploy on the node
    ansible_module_path = "packaging/os/yum.py"
    attributes = dict()
    attributes["name"] = "gdeploy"
    try:
        runner = ansible_module_runner.AnsibleRunner(
            ansible_module_path,
            **attributes
        )
    except ansible_module_runner.AnsibleModuleNotFound:
        # Backward compat ansible<=2.2
        runner = ansible_module_runner.AnsibleRunner(
            "core/" + ansible_module_path,
            **attributes
        )
    try:
        result, err = runner.run()
        if result.get('failed', None):
            raise FlowExecutionFailedError(
                "Failed to install gdeploy. %s" % result['msg']
            )
    except ansible_module_runner.AnsibleExecutableGenerationFailed:
        raise FlowExecutionFailedError(
            "Failed to install gdeploy"
        )
Exemplo n.º 3
0
def install_python_gdeploy():
    attributes = {}
    # Install python-gdeploy on the node
    if NS.config.data['package_source_type'] == 'pip':
        name = "https://github.com/Tendrl/python-gdeploy/archive/master.tar.gz"
        attributes["name"] = name
        attributes["editable"] = "false"
        ansible_module_path = "packaging/language/pip.py"
    elif NS.config.data['package_source_type'] == 'rpm':
        name = "python-gdeploy"
        ansible_module_path = "packaging/os/yum.py"
        attributes["name"] = name
    else:
        raise FlowExecutionFailedError(
            "Failed to install python-gdeploy. Invalid package source type")

    try:
        runner = ansible_module_runner.AnsibleRunner(ansible_module_path,
                                                     **attributes)
    except ansible_module_runner.AnsibleModuleNotFound:
        # Backward compat ansible<=2.2
        runner = ansible_module_runner.AnsibleRunner(
            "core/" + ansible_module_path, **attributes)
    try:
        result, err = runner.run()
        if result.get('failed', None):
            raise FlowExecutionFailedError(
                "Failed to install python-gdeploy. %s" % result['msg'])
    except ansible_module_runner.AnsibleExecutableGenerationFailed:
        raise FlowExecutionFailedError("Failed to install python-gdeploy")
Exemplo n.º 4
0
    def install(self):
        try:
            runner = ansible_module_runner.AnsibleRunner(
                self.ansible_module_path, **self.attributes)
        except ansible_module_runner.AnsibleModuleNotFound:
            # Backward compat ansible<=2.2
            self.ansible_module_path = "core/" + self.ansible_module_path
            runner = ansible_module_runner.AnsibleRunner(
                self.ansible_module_path, **self.attributes)

        try:
            result, err = runner.run()
            logger.log("debug", NS.publisher_id,
                       {"message": "INSTALLATION: %s" % result})
        except ansible_module_runner.AnsibleExecutableGenerationFailed as e:
            logger.log(
                "debug", NS.publisher_id, {
                    "message":
                    "Could not install package: %s. Error:"
                    " %s" % (self.attributes["name"], str(e))
                })
            return e.message, False
        message = result.get("msg", "").encode("ascii")
        if result.get("rc", -1) == 0:
            success = True
        else:
            success = False
        return message, success
Exemplo n.º 5
0
 def __run_module(self, attr):
     try:
         runner = ansible_module_runner.AnsibleRunner(
             ANSIBLE_MODULE_PATH,
             publisher_id=self.publisher_id,
             node_id=self.node_id,
             socket_path=self.socket_path,
             **attr)
     except ansible_module_runner.AnsibleModuleNotFound:
         # Backward compat ansible<=2.2
         runner = ansible_module_runner.AnsibleRunner(
             "core/" + ANSIBLE_MODULE_PATH,
             publisher_id=self.publisher_id,
             node_id=self.node_id,
             socket_path=self.socket_path,
             **attr)
     try:
         result, err = runner.run()
         Event(Message(
             priority="debug",
             publisher=self.publisher_id,
             payload={"message": "Service Management: %s" % result},
             node_id=self.node_id),
               socket_path=self.socket_path)
     except ansible_module_runner.AnsibleExecutableGenerationFailed as e:
         Event(Message(priority="error",
                       publisher=self.publisher_id,
                       payload={
                           "message":
                           "Error switching the service: "
                           "%s to %s state. Error: %s" %
                           (self.attributes["name"], attr["state"], str(e))
                       },
                       node_id=self.node_id),
               socket_path=self.socket_path)
         return e.message, False
     message = result.get("msg", "").encode("ascii")
     state = result.get("state", "").encode("ascii")
     if attr["state"] in ["started", "restarted", "reloaded"]:
         if state == "started":
             success = True
         else:
             success = False
     else:
         if attr["state"] == state:
             success = True
         else:
             success = False
     return message, success
Exemplo n.º 6
0
    def run(self):
        result = None
        out = None
        try:
            runner = ansible_module_runner.AnsibleRunner(
                ANSIBLE_MODULE_PATH, **self.attributes)
        except ansible_module_runner.AnsibleModuleNotFound:
            # Backward compat ansible<=2.2
            runner = ansible_module_runner.AnsibleRunner(
                "core/" + ANSIBLE_MODULE_PATH, **self.attributes)
        try:
            out, err = runner.run()
            Event(
                Message(priority="debug",
                        publisher="commons",
                        payload={"message": "SSH-key Generation: %s" % out}))
        except ansible_module_runner.AnsibleExecutableGenerationFailed as e:
            err = str(e.message)
            Event(
                Message(priority="debug",
                        publisher="commons",
                        payload={
                            "message":
                            "SSH-Key Genertion failed %s. "
                            "Error: %s" % (self.attributes["_raw_params"], err)
                        }))
            out = "Ansible Executable Generation Failed"
        if out is None:
            _msg = "No output after Ansible Executable Generation"
            Event(
                Message(priority="debug",
                        publisher="commons",
                        payload={"message": _msg}))
            return None, "No Output"
        if out is not None and "ssh_public_key" not in out:
            err = out
            Event(
                Message(priority="debug",
                        publisher="commons",
                        payload={
                            "message":
                            "Unable to generate ssh-key .err: "
                            "%s" % err
                        }))
        elif "ssh_public_key" in out:
            result = out["ssh_public_key"]

        return result, err
Exemplo n.º 7
0
    def run(self):
        name = self.parameters.get("Package.name")
        package_type = self.parameters.get("Package.pkg_type", "pip")
        version = self.parameters.get("Package.version", None)
        attributes = {}
        attributes["name"] = name
        if version:
            attributes["version"] = version

        if package_type == "pip":
            attributes["editable"] = "false"
            ansible_module_path = "core/packaging/language/pip.py"
        elif package_type == "rpm":
            ansible_module_path = "core/packaging/os/yum.py"
        elif package_type == "deb":
            ansible_module_path = "core/packaging/os/apt.py"

        try:
            runner = ansible_module_runner.AnsibleRunner(
                ansible_module_path,
                tendrl_ns.config.data['tendrl_ansible_exec_file'],
                **attributes)
            result, err = runner.run()
        except ansible_module_runner.AnsibleExecutableGenerationFailed:
            self.parameters.update({"Package.state": "uninstalled"})
            return False
        self.parameters.update({"Package.state": "installed"})
        return True
Exemplo n.º 8
0
    def run(self):
        """This function is used to copy the given authorize ssh-key

        output:
            True/False, error
        """
        try:
            runner = ansible_module_runner.AnsibleRunner(
                ANSIBLE_MODULE_PATH, **self.attributes)
        except ansible_module_runner.AnsibleModuleNotFound:
            # Backward compat ansible<=2.2
            runner = ansible_module_runner.AnsibleRunner(
                "core/" + ANSIBLE_MODULE_PATH, **self.attributes)
        try:
            result, err = runner.run()
            if 'failed' in result:
                err = result
            else:
                Event(
                    Message(priority="debug",
                            publisher="commons",
                            payload={"message": "Authorize key: %s" % result}))
        except ansible_module_runner.AnsibleExecutableGenerationFailed as e:
            Event(
                Message(priority="debug",
                        publisher="commons",
                        payload={
                            "message":
                            "Copying authorize key failed %s. "
                            "Error: %s" %
                            (self.attributes["_raw_params"], str(e.message))
                        }))
            err = str(e.message)
        if err != "":
            Event(
                Message(priority="debug",
                        publisher="commons",
                        payload={
                            "message":
                            "Unable to copy authorize key "
                            ".err:%s" % err
                        }))
            return False, err
        else:
            return True, err
Exemplo n.º 9
0
def import_gluster(integration_id):
    attributes = {}
    if tendrl_ns.config.data['package_source_type'] == 'pip':
        name = "git+https://github.com/Tendrl/[email protected]"
        attributes["name"] = name
        attributes["editable"] = "false"
        ansible_module_path = "core/packaging/language/pip.py"
    elif tendrl_ns.config.data['package_source_type'] == 'rpm':
        name = "tendrl-gluster-integration"
        ansible_module_path = "core/packaging/os/yum.py"
        attributes["name"] = name
    else:
        return False

    try:
        runner = ansible_module_runner.AnsibleRunner(
            ansible_module_path,
            tendrl_ns.config.data['tendrl_ansible_exec_file'], **attributes)
        result, err = runner.run()
    except ansible_module_runner.AnsibleExecutableGenerationFailed:
        return False

    with open(
            "/etc/tendrl/gluster-integration/gluster-integration_logging"
            ".yaml", 'w+') as f:
        f.write(logging_file)

    config_data = {
        "etcd_port":
        tendrl_ns.config.data['etcd_port'],
        "etcd_connection":
        tendrl_ns.config.data['etcd_connection'],
        "tendrl_ansible_exec_file":
        "$HOME/.tendrl/gluster-integration/ansible_exec",
        "log_cfg_path":
        "/etc/tendrl/gluster-integration/gluster-integration_logging"
        ".yaml",
        "log_level":
        "DEBUG"
    }
    with open(
            "/etc/tendrl/gluster-integration/gluster-integration"
            ".conf.yaml", 'w') as outfile:
        yaml.dump(config_data, outfile, default_flow_style=False)

    gluster_integration_context = "/etc/tendrl/gluster-integration/integration_id"
    with open(gluster_integration_context, 'wb+') as f:
        f.write(integration_id)

    subprocess.Popen(["nohup", "tendrl-gluster-integration", "&"])
Exemplo n.º 10
0
def import_gluster(parameters):
    logging_file_name = "gluster-integration_logging.yaml"
    logging_config_file_path = "/etc/tendrl/gluster-integration/"

    attributes = {}
    if NS.config.data['package_source_type'] == 'pip':
        _cmd = "nohup tendrl-gluster-integration &"
        name = "https://github.com/Tendrl/gluster-integration/archive/master" \
               ".tar.gz"
        attributes["name"] = name
        attributes["editable"] = "false"
        ansible_module_path = "packaging/language/pip.py"
    elif NS.config.data['package_source_type'] == 'rpm':
        name = "tendrl-gluster-integration"
        _cmd = "systemctl restart %s" % name
        ansible_module_path = "packaging/os/yum.py"
        attributes["name"] = name
    else:
        return False

    logger.log(
        "info",
        NS.publisher_id,
        {
            'message':
            "Installing tendrl-gluster-integration on "
            "Node %s" % NS.node_context.fqdn
        },
        job_id=parameters['job_id'],
        flow_id=parameters['flow_id'],
    )

    try:
        runner = ansible_module_runner.AnsibleRunner(ansible_module_path,
                                                     **attributes)
    except ansible_module_runner.AnsibleModuleNotFound:
        # Backward compat ansible<=2.2
        runner = ansible_module_runner.AnsibleRunner(
            "core/" + ansible_module_path, **attributes)
    try:
        out, err = runner.run()
        if out['rc'] != 0:
            logger.log(
                "error",
                NS.publisher_id,
                {
                    "message":
                    "Could not install "
                    "tendrl-gluster-integration on Node %s"
                    "Error: %s" % (NS.node_context.fqdn, out['msg'])
                },
                job_id=parameters['job_id'],
                flow_id=parameters['flow_id'],
            )
            return False
    except ansible_module_runner.AnsibleExecutableGenerationFailed:
        logger.log(
            "error",
            NS.publisher_id,
            {
                "message":
                "Error: Could not install "
                "tendrl-gluster-integration on Node %s" % NS.node_context.fqdn
            },
            job_id=parameters['job_id'],
            flow_id=parameters['flow_id'],
        )
        return False

    logger.log(
        "info",
        NS.publisher_id,
        {
            "message":
            "Generating configuration for "
            "tendrl-gluster-integration on Node %s" % NS.node_context.fqdn
        },
        job_id=parameters['job_id'],
        flow_id=parameters['flow_id'],
    )

    with open(logging_config_file_path + logging_file_name, 'w+') as f:
        f.write(pkg_resources.resource_string(__name__, logging_file_name))
    gluster_integration_tag = NS.compiled_definitions.get_parsed_defs(
    )['namespace.tendrl']['tags']['tendrl-gluster-integration']
    config_data = {
        "etcd_port": int(NS.config.data['etcd_port']),
        "etcd_connection": str(NS.config.data['etcd_connection']),
        "log_cfg_path": (logging_config_file_path + logging_file_name),
        "log_level": "DEBUG",
        "logging_socket_path": "/var/run/tendrl/message.sock",
        "sync_interval": int(NS.config.data['sync_interval']),
        "tags": [gluster_integration_tag]
    }
    etcd_ca_cert_file = NS.config.data.get("etcd_ca_cert_file")
    etcd_cert_file = NS.config.data.get("etcd_cert_file")
    etcd_key_file = NS.config.data.get("etcd_key_file")
    if etcd_ca_cert_file and str(
            etcd_ca_cert_file) != "" and etcd_cert_file and str(
                etcd_cert_file) != "" and etcd_key_file and str(
                    etcd_key_file) != "":
        config_data.update({
            "etcd_ca_cert_file":
            NS.config.data['etcd_ca_cert_file'],
            "etcd_cert_file":
            NS.config.data['etcd_cert_file'],
            "etcd_key_file":
            NS.config.data['etcd_key_file']
        })

    _gluster_integration_conf_file_path = \
        "/etc/tendrl/gluster-integration/gluster-integration.conf.yaml"
    with open(_gluster_integration_conf_file_path, 'w') as outfile:
        yaml.dump(config_data, outfile, default_flow_style=False)
    logger.log(
        "info",
        NS.publisher_id,
        {
            "message":
            "Running tendrl-gluster-integration on Node "
            "%s" % NS.node_context.fqdn
        },
        job_id=parameters['job_id'],
        flow_id=parameters['flow_id'],
    )
    os.chmod(_gluster_integration_conf_file_path, 0o640)

    if NS.config.data['package_source_type'] == 'rpm':
        command = cmd_utils.Command(
            "systemctl enable tendrl-gluster-integration")
        err, out, rc = command.run()
        if err:
            logger.log(
                "error",
                NS.publisher_id,
                {
                    "message":
                    "Could not enable gluster-integration"
                    " service. Error: %s" % err
                },
                job_id=parameters['job_id'],
                flow_id=parameters['flow_id'],
            )
            return False

    cmd = cmd_utils.Command(_cmd)
    err, out, rc = cmd.run()
    if err:
        logger.log(
            "error",
            NS.publisher_id,
            {
                "message":
                "Could not start gluster-integration"
                " service. Error: %s" % err
            },
            job_id=parameters['job_id'],
            flow_id=parameters['flow_id'],
        )
        return False

    return True
Exemplo n.º 11
0
def import_gluster(parameters):
    logging_file_name = "gluster-integration_logging.yaml"
    logging_config_file_path = "/etc/tendrl/gluster-integration/"

    attributes = {}
    if NS.config.data['package_source_type'] == 'pip':
        _cmd = "nohup tendrl-gluster-integration &"
        name = "https://github.com/Tendrl/gluster-integration/archive/master" \
               ".tar.gz"
        attributes["name"] = name
        attributes["editable"] = "false"
        ansible_module_path = "packaging/language/pip.py"
    elif NS.config.data['package_source_type'] == 'rpm':
        name = "tendrl-gluster-integration"
        _cmd = "systemctl restart %s" % name
        ansible_module_path = "packaging/os/yum.py"
        attributes["name"] = name
    else:
        return False

    Event(
        Message(job_id=parameters['job_id'],
                flow_id=parameters['flow_id'],
                priority="info",
                publisher=NS.publisher_id,
                payload={
                    "message":
                    "Installing tendrl-gluster-integration on "
                    "Node %s" % NS.node_context.node_id
                }))

    try:
        runner = ansible_module_runner.AnsibleRunner(ansible_module_path,
                                                     **attributes)
    except ansible_module_runner.AnsibleModuleNotFound:
        # Backward compat ansible<=2.2
        runner = ansible_module_runner.AnsibleRunner(
            "core/" + ansible_module_path, **attributes)
    try:
        runner.run()
    except ansible_module_runner.AnsibleExecutableGenerationFailed:
        Event(
            Message(job_id=parameters['job_id'],
                    flow_id=parameters['flow_id'],
                    priority="error",
                    publisher=NS.publisher_id,
                    payload={
                        "message":
                        "Error: Could not install "
                        "tendrl-gluster-integration on Node %s" %
                        NS.node_context.node_id
                    }))

        return False

    Event(
        Message(job_id=parameters['job_id'],
                flow_id=parameters['flow_id'],
                priority="info",
                publisher=NS.publisher_id,
                payload={
                    "message":
                    "Generating configuration for "
                    "tendrl-gluster-integration on Node %s" %
                    NS.node_context.node_id
                }))

    with open(logging_config_file_path + logging_file_name, 'w+') as f:
        f.write(pkg_resources.resource_string(__name__, logging_file_name))
    gluster_integration_tag = NS.compiled_definitions.get_parsed_defs(
    )['namespace.tendrl']['tags']['tendrl-gluster-integration']
    config_data = {
        "etcd_port": int(NS.config.data['etcd_port']),
        "etcd_connection": str(NS.config.data['etcd_connection']),
        "log_cfg_path": (logging_config_file_path + logging_file_name),
        "log_level": "DEBUG",
        "logging_socket_path": "/var/run/tendrl/message.sock",
        "sync_interval": 10,
        "tags": [gluster_integration_tag]
    }
    with open("/etc/tendrl/gluster-integration/gluster-integration.conf.yaml",
              'w') as outfile:
        yaml.dump(config_data, outfile, default_flow_style=False)
    Event(
        Message(job_id=parameters['job_id'],
                flow_id=parameters['flow_id'],
                priority="info",
                publisher=NS.publisher_id,
                payload={
                    "message":
                    "Running tendrl-gluster-integration on Node "
                    "%s" % NS.node_context.node_id
                }))

    subprocess.Popen(_cmd.split())
Exemplo n.º 12
0
def import_gluster(integration_id, request_id, flow_id):
    attributes = {}
    if tendrl_ns.config.data['package_source_type'] == 'pip':
        name = "git+https://github.com/Tendrl/[email protected]"
        attributes["name"] = name
        attributes["editable"] = "false"
        ansible_module_path = "core/packaging/language/pip.py"
    elif tendrl_ns.config.data['package_source_type'] == 'rpm':
        name = "tendrl-gluster-integration"
        ansible_module_path = "core/packaging/os/yum.py"
        attributes["name"] = name
    else:
        return False

    try:
        runner = ansible_module_runner.AnsibleRunner(
            ansible_module_path,
            tendrl_ns.config.data['tendrl_ansible_exec_file'], **attributes)
        result, err = runner.run()
    except ansible_module_runner.AnsibleExecutableGenerationFailed:
        return False

    Event(
        Message(
            priority="info",
            publisher=tendrl_ns.publisher_id,
            payload={
                "message":
                "Installed storage binaries on node %s" %
                tendrl_ns.node_context.fqdn
            },
            request_id=request_id,
            flow_id=flow_id,
            cluster_id=integration_id,
        ))

    with open(
            "/etc/tendrl/gluster-integration/gluster-integration_logging"
            ".yaml", 'w+') as f:
        f.write(logging_file)

    config_data = {
        "etcd_port":
        tendrl_ns.config.data['etcd_port'],
        "etcd_connection":
        tendrl_ns.config.data['etcd_connection'],
        "tendrl_ansible_exec_file":
        "$HOME/.tendrl/gluster-integration/ansible_exec",
        "log_cfg_path":
        "/etc/tendrl/gluster-integration/gluster-integration_logging"
        ".yaml",
        "log_level":
        "DEBUG",
        "logging_socket_path":
        "/var/run/tendrl/message.sock"
    }
    with open(
            "/etc/tendrl/gluster-integration/gluster-integration"
            ".conf.yaml", 'w') as outfile:
        yaml.dump(config_data, outfile, default_flow_style=False)

    gluster_integration_context = "/etc/tendrl/gluster-integration/integration_id"
    with open(gluster_integration_context, 'wb+') as f:
        f.write(integration_id)
    Event(
        Message(
            priority="info",
            publisher=tendrl_ns.publisher_id,
            payload={
                "message": "Created gluster integration configuration file"
            },
            request_id=request_id,
            flow_id=flow_id,
            cluster_id=integration_id,
        ))

    subprocess.Popen(["nohup", "tendrl-gluster-integration", "&"])

    Event(
        Message(
            priority="info",
            publisher=tendrl_ns.publisher_id,
            payload={
                "message":
                "Started gluster integration daemon on node %s" %
                tendrl_ns.node_context.fqdn
            },
            request_id=request_id,
            flow_id=flow_id,
            cluster_id=integration_id,
        ))