-
Notifications
You must be signed in to change notification settings - Fork 463
Expand file tree
/
Copy pathservice.py
More file actions
263 lines (234 loc) · 8.17 KB
/
Copy pathservice.py
File metadata and controls
263 lines (234 loc) · 8.17 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
252
253
254
255
256
257
258
259
260
261
262
263
from enum import Enum, auto
import json
import logging
import os
import subprocess
import tarfile
import threading
from typing import Optional, List, Any, Dict, Tuple
from urllib.parse import urljoin
import requests
from django.db import transaction
from api_engine.settings import CELLO_HOME, FABRIC_TOOL
from chaincode.models import Chaincode
from channel.models import Channel
from node.models import Node
from node.service import get_domain_name, get_peer_directory, get_org_directory, get_orderer_directory
from organization.models import Organization
from user.models import UserProfile
LOG = logging.getLogger(__name__)
class ChaincodeTransactionError(Exception):
pass
peer_command = os.path.join(FABRIC_TOOL, "peer")
def get_chaincode(pk: str) -> Optional[Chaincode]:
return Chaincode.objects.get(id=pk)
def get_chaincode_status(organization: Organization, chaincode: Chaincode) -> str:
agent_url = organization.agent_url
requests.get(urljoin(agent_url, "health")).raise_for_status()
response = requests.get(
urljoin(agent_url, "chaincodes/status"),
params=dict(
name=chaincode.name,
package_id=chaincode.package_id,
sequence=chaincode.sequence,
channel=chaincode.channel.name
)
)
response.raise_for_status()
return response.json()["status"]
def get_chaincode_commit_readiness(organization: Organization, chaincode: Chaincode) -> str:
agent_url = organization.agent_url
requests.get(urljoin(agent_url, "health")).raise_for_status()
response = requests.get(
urljoin(agent_url, "chaincodes/commit/readiness"),
params=dict(
name=chaincode.name,
version=chaincode.version,
sequence=chaincode.sequence,
channel=chaincode.channel.name,
init_required=(chaincode.init_required is not None and chaincode.init_required),
)
)
response.raise_for_status()
return response.json()["approvals"]
def create_chaincode(
name: str,
version: str,
sequence: int,
package,
channel: Channel,
user: UserProfile,
organization: Organization,
description: str = None,
init_required: bool = False,
signature_policy: str = None) -> Chaincode:
agent_url = organization.agent_url
requests.get(urljoin(agent_url, "health")).raise_for_status()
response = requests.post(
urljoin(agent_url, "chaincodes"),
data=dict(
name=name,
version=version,
sequence=sequence,
channel_name=channel.name,
init_required=init_required,
signature_policy=signature_policy
),
files=dict(
file=package
)
)
response.raise_for_status()
response_json = response.json()
chaincode = Chaincode(
package_id=response_json["package_id"],
name=name,
version=version,
sequence=sequence,
label=response_json["label"],
language=response_json["language"],
package=package,
init_required=init_required,
signature_policy=signature_policy,
channel=channel,
creator=user,
description=description,
)
chaincode.save()
return chaincode
def metadata_exists(file) -> bool:
file.seek(0)
res = False
with tarfile.open(fileobj=file, mode='r:gz') as tar:
for member in tar.getmembers():
if member.name.endswith("metadata.json"):
res = True
break
file.seek(0)
return res
def install_chaincode(organization: Organization, chaincode: Chaincode) -> None:
agent_url = organization.agent_url
requests.get(urljoin(agent_url, "health")).raise_for_status()
requests.put(
urljoin(agent_url, "chaincodes/install"),
data=dict(
name=chaincode.name,
version=chaincode.version,
sequence=chaincode.sequence,
channel_name=chaincode.channel.name
),
files=dict(
file=chaincode.package
)
).raise_for_status()
def approve_chaincode(
organization: Organization,
chaincode: Chaincode) -> None:
agent_url = organization.agent_url
requests.get(urljoin(agent_url, "health")).raise_for_status()
requests.put(
urljoin(agent_url, "chaincodes/approve"),
json=dict(
name=chaincode.name,
version=chaincode.version,
sequence=chaincode.sequence,
package_id=chaincode.package_id,
channel_name=chaincode.channel.name,
init_required=chaincode.init_required,
signature_policy=chaincode.signature_policy
)
).raise_for_status()
def commit_chaincode(
organization: Organization,
chaincode: Chaincode) -> None:
agent_url = organization.agent_url
requests.get(urljoin(agent_url, "health")).raise_for_status()
requests.put(
urljoin(agent_url, "chaincodes/commit"),
json=dict(
name=chaincode.name,
version=chaincode.version,
sequence=chaincode.sequence,
channel_name=chaincode.channel.name
)
).raise_for_status()
class ChaincodeAction(Enum):
SUBMIT = auto()
EVALUATE = auto()
def send_chaincode_request(
organization: Organization,
chaincode: Chaincode,
action: ChaincodeAction,
function: str,
*args: str) -> str:
# Pick any organization peer
peer_env: Dict[str, str] = get_peers_root_certs_and_addresses_and_envs(
organization.name,
[chaincode.peers.filter(organization=organization).first()]
)[2][0]
go = subprocess.run(
["which", "go"],
check=True,
capture_output=True,
text=True).stdout.rstrip('\n')
try:
command = [
go,
"run",
".",
action.name,
function,
*args
]
LOG.info(" ".join(command))
result = subprocess.run(
command,
env={
**peer_env,
"CHANNEL_NAME": chaincode.channel.name,
"CHAINCODE_NAME": chaincode.name,
"GOPATH": subprocess.run(
[go, "env", "GOPATH"],
check=True,
capture_output=True,
text=True).stdout.rstrip('\n'),
"GOCACHE": subprocess.run(
[go, "env", "GOCACHE"],
check=True,
capture_output=True,
text=True).stdout.rstrip('\n')
},
cwd=os.path.join(CELLO_HOME, "application-gateway"),
check=True,
capture_output=True,
text=True)
LOG.info(result.stdout)
return result.stdout
except subprocess.CalledProcessError as e:
LOG.error(e.stderr)
raise ChaincodeTransactionError(e.stderr)
def get_peers_root_certs_and_addresses_and_envs(
organization_name: str,
peers: List[Node]) -> Tuple[List[str], List[str], List[Dict[str, str]]]:
peer_root_certs: List[str] = []
peer_addresses: List[str] = []
peer_envs: List[Dict[str, str]] = []
for peer_name in [peer.name for peer in peers]:
peer_domain_name: str = get_domain_name(organization_name, Node.Type.PEER, peer_name)
peer_dir: str = get_peer_directory(organization_name, peer_domain_name)
peer_root_cert: str = os.path.join(peer_dir, "tls/ca.crt")
peer_address: str = "{}:7051".format(peer_domain_name)
peer_root_certs.append(peer_root_cert)
peer_addresses.append(peer_address)
peer_envs.append({
"CORE_PEER_TLS_ENABLED": "true",
"CORE_PEER_LOCALMSPID": "{}MSP".format(organization_name.split(".", 1)[0].capitalize()),
"CORE_PEER_TLS_ROOTCERT_FILE": peer_root_cert,
"CORE_PEER_MSPCONFIGPATH": "{}/users/Admin@{}/msp".format(
get_org_directory(organization_name, Node.Type.PEER),
organization_name
),
"CORE_PEER_ADDRESS": peer_address,
"FABRIC_CFG_PATH": peer_dir,
})
return peer_root_certs, peer_addresses, peer_envs