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
=== modified file 'vainstall'
--- vainstall 2009-08-31 21:20:39 +0000
+++ vainstall 2009-09-12 04:13:32 +0000
@@ -40,9 +40,16 @@
40 else:40 else:
41 return False41 return False
4242
43def get_seed_url(appliance):43def run_as_root(appliance):
44 return os.getuid() == 0
45
46
47def get_seed_url(appliance, seedfile=None):
44 """Get the URL of the seed file for a given appliance"""48 """Get the URL of the seed file for a given appliance"""
4549
50 if seedfile:
51 return 'file://%s' % os.path.abspath(seedfile)
52
46 # use apt-get to get the URL of our appliance package53 # use apt-get to get the URL of our appliance package
47 cmd = 'apt-get -qq --print-uris install ' + appliance + '|grep ' + appliance54 cmd = 'apt-get -qq --print-uris install ' + appliance + '|grep ' + appliance
48 cmd += "|cut -d ' ' -f 1|tr -d \"'\n\""55 cmd += "|cut -d ' ' -f 1|tr -d \"'\n\""
@@ -53,54 +60,71 @@
53 # replace the '.deb' extension with '.seed' extension to get the url of the60 # replace the '.deb' extension with '.seed' extension to get the url of the
54 # seedfile for the corresponding package version61 # seedfile for the corresponding package version
55 url = re.sub('\.deb$', '.seed', url)62 url = re.sub('\.deb$', '.seed', url)
5663
57 return url64 return url
5865
59if len(sys.argv) != 2:66def get_seeddata(seedurl):
60 usage()67 sys.stdout.write('Getting the seed file for this appliance from: ' + seedurl + '\n')
6168 seeddata = ''
62if sys.argv[1] == '--help':69
63 usage(False)70 try:
6471 f = urllib2.urlopen(seedurl)
65appliance = sys.argv[1]72 blocksize = 4096
6673
67if not package_exists(appliance):
68 sys.stderr.write("Couldn't find a package with the name '" + appliance + "'\n")
69 sys.exit(1)
70
71if not is_appliance(appliance):
72 sys.stderr.write("Package with the name '" + appliance + "' exits but is no appliance\n")
73 sys.exit(1)
74
75if os.getuid() != 0:
76 sys.stderr.write('This program must be run as root!\n')
77 sys.exit(1)
78
79seedurl = get_seed_url(appliance)
80sys.stdout.write('Getting the seed file for this appliance from: ' + seedurl + '\n')
81
82seeddata = ''
83
84try:
85 f = urllib2.urlopen(seedurl)
86 blocksize = 4096
87
88 curblock = f.read(blocksize)
89 while curblock:
90 seeddata += curblock
91 curblock = f.read(blocksize)74 curblock = f.read(blocksize)
92 f.close()75 while curblock:
93except urllib2.HTTPError as e:76 seeddata += curblock
94 if e.getcode() == 404:77 curblock = f.read(blocksize)
95 sys.stderr.write("\nError: Seedfile for the appliance '" + appliance + "' couldn't be found\n")78 f.close()
96 sys.stderr.write("It was expected at the URL " + seedurl + '\n')79
97 else:80 return seeddata
81 except urllib2.HTTPError as e:
82 if e.getcode() == 404:
83 sys.stderr.write("\nError: Seedfile for the appliance '" + appliance + "' couldn't be found\n")
84 sys.stderr.write("It was expected at the URL " + seedurl + '\n')
85 else:
86 sys.stderr.write(str(e) + '\n')
87
88 sys.exit(1)
89 except Exception as e:
98 sys.stderr.write(str(e) + '\n')90 sys.stderr.write(str(e) + '\n')
9991 sys.exit(1)
100 sys.exit(1)92
101except Exception as e:93def preseed_and_install(appliance, seedurl):
102 sys.stderr.write(str(e) + '\n')94 p = subprocess.Popen(['debconf-set-selections'], stdin=subprocess.PIPE)
103 sys.exit(1)95 p.stdin.write(get_seeddata(seedurl))
10496 p.stdin.flush()
105os.system('echo "' + seeddata + '" | debconf-set-selections')97 p.stdin.close()
106os.system('apt-get install ' + appliance)98 p.wait()
99
100 subprocess.call(['apt-get', 'install', appliance])
101
102if __name__ == '__main__':
103 from optparse import OptionParser
104
105 p = OptionParser('''%prog [options] <APPLIANCENAME>
106
107This program installs a single virtual appliance package''')
108
109 p.add_option('--preseed', dest='seedfile', default=None, help='Preseed override file for this virtual appliance')
110
111 options, args = p.parse_args()
112
113 if not args:
114 p.print_help()
115 sys.exit(1)
116
117 appliance = args[0]
118
119 checks = (
120 (package_exists, "Couldn't find a package with the name '" + appliance + "'"),
121 (is_appliance, "Package with the name '" + appliance + "' exits but is no appliance"),
122 (run_as_root, 'This program must be run as root!')
123 )
124
125 for check_f, msg in checks:
126 if not check_f(appliance):
127 sys.stderr.write(msg + '\n')
128 sys.exit(1)
129
130 preseed_and_install(appliance, get_seed_url(appliance, seedfile=options.seedfile))

Subscribers

People subscribed via source and target branches

to all changes: