17
|
1 #!/usr/bin/python
|
|
2 # -*- encoding: utf-8 -*-
|
|
3 import shutil
|
|
4 import sys,os
|
|
5 from optparse import OptionParser
|
|
6
|
|
7 FILE = "metadata.json"
|
|
8 PROVIDER_VB = "virtualbox"
|
|
9 PROVIDER_KVM = "kvm"
|
|
10 BASE_DIR = "/root/.vagrant.d/boxes/"
|
|
11
|
|
12 # read json file
|
|
13 # return list of file contents
|
|
14
|
|
15 def change_kvm_metadata():
|
|
16 box_base = os.path.join(BASE_DIR, name)
|
|
17 dir_name = os.path.join(box_base, PROVIDER_KVM)
|
|
18 return os.path.join(dir_name, FILE)
|
|
19
|
|
20
|
|
21 def read_metadata_json():
|
|
22 metadata = change_kvm_metadata()
|
|
23 f = open(metadata, 'r')
|
|
24 result = []
|
|
25 for l in f:
|
|
26 result.append(l)
|
|
27 f.close()
|
|
28 return result
|
|
29
|
|
30 # write json file
|
|
31
|
|
32 def write_metadata_json(data):
|
|
33 metadata = change_kvm_metadata()
|
|
34 f = open(metadata, 'w')
|
|
35 for l in data:
|
|
36 f.write(l)
|
|
37 f.close()
|
|
38
|
|
39 def replace_data(data):
|
|
40 data1 = []
|
|
41 for l in data:
|
|
42 if 'provider' in l:
|
|
43 data1.append(l.replace(PROVIDER_VB, PROVIDER_KVM))
|
|
44 else:
|
|
45 data1.append(l)
|
|
46 return data1
|
|
47
|
|
48 def fix_provider_name(name):
|
|
49 box_base = os.path.join(BASE_DIR, name)
|
|
50 before_name = os.path.join(box_base, PROVIDER_VB)
|
|
51 if not os.path.exists(before_name):
|
|
52 print(before_name + "not found.")
|
|
53 return
|
|
54 after_name = os.path.join(box_base, PROVIDER_KVM)
|
|
55 shutil.move(before_name, after_name)
|
|
56
|
|
57 parser = OptionParser()
|
|
58 parser.add_option("-n", "--name", dest="name", help="VM name")
|
|
59
|
|
60 (options, args) = parser.parse_args()
|
|
61
|
|
62 name = options.name
|
|
63
|
|
64 fix_provider_name(name)
|
|
65
|
|
66 data = read_metadata_json()
|
|
67
|
|
68 data1 = replace_data(data)
|
|
69
|
|
70 write_metadata_json(data1)
|