|
| 1 | +import os.path |
| 2 | +import signal |
| 3 | +import time |
| 4 | +from enum import Enum |
| 5 | +from typing import List, Dict |
| 6 | + |
| 7 | +import yaml |
| 8 | +from pydantic import BaseModel |
| 9 | + |
| 10 | +from saas.cli.helpers import CLICommand, Argument, extract_address |
| 11 | +from saas.core.keystore import Keystore |
| 12 | +from saas.dor.proxy import DORProxy |
| 13 | +from saas.dor.service import fetch_proc_descriptor, _generate_gpp_hash |
| 14 | +from saas.node import Node |
| 15 | +from saas.rti.proxy import RTIProxy |
| 16 | +from saas.service import SignalListener |
| 17 | + |
| 18 | + |
| 19 | +class ProcInfo(BaseModel): |
| 20 | + name: str |
| 21 | + deployment: str = "native" |
| 22 | + ssh_credentials: str = None |
| 23 | + |
| 24 | + |
| 25 | +class NodeType(str, Enum): |
| 26 | + full = 'full' |
| 27 | + storage = 'storage' |
| 28 | + execution = 'execution' |
| 29 | + |
| 30 | + |
| 31 | +class DeploySpec(BaseModel): |
| 32 | + class NodeSpec(BaseModel): |
| 33 | + datastore_path: str = "~/.datastore" |
| 34 | + keystore_path: str = "~/.keystore" |
| 35 | + log_path: str = "~/.log" |
| 36 | + keystore_id: str |
| 37 | + password: str |
| 38 | + rest_address: str |
| 39 | + p2p_address: str |
| 40 | + boot_node_address: str |
| 41 | + type: NodeType = "full" |
| 42 | + processors: List[ProcInfo] |
| 43 | + |
| 44 | + class ProcessorSpec(BaseModel): |
| 45 | + source: str |
| 46 | + # TODO: Allow commit_id to be None as latest commit. Let node figure it out. |
| 47 | + commit_id: str |
| 48 | + proc_path: str |
| 49 | + proc_config: str |
| 50 | + dor: str = None |
| 51 | + |
| 52 | + nodes: Dict[str, NodeSpec] |
| 53 | + processors: Dict[str, ProcessorSpec] |
| 54 | + |
| 55 | + |
| 56 | +def startup_node(node_spec: DeploySpec.NodeSpec): |
| 57 | + # create node and startup services |
| 58 | + keystore_path = os.path.join(os.path.expanduser(os.path.expanduser(node_spec.keystore_path)), |
| 59 | + f"{node_spec.keystore_id}.json") |
| 60 | + datastore_path = os.path.expanduser(os.path.expanduser(node_spec.datastore_path)) |
| 61 | + |
| 62 | + keystore = Keystore.load(keystore_path, node_spec.password) |
| 63 | + node = Node.create(keystore=keystore, |
| 64 | + storage_path=datastore_path, |
| 65 | + p2p_address=extract_address(node_spec.p2p_address), |
| 66 | + boot_node_address=extract_address(node_spec.boot_node_address), |
| 67 | + rest_address=extract_address(node_spec.rest_address), |
| 68 | + enable_dor=node_spec.type == 'full' or node_spec.type == 'storage', |
| 69 | + enable_rti=node_spec.type == 'full' or node_spec.type == 'storage') |
| 70 | + |
| 71 | + return node |
| 72 | + |
| 73 | + |
| 74 | +def deploy_processors(spec: DeploySpec, nodes: dict[str, Node]): |
| 75 | + for node_key, node_spec in spec.nodes.items(): |
| 76 | + node_address = extract_address(node_spec.rest_address) |
| 77 | + rti = RTIProxy(node_address) |
| 78 | + |
| 79 | + keystore = nodes[node_key].keystore |
| 80 | + |
| 81 | + for proc in node_spec.processors: |
| 82 | + proc_spec = spec.processors[proc.name] |
| 83 | + ssh_credentials = keystore.ssh_credentials.get(proc.ssh_credentials) |
| 84 | + github_credentials = keystore.github_credentials.get(proc_spec.source) |
| 85 | + |
| 86 | + dor_address = extract_address(spec.nodes[proc_spec.dor].rest_address) if proc_spec.dor else node_address |
| 87 | + dor = DORProxy(dor_address) |
| 88 | + |
| 89 | + # fetch proc descriptor |
| 90 | + proc_descriptor = fetch_proc_descriptor(proc_spec.source, proc_spec.commit_id, proc_spec.proc_path, |
| 91 | + proc_spec.proc_config, github_credentials) |
| 92 | + # determine the content hash for the GPP |
| 93 | + c_hash = _generate_gpp_hash(proc_spec.source, proc_spec.commit_id, proc_spec.proc_path, |
| 94 | + proc_spec.proc_config, proc_descriptor.dict()) |
| 95 | + # check if gpp already found in dor |
| 96 | + items = dor.search(c_hashes=[c_hash]) |
| 97 | + if items: |
| 98 | + # use the first item if multiple exist |
| 99 | + obj_id = items[0].obj_id |
| 100 | + else: |
| 101 | + print(f"Uploading proc gpp to {dor_address}: {proc_spec}") |
| 102 | + meta = dor.add_gpp_data_object(proc_spec.source, |
| 103 | + proc_spec.commit_id, |
| 104 | + proc_spec.proc_path, |
| 105 | + proc_spec.proc_config, |
| 106 | + keystore.identity, |
| 107 | + github_credentials=github_credentials) |
| 108 | + obj_id = meta.obj_id |
| 109 | + |
| 110 | + print(f"Deploying proc ({obj_id}): {proc}") |
| 111 | + rti.deploy(obj_id, |
| 112 | + keystore, |
| 113 | + proc.deployment, |
| 114 | + ssh_credentials=ssh_credentials, |
| 115 | + github_credentials=github_credentials) |
| 116 | + |
| 117 | + |
| 118 | +class Compose(CLICommand): |
| 119 | + def __init__(self) -> None: |
| 120 | + super().__init__('compose', 'deploy processors based on yml file', arguments=[ |
| 121 | + Argument('file', metavar='file', type=str, nargs=1, |
| 122 | + help="file containing the content of the data object") |
| 123 | + ]) |
| 124 | + |
| 125 | + def execute(self, args: dict) -> None: |
| 126 | + spec_file = args["file"][0] |
| 127 | + with open(spec_file, 'r') as stream: |
| 128 | + data_loaded = yaml.load(stream, Loader=yaml.Loader) |
| 129 | + |
| 130 | + spec: DeploySpec = DeploySpec.parse_obj(data_loaded) |
| 131 | + |
| 132 | + nodes = dict() |
| 133 | + try: |
| 134 | + for node_key, node_spec in spec.nodes.items(): |
| 135 | + print(f"Starting up {node_key} | {node_spec.keystore_id}") |
| 136 | + node = startup_node(node_spec) |
| 137 | + nodes[node_key] = node |
| 138 | + |
| 139 | + print("Deploying processors to nodes") |
| 140 | + deploy_processors(spec, nodes) |
| 141 | + |
| 142 | + print("Nodes ready") |
| 143 | + |
| 144 | + # Block until interrupt |
| 145 | + signal_listener = SignalListener([signal.SIGTERM]) |
| 146 | + while not signal_listener.triggered: |
| 147 | + time.sleep(1) |
| 148 | + except KeyboardInterrupt: |
| 149 | + print("Interrupted by user. Shutting down.") |
| 150 | + finally: |
| 151 | + for _, node in nodes.items(): |
| 152 | + node.shutdown() |
| 153 | + |
| 154 | + |
| 155 | +if __name__ == '__main__': |
| 156 | + compose = Compose() |
| 157 | + compose.execute({ |
| 158 | + "file": ["/Users/reynoldmok/Library/Application Support/JetBrains/PyCharm2022.2/scratches/compose.yml"], |
| 159 | + }) |
0 commit comments