Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import abc
- import os
- import re
- import jinja2 as j2
- import six
- import sahara.exceptions as e
- from sahara.i18n import _
- import sahara.utils.xmlutils as xml
- @six.add_metaclass(abc.ABCMeta)
- class BaseConfigurationFile(object):
- def __init__(self, file_name):
- self.f_name = file_name
- self._config_dict = dict()
- self._local_path = None
- self._remote_path = None
- @property
- def remote_path(self):
- return self._remote_path
- @remote_path.setter
- def remote_path(self, path):
- self._remote_path = os.path.join(path, self.f_name)
- @abc.abstractmethod
- def render(self):
- pass
- @abc.abstractmethod
- def parse(self, content):
- pass
- def fetch(self, instance):
- with instance.remote() as r:
- content = r.read_file_from(self.remote_path, run_as_root=True)
- self.parse(content)
- def load_properties(self, config_dict):
- for k, v in six.iteritems(config_dict):
- self.add_property(k, v)
- def add_property(self, name, value):
- self._config_dict[name] = value
- def add_properties(self, properties):
- for prop in six.iteritems(properties):
- self.add_property(*prop)
- def _get_config_value(self, name):
- return self._config_dict.get(name, None)
- def __repr__(self):
- return '<Configuration file %s>' % self.f_name
- class HadoopXML(BaseConfigurationFile):
- def __init__(self, file_name):
- super(HadoopXML, self).__init__(file_name)
- def parse(self, content):
- configs = xml.parse_hadoop_xml_with_name_and_value(content)
- for cfg in configs:
- self.add_property(cfg["name"], cfg["value"])
- def render(self):
- return xml.create_hadoop_xml(self._config_dict)
- class RawFile(BaseConfigurationFile):
- def __init__(self, file_name):
- super(RawFile, self).__init__(file_name)
- def render(self):
- return self._config_dict.get('content', '')
- def parse(self, content):
- self._config_dict.update({'content': content})
- class PropertiesFile(BaseConfigurationFile):
- def __init__(self, file_name):
- super(PropertiesFile, self).__init__(file_name)
- def parse(self, content):
- for line in content.splitlines():
- prop = line.strip()
- if len(prop) == 0:
- continue
- if prop[0] in ['#', '!']:
- continue
- name, value = prop.split("=")
- self.add_property(name.strip(), value.strip())
- def render(self):
- lines = ['%s=%s' % (k, v) for k, v in six.iteritems(self._config_dict)]
- return "\n".join(lines)
- class TemplateFile(BaseConfigurationFile):
- def __init__(self, file_name):
- super(TemplateFile, self).__init__(file_name)
- self._template = None
- @staticmethod
- def _j2_render(template, arg_dict):
- if template:
- return template.render(arg_dict)
- else:
- raise e.InvalidDataException(_('Template object must be defined'))
- def render(self):
- return self._j2_render(self._template, self._config_dict)
- def parse(self, content):
- self._template = j2.Template(content)
- class EnvironmentConfig(BaseConfigurationFile):
- @six.add_metaclass(abc.ABCMeta)
- class Handler(object):
- def __init__(self, value):
- self.value = value
- self.key = None
- @classmethod
- def matches(cls, line):
- return cls.regex.match(line) is not None
- def __str__(self):
- return str(self.value)
- class EmptyLine(Handler):
- @classmethod
- def matches(cls, line):
- return len(line.strip()) == 0
- class Comment(Handler):
- regex = re.compile(b'([#\s].*)')
- class Property(Handler):
- regex = re.compile(b'(\w+)=(.*)')
- def __init__(self, value, key=None):
- super(EnvironmentConfig.Property, self).__init__(value)
- if not key:
- match = self.regex.match(value)
- key, value = match.groups()
- self.key = key
- self.value = value
- def __str__(self):
- return "%s=%s" % (self.key, self.value)
- class ExportProperty(Property):
- regex = re.compile(b'(?:export)(?:\s+)(\w+)=(.*)')
- def __init__(self, value, key=None):
- super(EnvironmentConfig.ExportProperty, self).__init__(value, key)
- def __str__(self):
- return "export %s=%s" % (self.key, self.value)
- _HANDLERS = [Comment, EmptyLine, Property, ExportProperty]
- def __init__(self, file_name):
- super(EnvironmentConfig, self).__init__(file_name)
- self._members = []
- def parse(self, content):
- lines = content.splitlines()
- from pprint import pprint
- pprint('File %s content' % self.f_name)
- pprint(content)
- for line in lines:
- line = self._escape(line)
- for handler in EnvironmentConfig._HANDLERS:
- if handler.matches(line):
- member = handler(line)
- self._members.append(member)
- if member.key:
- if self._config_dict.get(member.key, None):
- existing = self._config_dict[member.key]
- self._config_dict[member.key] = existing + " " + member.value
- else:
- self._config_dict.update({member.key: member.value})
- break
- @staticmethod
- def _escape(string):
- string = six.text_type(string.strip())
- string = string.replace("\"", "")
- try:
- string = string.encode("utf-8")
- except AttributeError:
- pass
- return string
- def render(self):
- for member in self._members:
- if member.key in self._config_dict:
- member.value = self._config_dict[member.key]
- del self._config_dict[member.key]
- return '\n'.join(str(m) for m in self._members)
- def add_property(self, name, value):
- if name not in self._config_dict:
- self._members.append(
- EnvironmentConfig.ExportProperty(value, key=name))
- super(EnvironmentConfig, self).add_property(name, value)
- conf = EnvironmentConfig("f")
- conf.remote_path = "/dgf/"
- print conf
- content = '''
- # Licensed to the Apache Software Foundation (ASF) under one or more\n
- # contributor license agreements. See the NOTICE file distributed with\n
- # this work for additional information regarding copyright ownership.\n
- # The ASF licenses this file to You under the Apache License, Version 2.0\n
- # (the "License"); you may not use this file except in compliance with\n
- # the License. You may obtain a copy of the License at\n
- #\n
- # http://www.apache.org/licenses/LICENSE-2.0\n
- #\n
- # Unless required by applicable law or agreed to in writing, software\n
- # distributed under the License is distributed on an "AS IS" BASIS,\n
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n
- # See the License for the specific language governing permissions and\n
- # limitations under the License.\n
- \n
- DRILL_MAX_DIRECT_MEMORY="8G"\n
- DRILL_HEAP="4G"\n
- \n
- export DRILL_JAVA_OPTS="-Xms$DRILL_HEAP -Xmx$DRILL_HEAP -XX:MaxDirectMemorySize=$DRILL_MAX_DIRECT_MEMORY -XX:MaxPermSize=512M -XX:ReservedCodeCacheSize=1G -Ddrill.exec.enable-epoll=true"\n
- \n
- # Class unloading is disabled by default in Java 7\n
- # http://hg.openjdk.java.net/jdk7u/jdk7u60/hotspot/file/tip/src/share/vm/runtime/globals.hpp#l1622\n
- export SERVER_GC_OPTS="-XX:+CMSClassUnloadingEnabled -XX:+UseG1GC "\n
- \n
- export DRILL_JAVA_OPTS="$DRILL_JAVA_OPTS -Djava.security.auth.login.config=/opt/mapr/conf/mapr.login.conf -Dzookeeper.sasl.client=false "\n
- \n
- export DRILL_LOG_DIR="/opt/mapr/drill/drill-1.1.0/logs"\n
- \n
- export MAPR_IMPERSONATION_ENABLED=true\n
- export MAPR_TICKETFILE_LOCATION=/opt/mapr/conf/mapruserticket\n
- '''
- conf.parse(content)
- conf.add_property('DRILL_HUY', "HUY")
- conf.add_property('DRILL_HUY', "HUY1")
- from pprint import pprint
- pprint(conf._config_dict)
- pprint(conf.render())
Advertisement
Add Comment
Please, Sign In to add comment