def init_logger(): FORMAT = "[%(filename)s:%(lineno)s - %(funcName)s ] %(message)s" log_dir = settings.LOG_DIR make_directory(log_dir) logging.basicConfig(filename=os.path.join(log_dir, 'omc.log'), format=FORMAT, level=logging.INFO)
def sync(self, k8s_resource, kube_config_dir): the_kube_config_dir = os.path.join(kube_config_dir, k8s_resource) if os.path.exists(the_kube_config_dir): shutil.rmtree(the_kube_config_dir) file_utils.make_directory(the_kube_config_dir) # download config file console.log("downloading config file to %s" % the_kube_config_dir) config_download_cmd = 'scp %s:/root/.kube/config %s' % ( k8s_resource, the_kube_config_dir) self.run_cmd(config_download_cmd) ssl_dir = self._get_cert_dir( os.path.join(the_kube_config_dir, 'config')) # download ssl files console.log("downloading ssl certificate to %s" % the_kube_config_dir) certificate_download_cmd = 'scp -r %s:%s %s' % (k8s_resource, ssl_dir, the_kube_config_dir) self.run_cmd(certificate_download_cmd) kube_config_file = os.path.join(the_kube_config_dir, 'config') file_utils.inplace_replace(kube_config_file, ssl_dir, os.path.join(the_kube_config_dir, 'ssl'))
def _save(self): 'save configuration in file cache to be restored' resource_name = self._get_one_resource_value() namespace = self.client.get_namespace( self._get_kube_api_resource_type(), resource_name) kube_instance = self._get_one_resource_value("kube") if not kube_instance: kube_instance = 'local' cache_folder = os.path.join(settings.OMC_KUBE_CACHE_DIR, kube_instance, namespace, self._get_kube_resource_type()) result = self._read_namespaced_resource(resource_name, namespace, _preload_content=False) stream = StringIO() the_result = json.loads(result.data.decode('UTF-8')) ObjectUtils.delete_node(the_result, 'metadata.creationTimestamp') ObjectUtils.delete_node(the_result, 'metadata.resourceVersion') yaml = YAML() yaml.dump(the_result, stream) content = stream.getvalue() make_directory(cache_folder) with open(os.path.join(cache_folder, resource_name + '.yaml'), 'w') as f: f.write(content)
def init(self): # 1. set up omc-completion.sh completion_helper = 'omc-completion.sh' completion_scripts = pkg_resources.resource_filename( __name__, '../../lib/' + completion_helper) file_utils.make_directory(settings.BIN_DIR) file_utils.copy(completion_scripts, settings.BIN_DIR) # 2. change the file to executable the_completion_file_name = os.path.join(settings.BIN_DIR, completion_helper) mode = os.stat(the_completion_file_name).st_mode mode |= (mode & 0o444) >> 2 os.chmod(the_completion_file_name, mode) # 3. install omc plugins to omz omz_custom = os.path.join(os.environ.get("HOME"), ".oh-my-zsh/custom/plugins") omw_plugin_dir = os.path.join(omz_custom, 'omc') if os.path.exists(omz_custom): file_utils.make_directory(omw_plugin_dir) omz_completion_file_ = pkg_resources.resource_filename( __name__, '../../lib/_omc') file_utils.copy(omz_completion_file_, omw_plugin_dir)
def _restore(self): 'restore configuration saved in file cache' resource_name = self._get_one_resource_value() namespace = self.client.get_namespace( self._get_kube_api_resource_type(), resource_name) kube_instance = self._get_one_resource_value("kube") if not kube_instance: kube_instance = 'local' cache_folder = os.path.join(settings.OMC_KUBE_CACHE_DIR, kube_instance, namespace, self._get_kube_resource_type()) make_directory(cache_folder) config_file = os.path.join(cache_folder, resource_name + '.yaml') if os.path.exists(config_file): self.client.apply(config_file) else: raise Exception("no config file found")
def wrapper(*args, **kwargs): from datetime import datetime if callable(file): if _is_class_method(file): cache_file = file(args[0]) else: cache_file = file() else: cache_file = file cache_is_valid = False if not os.path.exists(cache_file): cache_is_valid = False else: if duration is None or duration == -1: cache_is_valid = True else: # duration and file all exists os.path.getctime(cache_file) the_duration = datetime.now().timestamp() - os.path.getctime(cache_file) if the_duration > duration: cache_is_valid = False else: cache_is_valid = True if cache_is_valid: with open(cache_file, 'r') as f: return f.read() else: # refresh cache if os.path.exists(cache_file): os.remove(cache_file) make_directory(os.path.dirname(cache_file)) result = func(*args, **kwargs) with open(cache_file, 'w') as f: f.write(result) duration_file_name = os.path.join(os.path.dirname(cache_file), 'duration') with open(duration_file_name, 'w') as f: f.write("-1" if duration is None else str(duration)) return result
def wrapper(*args, **kwargs): from datetime import datetime if callable(duration): if _is_class_method(duration): cache_duration = duration(args[0]) else: cache_duration = duration() else: cache_duration = duration if callable(file): if _is_class_method(file): cache_file = file(args[0]) else: cache_file = file() else: cache_file = file cache_is_valid = False if not os.path.exists(cache_file): cache_is_valid = False else: if cache_duration is None or cache_duration == -1: cache_is_valid = True else: # duration and file all exists os.path.getctime(cache_file) the_duration = datetime.now().timestamp() - os.path.getctime(cache_file) if the_duration > cache_duration: cache_is_valid = False else: cache_is_valid = True if cache_is_valid: with open(cache_file, 'r') as f: return CompletionContent(f.read()) else: # refresh cache if os.path.exists(cache_file): os.remove(cache_file) make_directory(os.path.dirname(cache_file)) try: result = func(*args, **kwargs) if not isinstance(result, CompletionContent): return CompletionContent('', valid=False) if not result.is_valid(): # don't cache return result with open(cache_file, 'w') as f: f.write(result.get_raw_content()) duration_file_name = os.path.join(os.path.dirname(cache_file), 'duration') with open(duration_file_name, 'w') as f: f.write("-1" if cache_duration is None else str(cache_duration)) return result except Exception as e: logger.error(e, exc_info=True) return CompletionContent('', valid=False)