added command-line record editer, a few SFI demo scripts
[sfa.git] / cmdline / editRecord.py
1 #! /usr/bin/env python
2 from __future__ import with_statement
3
4 # sfi -- slice-based facility interface
5
6 import sys
7 import os, os.path
8 import getopt
9 from util.cert import Keypair, Certificate
10 from util.credential import Credential
11 from util.geniclient import GeniClient
12 from util.record import GeniRecord
13 from util.gid import GID
14
15 infile = None
16 outfile = None
17 gidfile = None
18 email = None
19 ip = None
20 dns = None
21 hrn = None
22 type = None
23 dump = False
24 researcher = []
25
26 long_opts = ["infile=", "outfile=", "email=", "ip=", "dns=", "gidfile=", "hrn=", "type=", "addresearcher=", "delresearcher=", "dump"]
27
28 def showhelp():
29    print "syntax: editRecord.py <options>"
30    print "    --help                ... show help"
31    print "    --infile <name>       ... read record from file"
32    print "    --outfile <name>      ... write record to file"
33    print "    --dump                ... dump record to stdout"
34    print "    --gidfile <fn>        ... load gid from file"
35    print "    --hrn <name>          ... set hrn"
36    print "    --type <type>         ... set type (user|slice|sa|ma|...)"
37    print "    --email <addr>        ... user: set email address"
38    print "    --ip <addr>           ... node: set ip address"
39    print "    --dns <hostname>      ... node: set hostname"
40    print "    --addresearcher <hrn> ... slice: add researcher"
41    print "    --delresearcher <hrn> ... slice: delete researcher"
42
43 def process_options():
44    global infile, outfile
45    global email, ip, dns, gidfile, hrn, type
46    global researcher
47    global dump
48
49    (options, args) = getopt.getopt(sys.argv[1:], '', long_opts)
50    for opt in options:
51        name = opt[0]
52        val = opt[1]
53
54        if name == "--help":
55            showhelp()
56            sys.exit(0)
57        elif name == "--infile":
58            infile = val
59        elif name == "--outfile":
60            outfile = val
61        elif name == "--email":
62            email = val
63        elif name == "--ip":
64            ip = val
65        elif name == "--dns":
66            dns = val
67        elif name == "--gidfile":
68            gidfile = val
69        elif name == "--hrn":
70            hrn = val
71        elif name == "--type":
72            type = val
73        elif name == "--dump":
74            dump = True
75        elif name == "--addresearcher":
76            researcher.append(val)
77        elif name == "--delresearcher":
78            researcher.append("-" + val)
79
80 def errorcheck(record):
81    geni_info = record.get_geni_info()
82
83    if not record.type:
84        print "Warning: no type specified"
85    if not record.type in ["user", "sa", "ma", "slice", "node"]:
86        print "Warning: unknown record type"
87    if not record.name:
88        print "Warning: unknown record name"
89    if not record.gid:
90        print "Warning: unknown record gid"
91
92    if record.type == "user":
93        if not geni_info.get("email",None):
94            print "Warning: unknown email in user record"
95
96    if record.type == "node":
97        if not geni_info.get("ip",None):
98            print "Warning: unknown ip in node record"
99        if not geni_info.get("dns",None):
100            print "Warning: unknown dns in node record"
101
102 # updates is a list of items to add or remove. If an item starts with "-", then
103 # it will be removed. Otherwise it will be added
104 def update_list(dict, listname, updates):
105    list = dict.get(listname, [])
106    for hrn in updates:
107        if hrn.startswith("-"):
108            real_hrn = hrn[1:]
109            if real_hrn in list:
110                list.delete(real_hrn)
111        else:
112            if not hrn in list:
113                list.append(hrn)
114
115    dict[listname] = list
116
117 def main():
118    process_options()
119
120    # if the user didn't tell us to do much of anything, then maybe he needs
121    # some help
122    if (not infile) and (not outfile) and (not dump):
123        showhelp()
124        return
125
126    if infile:
127        str = file(infile, "r").read()
128        record = GeniRecord(string = str)
129    else:
130        record = GeniRecord()
131
132    geni_info = record.get_geni_info()
133    geni_info_orig = geni_info.copy()
134
135    if email:
136        geni_info["email"] = email
137
138    if ip:
139        geni_info["ip"] = ip
140
141    if dns:
142        geni_info["dns"] = dns
143
144    if hrn:
145        record.name = hrn
146
147    if type:
148        record.type = type
149
150    if gidfile:
151        gid_str = file(gidfile, "r").read()
152        gid = GID(string=gid_str)
153        record.set_gid(gid)
154
155    if researcher:
156        update_list(geni_info, "researcher", researcher)
157
158    if (geni_info != geni_info_orig):
159        record.set_geni_info(geni_info)
160
161    errorcheck(record)
162
163    if dump:
164        record.dump(False)
165
166    if outfile:
167        str = record.save_to_string()
168        file(outfile, "w").write(str)
169        print "wrote record to", outfile
170
171 if __name__=="__main__":
172    main()