Merge lp:~ilowe/vmbuilder/appliances-seedfile into lp:~aheck/vmbuilder/appliances

Proposed by Iain Lowe
Status: Merged
Approved by: Andreas Heck
Approved revision: 17
Merged at revision: not available
Proposed branch: lp:~ilowe/vmbuilder/appliances-seedfile
Merge into: lp:~aheck/vmbuilder/appliances
Diff against target: None lines
To merge this branch: bzr merge lp:~ilowe/vmbuilder/appliances-seedfile
Reviewer Review Type Date Requested Status
Andreas Heck Approve
Review via email: mp+11643@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Iain Lowe (ilowe) wrote :

Sorry I put three changes in the same commit. I just threw them in there.

Revision history for this message
Andreas Heck (aheck) wrote :

Thanks, great patch :)

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'vainstall'
2--- vainstall 2009-08-31 21:20:39 +0000
3+++ vainstall 2009-09-12 04:13:32 +0000
4@@ -40,9 +40,16 @@
5 else:
6 return False
7
8-def get_seed_url(appliance):
9+def run_as_root(appliance):
10+ return os.getuid() == 0
11+
12+
13+def get_seed_url(appliance, seedfile=None):
14 """Get the URL of the seed file for a given appliance"""
15
16+ if seedfile:
17+ return 'file://%s' % os.path.abspath(seedfile)
18+
19 # use apt-get to get the URL of our appliance package
20 cmd = 'apt-get -qq --print-uris install ' + appliance + '|grep ' + appliance
21 cmd += "|cut -d ' ' -f 1|tr -d \"'\n\""
22@@ -53,54 +60,71 @@
23 # replace the '.deb' extension with '.seed' extension to get the url of the
24 # seedfile for the corresponding package version
25 url = re.sub('\.deb$', '.seed', url)
26-
27+
28 return url
29
30-if len(sys.argv) != 2:
31- usage()
32-
33-if sys.argv[1] == '--help':
34- usage(False)
35-
36-appliance = sys.argv[1]
37-
38-if not package_exists(appliance):
39- sys.stderr.write("Couldn't find a package with the name '" + appliance + "'\n")
40- sys.exit(1)
41-
42-if not is_appliance(appliance):
43- sys.stderr.write("Package with the name '" + appliance + "' exits but is no appliance\n")
44- sys.exit(1)
45-
46-if os.getuid() != 0:
47- sys.stderr.write('This program must be run as root!\n')
48- sys.exit(1)
49-
50-seedurl = get_seed_url(appliance)
51-sys.stdout.write('Getting the seed file for this appliance from: ' + seedurl + '\n')
52-
53-seeddata = ''
54-
55-try:
56- f = urllib2.urlopen(seedurl)
57- blocksize = 4096
58-
59- curblock = f.read(blocksize)
60- while curblock:
61- seeddata += curblock
62+def get_seeddata(seedurl):
63+ sys.stdout.write('Getting the seed file for this appliance from: ' + seedurl + '\n')
64+ seeddata = ''
65+
66+ try:
67+ f = urllib2.urlopen(seedurl)
68+ blocksize = 4096
69+
70 curblock = f.read(blocksize)
71- f.close()
72-except urllib2.HTTPError as e:
73- if e.getcode() == 404:
74- sys.stderr.write("\nError: Seedfile for the appliance '" + appliance + "' couldn't be found\n")
75- sys.stderr.write("It was expected at the URL " + seedurl + '\n')
76- else:
77+ while curblock:
78+ seeddata += curblock
79+ curblock = f.read(blocksize)
80+ f.close()
81+
82+ return seeddata
83+ except urllib2.HTTPError as e:
84+ if e.getcode() == 404:
85+ sys.stderr.write("\nError: Seedfile for the appliance '" + appliance + "' couldn't be found\n")
86+ sys.stderr.write("It was expected at the URL " + seedurl + '\n')
87+ else:
88+ sys.stderr.write(str(e) + '\n')
89+
90+ sys.exit(1)
91+ except Exception as e:
92 sys.stderr.write(str(e) + '\n')
93-
94- sys.exit(1)
95-except Exception as e:
96- sys.stderr.write(str(e) + '\n')
97- sys.exit(1)
98-
99-os.system('echo "' + seeddata + '" | debconf-set-selections')
100-os.system('apt-get install ' + appliance)
101+ sys.exit(1)
102+
103+def preseed_and_install(appliance, seedurl):
104+ p = subprocess.Popen(['debconf-set-selections'], stdin=subprocess.PIPE)
105+ p.stdin.write(get_seeddata(seedurl))
106+ p.stdin.flush()
107+ p.stdin.close()
108+ p.wait()
109+
110+ subprocess.call(['apt-get', 'install', appliance])
111+
112+if __name__ == '__main__':
113+ from optparse import OptionParser
114+
115+ p = OptionParser('''%prog [options] <APPLIANCENAME>
116+
117+This program installs a single virtual appliance package''')
118+
119+ p.add_option('--preseed', dest='seedfile', default=None, help='Preseed override file for this virtual appliance')
120+
121+ options, args = p.parse_args()
122+
123+ if not args:
124+ p.print_help()
125+ sys.exit(1)
126+
127+ appliance = args[0]
128+
129+ checks = (
130+ (package_exists, "Couldn't find a package with the name '" + appliance + "'"),
131+ (is_appliance, "Package with the name '" + appliance + "' exits but is no appliance"),
132+ (run_as_root, 'This program must be run as root!')
133+ )
134+
135+ for check_f, msg in checks:
136+ if not check_f(appliance):
137+ sys.stderr.write(msg + '\n')
138+ sys.exit(1)
139+
140+ preseed_and_install(appliance, get_seed_url(appliance, seedfile=options.seedfile))

Subscribers

People subscribed via source and target branches

to all changes: