예제 #1
0
 def reset_default_values(self):
     """Resets default values for paths or other elements which are not empty strings"""
     OUTPUT.info(
         'Resetting path elements in the loaded configuration file.')
     for key, val in self.configdefaults.items():
         if val:
             self.set_value(key, val)
예제 #2
0
 def merge_default_values(self):
     """Merge in default values if they don't already exist or are set to none"""
     OUTPUT.info(
         'Updating existing configuration item that are not yet set to the default values.',
         suppress=True)
     for key, val in self.configdefaults.items():
         if self.get_value(key) is None:
             self.set_value(key, val)
예제 #3
0
 def get_archivepath(self):
     """Path to old job data files"""
     ourpath = self.values['archivejobpath']
     if self._get_relative_path(ourpath):
         return self._get_relative_path(ourpath)
     else:
         OUTPUT.warning(
             'Not able to resolve the archivejobpath in your configuration file - {0}'
             .format(ourpath))
         return os.path.abspath(ourpath)
예제 #4
0
 def get_ansibletemplatepath(self):
     """Path of local ansible template files"""
     ourpath = self.values['ansibletemplatepath']
     if self._get_relative_path(ourpath):
         return self._get_relative_path(ourpath)
     else:
         OUTPUT.warning(
             'Not able to resolve the ansibletemplatepath in your configuration file - {0}'
             .format(ourpath))
         return os.path.abspath(ourpath)
예제 #5
0
 def get_logpath(self):
     """Path to log files"""
     ourpath = self.values['logpath']
     if self._get_relative_path(ourpath):
         return self._get_relative_path(ourpath)
     else:
         OUTPUT.warning(
             'Not able to resolve the logpath in your configuration file - {0}'
             .format(ourpath))
         return os.path.abspath(ourpath)
예제 #6
0
 def get_workingjobpath(self):
     """Path of job data files this script produces"""
     ourpath = self.values['workingjobpath']
     if self._get_relative_path(ourpath):
         return self._get_relative_path(ourpath)
     else:
         OUTPUT.warning(
             'Not able to resolve the workingjobpath in your configuration file - {0}'
             .format(ourpath))
         return os.path.abspath(ourpath)
예제 #7
0
    def exe_proc(self, params, shell=False):
        """
        Execute a process
        """
        proc = subprocess.Popen(params,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE,
                                shell=shell)

        stdout_data, stderr_data = proc.communicate()
        if stdout_data != "":
            OUTPUT.procout(self.decode(stdout_data))

        if proc.returncode != 0:
            self._add_log(self.decode(stderr_data), logtype='error')
            sys.exit()
예제 #8
0
 def connect_session(self):
     """Connect with current profile or id/secret"""
     try:
         if self.profileid:
             OUTPUT.info('Using passed in profile - {0}'.format(
                 self.profileid))
             self.session = boto3.Session(profile_name=self.profileid,
                                          region_name=self.region)
         else:
             self._add_log('Connecting to AWS with key id - {0}'.format(
                 self.awsid))
             self.session = boto3.Session(aws_access_key_id=self.awsid,
                                          aws_secret_access_key=self.secret,
                                          region_name=self.region)
     except ClientError as e:
         self._add_log(e, 'error')
         raise e
예제 #9
0
    def send_email_template(self,
                            jinjatemplate,
                            jinjadata,
                            subject,
                            recipients,
                            ccrecipients=None,
                            attachments=None):
        try:
            # Render our jinja template
            html = self.render_jinja_template(jinjadata, jinjatemplate)
        except Exception as jinjaexception:
            OUTPUT.error('Unable to render jinja template!')
            raise jinjaexception

        return self.send_email(html=html,
                               subject=subject,
                               recipients=recipients,
                               ccrecipients=ccrecipients,
                               attachments=attachments)
예제 #10
0
    def __init__(self, *args, **kwargs):
        # Default global configuration settings for a project
        self.configdefaults = DEFAULTAPPCONFIG.copy()
        self.configfile = kwargs.pop('config_file', '')
        try:
            self.scriptpath = os.path.abspath(os.path.split(__file__)[0])
        except:
            self.scriptpath = ''

        if not self.configfile:
            self.configfile = os.path.join(self.scriptpath,
                                           ('config' + os.sep + 'config.yml'))

        # Initialize the data dictionary for the default settings
        self._defaults = {}
        for key, value in self.configdefaults.items():
            self._defaults[key] = value

        # If there is no default config file then lets try to create one
        if not os.path.isfile(self.configfile):
            OUTPUT.info(
                'Unable to find config file - {0}, initializing a new default config file instead.'
                .format(self.configfile),
                suppress=True)
            with open(self.configfile, "wb") as cfgfile:
                cfgfile.write(
                    yaml.safe_dump(self._defaults,
                                   encoding='utf-8',
                                   allow_unicode=True,
                                   default_flow_style=False))
            OUTPUT.info('File saved!')

        # pass through all args to the base class init along with our config file path
        kwargs['config_file'] = self.configfile
        super(Settings, self).__init__(*args, **kwargs)

        # Merge any default values that may be missing
        self.merge_default_values()
        self.emailhandler = self.get_emailhandler()
예제 #11
0
    def save_html_report(self, jinjatemplate, jinjadata, filename=None):
        """Save an html file instead of sending email"""
        try:
            # Render our jinja template
            html = self.render_jinja_template(jinjadata, jinjatemplate)
        except Exception as jinjaexception:
            OUTPUT.error('Unable to render jinja template!')
            raise jinjaexception

        if not filename:
            htmlfilepath = os.path.join(os.getcwd(),
                                        'aws-instance-report.html')
        else:
            htmlfilepath = filename

        try:
            outputfile = open(htmlfilepath, "w")
            outputfile.write(html)
            outputfile.close()
        except Exception as smtplibraryexception:
            OUTPUT.error('Unable to save {0}!'.format(htmlfilepath))
            raise smtplibraryexception
예제 #12
0
 def _add_log(self, mylog, logtype='info'):
     """Add a log generated from this module"""
     if logtype == 'error':
         OUTPUT.error('{0}: {1}'.format(str(self.__class__.__name__),
                                        str(mylog)))
     elif logtype == 'warning':
         OUTPUT.warning('{0}: {1}'.format(str(self.__class__.__name__),
                                          str(mylog)))
     else:
         OUTPUT.info('{0}: {1}'.format(str(self.__class__.__name__),
                                       str(mylog)))
예제 #13
0
    def reset_default_values(self):
        """Resets default values for paths or other elements which are not empty strings"""
        OUTPUT.info(
            'Resetting path elements in the loaded configuration file.')
        for key, val in self.configdefaults.items():
            if val:
                self.set_value(key, val)


# The primary way to interact with settings is to simply hit the
# already constructed CFG object.
try:
    #OUTPUT.info('Attempting to load global config from {0}'.format(CONFIGPATH), suppress=True)
    CFG = Settings(config_file=CONFIGPATH)
    OUTPUT.suppress_console_output(
        suppress=CFG.values.get('suppressconsoleoutput'))
except:
    raise Exception(
        'Error loading script configuration file: {0}'.format(CONFIGPATH))

# Supress output if the global config tells us to do so
OUTPUT.suppress_console_output(CFG.values.get('suppressconsoleoutput'))
""" 
try:
    # Create email notification handler
    EMAIL = EmailNotification(
        smtphost=CFG.values['emailsmtpserver'],
        fromuser=CFG.values['emailstatussender'],
        fromemail=CFG.values['emailstatussenderaddress'],
        port=(25 if not CFG.values['emailsmtpserverport'] else CFG.values['emailsmtpserverport']),
        templatedir=CFG.get_emailtemplatepath())
예제 #14
0
 def render_jinja_template(self, data, template):
     template = template + '.{0}'.format(self._template_extension)
     OUTPUT.info('Rendering template: {0}'.format(template))
     text = self._env.get_template(template)
     msg = text.render(data)
     return msg