#!/usr/bin/env python

#curl -i -F name=test -F file=@/tmp/hw.lst http://localhost/cgi-bin/upload.py

import cgi
import cgitb
import pprint
import sys

import matcher

DIR = '/root/edeploy/config/'

cgitb.enable()

print "Content-Type: text/x-python"     # HTML is following
print                                   # blank line, end of headers

form = cgi.FieldStorage()

fileitem = form["file"]
l = eval(fileitem.file.read(-1))

names = ('hp', 'vm')

for name in names:
    specs = eval(open(DIR + name + '.specs', 'r').read(-1))
    var = {}
    if matcher.match_all(l, specs, var):
        break
else:
    sys.stderr.write('Unable to match requirements\n')
    sys.stderr.write('Specs: %s\n' % repr(specs))
    sys.stderr.write('Lines: %s\n' % repr(l))
    sys.exit(1)


cfg = open(DIR + name + '.configure').read(-1)

sys.stdout.write('''#!/usr/bin/env python

import sys
import commands

def run(cmd):
    print '+ ' + cmd
    status, output = commands.getstatusoutput(cmd)
    print output
    if status != 0:
        sys.exit(status)

def set_role(role, version, disk):
    open('/role', 'w').write("ROLE=%s\\nVERS=%s\\nDISK=%s\\n" % (role, version, disk))

var = ''')

pprint.pprint(var)

sys.stdout.write(cfg)

@@ -148,52 +148,159 @@
 sys%0A
-%0Aimport matcher%0A%0ADIR = '/root/edeploy/config
+import ConfigParser%0A%0Aimport matcher%0A%0Aconfig = ConfigParser.ConfigParser()%0Aconfig.read('/etc/edeploy.conf')%0A%0ADIR = config.get('SERVER', 'CONFIGDIR') + '
 /'%0A%0A
@@ -534,29 +534,91 @@
 ))%0A%0A
+state_file
 name
-s
  = 
-('hp', 'vm')%0A
+DIR + 'state'%0Anames = eval(open(state_filename).read(-1))%0A%0Aidx = 0
 %0Afor
@@ -622,16 +622,23 @@
 for name
+, times
  in name
@@ -640,16 +640,54 @@
  names:%0A
+    if times == '*' or times %3E 0:%0A    
     spec
@@ -738,16 +738,20 @@
 ad(-1))%0A
+    
     var 
@@ -759,16 +759,20 @@
  %7B%7D%0A    
+   
+ 
 if match
@@ -812,14 +812,31 @@
     
+    
 break%0A
+    idx += 1%0A
 else
@@ -1005,16 +1005,68 @@
 xit(1)%0A%0A
+if times != '*':%0A    names%5Bidx%5D = (name, times - 1)%0A
 %0Acfg = o
@@ -1503,8 +1503,64 @@
 te(cfg)%0A
+%0Apprint.pprint(names, stream=open(state_filename, 'w'))%0A

read config from /etc/edeploy.conf and store state in CONFIGDIR/state