-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrebuild-machine-index
executable file
·127 lines (113 loc) · 4.44 KB
/
rebuild-machine-index
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python
"""
[jaredm@Serenity: machine-index]$ VBoxManage list vms
"MS-DOS" {03790e0b-a022-44c1-9bc9-6db0addf0f10}
"Win7" {2760d195-288f-40bd-bfc2-0eeff833377c}
"fkdev_testmaster_1471813882758_65725" {cc677375-af7d-4b46-9a03-e137061c5d12}
"archer_testmaster_1480109416529_58845" {effbec90-d47e-4c6c-95e4-4eb5de34a557}
"archer_gateway_1480126697743_11927" {8a8fd4c9-6b29-499b-a825-bfc321531cff}
"fkdev_gateway_1486575656247_8433" {de02cbc7-cdf0-4b0d-9490-4a089e64464f}
"fk2015_gateway_1486589520962_35232" {1c614076-13da-4985-8362-d6769dd0fb39}
"fkdev_spaces_1495054151160_41237" {b624aa98-5bcf-456e-a0f2-b2dcf4999dc6}
"fkdev_elysium_1496248173634_46954" {c0413233-bf4b-4062-ab0b-a853488f6558}
"fkdev_irc_1496939783056_8942" {b212861e-c085-4f6e-b861-7658ee9694e9}
"fkdev_static_1497292589277_88367" {17fd261c-a2a2-4109-882d-3b6f10c09cd1}
"fkdev_paperless_1498660800739_81605" {42e54d51-f6c3-4033-8c9f-53ba1a448624}
"fkdev_natgeo_1499347935080_2660" {2b386ae4-4b61-4a1b-99ff-9df65bdf62d3}
[jaredm@Serenity: machine-index]$ pwd
/Users/jaredm/.vagrant.d/data/machine-index
[jaredm@Serenity: machine-index]$ cat index|python -m json.tool
{
"machines": {
"b7fcd97be8c849779ca64066b7867c57": {
"extra_data": {
"box": {
"name": "fk-debian-jessie-20150217131542",
"provider": "virtualbox",
"version": "0"
}
},
"local_data_path": "/Users/jaredm/vms/fkdev/.vagrant",
"name": "natgeo",
"provider": "virtualbox",
"state": "poweroff",
"updated_at": null,
"vagrantfile_name": null,
"vagrantfile_path": "/Users/jaredm/vms/fkdev"
}
},
"version": 1
}
"""
import hashlib
import json
import os
import shutil
import sys
from vbox import VirtualBox
index_path = os.path.join(os.environ.get('HOME'), '.vagrant.d','data','machine-index')
base_image = 'fk-debian-jessie-20150217131542'
shutil.copy(os.path.join(index_path, 'index'), os.path.join(index_path, 'index.bak'))
vms = {}
vbox = VirtualBox()
machines = vbox.machines(with_meta=True)
for vm in machines.keys():
pieces = vm.split('_')
try:
if machines[vm].shared_folders[0]['guest_path'] != 'vagrant':
continue
vagrantfile_path = machines[vm].shared_folders[0]['host_path']
data = {
'local_data_path': os.path.join(vagrantfile_path, '.vagrant'),
'name': pieces[1],
'provider': 'virtualbox',
'state': machines[vm].state,
'updated_at': None,
'vagrantfile_name': None,
'vagrantfile_path': vagrantfile_path,
'extra_data': {
'box': {
'name': base_image,
'provider': 'virtualbox',
'version': '0'
}
}
}
vms.update({vm: data})
except KeyError:
# Means that there is no shared folder, so, it's probably not a vagrant box
pass
index = {
'machines': {},
'version': 1
}
for vm in vms.keys():
meta_path = os.path.join(vms[vm]['local_data_path'], 'machines', vms[vm]['name'], 'virtualbox')
# Hash is built as: #{@env.root_path}#{vf}#{@env.local_data_path}#{@name}
# This is where vagrant stores the prebuilt hash for the VM, if it exists,
# use it to avoid potential problems
vm_uuid_file = os.path.join(meta_path, 'index_uuid')
if os.path.isfile(vm_uuid_file):
with open(vm_uuid_file, 'r') as uuid_file:
index_uuid = uuid_file.read()
print('{}: {}'.format(vm, index_uuid))
index['machines'].update({index_uuid: vms[vm]})
continue
box_meta_file = os.path.join(meta_path, 'box_meta')
if os.path.isfile(box_meta_file):
with open(box_meta_file, 'r') as box_meta:
vms[vm]['extra_data'] = json.load(box_meta)
# Original hash file wasn't found, build one.
try:
md5hash = hashlib.md5()
md5hash.update('{}{}{}{}'.format(
vms[vm]['vagrantfile_path'],
os.path.join(vms[vm]['vagrantfile_path'], 'Vagrantfile'),
vms[vm]['local_data_path'],
vms[vm]['name']))
print('{}: {}'.format(vm, md5hash.hexdigest()))
index['machines'].update({md5hash.hexdigest(): vms[vm]})
except KeyError:
pass
with open(os.path.join(index_path, 'index'), 'w') as index_file:
index_file.write(json.dumps(index))