This repository has been archived by the owner on Jul 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdeployproxy.py
executable file
·130 lines (110 loc) · 3.4 KB
/
deployproxy.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
import base64
import getopt
import httplib
import json
import re
import os
import sys
import StringIO
import urlparse
import xml.dom.minidom
import zipfile
import getpass
from ApigeePlatformTools import httptools, deploytools
def printUsage():
print 'Usage: deployproxy -n [name] -o [organization] -e [environment]'
print ' -d [directory name]'
print ' -u [username] -p [password]'
print ' -b [base path] -l [apigee API url] -z [zip file] -i -h'
print ''
print '-o Apigee organization name'
print '-e Apigee environment name'
print '-n Apigee proxy name'
print '-d Apigee proxy directory'
print '-u Apigee user name'
print '-p Apigee password (optional, will prompt if not supplied)'
print '-b Base path (optional, defaults to /)'
print '-l Apigee API URL (optional, defaults to https://api.enterprise.apigee.com)'
print '-z ZIP file to save (optional for debugging)'
print '-i import only, do not deploy'
print '-h Print this message'
def run():
ApigeeURL = 'https://api.enterprise.apigee.com'
Username = None
Password = None
Directory = None
Organization = None
Environment = None
Name = None
BasePath = '/'
ShouldDeploy = True
ZipFile = None
Options = 'o:e:n:d:u:p:b:l:z:ih'
opts = getopt.getopt(sys.argv[2:], Options)[0]
for o in opts:
if o[0] == '-n':
Name = o[1]
elif o[0] == '-o':
Organization = o[1]
elif o[0] == '-d':
Directory =o[1]
elif o[0] == '-e':
Environment =o[1]
elif o[0] == '-b':
BasePath = o[1]
elif o[0] == '-u':
Username = o[1]
elif o[0] == '-p':
Password = o[1]
elif o[0] == '-l':
ApigeeURL = o[1]
elif o[0] == '-z':
ZipFile = o[1]
elif o[0] == '-i':
ShouldDeploy = False
elif o[0] == '-h':
printUsage()
sys.exit(0)
if not Password:
Password = getpass.getpass()
if not Username or not Password or not Directory or \
not Environment or not Name or not Organization:
printUsage()
sys.exit(1)
httptools.setup(ApigeeURL, Username, Password)
# Return TRUE if any component of the file path contains a directory name that
# starts with a "." like '.svn', but not '.' or '..'
def pathContainsDot(p):
c = re.compile('\.\w+')
for pc in p.split('/'):
if c.match(pc) != None:
return True
return False
# Construct a ZIPped copy of the bundle in memory
tf = StringIO.StringIO()
zipout = zipfile.ZipFile(tf, 'w')
dirList = os.walk(Directory)
for dirEntry in dirList:
if not pathContainsDot(dirEntry[0]):
for fileEntry in dirEntry[2]:
if not fileEntry.endswith('~'):
fn = os.path.join(dirEntry[0], fileEntry)
en = os.path.join(os.path.relpath(dirEntry[0], Directory), fileEntry)
zipout.write(fn, en)
zipout.close()
if (ZipFile != None):
tzf = open(ZipFile, 'w')
tzf.write(tf.getvalue())
tzf.close()
revision = deploytools.importBundle(Organization, Name, tf.getvalue())
if (revision < 0):
sys.exit(2)
print 'Imported new proxy revision %i' % revision
if ShouldDeploy:
status = deploytools.deployWithoutConflict(Organization, Environment, Name, BasePath, revision)
if status == False:
sys.exit(2)
response = httptools.httpCall('GET',
'/v1/o/%s/apis/%s/deployments' % (Organization, Name))
deps = deploytools.parseAppDeployments(Organization, response, Name)
deploytools.printDeployments(deps)