-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeploy.py
More file actions
251 lines (168 loc) · 7.42 KB
/
Deploy.py
File metadata and controls
251 lines (168 loc) · 7.42 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#
# (c) Peralta Informatics 2007
# $Id: Deploy.py 133 2007-12-21 00:12:22Z andrei $
#
import clr
import imp
import getopt
import new
import os
import shutil
import string
import sys
import traceback
from os import path
clr.AddReference('System.Xml')
import System.Collections.Generic
import System.Text
import System.Xml
PI_DEPLOY_HOME=os.environ.get('PI_DEPLOY_HOME')
sys.path.append(os.path.join('PI_DEPLOY_HOME', 'Pi'))
from Pi.Deploy import DeployUtilities
from Pi.Deploy import DeployModule
from Pi.Deploy.DeployAction import Action
from Pi.Deploy.DeployConfiguration import Configuration
DeployNamespaceUri = 'http://schemas.peralta-informatics.com/Deploy/2007'
DepModulesElementName = 'Modules'
DepModuleElementName = 'Module'
DepNamespaceAttributeName = 'Namespace'
DepImportAttributeName = 'Import'
DepHandlerAttributeName = 'Handler'
DepNameAttributeName = 'Name'
def ReadModule(reader, modules):
namespace = None
handler = None
if reader.MoveToAttribute(DepNamespaceAttributeName):
namespace = reader.ReadContentAsString()
if reader.MoveToAttribute(DepHandlerAttributeName):
handler = reader.ReadContentAsString()
if handler is not None and namespace is not None:
if reader.MoveToAttribute(DepImportAttributeName):
moduleName = reader.ReadContentAsString()
components = moduleName.split('.')
name = components[len(components) - 1]
sep = moduleName.replace('.', '/').rfind('/')
moduleInfo = imp.find_module(name, [os.path.join(PI_DEPLOY_HOME, moduleName.replace('.', '/')[0:sep]), '.'])
module = imp.load_module(moduleName, moduleInfo[0], moduleInfo[1], moduleInfo[2])
deployModuleType = type('%sHandler' % (handler), (getattr(module, handler),), {})
if issubclass(deployModuleType, DeployModule.DeployModule):
modules[namespace] = deployModuleType()
else:
raise Exception('%s.%s does not implement DeployModule interface.' % (moduleName, handler))
else:
raise Exception('%s error: %s or %s were not specified' % (DepModuleElementName, DepNamespaceAttributeName, DepHandlerAttributeName))
def ReadModules(reader, modules):
depth = reader.Depth
while reader.Read() and reader.Depth > depth:
if reader.NodeType == System.Xml.XmlNodeType.Element:
if reader.LocalName == DepModuleElementName and reader.NamespaceURI == DeployNamespaceUri:
ReadModule(reader, modules)
def ReadConfiguration(configurationFile):
configuration = Configuration()
reader = System.Xml.XmlReader.Create(configurationFile)
depth = reader.Depth
while reader.Read():
if reader.NodeType == System.Xml.XmlNodeType.Element:
if reader.LocalName == DepModulesElementName and reader.NamespaceURI == DeployNamespaceUri:
ReadModules(reader, configuration.Modules)
if reader.NamespaceURI in configuration.Modules:
configuration.Modules[reader.NamespaceURI].ReadConfiguration(reader, configuration)
return configuration
def __PrintConfiguration(configuration):
if len(configuration.Modules) > 0:
print '>> Modules'
for namespace, handler in configuration.Modules.iteritems():
print ' Namespace: ' + namespace
print ' Handler: ' + str(handler)
for namespace, handler in configuration.Modules.iteritems():
handler.PrintConfiguration(configuration)
def __PrintHelp(configuration = None):
print ''
print 'Command line options:'
print 'ipy Deploy.py <configuration file> Deploy [--skip-database] [--only-me={deployment name}]'
print 'ipy Deploy.py <configuration file> DeployDatabase'
print 'ipy Deploy.py <configuration file> UpdateWebConfig'
print 'ipy Deploy.py <configuration file> DeleteWebsite'
print 'ipy Deploy.py <configuration file> PushFiles'
print 'ipy Deploy.py <configuration file> BuildReleaseBundle <--release-label={your label (e.g. use svn revision number)}>'
print 'ipy Deploy.py <configuration file> Info'
print 'ipy Deploy.py <configuration file> Help'
if configuration is not None:
for handler in configuration.Modules.values():
handler.PrintHelp()
sys.exit(2)
def ParseArguments():
action = Action()
action.Workflow = Action.Empty
actionString = None
if len(sys.argv) < 2:
raise System.Exception('Parameter missing: path to configuration file.')
if len(sys.argv) >= 3:
actionString = sys.argv[2]
if len(actionString) == 0:
action.Workflow |= Action.Help
if len(sys.argv) >= 4:
try:
options, theirValues = getopt.getopt(sys.argv[3:], 'h', ['skip-database', 'only-me=', 'help', 'release-label='])
for o, a in options:
if o in ('-h', '--help'):
__PrintHelp()
elif o == '--skip-database':
action.Workflow |= Action.SkipDatabase
elif o == '--only-me':
action.Workflow |= Action.OnlyMe
action.Name = a
elif o == '--release-label':
action.ReleaseLabel = a
except getopt.GetoptError, error:
print str(error)
__PrintHelp()
if actionString == 'Help':
action.Workflow |= Action.Help
if actionString == 'DeployDatabase' or actionString == 'Deploy':
action.Workflow |= Action.DeleteDatabase
action.Workflow |= Action.DeployDatabase
if actionString == 'DeleteDatabase':
action.Workflow |= Action.DeleteDatabase
if actionString == 'Deploy':
action.Workflow |= Action.DeleteWebsite
action.Workflow |= Action.CreateWebsite
action.Workflow |= Action.PushFiles
action.Workflow |= Action.UpdateWebConfig
if actionString == 'UpdateWebConfig':
action.Workflow |= Action.UpdateWebConfig
if actionString == 'DeleteWebsite':
action.Workflow |= Action.DeleteWebsite
if actionString == 'PushFiles':
action.Workflow |= Action.PushFiles
if actionString == 'BuildReleaseBundle':
action.Workflow |= Action.BuildReleaseBundle
if not hasattr(action, 'ReleaseLabel'):
print 'Parameter missing: --release-label.'
__PrintHelp()
if actionString == 'Info':
action.Workflow |= Action.Info
return action
def main():
try:
action = ParseArguments()
configuration = ReadConfiguration(sys.argv[1])
if action.Workflow == Action.Empty or action.Workflow & Action.Help:
__PrintHelp(configuration)
return
if action.Workflow & Action.Info:
__PrintConfiguration(configuration)
if action.Workflow & Action.BuildReleaseBundle:
configuration.ReleaseLabel = action.ReleaseLabel
for namepsace, handler in configuration.Modules.iteritems():
handler.Execute(configuration, action)
except Exception, e:
print 'python exception:'
exception = traceback.format_exc()
print exception
return 1
except System.Exception, e:
print 'System.Exception'
return 1
if __name__ == '__main__':
sys.exit(main())