annotate vagrant_newvm.py @ 24:e372e7648da0

remove test vms, and add managers vms
author taiki <taiki@cr.ie.u-ryukyu.ac.jp>
date Mon, 21 Jul 2014 09:05:59 -1000
parents fb9f3738a8e6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
1 #!/usr/bin/python
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
2 # -*- encoding: utf-8 -*-
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
3 import shutil
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
4 import sys,os
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
5 from optparse import OptionParser
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
6
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
7 FILE = "metadata.json"
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
8 PROVIDER_VB = "virtualbox"
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
9 PROVIDER_KVM = "kvm"
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
10 BASE_DIR = "/root/.vagrant.d/boxes/"
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
11
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
12 # read json file
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
13 # return list of file contents
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
14
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
15 def change_kvm_metadata():
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
16 box_base = os.path.join(BASE_DIR, name)
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
17 dir_name = os.path.join(box_base, PROVIDER_KVM)
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
18 return os.path.join(dir_name, FILE)
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
19
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
20
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
21 def read_metadata_json():
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
22 metadata = change_kvm_metadata()
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
23 f = open(metadata, 'r')
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
24 result = []
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
25 for l in f:
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
26 result.append(l)
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
27 f.close()
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
28 return result
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
29
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
30 # write json file
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
31
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
32 def write_metadata_json(data):
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
33 metadata = change_kvm_metadata()
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
34 f = open(metadata, 'w')
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
35 for l in data:
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
36 f.write(l)
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
37 f.close()
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
38
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
39 def replace_data(data):
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
40 data1 = []
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
41 for l in data:
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
42 if 'provider' in l:
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
43 data1.append(l.replace(PROVIDER_VB, PROVIDER_KVM))
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
44 else:
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
45 data1.append(l)
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
46 return data1
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
47
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
48 def fix_provider_name(name):
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
49 box_base = os.path.join(BASE_DIR, name)
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
50 before_name = os.path.join(box_base, PROVIDER_VB)
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
51 if not os.path.exists(before_name):
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
52 print(before_name + "not found.")
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
53 return
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
54 after_name = os.path.join(box_base, PROVIDER_KVM)
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
55 shutil.move(before_name, after_name)
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
56
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
57 parser = OptionParser()
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
58 parser.add_option("-n", "--name", dest="name", help="VM name")
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
59
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
60 (options, args) = parser.parse_args()
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
61
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
62 name = options.name
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
63
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
64 fix_provider_name(name)
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
65
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
66 data = read_metadata_json()
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
67
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
68 data1 = replace_data(data)
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
69
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents:
diff changeset
70 write_metadata_json(data1)