def run(self): _g_logger.info("Running configuration management of type " + self.args.configType) if self.args.configType.upper() == "CHEF": (stdout, stderr, rc) = self.configure_server_with_chef() elif self.args.configType.upper() == "PUPPET": (stdout, stderr, rc) = self.configure_server_with_puppet() else: raise plugin_exceptions.AgentPluginParameterBadValueException( "configType", "CHEF or PUPPET") if stderr: dcm_logger.log_to_dcm_console_configuration_management_error( stderr=stderr) if stdout: dcm_logger.log_to_dcm_console_configuration_management_output( stdout=stdout) if rc != 0: return plugin_base.PluginReply(rc, message=stderr) else: return plugin_base.PluginReply(rc, reply_type="string", reply_object=stdout)
def run(self): _g_logger.debug("Initialize run") # verify that the parameters in initialize match what came in on the # connection try: plugin_utils.log_to_dcm_console_job_details( job_name=self.name, details="Renaming the host to %s" % self.args.serverName) res_obj = self.rename.run() if res_obj.get_return_code() != 0: res_obj.set_message(res_obj.get_message() + " : rename failed") return res_obj # add customer user plugin_utils.log_to_dcm_console_job_details( job_name=self.name, details="Adding the user") res_obj = self.add_user.run() if res_obj.get_return_code() != 0: res_obj.set_message(res_obj.get_message() + " : addUser failed") return res_obj self.conf.state = "RUNNING" return plugin_base.PluginReply(0, reply_type="void") except Exception as ex: _g_logger.exception("initialize failed: " + str(ex)) return plugin_base.PluginReply(1, message=str(ex))
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)
def run(self): try: device_mapping_list = utils.get_device_mappings(self.conf) except exceptions.AgentExecutableException as ex: return plugin_base.PluginReply(1, message=str(ex)) return plugin_base.PluginReply(0, reply_type="device_mapping_array", reply_object=device_mapping_list)
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))
def run(self): job_id = self.arguments["jobId"] lr = self.items_map["long_runner"] job_description = lr.lookup_job(job_id) if job_description is None: msg = "no such job id %d" % job_id return plugin_base.PluginReply(1, message=msg, error_message=msg) return plugin_base.PluginReply( 0, reply_object=job_description.get_message_payload(), reply_type='job_description')
def incoming_request(self, reply_obj): payload = reply_obj.get_message_payload() _g_logger.debug("Incoming request %s" % str(payload)) request_id = reply_obj.get_request_id() _g_logger.info("Creating a request ID %s" % request_id) items_map = plugin_loader.parse_plugin_doc(self._conf, payload["command"]) dcm_logger.log_to_dcm_console_incoming_message( job_name=payload["command"]) immediate = "immediate" in items_map long_runner = "longer_runner" in items_map if "longer_runner" in payload: long_runner = bool(payload["longer_runner"]) # we ack first. This will write it to the persistent store before # sending the message so the agent will have it for restarts reply_obj.ack(None, None, None) if long_runner: try: dj = self._long_runner.start_new_job(self._conf, request_id, items_map, payload["command"], payload["arguments"]) except BaseException as ex: reply_obj = plugin_base.PluginReply( 1, error_message=urllib.parse.quote(str(ex).encode('utf-8'))) else: payload_doc = dj.get_message_payload() reply_obj = plugin_base.PluginReply( 0, reply_type="job_description", reply_object=payload_doc) reply_doc = reply_obj.get_reply_doc() wr = WorkReply(request_id, reply_doc) dcm_events.register_callback(self.work_complete_callback, args=[wr]) elif immediate: items_map["long_runner"] = self._long_runner reply_doc = _run_plugin(self._conf, items_map, request_id, payload["command"], payload["arguments"]) wr = WorkReply(request_id, reply_doc) dcm_events.register_callback(self.work_complete_callback, args=[wr]) else: workload = WorkLoad(request_id, payload, items_map) self.worker_q.put(workload) _g_logger.debug("The request %s has been set to send an ACK" % request_id)
def run_scrubber(self, opts): exe = os.path.join(os.path.dirname(sys.executable), "dcm-agent-scrubber") cmd = [self.conf.system_sudo, '-E', exe] if opts: cmd.extend(opts) (stdout, stderr, rc) = utils.run_command(self.conf, cmd) if rc != 0: return plugin_base.PluginReply(rc, message=stdout, error_message=stderr) return plugin_base.PluginReply( 0, message="The image was scrubbed successfully")
def run(self): _scheme_map = {'http': self._do_http_download, 'https': self._do_http_download, 'file': self._do_file} url_parts = urllib.parse.urlparse(self.args.url) if url_parts.scheme not in list(_scheme_map.keys()): # for now we are only accepting http. in the future we will # switch on scheme to decide what cloud storage protocol module # to use raise plugin_exceptions.AgentPluginParameterBadValueException( "url", url_parts.scheme, expected_values=str(list(_scheme_map.keys()))) func = _scheme_map[url_parts.scheme] try: exe_file, cleanup = func() except BaseException as ex: if type(ex) == plugin_exceptions.AgentPluginOperationException: raise return plugin_base.PluginReply( 1, error_message="Failed to download the URL %s: %s" % (self.args.url, str(ex))) try: os.chmod(exe_file, 0o755) command_list = [] if self.args.runUnderSudo: command_list.append(self.conf.system_sudo) if self.args.inpython: command_list.append(sys.executable) command_list.append(exe_file) if self.args.arguments: command_list.extend(self.args.arguments) _g_logger.debug("FetchRunScript is running the command %s" % str(command_list)) (stdout, stderr, rc) = plugin_utils.run_command( self.conf, command_list) _g_logger.debug("Command %s: stdout %s. stderr: %s" % (str(command_list), stdout, stderr)) return plugin_base.PluginReply( rc, message=stdout, error_message=stderr, reply_type="void") finally: if exe_file and cleanup and os.path.exists(exe_file): os.remove(exe_file)
def _exec(self): args = [self.exe] args.extend(self.arguments) self.logger.info("Forking the command " + str(args)) args = ' '.join(args) # for some reason i cannot just pass the array # at least should do a shell join process = subprocess.Popen(args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=self.cwd) # TODO iterate over the output so that it does not all come just at # the end stdout, stderr = process.communicate() if stdout is not None: stdout = stdout.decode() else: stdout = "" if stderr is not None: stderr = stderr.decode() else: stderr = "" self.logger.info("STDOUT: " + stdout) self.logger.info("STDERR: " + stderr) self.logger.info("Return code: " + str(process.returncode)) return plugin_base.PluginReply(process.returncode, message=stdout, error_message=stderr)
def run(self): if not _is_supported(self.conf): raise exceptions.AgentUnsupportedCloudFeature( "mount is not supported on the distro " + self.conf.cloud_type.lower() + " and cloud " + self.conf.platform_name.lower()) if self.args.mountPoint is None: self.args.mountPoint = self.conf.storage_mountpoint # if the mount point exists and is not empty we cannot use it if os.path.exists(self.args.mountPoint): if os.listdir(self.args.mountPoint): raise exceptions.AgentOptionException( "The path %s exists. For safety the agent only mounts " "volumes on paths that do not exist or are empty.") if self.args.fileSystem is None: self.args.fileSystem = self.conf.storage_default_file_system self.args.devices = self._normalize_device() self._install_deps(_g_platform_dep_installer) rc = self.mount_block_volume() return plugin_base.PluginReply(rc, reply_type="void")
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)
def _run_plugin(conf, items_map, request_id, command, arguments): try: plugin = plugin_loader.load_plugin(conf, items_map, request_id, command, arguments) dcm_logger.log_to_dcm_console_job_started(job_name=command, request_id=request_id) reply_obj = plugin.run() reply_doc = reply_obj.get_reply_doc() dcm_logger.log_to_dcm_console_job_succeeded(job_name=command, request_id=request_id) except Exception as ex: _g_logger.exception( "Worker %s thread had a top level error when " "running job %s : %s" % (threading.current_thread().getName(), request_id, str(ex))) dcm_logger.log_to_dcm_console_job_failed(job_name=command, request_id=request_id) reply_obj = plugin_base.PluginReply(1, error_message=urllib.parse.quote( str(ex).encode('utf-8'))) reply_doc = reply_obj.get_reply_doc() finally: _g_logger.info("Task done job " + request_id) return reply_doc
def run(self): command = [self.conf.get_script_location("removeUser"), self.args.userId] (stdout, stderr, rc) = plugin_utils.run_command(self.conf, command, with_sudo=True) if rc != 0: raise exceptions.AgentExecutableException( command, rc, stdout, stderr) return plugin_base.PluginReply(rc, message="job removeUser succeeded.")
def run(self): out = self.docker_conn.import_image(src=self.args.src, repository=self.args.repository, tag=self.args.tag, image=self.args.image) out = json.loads(out) return plugin_base.PluginReply(0, reply_type="docker_import_image", reply_object=out)
def run(self): kwargs = self.args.kwargs if kwargs is None: kwargs = {} systemstats.start_new_system_stat( self.args.statName, self.args.statType, self.args.holdCount, self.args.checkInterval, **kwargs) return plugin_base.PluginReply(0, reply_type="void")
def run(self): private_ips = self.conf.meta_data_object.get_ipv4_addresses() if not private_ips: return plugin_base.PluginReply( 1, error_message="No IP Address was found") self.ordered_param_list.append(private_ips[0]) plugin_utils.log_to_dcm_console_job_details( job_name=self.name, details="Renaming the server to %s with the local IP %s" % (self.args.serverName, private_ips[0])) return super(Rename, self).run()
def run(self): device_mappings = utils.get_device_mappings(self.conf) platform = self.conf.platform_name try: device_id = _check_if_device_encrypted(self.args.deviceId, device_mappings) (d_id, mount_point, encrypted) = _find_device_to_unmount(self.conf, device_id, platform, device_mappings) if "No such volume" in mount_point: return plugin_base.PluginReply(1, reply_type="void", error_message=mount_point) self.unmount(mount_point) if encrypted: _g_logger.info("Attempting to close encrypted device %s" % d_id) _close_encrypted_device(self.conf, d_id) return plugin_base.PluginReply(0, reply_type="void") except exceptions.AgentExecutableException as aex: return plugin_base.PluginReply(1, reply_type="void", error_message=str(aex))
def run(self): response = urllib.request.urlopen(self.args.url) data = response.read() script_file = self.conf.get_temp_file("upgradescript") opts_file = self.conf.get_temp_file("upgradeopts") try: with open(script_file, "wb") as f: f.write(data) os.chmod(script_file, 0o755) # write the configuration to a file. We may not be safe assuming # that the default configuration location is correct opts_list = config.build_options_list() opts_dict = {} for opt in opts_list: if opt.section not in opts_dict: opts_dict[opt.section] = {} opts_dict[opt.section][opt.name] = getattr( self.conf, opt.get_option_name()) with open(opts_file, "w") as f: for section_name in opts_dict: f.write("[" + section_name + "]" + os.linesep) section = opts_dict[section_name] for key in section: f.write("%s=%s" % (key, section[key])) f.write(os.linesep) command_list = [ script_file, self.args.newVersion, dcm.agent.g_version, opts_file ] command_list.extend(self.args.args) _g_logger.debug("Plugin running the command %s" % str(command_list)) (stdout, stderr, rc) = plugin_utils.run_command(self.conf, command_list) _g_logger.debug("Command %s: stdout %s. stderr: %s" % (str(command_list), stdout, stderr)) return plugin_base.PluginReply(rc, message=stdout, error_message=stderr, reply_type="void") finally: if os.path.exists(script_file): plugin_utils.secure_delete(self.conf, script_file) if os.path.exists(opts_file): plugin_utils.secure_delete(self.conf, opts_file)
def run(self): if self.args.page_token is None: out = self.docker_conn.images(name=self.args.name, quiet=self.args.quiet, all=self.args.all, viz=self.args.viz) token = str(uuid.uuid4()).replace("-", "") pager = pages.JsonPage(12 * 1024, out) self.conf.page_monitor.new_pager(pager, token) else: token = self.args.page_token page, token = self.conf.page_monitor.get_next_page(token) out = {'next_token': token, 'images': page} return plugin_base.PluginReply(0, reply_type="docker_image_array", reply_object=out)
def run(self): if self.args.page_token is None: out = self.docker_conn.logs(self.args.container, stdout=self.args.stdout, stderr=self.args.stderr, stream=False, timestamps=self.args.timestamps) token = str(uuid.uuid4()).replace("-", "") pager = pages.StringPage(12 * 1024, out) self.conf.page_monitor.new_pager(pager, token) else: token = self.args.page_token page, token = self.conf.page_monitor.get_next_page(token) out = {'next_token': token, 'log_data': page} return plugin_base.PluginReply(0, reply_type="docker_logs", reply_object=out)
def run(self): load = psutil.cpu_percent(0.1) # NOTE(buzztroll) blocks for 0.1s mem_usage = psutil.phymem_usage() reply_object = { "cpu_count": multiprocessing.cpu_count(), "cpu_load": load, "current_ram": mem_usage.used, "max_ram": mem_usage.total, "processes": len(psutil.get_pid_list()), "platform": platform.platform(), # NOTE(buzztroll) I am not sure what to do with server state. The # available options suggest error or contention. I think if we get # to this point in system those values should all be invalid. # meaning we should have rejected a new command long ago. "server_state": "OK" } return plugin_base.PluginReply(0, reply_type="agent_data", reply_object=reply_object)
def run(self): if self.args.page_token is None: out = self.docker_conn.containers(quiet=self.args.quiet, all=self.args.all, trunc=self.args.trunc, latest=self.args.latest, since=self.args.since, before=self.args.before, limit=self.args.limit) token = str(uuid.uuid4()).replace("-", "") pager = pages.JsonPage(12 * 1024, out) self.conf.page_monitor.new_pager(pager, token) else: token = self.args.page_token page, token = self.conf.page_monitor.get_next_page(token) out = {'next_token': token, 'containers': page} return plugin_base.PluginReply(0, reply_type="docker_container_array", reply_object=out)
def run(self): # make a list a tuple if self.args.port_bindings: for internal_port in self.args.port_bindings: binding_list = self.args.port_bindings[internal_port] new_binding_list = [] for bind in binding_list: host, port = bind new_binding_list.append(( host, port, )) self.args.port_bindings[internal_port] = new_binding_list self.docker_conn.start(self.args.container, port_bindings=self.args.port_bindings, lxc_conf=self.args.lxc_conf, links=self.args.links, privileged=self.args.privileged, publish_all_ports=self.args.publish_all_ports, cap_add=self.args.cap_add, cap_drop=self.args.cap_drop, network_mode="bridge") return plugin_base.PluginReply(0, reply_type="void")
def run(self): out = self.docker_conn.create_container( self.args.image, command=self.args.command, hostname=self.args.hostname, user=self.args.user, detach=True, stdin_open=False, tty=False, mem_limit=self.args.mem_limit, ports=self.args.ports, environment=self.args.environment, dns=None, volumes=None, volumes_from=None, network_disabled=False, name=self.args.name, entrypoint=None, cpu_shares=None, working_dir=None, memswap_limit=self.args.memswap_limit) return plugin_base.PluginReply(0, reply_type="docker_create_container", reply_object=out)
def run(self): systemstats.stop_stats(self.args.statName) return plugin_base.PluginReply(0, reply_type="void")
def run(self): tm = datetime.datetime.now() return plugin_base.PluginReply(0, reply_type="string", reply_object=str(tm))
def run(self): return plugin_base.PluginReply( 0, reply_type=systemstats.get_stats_type(self.args.statName), reply_object=systemstats.get_stats(self.args.statName))
def run(self): self.docker_conn.stop(self.args.container, timeout=self.args.timeout) return plugin_base.PluginReply(0, reply_type="void")
def run(self): return plugin_base.PluginReply(0, reply_type="string", reply_object=self.conf.state)