Ejemplo n.º 1
0
    def run(self):
        out = self.docker_conn.pull(self.args.repository,
                                    tag=self.args.tag,
                                    stream=True)

        # only log the last line at info level
        id_map = {}
        for line in out:
            _g_logger.debug(line)
            line = line.decode()
            j_obj = json.loads(line)
            if 'id' in j_obj:
                id_map[j_obj['id']] = line
            elif 'error' in j_obj:
                _g_logger.error("Error pulling the image " + line)
                raise docker_utils.DCMDockerPullException(
                    repo=self.args.repository,
                    tag=self.args.tag,
                    error_msg=j_obj['error'])
        for k in id_map:
            dcm_logger.log_to_dcm_console_job_details(job_name=self.name,
                                                      details="pulled " +
                                                      id_map[k])
        return plugin_base.PluginReply(0,
                                       reply_type="docker_pull",
                                       reply_object=None)
Ejemplo n.º 2
0
    def run(self):
        try:
            dcm_logger.log_to_dcm_console_job_details(
                job_name=self.name,
                details="Test remote logging. %s" % str(self.arguments))
            for i in range(3):
                try:
                    self.sock = socket.socket(socket.AF_INET,
                                              socket.SOCK_STREAM)
                    self.sock.connect((self._host, self._port))
                    break
                except:
                    if i == 2:
                        raise

            msg = {"name": self.name, "arguments": self.arguments}

            self._msg = json.dumps(msg)

            _g_logger.info("Start tester remote socket.  Send " + self._msg)
            self.sock.send(self._msg.encode())
            _g_logger.info("waiting to get a message back")

            in_msg = b''
            ch = b'123'
            while len(ch) > 0:
                ch = self.sock.recv(1024)
                in_msg = in_msg + ch
            _g_logger.info("Tester plugin Received " + in_msg.decode())
            self.sock.close()
            rc_dict = json.loads(in_msg.decode())
            rc = rc_dict['return_code']
            try:
                reply_type = rc_dict['reply_type']
            except KeyError:
                reply_type = None
            try:
                reply_object = rc_dict['reply_object']
            except KeyError:
                reply_object = None
            try:
                message = rc_dict['message']
            except KeyError:
                message = None
            try:
                error_message = rc_dict['error_message']
            except KeyError:
                error_message = None

            rc = plugin_base.PluginReply(rc,
                                         reply_type=reply_type,
                                         reply_object=reply_object,
                                         message=message,
                                         error_message=error_message)
            _g_logger.info("Tester plugin sending back " + str(rc))
            return rc
        except:
            _g_logger.exception("Something went wrong here")
            return plugin_base.PluginReply(1)
Ejemplo n.º 3
0
    def run(self):
        try:
            dcm_logger.log_to_dcm_console_job_details(
                job_name=self.name,
                details="Test remote logging. %s" % str(self.arguments))
            for i in range(3):
                try:
                    self.sock = socket.socket(socket.AF_INET,
                                              socket.SOCK_STREAM)
                    self.sock.connect((self._host, self._port))
                    break
                except:
                    if i == 2:
                        raise

            msg = {"name": self.name, "arguments": self.arguments}

            self._msg = json.dumps(msg)

            _g_logger.info("Start tester remote socket.  Send " + self._msg)
            self.sock.send(self._msg.encode())
            _g_logger.info("waiting to get a message back")

            in_msg = b''
            ch = b'123'
            while len(ch) > 0:
                ch = self.sock.recv(1024)
                in_msg = in_msg + ch
            _g_logger.info("Tester plugin Received " + in_msg.decode())
            self.sock.close()
            rc_dict = json.loads(in_msg.decode())
            rc = rc_dict['return_code']
            try:
                reply_type = rc_dict['reply_type']
            except KeyError:
                reply_type = None
            try:
                reply_object = rc_dict['reply_object']
            except KeyError:
                reply_object = None
            try:
                message = rc_dict['message']
            except KeyError:
                message = None
            try:
                error_message = rc_dict['error_message']
            except KeyError:
                error_message = None

            rc = plugin_base.PluginReply(rc,
                                         reply_type=reply_type,
                                         reply_object=reply_object,
                                         message=message,
                                         error_message=error_message)
            _g_logger.info("Tester plugin sending back " + str(rc))
            return rc
        except:
            _g_logger.exception("Something went wrong here")
            return plugin_base.PluginReply(1)
Ejemplo n.º 4
0
    def run(self):
        try:
            events.global_pubsub.publish(
                events.DCMAgentTopics.CLEANUP,
                topic_kwargs={'request_id': self.job_id},
                done_cb=self._clean_topic_done)

            if self.args.delUser:
                dcm_logger.log_to_dcm_console_job_details(
                    job_name=self.name, details='Deleting users.')
                for user in self.args.delUser:
                    rdoc = remove_user.RemoveUser(self.conf, self.job_id, {
                        'script_name': 'removeUser'
                    }, 'remove_user', {
                        'userId': user
                    }).run()
                    if rdoc.get_return_code() != 0:
                        rdoc.set_message(rdoc.get_message() +
                                         " : Delete users failed on %s" % user)
                        return rdoc

            scrub_opts = ["-X", "-b", "-A"]
            if self.args.delHistory:
                scrub_opts.append("-H")
            if self.args.delKeys:
                dcm_logger.log_to_dcm_console_job_details(
                    job_name=self.name, details='Deleting private keys.')
                scrub_opts.append("-k")
            if self.args.recovery:
                # find the public key, if not there abort
                try:
                    username, public_key = self._db.get_owner()
                except:
                    _g_logger.exception("Could not get the owning user")
                    raise Exception(
                        "The agent could not encrypt the rescue image")
                if public_key is None:
                    raise Exception(
                        "The agent could not encrypt the rescue image")
                tar_file = "/tmp/dcm_agent_recovery.tar.gz"
                scrub_opts.extent(["-r", tar_file, "-e", public_key])

            self.run_scrubber(scrub_opts)

            self._done_event.wait()
            if self._topic_error is not None:
                return plugin_base.PluginReply(1,
                                               error_message=str(
                                                   self._topic_error))

            return plugin_base.PluginReply(
                0, message="Clean image command ran successfully")
        except Exception as ex:
            _g_logger.exception("clean_image failed: " + str(ex))
            return plugin_base.PluginReply(1,
                                           message=str(ex),
                                           error_message=str(ex))
Ejemplo n.º 5
0
    def run(self):
        try:
            events.global_pubsub.publish(
                events.DCMAgentTopics.CLEANUP,
                topic_kwargs={'request_id': self.job_id},
                done_cb=self._clean_topic_done)

            if self.args.delUser:
                dcm_logger.log_to_dcm_console_job_details(
                    job_name=self.name,
                    details='Deleting users.')
                for user in self.args.delUser:
                    rdoc = remove_user.RemoveUser(
                        self.conf,
                        self.job_id,
                        {'script_name': 'removeUser'},
                        'remove_user',
                        {'userId': user}).run()
                    if rdoc.get_return_code() != 0:
                        rdoc.set_message(rdoc.get_message() +
                                         " : Delete users failed on %s" % user)
                        return rdoc

            scrub_opts = ["-X", "-b", "-A"]
            if self.args.delHistory:
                scrub_opts.append("-H")
            if self.args.delKeys:
                dcm_logger.log_to_dcm_console_job_details(
                    job_name=self.name, details='Deleting private keys.')
                scrub_opts.append("-k")
            if self.args.recovery:
                # find the public key, if not there abort
                try:
                    username, public_key = self._db.get_owner()
                except:
                    _g_logger.exception("Could not get the owning user")
                    raise Exception(
                        "The agent could not encrypt the rescue image")
                if public_key is None:
                    raise Exception(
                        "The agent could not encrypt the rescue image")
                tar_file = "/tmp/dcm_agent_recovery.tar.gz"
                scrub_opts.extent(["-r", tar_file, "-e", public_key])

            self.run_scrubber(scrub_opts)

            self._done_event.wait()
            if self._topic_error is not None:
                return plugin_base.PluginReply(
                    1, error_message=str(self._topic_error))

            return plugin_base.PluginReply(
                0, message="Clean image command ran successfully")
        except Exception as ex:
            _g_logger.exception("clean_image failed: " + str(ex))
            return plugin_base.PluginReply(
                1, message=str(ex), error_message=str(ex))
Ejemplo n.º 6
0
def run_command(conf, cmd_line, cwd=None, in_env=None):
    _, log_file = tempfile.mkstemp(dir=conf.storage_temppath)
    env = {
        b"DCM_USER": conf.system_user.encode('utf-8'),
        b"DCM_BASEDIR": conf.storage_base_dir.encode('utf-8'),
        b"DCM_TMP_DIR": conf.storage_temppath.encode('utf-8'),
        b"DCM_LOG_FILE": log_file.encode('utf-8'),
        b"DCM_PYTHON": sys.executable.encode('utf-8')
    }
    if conf.platform_name:
        env[b"DCM_AGENT_PLATFORM_NAME"] = conf.platform_name
    if conf.platform_version:
        env[b"DCM_AGENT_PLATFORM_VERSION"] = conf.platform_version
    if 'PATH' in os.environ:
        env[b'PATH'] = os.environ['PATH']
    if in_env is not None:
        env.update(in_env)
    if conf.jr is not None:
        rc = conf.jr.run_command(cmd_line, cwd=cwd, env=env)
    else:
        _g_logger.warn("Running without the job runner process")
        process = subprocess.Popen(cmd_line,
                                   shell=False,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE,
                                   cwd=cwd,
                                   env=env)
        stdout, stderr = process.communicate()
        if stdout is not None:
            stdout = stdout.decode()
        else:
            stdout = ""
        if stderr is not None:
            stderr = stderr.decode()
        else:
            stderr = ""

        rc = (stdout, stderr, process.returncode)
    if log_file:
        # read everything logged and send it to the logger
        with open(log_file, "r") as fptr:
            for line in fptr.readlines():
                if line.strip():
                    dcm_logger.log_to_dcm_console_job_details(
                        job_name=str(cmd_line), details=line)
        os.remove(log_file)
    return rc
Ejemplo n.º 7
0
def run_command(conf, cmd_line, cwd=None, in_env=None):
    _, log_file = tempfile.mkstemp(dir=conf.storage_temppath)
    env = {b"DCM_USER": conf.system_user.encode('utf-8'),
           b"DCM_BASEDIR": conf.storage_base_dir.encode('utf-8'),
           b"DCM_TMP_DIR": conf.storage_temppath.encode('utf-8'),
           b"DCM_LOG_FILE": log_file.encode('utf-8'),
           b"DCM_PYTHON": sys.executable.encode('utf-8')}
    if conf.platform_name:
        env[b"DCM_AGENT_PLATFORM_NAME"] = conf.platform_name
    if conf.platform_version:
        env[b"DCM_AGENT_PLATFORM_VERSION"] = conf.platform_version
    if 'PATH' in os.environ:
        env[b'PATH'] = os.environ['PATH']
    if in_env is not None:
        env.update(in_env)
    if conf.jr is not None:
        rc = conf.jr.run_command(cmd_line, cwd=cwd, env=env)
    else:
        _g_logger.warn("Running without the job runner process")
        process = subprocess.Popen(cmd_line,
                                   shell=False,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE,
                                   cwd=cwd,
                                   env=env)
        stdout, stderr = process.communicate()
        if stdout is not None:
            stdout = stdout.decode()
        else:
            stdout = ""
        if stderr is not None:
            stderr = stderr.decode()
        else:
            stderr = ""

        rc = (stdout, stderr, process.returncode)
    if log_file:
        # read everything logged and send it to the logger
        with open(log_file, "r") as fptr:
            for line in fptr.readlines():
                if line.strip():
                    dcm_logger.log_to_dcm_console_job_details(
                        job_name=str(cmd_line), details=line)
        os.remove(log_file)
    return rc
Ejemplo n.º 8
0
def log_to_dcm_console_job_details(job_name=None, details=None):
    """Log a line back to DCM.  Lines logged in this way will show up in the
    DCM console.  This should be used sparingly.

    :param job_name: The name of the command logging this message.
    :param details: The log message string.
    """
    return dcm_logger.log_to_dcm_console_job_details(
        job_name=job_name, details=details)
Ejemplo n.º 9
0
def log_to_dcm_console_job_details(job_name=None, details=None):
    """Log a line back to DCM.  Lines logged in this way will show up in the
    DCM console.  This should be used sparingly.

    :param job_name: The name of the command logging this message.
    :param details: The log message string.
    """
    return dcm_logger.log_to_dcm_console_job_details(job_name=job_name,
                                                     details=details)
Ejemplo n.º 10
0
    def run(self):
        out = self.docker_conn.pull(
            self.args.repository, tag=self.args.tag, stream=True)

        # only log the last line at info level
        id_map = {}
        for line in out:
            _g_logger.debug(line)
            line = line.decode()
            j_obj = json.loads(line)
            if 'id' in j_obj:
                id_map[j_obj['id']] = line
            elif 'error' in j_obj:
                _g_logger.error(
                    "Error pulling the image " + line)
                raise docker_utils.DCMDockerPullException(
                    repo=self.args.repository,
                    tag=self.args.tag,
                    error_msg=j_obj['error'])
        for k in id_map:
            dcm_logger.log_to_dcm_console_job_details(
                job_name=self.name, details="pulled " + id_map[k])
        return plugin_base.PluginReply(
            0, reply_type="docker_pull", reply_object=None)