-
Notifications
You must be signed in to change notification settings - Fork 268
Expand file tree
/
Copy pathparameterparser.py
More file actions
149 lines (139 loc) · 8.45 KB
/
parameterparser.py
File metadata and controls
149 lines (139 loc) · 8.45 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
#!/usr/bin/env python
#
#CustomScript extension
#
# Copyright 2014 Microsoft Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from pickle import NONE
from common import CommonVariables
import base64
import json
import sys
class ParameterParser(object):
port = None
def __init__(self, protected_settings, public_settings, backup_logger):
"""
TODO: we should validate the parameter first
"""
self.blobs = []
self.backup_metadata = None
self.public_config_obj = None
self.private_config_obj = None
self.blobs = None
self.customSettings = None
self.snapshotTaskToken = ''
self.includedDisks = None
self.dynamicConfigsFromCRP = None
self.wellKnownSettingFlags = {CommonVariables.isSnapshotTtlEnabled: False, CommonVariables.useMccfToFetchDsasForAllDisks: False, CommonVariables.useMccfForLad: False}
settingKeysMapping= {}
settingKeysMapping[CommonVariables.isSnapshotTtlEnabled.lower()] = CommonVariables.isSnapshotTtlEnabled
settingKeysMapping[CommonVariables.useMccfToFetchDsasForAllDisks.lower()] = CommonVariables.useMccfToFetchDsasForAllDisks
settingKeysMapping[CommonVariables.useMccfForLad.lower()] = CommonVariables.useMccfForLad
self.includeLunList = [] #To be shared with HP
"""
get the public configuration
"""
self.commandToExecute = public_settings.get(CommonVariables.command_to_execute)
self.taskId = public_settings.get(CommonVariables.task_id)
self.locale = public_settings.get(CommonVariables.locale)
self.logsBlobUri = public_settings.get(CommonVariables.logs_blob_uri)
self.statusBlobUri = public_settings.get(CommonVariables.status_blob_uri)
self.commandStartTimeUTCTicks = public_settings.get(CommonVariables.commandStartTimeUTCTicks)
self.vmType = public_settings.get(CommonVariables.vmType)
if(CommonVariables.customSettings in public_settings.keys() and public_settings.get(CommonVariables.customSettings) is not None and public_settings.get(CommonVariables.customSettings) != ""):
backup_logger.log("Reading customSettings from public_settings", True)
self.customSettings = public_settings.get(CommonVariables.customSettings)
elif(CommonVariables.customSettings in protected_settings.keys()):
backup_logger.log("Reading customSettings from protected_settings", True)
self.customSettings = protected_settings.get(CommonVariables.customSettings)
self.publicObjectStr = public_settings.get(CommonVariables.object_str)
if(self.publicObjectStr is not None and self.publicObjectStr != ""):
if sys.version_info > (3,):
decoded_public_obj_string = base64.b64decode(self.publicObjectStr)
decoded_public_obj_string = decoded_public_obj_string.decode('ascii')
else:
decoded_public_obj_string = base64.standard_b64decode(self.publicObjectStr)
decoded_public_obj_string = decoded_public_obj_string.strip()
decoded_public_obj_string = decoded_public_obj_string.strip('\'')
self.public_config_obj = json.loads(decoded_public_obj_string)
self.backup_metadata = self.public_config_obj['backupMetadata']
if(self.logsBlobUri is None or self.logsBlobUri == ""):
self.logsBlobUri = protected_settings.get(CommonVariables.logs_blob_uri)
if(self.statusBlobUri is None or self.statusBlobUri == ""):
self.statusBlobUri = protected_settings.get(CommonVariables.status_blob_uri)
if(CommonVariables.snapshotTaskToken in self.public_config_obj.keys()):
self.snapshotTaskToken = self.public_config_obj[CommonVariables.snapshotTaskToken]
elif(CommonVariables.snapshotTaskToken in protected_settings.keys()):
self.snapshotTaskToken = protected_settings.get(CommonVariables.snapshotTaskToken)
if(CommonVariables.includedDisks in self.public_config_obj.keys()):
self.includedDisks = self.public_config_obj[CommonVariables.includedDisks]
if("dynamicConfigsFromCRP" in self.public_config_obj):
self.dynamicConfigsFromCRP = self.public_config_obj['dynamicConfigsFromCRP']
"""
first get the protected configuration
"""
self.privateObjectStr = protected_settings.get(CommonVariables.object_str)
if(self.privateObjectStr is not None and self.privateObjectStr != ""):
if sys.version_info > (3,):
decoded_private_obj_string = base64.b64decode(self.privateObjectStr)
decoded_private_obj_string = decoded_private_obj_string.decode('ascii')
else:
decoded_private_obj_string = base64.standard_b64decode(self.privateObjectStr)
decoded_private_obj_string = decoded_private_obj_string.strip()
decoded_private_obj_string = decoded_private_obj_string.strip('\'')
self.private_config_obj = json.loads(decoded_private_obj_string)
self.blobs = self.private_config_obj['blobSASUri']
try:
if(self.includedDisks != None):
if(CommonVariables.dataDiskLunList in self.includedDisks.keys() and self.includedDisks[CommonVariables.dataDiskLunList] != None):
self.includeLunList = self.includedDisks[CommonVariables.dataDiskLunList]
if(CommonVariables.isOSDiskIncluded in self.includedDisks.keys() and self.includedDisks[CommonVariables.isOSDiskIncluded] == True):
self.includeLunList.append(-1)
backup_logger.log("LUN list - " + str(self.includeLunList), True)
except Exception as e:
errorMsg = "Exception occurred while populating includeLunList, Exception: %s" % (str(e))
backup_logger.log(errorMsg, True)
if(self.dynamicConfigsFromCRP != None):
try:
backup_logger.log("settings received " + str(self.dynamicConfigsFromCRP), True)
for config in self.dynamicConfigsFromCRP:
if CommonVariables.key in config and CommonVariables.value in config:
config_key = config[CommonVariables.key].lower()
if(config_key in settingKeysMapping):
self.wellKnownSettingFlags[settingKeysMapping[config_key]] = config[CommonVariables.value]
else:
backup_logger.log("The received " + str(config[CommonVariables.key]) + " is not an expected setting name.", True)
else:
backup_logger.log("The received dynamicConfigsFromCRP is not in expected format.", True)
except Exception as e:
errorMsg = "Exception occurred while populating settings, Exception: %s" % (str(e))
backup_logger.log(errorMsg, True)
backup_logger.log("settings to be sent " + str(self.wellKnownSettingFlags), True)
createdByKey = "createdby"
createdByValue = "CRP"
if(self.backup_metadata != None):
try:
backup_logger.log("metadata received " + str(self.backup_metadata), True)
for data in self.backup_metadata:
if CommonVariables.key in data and CommonVariables.value in data:
data_key = data[CommonVariables.key].lower()
if(data_key == createdByKey):
data_value = data[CommonVariables.value].upper()
if(data_value == createdByValue):
backup_logger.log("it is a managed vm and request is being routed to Xstore's native path")
ParameterParser.port = 8443
break
except Exception as e:
errorMsg = "Exception occurred while populating metadata, Exception: %s" % (str(e))
backup_logger.log(errorMsg, True)