remove creategid from sfi; store gid creating info in record and silently create...
[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 import tempfile
10 from geni.util.cert import Keypair, Certificate
11 from geni.util.credential import Credential
12 from geni.util.geniclient import GeniClient
13 from geni.util.record import GeniRecord
14 from geni.util.gid import GID
15 from geni.util.gid import create_uuid
16
17 gidhrn = None
18 gidkeyfile = None
19 infile = None
20 outfile = None
21 gidfile = None
22 email = None
23 ip = None
24 dns = None
25 hrn = None
26 type = None
27 dump = False
28 researcher = []
29
30 long_opts = ["infile=", "outfile=", "email=", "ip=", "dns=", "gidfile=", "gidhrn=", "gidkeyfile=", "hrn=", "type=", "addresearcher=", "delresearcher=", "dump"]
31
32 def showhelp():
33    print "syntax: editRecord.py <options>"
34    print "    --help                ... show help"
35    print "    --infile <name>       ... read record from file"
36    print "    --outfile <name>      ... write record to file"
37    print "    --dump                ... dump record to stdout"
38    print "    --gidfile <fn>        ... load gid from file"
39    print "    --gidhrn <name>       ... name to use when creating gid"
40    print "    --gidkeyfile <name>   ... key to use when creating gid"
41    print "    --hrn <name>          ... set hrn"
42    print "    --type <type>         ... set type (user|slice|sa|ma|...)"
43    print "    --email <addr>        ... user: set email address"
44    print "    --ip <addr>           ... node: set ip address"
45    print "    --dns <hostname>      ... node: set hostname"
46    print "    --addresearcher <hrn> ... slice: add researcher"
47    print "    --delresearcher <hrn> ... slice: delete researcher"
48
49 def load_publickey_string(fn):
50    f = file(fn,"r")
51    key_string = f.read()
52
53    # if the filename is a private key file, then extract the public key
54    if "PRIVATE KEY" in key_string:
55        outfn = tempfile.mktemp()
56        cmd = "openssl rsa -in " + fn + " -pubout -outform PEM -out " + outfn
57        os.system(cmd)
58        f = file(outfn, "r")
59        key_string = f.read()
60        os.remove(outfn)
61
62    return key_string
63
64 def process_options():
65    global infile, outfile
66    global email, ip, dns, gidfile, hrn, type
67    global researcher
68    global dump
69    global gidkeyfile, gidhrn
70
71    (options, args) = getopt.getopt(sys.argv[1:], '', long_opts)
72    for opt in options:
73        name = opt[0]
74        val = opt[1]
75
76        if name == "--help":
77            showhelp()
78            sys.exit(0)
79        elif name == "--infile":
80            infile = val
81        elif name == "--outfile":
82            outfile = val
83        elif name == "--email":
84            email = val
85        elif name == "--ip":
86            ip = val
87        elif name == "--dns":
88            dns = val
89        elif name == "--gidfile":
90            gidfile = val
91        elif name == "--gidhrn":
92            gidhrn = val
93        elif name == "--gidkeyfile":
94            gidkeyfile = val
95        elif name == "--hrn":
96            hrn = val
97        elif name == "--type":
98            type = val
99        elif name == "--dump":
100            dump = True
101        elif name == "--addresearcher":
102            researcher.append(val)
103        elif name == "--delresearcher":
104            researcher.append("-" + val)
105
106 def errorcheck(record):
107    geni_info = record.get_geni_info()
108
109    if not record.type:
110        print "Warning: no type specified"
111    if not record.type in ["user", "sa", "ma", "slice", "node"]:
112        print "Warning: unknown record type"
113    if not record.name:
114        print "Warning: unknown record name"
115    if (not record.gid) and (not ("create_gid" in geni_info)):
116        print "Warning: unknown record gid"
117
118    if record.type == "user":
119        if not geni_info.get("email",None):
120            print "Warning: unknown email in user record"
121
122    if record.type == "node":
123        if not geni_info.get("ip",None):
124            print "Warning: unknown ip in node record"
125        if not geni_info.get("dns",None):
126            print "Warning: unknown dns in node record"
127
128 # updates is a list of items to add or remove. If an item starts with "-", then
129 # it will be removed. Otherwise it will be added
130 def update_list(dict, listname, updates):
131    list = dict.get(listname, [])
132    for hrn in updates:
133        if hrn.startswith("-"):
134            real_hrn = hrn[1:]
135            if real_hrn in list:
136                list.delete(real_hrn)
137        else:
138            if not hrn in list:
139                list.append(hrn)
140
141    dict[listname] = list
142
143 def main():
144    process_options()
145
146    # if the user didn't tell us to do much of anything, then maybe he needs
147    # some help
148    if (not infile) and (not outfile) and (not dump):
149        showhelp()
150        return
151
152    if infile:
153        str = file(infile, "r").read()
154        record = GeniRecord(string = str)
155    else:
156        record = GeniRecord()
157
158    geni_info = record.get_geni_info()
159    geni_info_orig = geni_info.copy()
160
161    if email:
162        geni_info["email"] = email
163
164    if ip:
165        geni_info["ip"] = ip
166
167    if dns:
168        geni_info["dns"] = dns
169
170    if hrn:
171        record.name = hrn
172
173    if type:
174        record.type = type
175
176    if gidfile:
177        gid_str = file(gidfile, "r").read()
178        gid = GID(string=gid_str)
179        record.set_gid(gid)
180
181    if gidhrn or gidkeyfile:
182        if not gidhrn:
183            print "must use --gidkeyfile with --gidhrn"
184            sys.exit(-1)
185        if not gidkeyfile:
186            print "must use --gidhrn with --gidkeyfile"
187            sys.exit(-1)
188
189        geni_info = record.get_geni_info()
190        geni_info["create_gid"] = True
191        geni_info["create_gid_hrn"] = gidhrn
192        geni_info["create_gid_key"] = load_publickey_string(gidkeyfile)
193
194    if researcher:
195        update_list(geni_info, "researcher", researcher)
196
197    if (geni_info != geni_info_orig):
198        record.set_geni_info(geni_info)
199
200    errorcheck(record)
201
202    if dump:
203        record.dump(False)
204
205    if outfile:
206        str = record.save_to_string()
207        file(outfile, "w").write(str)
208        print "wrote record to", outfile
209
210 if __name__=="__main__":
211    main()