summaryrefslogtreecommitdiff
path: root/overseerlib/app.py
blob: dc3e811257e238e21b497801f79a6f1949cdcb53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/python
#
# Copyright (C) 2014  Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


import os
import smtplib
import getpass

import cliapp

import overseerlib


defaults = {
    'work-area': '/home/trove-overseer',
    'config-name': 'monitoring.conf',
}


class TroveOverseer(cliapp.Application):

    def add_settings(self):
        self.settings.string(['work-area'],
                             'path to the area for  the controller to work in',
                             metavar='PATH',
                             default=defaults['work-area'])
        self.settings.string(['config-name'], 'configuration file name. '
                             'Defaults to monitoring.conf',
                             metavar='FILENAME',
                             default=defaults['config-name'])
        
    def process_args(self, args):
        self.rungit(['remote', 'update', 'origin'])
        self.rungit(['reset', '--hard', 'origin/master'])
        self.rungit(['clean', '-fdx'])
        config = os.path.join(self.settings['work-area'], 'git',
                              self.settings['config-name']))
        overseerlib.parseyaml.ParseYAML.load_config(config)
        
        cliapp.Application.process_args(self, args)

    def human_to_bytes(self, threshold):
        symbols = ('B', 'K', 'M', 'G', 'T')
        s = threshold
        letter = s[-1:].strip().upper()
        num = s[:-1]
        if letter not in symbols:
            raise cliapp.AppException('Valid symbols: B, K, M, G, T')
        num = float(num)
        prefix = {symbols[0]:1}
        for i, s in enumerate(symbols[1:]):
            prefix[s] = 1 << (i+1)*10
        return int(num * prefix[letter])

    def rungit(self, args):
        self.runcmd(['git']+args, cwd=os.path.join(self.settings['work-area'],
                                                   'git'))

    def write_to_html(self, html_file):
        if html_file:
            try:
                with open(html_file + '.new', 'w') as ofh:
                    ofh.write('<!DOCTYPE html>\n')
                    ofh.write(self.gen_html())
                    ofh.write('\n')
                target = filename
                os.rename(filename + '.new', target)
            except:
                os.unlink(filename + '.new')
                raise

    def gen_footer(self):
        curtime = format_time(time.time())
        return self.tag('div', Class='footer', content=
                        'Generated by Trove Monitoring at ' + curtime)

    def tag(self, tagname, content=None, gap=False, **kwargs):
        tagval = ' '.join([tagname] +
                          ['%s=%r' % (k.lower(), v)
                           for k, v in kwargs.iteritems()])
        gap = '\n' if gap else ''
        if content is None:
            return '<%s />' % tagval
        else:
            return '<%s>%s%s%s</%s>' % (tagval, gap, content, gap, tagname)