-
Notifications
You must be signed in to change notification settings - Fork 8
/
ProxmoxEventHandler.py
215 lines (156 loc) · 7.14 KB
/
ProxmoxEventHandler.py
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
215
import sys
import argparse
import logging
import time
import os
import json
import datetime
class ProxmoxEventHandler:
args = None
jobinfo = {}
jobFilePath = ""
def __init__(self, jobFolder, jobId = None):
if (jobId == None):
self.jobinfo = {'ppid': str(os.getppid())}
else:
self.jobinfo = {"ppid": jobId}
self.jobFilePath = jobFolder + self.jobinfo['ppid'] + ".job"
logging.info("Job File:" + self.jobFilePath)
logging.debug("Raw Input parameters:" + ",".join(sys.argv))
if not os.path.exists(jobFolder):
logging.debug("Job folder does not already exist, creating it.")
os.makedirs(jobFolder)
parser = argparse.ArgumentParser()
parser.add_argument("phase", help="backup phase")
parser.add_argument("attributes", help="backup attributes mode and vmid", nargs='*')
self.args = parser.parse_args()
logging.info("Phase:" + self.args.phase)
self.jobinfo[self.args.phase] = []
'''
prior to this __init__ must have been called with the jobId parameter
'''
def getOldJob(self):
if os.path.isfile(self.jobFilePath) is False:
logging.error("Existing job file not found: " + self.jobFilePath)
sys.exit(1)
with open(self.jobFilePath) as json_file:
logging.info("Parsing existing job file:" + self.jobFilePath)
self.jobinfo = json.load(json_file)
'''
Gets script input paramaters and enviromental variables and adds to job file.
Returns the current phase as a string
'''
def getPhase(self):
if self.args.phase == 'job-start':
dumpdir = os.environ["DUMPDIR"]
storeid = os.environ["STOREID"]
self.jobinfo[self.args.phase].append(
{
'dumpdir': dumpdir,
'storeid': storeid
}
)
logging.debug("Environment Variables:" + str(self.jobinfo[self.args.phase]))
if os.path.isfile(self.jobFilePath):
logging.error("Existing job file found: " + self.jobFilePath)
sys.exit(1)
with open(self.jobFilePath, 'w') as outfile:
logging.info("Creating new job file:" + self.jobFilePath)
json.dump(self.jobinfo, outfile)
return self.args.phase
elif self.args.phase == 'job-end':
dumpdir = os.environ["DUMPDIR"]
storeid = os.environ["STOREID"]
if os.path.isfile(self.jobFilePath) is False:
logging.error("Existing job file not found: " + self.jobFilePath)
sys.exit(1)
with open(self.jobFilePath) as json_file:
logging.info("Parsing existing job file:" + self.jobFilePath)
self.jobinfo = json.load(json_file)
self.jobinfo[self.args.phase] = {
'dumpdir': dumpdir,
'storeid': storeid
}
logging.debug("Environment Variables:" + str(self.jobinfo[self.args.phase]))
with open(self.jobFilePath, 'w') as outfile:
logging.info("Updating existing job file:" + self.jobFilePath)
json.dump(self.jobinfo, outfile)
return self.args.phase
elif self.args.phase == 'job-abort':
dumpdir = os.environ["DUMPDIR"]
storeid = os.environ["STOREID"]
if os.path.isfile(self.jobFilePath) is False:
logging.warning("Existing job file not found: " + self.jobFilePath)
with open(self.jobFilePath) as json_file:
logging.info("Parsing existing job file:" + self.jobFilePath)
self.jobinfo = json.load(json_file)
self.jobinfo[self.args.phase] = []
self.jobinfo[self.args.phase].append(
{
'dumpdir': dumpdir,
'storeid': storeid
}
)
logging.debug("Environment Variables:" + str(self.jobinfo[self.args.phase]))
with open(self.jobFilePath, 'w') as outfile:
logging.info("Updating job file:" + self.jobFilePath)
json.dump(self.jobinfo, outfile)
return self.args.phase
elif (self.args.phase == 'backup-start' or
self.args.phase == 'backup-end' or
self.args.phase == 'backup-abort' or
self.args.phase == 'log-end' or
self.args.phase == 'pre-stop' or
self.args.phase == 'post-restart' or
self.args.phase == 'pre-restart'):
vmid = self.args.attributes.pop()
mode = self.args.attributes.pop()
vmtype = os.environ["VMTYPE"] # openvz/qemu
dumpdir = os.environ["DUMPDIR"]
storeid = os.environ["STOREID"]
hostname = os.environ["HOSTNAME"]
# TARGET is only available in phase 'backup-end'
targetfile = os.environ["TARGET"] #Contains the full path to the current VM backup file
# logfile is only available in phase 'log-end'
logfile = os.environ["LOGFILE"]
if os.path.isfile(self.jobFilePath) is False:
logging.error("Existing job file not found: " + self.jobFilePath)
sys.exit(1)
with open(self.jobFilePath) as json_file:
logging.info("Parsing existing job file:" + self.jobFilePath)
self.jobinfo = json.load(json_file)
if self.args.phase not in self.jobinfo:
self.jobinfo[self.args.phase] = {}
self.jobinfo[self.args.phase][vmid] = {
'vmid': vmid,
'vmtype': vmtype,
'dumpdir': dumpdir,
'storeid': storeid,
'hostname': hostname,
'targetfile': targetfile,
'logfile': logfile,
'mode': mode
}
logging.debug("Environment Variables:" + str(self.jobinfo[self.args.phase][vmid]))
with open(self.jobFilePath, 'w') as outfile:
logging.info("Updating existing job file:" + self.jobFilePath)
json.dump(self.jobinfo, outfile)
return self.args.phase
else:
raise Exception("got unknown phase '%s'" % phase)
'''
Returns a an array with dics with quoted targetfile and logfile elements:
[
{targetfile: "vzdump-qemu-107-2019_12_29-23_22_54.vma",logfile: "vzdump-qemu-107-2019_12_29-23_22_54.log"},
{targetfile: "vzdump-qemu-108-2019_12_29-23_22_54.vma",logfile: "vzdump-qemu-108-2019_12_29-23_22_54.log"},
]
'''
def getFilesFromPhase(self, phase):
logging.debug("Gathering files from phase: " + phase)
filePaths = []
for x in self.jobinfo[phase]:
targetfile = self.jobinfo[phase][x]["targetfile"].split("/")[-1]
logfile = self.jobinfo[phase][x]["logfile"].split("/")[-1]
filePaths.append({"targetfile" : targetfile, "logfile" : logfile})
logging.debug("Got " + str(len(filePaths) * 2) + " files")
return filePaths