|
| 1 | +import { |
| 2 | + Features, |
| 3 | + FilterOptions, |
| 4 | + generateRandomHexSeed, |
| 5 | + generateString, |
| 6 | + GridClient, |
| 7 | + MachinesDeleteModel, |
| 8 | + MachinesModel, |
| 9 | +} from "../src"; |
| 10 | +import { config, getClient } from "./client_loader"; |
| 11 | +import { log, pingNodes } from "./utils"; |
| 12 | + |
| 13 | +async function deploy(client: GridClient, vms: MachinesModel) { |
| 14 | + const res = await client.machines.deploy(vms); |
| 15 | + log("================= Deploying VM ================="); |
| 16 | + log(res); |
| 17 | + log("================= Deploying VM ================="); |
| 18 | +} |
| 19 | + |
| 20 | +async function getDeployment(client: GridClient, name: string) { |
| 21 | + const res = await client.machines.getObj(name); |
| 22 | + log("================= Getting deployment information ================="); |
| 23 | + log(res); |
| 24 | + log("================= Getting deployment information ================="); |
| 25 | +} |
| 26 | + |
| 27 | +async function cancel(client: GridClient, name: string) { |
| 28 | + const res = await client.machines.delete({ name }); |
| 29 | + log("================= Canceling the deployment ================="); |
| 30 | + log(res); |
| 31 | + log("================= Canceling the deployment ================="); |
| 32 | +} |
| 33 | + |
| 34 | +async function getNodeId(client: GridClient, options: FilterOptions) { |
| 35 | + const nodes = await client.capacity.filterNodes(options); |
| 36 | + const nodeId = await pingNodes(client, nodes); |
| 37 | + return nodeId; |
| 38 | +} |
| 39 | + |
| 40 | +async function main() { |
| 41 | + const name = "vm" + generateString(6); |
| 42 | + const networkName = "nw" + generateString(6); |
| 43 | + const machine1Name = "machine" + generateString(6); |
| 44 | + const machine2Name = "machine" + generateString(6); |
| 45 | + const disk1Name = "disk" + generateString(6); |
| 46 | + const disk2Name = "disk" + generateString(6); |
| 47 | + |
| 48 | + const grid3 = await getClient(`vm/${name}`); |
| 49 | + |
| 50 | + const vmQueryOptions: FilterOptions = { |
| 51 | + cru: 1, |
| 52 | + mru: 1, // GB |
| 53 | + sru: 14, |
| 54 | + availableFor: grid3.twinId, |
| 55 | + features: [Features.zmachinelight, Features.networklight, Features.mycelium], |
| 56 | + nodeExclude: [259], |
| 57 | + farmName: "LiriaFarm", |
| 58 | + }; |
| 59 | + |
| 60 | + const nodeId = await getNodeId(grid3, vmQueryOptions); |
| 61 | + |
| 62 | + const vms: MachinesModel = { |
| 63 | + name, |
| 64 | + network: { |
| 65 | + name: networkName, |
| 66 | + ip_range: "10.238.0.0/16", |
| 67 | + }, |
| 68 | + machines: [ |
| 69 | + { |
| 70 | + name: machine1Name, |
| 71 | + node_id: nodeId!, |
| 72 | + disks: [ |
| 73 | + { |
| 74 | + name: disk1Name, |
| 75 | + size: 5, |
| 76 | + mountpoint: "/newDisk1", |
| 77 | + }, |
| 78 | + ], |
| 79 | + public_ip: false, |
| 80 | + public_ip6: false, |
| 81 | + planetary: false, |
| 82 | + mycelium: true, |
| 83 | + cpu: 1, |
| 84 | + memory: 1024, |
| 85 | + rootfs_size: 0, |
| 86 | + flist: "https://hub.grid.tf/tf-official-apps/base:latest.flist", |
| 87 | + entrypoint: "/sbin/zinit init", |
| 88 | + env: { |
| 89 | + SSH_KEY: config.ssh_key, |
| 90 | + }, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: machine2Name, |
| 94 | + node_id: nodeId!, |
| 95 | + disks: [ |
| 96 | + { |
| 97 | + name: disk2Name, |
| 98 | + size: 5, |
| 99 | + mountpoint: "/newDisk2", |
| 100 | + }, |
| 101 | + ], |
| 102 | + public_ip: false, |
| 103 | + public_ip6: false, |
| 104 | + planetary: false, |
| 105 | + mycelium: true, |
| 106 | + cpu: 1, |
| 107 | + memory: 1024, |
| 108 | + rootfs_size: 0, |
| 109 | + flist: "https://hub.grid.tf/tf-official-apps/base:latest.flist", |
| 110 | + entrypoint: "/sbin/zinit init", |
| 111 | + env: { |
| 112 | + SSH_KEY: config.ssh_key, |
| 113 | + }, |
| 114 | + }, |
| 115 | + ], |
| 116 | + metadata: "", |
| 117 | + description: "test deploying VMs via ts grid3 client", |
| 118 | + }; |
| 119 | + |
| 120 | + //Deploy VMs |
| 121 | + await deploy(grid3, vms); |
| 122 | + |
| 123 | + //Get the deployment |
| 124 | + await getDeployment(grid3, name); |
| 125 | + |
| 126 | + // Uncomment the line below to cancel the deployment |
| 127 | + // await cancel(grid3, name); |
| 128 | + |
| 129 | + await grid3.disconnect(); |
| 130 | +} |
| 131 | + |
| 132 | +main(); |
0 commit comments