cf23223fdb3bf7b98af56d0736dfbfe1419b6c55
[infrastructure.git] / scripts / alpha-repo.py
1 #!/usr/bin/python
2
3 # expected argument is a URL on build.onelab.eu
4 # this would then update the alpha node repo from that place
5 # NOTE
6 # the actual list of rpms that are needed or nodes is hard-coded
7
8 import sys, os, os.path, glob, commands
9 from optparse import OptionParser
10
11 standard_repo = '/var/www/html/install-rpms/onelab-i386'
12 alpha_repo = '%s-alpha'%standard_repo
13
14 #################### from module-tools
15 def prompt (question,default=True):
16     
17     choices = []
18     if default is True: choices.append('[y]')
19     else : choices.append('y')
20     if default is False: choices.append('[n]')
21     else : choices.append('n')
22
23     try:
24         answer=raw_input(question + " " + "/".join(choices) + " ? ")
25         if not answer:
26             return default
27         answer=answer[0].lower()
28         if answer == 'y':
29             return True
30         elif answer == 'n':
31             return False
32         return prompt(question,default)
33     except:
34         raise
35
36 def run (command,options,force=False):
37     if ( force or not options.dry_run):
38         print 'Running',command
39         os.system(command)
40     else:
41         print 'Would run',command
42
43 # rewrite the http part into a rsync part
44 def http_to_rsync (http):
45     result = http.replace ('http://build.onelab.eu/','root@build.onelab.eu:/build/')
46     # canonicalize : strip last / if present
47     while (result[-1]=='/'): result=result[0:-1]
48     return result
49
50 def rsync_xfer(http,repo,options):
51     xfer='%s/xfer'%repo
52     command='mkdir -p %s' % xfer
53     run(command,options)
54
55     command='rsync -av'
56     if options.dry_run: command += ' -n'
57     command += ' ' + http_to_rsync(http) +  ' ' + xfer
58     run (command,options)
59
60 def rpm_name (rpm_file):
61     status,output = commands.getstatusoutput("rpm -q --qf '%%{NAME}' -p %s " % rpm_file)
62     return output
63
64 def extract_contents (standard):
65     patt= '%s/*.rpm' % standard 
66     rpm_files=glob.glob(patt)
67     contents = [ rpm_name(rpm_file) for rpm_file in rpm_files]
68     return contents
69
70 def install_xfer (repo,rpm_name,options):
71     xfer_patt= '%s/xfer/RPMS/*/%s*.rpm' % (repo, rpm_name);
72     xfer_rpm_names=glob.glob(xfer_patt)
73     for xfer_rpm_name in xfer_rpm_names:
74         command = 'mv %s %s' % ( xfer_rpm_name, repo)
75         if prompt('Adopt %s'%os.path.basename(xfer_rpm_name)):
76             run(command,options)
77
78 def remaining_rpms (root):
79     command="find %s -name '*.rpm'"%root
80     status,output = commands.getstatusoutput(command)
81     return output.split("\n")
82
83 def scan_remaining (repo,options):
84     for rpm in remaining_rpms('%s/xfer' % repo):
85         basename=os.path.basename(rpm)
86         command="mv %s %s/%s"%(rpm,repo,basename)
87         if prompt('Adopt %s'%basename,False):
88             run (command,options)
89
90 def main ():
91     usage="Usage:%prog http://build.onelab.eu/.../RPMS"
92     parser=OptionParser(usage=usage)
93     parser.add_option("-n","--dry-run",action="store_true",dest="dry_run",default=False,
94                       help="dry run") 
95     parser.add_option("-r","--repo",action="store",dest="repo",default=alpha_repo,
96                       help="the repo to populate")
97     parser.add_option("-s","--std",action="store",dest="standard",default=standard_repo,
98                       help="the standard repo, used to compute the set of rpms")
99     (options, args) = parser.parse_args()
100     if len(args) != 1:
101         parser.print_help()
102         sys.exit(1)
103
104     http=args[0]
105     repo=options.repo
106     standard=options.standard
107
108     xfer='%s/xfer'%repo
109     if os.path.isdir(xfer):
110         if prompt('Want to clean up %s'%xfer,False):
111             run('rm -rf %s'%xfer,options)
112
113     if prompt ('Need to fetch rpms from %s ' % http):
114         rsync_xfer(http,repo,options)
115     
116     if prompt ('Get list of standard rpms from %s ' % standard):
117         rpm_names=extract_contents(standard)
118
119     if prompt ('Moving standard rpms to %s' % repo):
120         for rpm_name in rpm_names:
121             install_xfer(repo,rpm_name,options)
122
123     # once here, we have moved all the rpms that belonged to the standard noderepo
124     # let's manually scan the other ones as there might be new rpms needed
125     # the list proposed here contains mostly server-side rpms, 
126     # so the prompt default is False
127     if prompt ('Scan remaining rpms for double-check'):
128         scan_remaining(repo,options)
129
130     if prompt ('Nuke repodata (force complete reindexing)'):
131         command="rm -rf %s/repodata %s/signed-stamps"%(repo,repo)
132         run(command,options)
133
134     if prompt ('Reindex'):
135         run('service plc start packages',options)
136
137 if __name__ == '__main__':
138     main()
139
140     
141