cleanup
[infrastructure.git] / scripts / need-review / alpha-repo.py
diff --git a/scripts/need-review/alpha-repo.py b/scripts/need-review/alpha-repo.py
new file mode 100755 (executable)
index 0000000..fc3900e
--- /dev/null
@@ -0,0 +1,158 @@
+#!/usr/bin/python
+# $Id$
+# expected argument is a URL on build.onelab.eu
+# this would then update the alpha node repo from that place
+# NOTE
+# the actual list of rpms that are needed or nodes is hard-coded
+
+import sys, os, os.path, glob, commands
+from optparse import OptionParser
+
+standard_repo = '/var/www/html/install-rpms/onelab-i386'
+alpha_repo = '%s-alpha'%standard_repo
+
+#################### from module-tools
+def prompt (question,default=True):
+    
+    choices = []
+    if default is True: choices.append('[y]')
+    else : choices.append('y')
+    if default is False: choices.append('[n]')
+    else : choices.append('n')
+
+    try:
+        answer=raw_input(question + " " + "/".join(choices) + " ? ")
+        if not answer:
+            return default
+        answer=answer[0].lower()
+        if answer == 'y':
+            return True
+        elif answer == 'n':
+            return False
+        return prompt(question,default)
+    except:
+        raise
+
+##########
+def run (command,options,force=False):
+    if ( force or not options.dry_run):
+        print 'Running',command
+        os.system(command)
+    else:
+        print 'Would run',command
+
+# rewrite the http part into a rsync part
+def http_to_rsync (http):
+    result = http.replace ('http://build.onelab.eu/','root@build.onelab.eu:/build/')
+    # canonicalize : strip last / if present
+    while (result[-1]=='/'): result=result[0:-1]
+    return result
+
+def rsync_xfer(http,repo,options):
+    xfer='%s/xfer'%repo
+    command='mkdir -p %s' % xfer
+    run(command,options)
+
+    command='rsync -av'
+    if options.dry_run: command += ' -n'
+    command += ' ' + http_to_rsync(http) +  ' ' + xfer
+    run (command,options)
+
+def rpm_name (rpm_file):
+    status,output = commands.getstatusoutput("rpm -q --qf '%%{NAME}' -p %s " % rpm_file)
+    return output
+
+def extract_contents (standard):
+    patt= '%s/*.rpm' % standard 
+    rpm_files=glob.glob(patt)
+    contents = [ rpm_name(rpm_file) for rpm_file in rpm_files]
+    return contents
+
+def install_standard_rpm_name (repo,rpm_name,options):
+    former_alpha_rpms = set(glob.glob ('%s/%s*rpm'%(repo,rpm_name)))
+    xfer_patt= '%s/xfer/RPMS/*/%s*.rpm' % (repo, rpm_name);
+    xfer_rpm_names=glob.glob(xfer_patt)
+    for xfer_rpm_name in xfer_rpm_names:
+        command = 'mv %s %s' % ( xfer_rpm_name, repo)
+        if prompt('Adopt %s'%os.path.basename(xfer_rpm_name)):
+            run(command,options)
+            try:
+                former_alpha_rpms.remove('%s/%s'%(repo,rpm_name))
+            except:
+                pass
+    for former in former_alpha_rpms:
+        if prompt ('Remove old %s'%os.path.basename(former)):
+            command.run("rm %s"%former,options)
+
+def remaining_rpms (root):
+    command="find %s -name '*.rpm'"%root
+    status,output = commands.getstatusoutput(command)
+    return output.split("\n")
+
+def scan_remaining (repo,options):
+    for rpm in remaining_rpms('%s/xfer' % repo):
+        basename=os.path.basename(rpm)
+        command="mv %s %s/%s"%(rpm,repo,basename)
+        if prompt('Adopt %s'%basename,False):
+            run (command,options)
+
+def main ():
+    usage="Usage:%prog http://build.onelab.eu/.../RPMS"
+    parser=OptionParser(usage=usage)
+    parser.add_option("-n","--dry-run",action="store_true",dest="dry_run",default=False,
+                      help="dry run") 
+    parser.add_option("-r","--repo",action="store",dest="repo",default=alpha_repo,
+                      help="the repo to populate")
+    parser.add_option("-s","--std",action="store",dest="standard",default=standard_repo,
+                      help="the standard repo, used to compute the set of rpms")
+    (options, args) = parser.parse_args()
+    if len(args) != 1:
+        parser.print_help()
+        sys.exit(1)
+
+    http=args[0]
+    repo=options.repo
+    standard=options.standard
+
+    ### in case we have leftovers from a previous run days ago, better clean it up
+    xfer='%s/xfer'%repo
+    if os.path.isdir(xfer):
+        if prompt('Want to clean up %s'%xfer,False):
+            run('rm -rf %s'%xfer,options)
+
+    # go get the stuff
+    if prompt ('Need to fetch rpms from %s ' % http):
+        rsync_xfer(http,repo,options)
+    
+    # install yumgroups
+    if prompt ('Install yumgroups'):
+        command="mv %s/xfer/RPMS/yumgroups.xml %s/"%(repo,repo)
+        run(command,options)
+
+    # compute the set of rpms currently in the standard repo
+    # i.e. the ones that come with noderepo
+    if prompt ('Get list of standard rpms from %s ' % standard):
+        rpm_names=extract_contents(standard)
+
+    # interactively propose to move them in the right place
+    if prompt ('Moving standard rpms to %s' % repo):
+        for rpm_name in rpm_names:
+            install_standard_rpm_name(repo,rpm_name,options)
+
+    # once here, we have moved all the rpms that belonged to the standard noderepo
+    # let's manually scan the ones that remain in xfer/
+    # as there might be new rpms in this new build
+    # the list proposed here contains mostly server-side rpms, 
+    # so the prompt default is False
+    if prompt ('Scan remaining rpms for double-check'):
+        scan_remaining(repo,options)
+
+    if prompt ('Nuke repodata (force complete reindexing)'):
+        command="rm -rf %s/repodata %s/signed-stamps"%(repo,repo)
+        run(command,options)
+
+    if prompt ('Reindex'):
+        run('service plc start packages',options)
+
+if __name__ == '__main__':
+    main()