This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
bootstrap.py
166 lines (129 loc) · 4.51 KB
/
bootstrap.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
#!/usr/bin/env python
import os
import urllib2
import apt
import tarfile
import sys
import json
from distutils import dir_util
from os import chdir
from os import symlink
from subprocess import call
def get_settings():
import Utils.HandlerUtil as Util
from Utils.WAAgentUtil import waagent
waagent.LoggerInit('/var/log/waagent.log', '/dev/null')
hutil = Util.HandlerUtility(
waagent.Log,
waagent.Error,
"bosh-deploy-script")
hutil.do_parse_context("enable")
return hutil.get_public_settings()
def get_token_from_client_credentials(endpoint, client_id, client_secret):
from urllib2 import Request
from urllib2 import urlopen
from urllib import urlencode
payload = {
'grant_type': 'client_credentials',
'client_id': client_id,
'client_secret': client_secret,
'resource': 'https://management.core.windows.net/',
}
request = Request(endpoint)
request.data = urlencode(payload)
result = urlopen(request)
return json.loads(result.read())['access_token']
def check_quota(subscription_id,tenant,client_id,secret,location,numofcores):
from azure.mgmt.common import SubscriptionCloudCredentials
endpoint = "https://login.microsoftonline.com/{0}/oauth2/token".format(tenant)
token = get_token_from_client_credentials(endpoint,client_id,secret)
creds = SubscriptionCloudCredentials(subscription_id, token)
from azure.mgmt.compute import ComputeManagementClient
compute_client = ComputeManagementClient(creds)
usage_paged=compute_client.usage.list(location).usages
core_usage=[usage for usage in usage_paged if usage.name.value == 'cores' ]
available_cores=core_usage[0].limit-core_usage[0].current_value
print "Current Cores %d Current Limit %d Available Cores %d Requested Cores %d" %(core_usage[0].current_value,core_usage[0].limit,available_cores,numofcores)
print "###QUOTACHECK###"
if numofcores > available_cores:
print "CRITICAL Insufficient Quota, PCF will NOT deploy"
sys.exit(0)
else:
print "Subscription Has Enough Quota"
# install packages
package_list = [
"python-pip",
"build-essential",
"tmux",
"ruby2.0",
"ruby2.0-dev",
"libxml2-dev",
"libsqlite3-dev",
"libxslt1-dev",
"libpq-dev",
"libmysqlclient-dev",
"zlibc",
"zlib1g-dev",
"openssl",
"libxslt1-dev",
"libssl-dev",
"libreadline6",
"libreadline6-dev",
"libyaml-dev",
"sqlite3",
"libffi-dev"]
print "Updating apt cache"
os.environ['DEBIAN_FRONTEND']='noninteractive'
cache = apt.cache.Cache()
cache.update(raise_on_error=False)
cache.open(None)
for package in package_list:
pkg = cache[package]
if not pkg.is_installed:
pkg.mark_install(auto_inst=True)
try:
cache.commit()
except Exception as arg:
print >> sys.stderr, "Sorry, package installation failed [{err}]".format(
err=str(arg))
import pip
pip_packages = ['jinja2', 'azure', 'azure-mgmt', 'click']
for package in pip_packages:
pip.main(['install', package])
release_url = 'https://s3-us-west-2.amazonaws.com/bosh-azure-releases/latest.tgz'
res = urllib2.urlopen(release_url)
code = res.getcode()
length = int(res.headers["Content-Length"])
# content-length
if code is 200:
CHUNK = 16 * 1024
filename = '/tmp/archive.tgz'
with open(filename, 'wb') as temp:
while True:
chunk = res.read(CHUNK)
if not chunk:
break
temp.write(chunk)
print "Download complete."
tfile = tarfile.open(filename, 'r:gz')
tfile.extractall(".")
dir_util.copy_tree(".", "../..")
symlink('/usr/local/lib/python2.7/dist-packages/azure/mgmt', '../../azure/mgmt')
chdir("../..")
sys.path.append('')
print "Current working dir : %s" % os.getcwd()
print "Sys.Path: %s" % sys.path
# read values from settings and check quota
settings = get_settings()
subscription_id = settings['SUBSCRIPTION-ID']
tenant = settings['TENANT-ID']
client_id = settings['CLIENT-ID']
client_secret = settings['CLIENT-SECRET']
location = settings['location']
numofcores = 65
check_quota(subscription_id, tenant, client_id, client_secret, location, numofcores)
index_file = "index-{0}.yml".format(sys.argv[1].lower())
gamma_cmd = "./gamma.py --index {0}".format(index_file)
# start tmux, running deploy_bosh_and_releases
call("tmux new -d -s shared '{0}'".format(gamma_cmd), shell=True)
call("./gotty -c gamma:{0} -t --tls-crt '.gotty.crt' --tls-key '.gotty.key' -p '443' tmux attach -d -t shared &".format(sys.argv[2]), shell=True)