-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcondensed_server.py
224 lines (166 loc) · 8.87 KB
/
condensed_server.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
216
217
218
219
220
221
222
import os
import flask
from flask import json, request
from flask_cors import CORS
import subprocess
from subprocess import check_output as CO
import time
from flask import jsonify
from kubernetes import client, config
from pprint import pprint
config.load_kube_config()
v1 = client.CoreV1Api()
apps = client.AppsV1Api()
app = flask.Flask(__name__)
CORS(app)
app.config["DEBUG"] = True
def read_json(filename):
with open(filename) as f:
data = json.load(f)
return data
@app.route('/', methods=['GET'])
def home():
return("hello_world")
@app.route('/create_and_connect_to_cluster', methods=['GET'])
def ClusterInit():
try:
os.system('gcloud beta container --project "busy-burglar" clusters create "test-k8-1" --zone "asia-southeast1-b" --no-enable-basic-auth --cluster-version "1.14.10-gke.17" --machine-type "n1-standard-1" --image-type "COS" --disk-type "pd-standard" --disk-size "100" --scopes "https://www.googleapis.com/auth/devstorage.read_only","https://www.googleapis.com/auth/logging.write","https://www.googleapis.com/auth/monitoring","https://www.googleapis.com/auth/servicecontrol","https://www.googleapis.com/auth/service.management.readonly","https://www.googleapis.com/auth/trace.append" --num-nodes "8" --enable-stackdriver-kubernetes --enable-ip-alias --network "projects/busy-burglar/global/networks/default" --subnetwork "projects/busy-burglar/regions/asia-southeast1/subnetworks/default" --default-max-pods-per-node "110" --addons HorizontalPodAutoscaling,HttpLoadBalancing --enable-autoupgrade --enable-autorepair')
os.system('gcloud container clusters get-credentials test-k8-1')
except:
print("something went wrong")
return("something is wrong")
return "cluster created and connected to"
@app.route('/prep_cluster_step1', methods=['GET'])
def prep_cluster_step1():
res = os.system("kubectl create -f ./helm-rbac.yaml")
if res == 0:
res = os.system("helm init --service-account tiller")
res = os.system("kubectl create namespace argo")
res = os.system("kubectl apply -n argo -f https://raw.githubusercontent.com/argoproj/argo/v2.4.3/manifests/install.yaml")
return "done"
@app.route('/prep_cluster_step2', methods=['GET'])
def prep_cluster_step2():
res = os.system("kubectl create rolebinding default-admin --clusterrole=admin --serviceaccount=default:default")
res = os.system("helm install stable/minio -n argo-artifacts --set service.type=LoadBalancer --set defaultBucket.enabled=true --set defaultBucket.name=my-bucket --set persistence.enabled=false --set fullnameOverride=argo-artifacts")
time.sleep(5)
res = os.system("kubectl apply -f temp.yaml")
return "done"
@app.route('/prep_cluster_step3', methods=['GET'])
def prep_cluster_step3():
res = os.system("kubectl apply -f temp.yaml")
return "done"
@app.route('/start_network1', methods=['GET'])
def start_network1():
old = os.getcwd()
os.chdir("../PIVT3/PIVT/fabric-kube")
res = os.system("./init.sh ./samples/simple/ ./samples/chaincode/")
res = os.system("helm install ./hlf-kube --name hlf-kube -f samples/simple/network.yaml -f samples/simple/crypto-config.yaml")
os.chdir(old)
res = os.system("rm -rf rm -rf node-app-files/channel/*")
res = os.system("cp -R ../PIVT3/PIVT/fabric-kube/hlf-kube/crypto-config node-app-files/channel/")
res = os.system("cp ../PIVT3/PIVT/fabric-kube/hlf-kube/channel-artifacts/* node-app-files/channel/")
return "done"
@app.route('/start_network2', methods=['GET'])
def start_network2():
old = os.getcwd()
os.chdir("../PIVT3/PIVT/fabric-kube")
res = os.system("helm template channel-flow/ -f samples/simple/network.yaml -f samples/simple/crypto-config.yaml | argo submit - --watch")
res = os.system("helm template chaincode-flow/ -f samples/simple/network.yaml -f samples/simple/crypto-config.yaml | argo submit - --watch")
os.chdir(old)
return "done"
@app.route('/extend_network1', methods=['GET'])
def extend_network1():
old = os.getcwd()
os.chdir("../PIVT3/PIVT/fabric-kube")
res = os.system("./extend.sh samples/simple")
res = os.system("helm upgrade hlf-kube ./hlf-kube -f samples/simple/network.yaml -f samples/simple/crypto-config.yaml")
os.chdir(old)
res = os.system("rm -rf rm -rf node-app-files/channel/*")
res = os.system("cp -R ../PIVT3/PIVT/fabric-kube/hlf-kube/crypto-config node-app-files/channel/")
res = os.system("cp ../PIVT3/PIVT/fabric-kube/hlf-kube/channel-artifacts/* node-app-files/channel/")
return "done"
@app.route('/extend_network2/<version>', methods=['GET'])
def extend_network2(version):
old = os.getcwd()
os.chdir("../PIVT3/PIVT/fabric-kube")
res = os.system("helm template peer-org-flow/ -f samples/simple/network.yaml -f samples/simple/crypto-config.yaml -f samples/simple/configtx.yaml | argo submit - --watch")
res = os.system("helm template channel-flow/ -f samples/simple/network.yaml -f samples/simple/crypto-config.yaml | argo submit - --watch")
res = os.system("helm template chaincode-flow/ -f samples/simple/network.yaml -f samples/simple/crypto-config.yaml --set chaincode.version="+ version +" | argo submit - --watch")
os.chdir(old)
return "done"
@app.route('/scrap_network', methods=['GET'])
def scrap_network():
old = os.getcwd()
os.chdir("../PIVT3/PIVT/fabric-kube")
res = os.system("helm del --purge hlf-kube")
os.chdir(old)
return "done"
@app.route('/set_net_json', methods=['POST'])
def set_net_json():
net_json = request.get_json()
with open("./config-files/generated_network_config/"+net_json["filename"], 'w') as outfile:
json.dump(net_json["network_config"], outfile, sort_keys=True, indent=4)
old = os.getcwd()
os.chdir('./config-files/')
os.system('python generate.py ' + net_json["filename"])
os.chdir(old)
return jsonify({"status":"done"})
@app.route('/add_org_to_json', methods=['POST'])
def add_org_to_json():
data = request.get_json()
print(data)
filename = data['filename']
filepath = "./config-files/generated_network_config/" + data["filename"]
orgname = data["name"]
domain = data["domain"]
net = read_json(filepath)
if orgname not in net["Channels"][0]["orgs"]:
net["OtherOrganizations"].append({"name": orgname, "domain": domain})
net["Channels"][0]["orgs"].append(orgname)
with open(filepath, 'w') as outfile:
json.dump(net, outfile, sort_keys=True, indent=4)
old = os.getcwd()
os.chdir('./config-files/')
os.system('python generate.py ' + filename)
os.chdir(old)
else:
return "org already exists with this name in network\n"
return "done"
@app.route('/deploy_node_app', methods=['GET'])
def deploy_node_app():
try:
os.system("kubectl create deployment hello-web --image=gcr.io/busy-burglar/hello-app:v1")
proc = subprocess.Popen(["kubectl get pods | grep hello | awk {'print $1'}"], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
print(out)
except:
print("didnt finish something went wrong")
return "didnt finish something went wrong"
return "node app deployed"
@app.route('/migrate_connection_profile/<filename>', methods=['GET'])
def migrate_connection_profile(filename):
try:
os.system("cd ./node-app-files/ && python generate-common-profile.py " + filename)
os.system("cd ./node-app-files/ && python generate-client-profiles.py " + filename)
proc = subprocess.Popen(["kubectl get pods | grep hello | awk {'print $1'}"], stdout=subprocess.PIPE, shell=True)
(pod_name, err) = proc.communicate()
print(pod_name.decode("utf-8")[:-1])
pod_name = pod_name.decode("utf-8")[:-1]
print("kubectl cp network-config.yaml " + pod_name+":/home/nodejs/app/artifacts/")
proc = subprocess.Popen(["kubectl cp node-app-files/network-config.yaml " + pod_name+":/home/nodejs/app/artifacts/"], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
print(out)
proc = subprocess.Popen(["kubectl cp node-app-files/org1.yaml " + pod_name+":/home/nodejs/app/artifacts/"], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
print(out)
proc = subprocess.Popen(["kubectl cp node-app-files/org2.yaml " + pod_name+":/home/nodejs/app/artifacts/"], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
print(out)
proc = subprocess.Popen(["kubectl cp node-app-files/channel/ " + pod_name+":/home/nodejs/app/artifacts/"], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
print(out)
except Exception:
print("didnt finish something went wrong, ", str(Exception))
return "didnt finish something went wrong"
return "connection profiles moved to node app"
app.run()