-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
214 lines (166 loc) · 6.13 KB
/
main.py
File metadata and controls
214 lines (166 loc) · 6.13 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import web
import subprocess
import time
import config
urls = (
'/menu', 'Menu',
'/create/(.*)/(.*)', 'Create',
'/delete_menu', 'DeleteMenu',
'/snapshot_menu', 'SnapshotMenu',
'/hook_menu', 'HookMenu',
'/delete/(.*)', 'Delete',
'/boot/(.*)/(.*)', 'Boot',
'/hook/(.*)', 'Hook',
'/clone_boot/(.*)/(.*)', 'CloneBoot',
'/snapshot/(.*)', 'Snapshot'
)
t_globals = dict(
datestr=web.datestr,
)
render = web.template.render('templates/', globals=t_globals)
app = web.application(urls, globals())
class Dataset:
parent_name = ""
name = ""
boot = ""
snapshot = 0
origin = ""
def __init__(self):
pass
def sanboot_dataset_list(boot_type):
result = subprocess.check_output(["zfs", "get", "-t volume", "com.qnap:sanboot", ])
dataset_list = dict()
for line in result.splitlines()[1:]:
item = line.split()
ds = Dataset()
name = item[0]
ds.boot = item[2]
try:
ds.parent_name = name[:name.rindex("/")]
ds.name = name[name.rindex("/") + 1:]
if ds.boot == boot_type:
dataset_list[ds.name] = ds
except:
pass
result = subprocess.check_output(["zfs", "get", "-t snapshot", "com.qnap:sanboot", ])
for line in result.splitlines()[1:]:
item = line.split()
name = item[0]
dsname = name[:name.rindex("@")]
snapname = name[name.rindex("@") + 1:]
if snapname == 'init' and item[2] == boot_type:
if dsname[dsname.rindex("/") + 1:] in dataset_list:
dataset_list[dsname[dsname.rindex("/") + 1:]].snapshot = 1
return dataset_list
def sanboot_children_list(volname):
result = subprocess.check_output(["zfs", "get", "-t volume", "origin", ])
dataset_list = dict()
for line in result.splitlines()[1:]:
item = line.split()
name = item[0]
origin = item[2]
try:
if origin == volname+"@init":
ds = Dataset()
ds.parent_name = name[:name.rindex("/")]
ds.name = name[name.rindex("/") + 1:]
ds.origin = origin
dataset_list[ds.name] = ds
except:
pass
return dataset_list
def reload_ctld():
ctld_org_conf = open("/etc/ctl.bsd.conf", "r")
ctld_conf = open("/etc/ctl.conf", "w")
org_conf = ctld_org_conf.read()
for k, ds in sanboot_dataset_list("on").items():
org_conf = org_conf + "target iqn.2014-04.com.qnap:{0} {{\n" \
" auth-group no-authentication\n" \
" portal-group pg0\n" \
" lun 0 {{\n" \
" path /dev/zvol/{1}{0}\n" \
" }}\n" \
"}} ".format(ds.name, config.zfsroot) + "\n"
for k, ds in sanboot_dataset_list("clone").items():
org_conf = org_conf + "target iqn.2014-04.com.qnap:{0} {{\n" \
" auth-group no-authentication\n" \
" portal-group pg0\n" \
" lun 0 {{\n" \
" path /dev/zvol/{1}{0}\n" \
" }}\n" \
"}} ".format(ds.name, config.zfsroot) + "\n"
ctld_conf.write(org_conf)
ctld_conf.close()
ctld_org_conf.close()
subprocess.check_output(["/etc/rc.d/ctld", "reload", ])
def create_zvol(volname, volsize):
try:
subprocess.check_output(
["zfs", "create", "-V", volsize, "-s", "-o", "com.qnap:sanboot=on", config.zfsroot + volname, ])
reload_ctld()
except:
pass
def clone_zvol(volname, mac):
try:
subprocess.check_output(
["zfs", "clone", "-o", "com.qnap:sanboot=clone", config.zfsroot + volname + "@init",
config.zfsroot + volname + "-" + mac])
subprocess.check_output(
["zfs", "snapshot", config.zfsroot + volname + "-" + mac+"@init"])
except:
pass
def snapshot_zvol(volname):
try:
subprocess.check_output(["zfs", "snapshot", config.zfsroot + volname + "@" + "init", ])
except:
pass
def delete_zvol(volname):
try:
subprocess.check_output(["zfs", "set", "com.qnap:sanboot=off", config.zfsroot + volname, ])
for k,v in sanboot_children_list("zdisk0/fedora").items():
subprocess.check_output(["zfs", "set", "com.qnap:sanboot=off", config.zfsroot + k, ])
reload_ctld()
time.sleep(1) # XXX
subprocess.check_output(["zfs", "destroy", "-R", config.zfsroot + volname, ])
except:
pass
class Menu:
def GET(self):
reload_ctld()
return render.menu(sanboot_dataset_list("on"), config.sanboot_ip)
class CloneBoot:
def GET(self, volname, mac):
clone_zvol(volname, mac)
time.sleep(1) # XXX
reload_ctld()
time.sleep(1) # XXX
return render.clone_boot(volname + "-" + mac, config.sanboot_ip)
class Boot:
def GET(self, volname, mac):
dataset_list = sanboot_dataset_list("on")
if volname in dataset_list and dataset_list[volname].snapshot == 1:
return render.boot(volname, mac, config.sanboot_ip)
return render.boot(volname, "", config.sanboot_ip)
class Create:
def GET(self, volname, volsize):
create_zvol(volname, volsize)
return "#!ipxe\nchain http://{0}:8080/menu".format(config.sanboot_ip)
class Delete:
def GET(self, volname):
delete_zvol(volname)
return "#!ipxe\nchain http://{0}:8080/menu".format(config.sanboot_ip)
class DeleteMenu:
def GET(self):
return render.delete(sanboot_dataset_list("on"), config.sanboot_ip)
class Snapshot:
def GET(self, volname):
snapshot_zvol(volname)
return "#!ipxe\nchain http://{0}:8080/menu".format(config.sanboot_ip)
class HookMenu:
def GET(self):
return render.hooklist(sanboot_dataset_list("on"), config.sanboot_ip)
class SnapshotMenu:
def GET(self):
return render.snapshot(sanboot_dataset_list("on"), config.sanboot_ip)
if __name__ == "__main__":
app.run()