summaryrefslogtreecommitdiff
path: root/tools/check.py
blob: efa2449460fd8abc0d9bd4f49cef09aca86ec0dc (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
import ConfigParser, os, getopt, sys, config

ini = ConfigParser.ConfigParser()

# Parse parameters
def get_opts(argv):
    try:
        opts, args = getopt.getopt(argv,"ha:c:d:l",["conf=","action=","device=","list-devices"])
    except getopt.GetoptError:
        usage()
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            return usage()
            sys.exit()
        elif opt in ("-l", "--list-devices"):
            print "You have configured these devices: " + list_devices()
            sys.exit()
        elif opt in ("-a", "--action"):
            config.action = arg
        elif opt in ("-c", "--conf"):
            config.configfile = arg
        elif opt in ("-d", "--device"):
            config.device = arg            

# Check parameters for errors
def check_opts():
    get_opts(sys.argv[1:])
    if not config.action:
        print "Error: No action has been specified. Use -a."
        return usage()
        sys.exit(2)
    if not config.action == "pull" or config.action == "push":
        print "Error: Action \"" + config.action + "\" is invalid."
        return usage()
        sys.exit(2)
    if not config.device:
        print "Error: No device has been specified. Use -d."
        return usage()
        sys.exit(2)
    if config.device:
        load_config(config.configfile)
        if not ini.has_section(config.device):
            print "Error: Device \"" + config.device + "\" not configured. Add it to\"" + config.configfile + "."
            sys.exit(2)
        
# Load provided config file or use default                
def load_config(configfile):
    if not configfile:
        config.configfile = "/etc/sync-rpi.conf"
    try:
        ini.read([config.configfile])
    except:
        sys.exit(2)

# Check if configuration file is sane
def check_config(configfile):
    get_opts(sys.argv[1:])
    load_config(config.configfile)
    error = 0
    #print config.configfile
    #print ini.items('config')
    #print ini.items('alcor-01')
    #print config.options_config
    #print config.options_device
    for option in config.options_config:
        if not ini.has_option('config', option):
            print "Error: Option \"" + option + "\" not set in [config]."
            error = 1
    for section in ini.sections():
        for option in config.options_device:
            if section != 'config' and not ini.has_option(section, option):
                print "Error: Option " + option + " not set in [" + section + "]."
                error = 1
    
    if error == 1:
        sys.exit(2)

# List all devices defined in config.configfile
def list_devices():
    load_config(config.configfile)
    return " ".join(ini.sections()[1:])

# Prin usage
def usage():
    print """Usage: sync-rpi-config -a ACTION -d DEVICE [ -c [FILE] ] [ -l ]
    Options
     -a, --action        ACTION can be either pull or push. You are pushing config from you workstation to device or pulling it back.
     -d, --device        DEVICE is set in configuration file. It is the machine you wish to push config to or pull from.
     -c, --conf          Path to configuration file.
     -l, --list-devices  List all devices defined in current configuration file."""