def instance_setup(instance_name): "When Bombardier is first getting set up, this method is called." progress_path = get_progress_path(instance_name) status_dict = None if os.path.isfile(progress_path): try: status_dict = json.loads(open(progress_path).read()) except: msg = "Unable to load existing json from %s" % progress_path Logger.warning(msg) if type(status_dict) != type({}): status_dict = {"install-progress": {}} status_dict["client_version"] = CLIENT_VERSION status_dict["core_version"] = CORE_VERSION status_dict["clientVersion"] = CLIENT_VERSION status_dict["coreVersion"] = CORE_VERSION pkg_dir = make_path(get_spkg_path(), instance_name, "packages") repos_dir = make_path(get_spkg_path(), "repos") tmp_dir = make_path(get_spkg_path(), "tmp") if not os.path.isdir(pkg_dir): os.makedirs(pkg_dir) if not os.path.isdir(repos_dir): os.makedirs(repos_dir) if not os.path.isdir(tmp_dir): os.makedirs(tmp_dir) open(progress_path, 'w').write(json.dumps(status_dict))
def make_symlinks(self, pkg_path, info_dict, full_name): ''' Create symlinks from the repository into where the package expects to run pkg_path -- directory where the versioned package will reside info_dict -- information regarding injectors and libraries full_name -- name of the package, including version info ''' base_path = make_path(get_spkg_path(), "repos") for component_type in info_dict: #Logger.info("Component: %s" % component_type) #Logger.info("info_dict: %s" % info_dict) component_dict = info_dict[component_type] for component_name in component_dict: full_path = component_dict[component_name]["path"] full_name = full_path.split('/')[-1] full_name = full_name.split('.tar.gz')[0] src = make_path(base_path, component_type, full_name) dst = make_path(pkg_path, component_type, component_name) if os.path.islink(dst): continue if not os.path.isdir(src): msg = "Package component (%s) does not exist." % src raise Exceptions.BadPackage(full_name, msg) if sys.platform == "cli": cmd = 'bash -c "cp -ax %s %s"' % (src, dst) else: cmd = 'bash -c "ln -s %s %s"' % (src, dst) #Logger.info("CMD: (%s)" % cmd) if os.system(cmd) != OK: msg = "Could not create symlink (%s)" % cmd raise Exceptions.BadPackage(full_name, msg)
def hunt_and_explode(self): ''' Used to find all type-5 package data in the repository and untar the files properly. ''' base_path = make_path(get_spkg_path(), "repos") output = "" if sys.platform == "cli": tfn = "c:/spkg/tmp/tarballs.txt" cmd = 'bash -c "find %s -name \"*.tar.gz\"" > %s' % (base_path, tfn) #Logger.info("CMD: (%s)" % cmd) os.system(cmd) output = open(tfn).read() else: _status, output = gso('find %s -name "*.tar.gz"' % base_path) start_dir = get_slash_cwd() for full_tar_file_name in output.split('\n'): tmp_list = full_tar_file_name.split('/') tar_file_name = tmp_list[-1] base_name = tar_file_name.split('.tar.gz')[0] tar_file_dir = make_path(tmp_list[:-1] + [base_name]) if not os.path.isdir(tar_file_dir): #Logger.info("Exploding %s..." % base_name) cmd = 'bash -c "mkdir -p %s"' % tar_file_dir #Logger.info("tar_file_dir: %s" % tar_file_dir) status = os.system(cmd) if status == OK: cmd = 'bash -c "cd %s && tar -mxzf ../%s"' cmd = cmd % (tar_file_dir, tar_file_name) #Logger.info("Cmd: %s" % cmd) if os.system(cmd) != OK: msg = "could not untar %s" % (tar_file_name) raise Exceptions.BadPackage(full_tar_file_name, msg) os.chdir(start_dir)
def _get_lib_path(self): ''' Need to modify our path and then clean it out again. This gets the data that both operations will need. ''' package_path = make_path(get_spkg_path(), self.instance_name, "packages", self.full_name) lib_path = make_path(package_path, "libs") return lib_path
def execute_maint_script(self, script_name): ''' execute a user-defined function script_name -- name of the function to run ''' self._download() # remove old history output_path = make_path(get_spkg_path(), self.instance_name, "output", "%s-output.yml" % script_name) if os.path.isfile(output_path): os.unlink(output_path) message = "Executing (%s) inside package (%s)" Logger.info(message % (script_name, self.full_name))
def _find_cmd(self, action, arguments=[], future_pkns=[], dry_run=False): ''' Perform the action on the system, importing modules from the package and running the appropriate method on the class within. action -- INSTALL, UNINSTALL, CONFIGURE, VERIFY future_pkns -- future package names. Some packages want to know about the packages that will come after them dry_run -- boolean flag to see if we're really going to do this ''' ret_val = None if type(action) == type(1): action = ACTION_REVERSE_LOOKUP[action] cwd = get_slash_cwd() obj, rand_string = self._get_object(future_pkns) try: if not hasattr(obj, action): msg = "Class %s does not have a %s method." raise BadPackage(self.name, msg % (self.class_name, action)) if not dry_run: if arguments: if ACTION_LOOKUP.get(action) == RESTORE: if len(arguments) != 1: Logger.error("Incorrect number of arguments passed to restore") return FAIL restore_path = make_path(get_spkg_path(), "archive", self.name, str(arguments[0])) if not os.path.isdir(restore_path): msg = "Cannot execute restore: archive data does not "\ "exist in %s" % (restore_path) Logger.error(msg) return FAIL self._prepare_restore(obj, restore_path) exec("ret_val = obj.%s('%s')" % (action, restore_path)) else: exec("ret_val = obj.%s()" % (action)) else: ret_val = OK self._cleanup(obj) del rand_string except SystemExit, err: if err.code: ret_val = err.code else: ret_val = OK del rand_string
def get_type_5(self, full_name, injectors_info, libs_info): ''' Get type-5 package components from the filesystem, and process them full_name -- name of the package, including version info injectors_info -- dictionary describing injector libraries libs_info -- dictionary describing python code libraries ''' self.hunt_and_explode() pkg_path = make_path(get_spkg_path(), self.instance_name, "packages", full_name) injector_path = make_path(pkg_path, "injectors") lib_path = make_path(pkg_path, "libs") if not os.path.isdir(pkg_path): Logger.info("Making directory %s" % pkg_path) for path in [pkg_path, injector_path, lib_path]: cmd = 'bash -c "mkdir -p %s"' % path if os.system(cmd) != OK: msg = "Could not create directory structure (%s)" % path raise Exceptions.BadPackage(full_name, msg) info_dict = {"injectors": injectors_info, "libs": libs_info} self.make_symlinks(pkg_path, info_dict, full_name) return OK
def data_request(self): "Obtain configuration data from the server" b64_data = [] while True: #Logger.info( "STARTING READ" ) chunk = sys.stdin.read(STREAM_BLOCK_SIZE).strip() #Logger.info( "READ FROM SERVER: [%s]" % chunk) if not chunk or chunk[0] == ' ' or chunk.endswith("-"): chunk = chunk[:-1] b64_data.append(chunk) break b64_data.append(chunk) sys.stdout.write(">\n") sys.stdout.flush() #Logger.info("FINISHED INPUT LOOP") #print "b64_data: ", b64_data json_data = '' #json_data = zlib.decompress(base64.decodestring(''.join(b64_data))) json_data = base64.decodestring(''.join(b64_data)) #print "json_dataa", json_data Logger.debug("Received %s lines of json" % len(json_data.split('\n'))) try: input_data = json.loads(json_data) #print "input_dataa", input_data except: ermsg = "Configuration data not YAML-parseable: %s" % (repr(json_data)) file_number, filename = tempfile.mkstemp(suffix=".yml") fh = open(filename, 'w') Logger.error("Writing bad data to %s" % filename) for line in json_data.split('\n'): fh.write("[%s]" % line) fh.flush() fh.close() raise ConfigurationException(ermsg) if type(input_data) == type("string"): ermsg = "Configuration data not YAML-parseable: %s" % (repr(json_data)) raise ConfigurationException(ermsg) if type(input_data) != type({}) and type(input_data) != type([]): input_data = input_data.next() config_key = input_data.get("config_key", None) if config_key: try: from bombardier_core.Cipher import Cipher except ImportError: msg = "This machine cannot accept an encrypted configuration" raise ConfigurationException(msg) enc_json_file = make_path(get_spkg_path(), self.instance_name, 'client.yml.enc') if not os.path.isfile(enc_json_file): msg = "%s file doesn't exist" % enc_json_file raise ConfigurationException(msg) enc_data = open(enc_json_file).read() cipher = Cipher(config_key) plain_json_str = cipher.decrypt_string(enc_data) try: input_data = json.loads(plain_json_str) except: ermsg = "Received bad YAML file: %s" % enc_json_file raise ConfigurationException(ermsg) config_data = input_data.get("config_data") if not config_data: raise ConfigurationException("No configuration data received") package_data = input_data.get("package_data", {}) self.config = Config(self.instance_name, config_data) self.repository = Repository(self.instance_name, package_data)