Skip to content
This repository has been archived by the owner on Dec 30, 2024. It is now read-only.

feat: Support GridClient types: #466

Merged
merged 16 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion crystallib/threefold/grid/deployer.v
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,21 @@ pub fn (mut d Deployer) get_deployment(contract_id u64, node_id u32) !models.Dep
payload := {
'contract_id': contract_id
}
res := d.rmb_deployment_get(twin_id, json.encode(payload))!
res := d.rmb_deployment_get(
twin_id,
json.encode(payload)
) or {
return error("Node ${node_id} might be down.")
}
return json.decode(models.Deployment, res)
}

pub fn (mut d Deployer) delete_deployment(contract_id u64, node_id u32) !models.Deployment {
twin_id := d.client.get_node_twin(node_id)!
payload := {
'contract_id': contract_id
}
res := d.rmb_deployment_delete(twin_id, json.encode(payload))!
return json.decode(models.Deployment, res)
}

Expand Down
2 changes: 1 addition & 1 deletion crystallib/threefold/grid/models/deployment.v
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ pub fn (mut dl Deployment) add_metadata(type_ string, project_name string) {
mut data := DeploymentData{
type_: type_
name: project_name
project_name: project_name
project_name: "${type_}/${project_name}" // To be listed in the dashboard.
}
dl.metadata = data.json_encode()
}
Expand Down
4 changes: 4 additions & 0 deletions crystallib/threefold/gridproxy/gridproxy_factory.v
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,7 @@ pub fn farmfilter() !model.FarmFilter {
pub fn twinfilter() !model.TwinFilter {
return model.TwinFilter{}
}

pub fn statfilter() !model.StatFilter {
return model.StatFilter{}
}
24 changes: 23 additions & 1 deletion crystallib/threefold/gridproxy/gridproxy_highlevel.v
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,29 @@ pub fn (mut c GridProxyClient) get_contracts_by_twin_id(twin_id u64) []Contract
return error_with_code('http client error: $err.msg()', gridproxy.err_http_client)
}*/
mut filter := ContractFilter{
twin_id: twin_id
twin_id: twin_id,
}
mut iter := c.get_contracts_iterator(filter)
mut result := []Contract{}
for f in iter {
result << f
}
return result
}

// get_active_contracts returns iterator over `created` contracts owned by specific twin.
//
// * `twin_id`: twin id.
//
// returns: `ContractIterator`.
pub fn (mut c GridProxyClient) get_active_contracts(twin_id u64) []Contract {
/*
contracts := c.get_contracts(twin_id: twin_id) or {
return error_with_code('http client error: $err.msg()', gridproxy.err_http_client)
}*/
mut filter := ContractFilter{
twin_id: twin_id,
state: "created"
}
mut iter := c.get_contracts_iterator(filter)
mut result := []Contract{}
Expand Down
36 changes: 18 additions & 18 deletions crystallib/threefold/gridproxy/model/filter.v
Original file line number Diff line number Diff line change
Expand Up @@ -479,26 +479,26 @@ pub fn (p &NodeFilter) to_map() map[string]string {
return m
}

// pub enum NodeStatus {
// all
// online
// }
pub enum NodeStatus {
all
online
}

// @[params]
// pub struct ResourceFilter {
// pub mut:
// free_mru_gb u64
// free_sru_gb u64
// free_hru_gb u64
// free_cpu u64
// free_ips u64
// }
@[params]
pub struct ResourceFilter {
pub mut:
free_mru_gb u64
free_sru_gb u64
free_hru_gb u64
free_cpu u64
free_ips u64
}

// @[params]
// pub struct StatFilter {
// pub mut:
// status NodeStatus
// }
@[params]
pub struct StatFilter {
pub mut:
status NodeStatus
}

@[params]
pub struct TwinFilter {
Expand Down
170 changes: 170 additions & 0 deletions crystallib/threefold/higher_grid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
# Higher Grid Client V Modules

This project provides a V modules (`models.v`) that interacts with the [ThreeFold Grid](https://manual.grid.tf/documentation/developers/grid_deployment/grid_deployment.html) to manage the deployment, listing, and deletion of virtual machines (VMs) on the ThreeFold grid using the `freeflowuniverse.crystallib.threefold.grid` library. The module handles workloads creation, network setup, and deployment processes.

## Features

- **Deploy Virtual Machines (VMs):** Deploy workloads (VMs and networks) to a specific node on the ThreeFold Grid.
- **List Deployments:** Retrieve active deployments and contracts on the ThreeFold network.
- **Delete Deployments:** Remove an active deployment by its name.
- **Fetch Deployment Results:** Obtain the results of a specific deployment.
- **Manage Network Workloads:** Automatically assign WireGuard ports and configure virtual networks.
- **Public IP Management:** Manage IPv4 and IPv6 configurations for public access to VMs.

## Table of Contents

- [Installation](#installation)
- [Usage](#usage)
- [Deployment](#deployment)
- [Listing Deployments](#listing-deployments)
- [Deleting a Deployment](#deleting-a-deployment)
- [Fetching Deployment Results](#fetching-deployment-results)
- [Environment Variables](#environment-variables)
- [Examples](#examples)

## Installation

1. Install the `v` programming language by following the instructions [here](https://vlang.io).
2. Clone the repository and ensure that you have linked the crystallib repository with the smae repository in the `.vmodules` in your system.

```bash
git clone https://github.com/freeflowuniverse/crystallib.git
cd crystallib
```

## Usage

### Deployment

To deploy VMs on the ThreeFold Grid, create a `GridMachinesModel` with the necessary configuration, such as the machine names, network access, and capacity requirements. Then call the `deploy` method.

```v
import freeflowuniverse.crystallib.threefold.higher_grid.models

mnemonic := os.getenv('TFGRID_MNEMONIC')
ssh_key := os.getenv('SSH_KEY')

// Create the GridConfig for deployment
// Assuming "dev" is the chain network
mut grid := models.new_grid_client(mnemonic, .dev, ssh_key)!

mut vms := models.GridMachinesModel{
name: "MyVMDeployment",
node_id: 177,
network: models.NetworkModel{
name: "MyNetwork",
ip_range: '10.249.0.0/16',
subnet: '10.249.0.0/24',
},
machines: [
models.MachineModel{
name: "MyVM",
network_access: models.MachineNetworkAccessModel{
public_ip4: false,
public_ip6: false,
planetary: true,
mycelium: true,
},
capacity: models.ComputeCapacity{
cpu: 4,
memory: 4096,
},
}
]
}

grid.machines.deploy(vms)!
```

### Listing Deployments

To list all active deployments associated with the current twin:

```v
deployments := grid.machines.list()!
for deployment in deployments {
println(deployment)
}
```

### Deleting a Deployment

To delete a deployment by name:

```v
grid.machines.delete("MyVMDeployment")!
```

### Fetching Deployment Results

To retrieve the results of a specific deployment:

```v
deployment_results := grid.machines.get("MyVMDeployment")!
println(deployment_results)
```

## Environment Variables

The following environment variables must be set to enable the module to interact with the ThreeFold Grid:

- `TFGRID_MNEMONIC`: The mnemonic phrase used to authenticate with the ThreeFold Grid.
- `SSH_KEY`: Your SSH key for accessing deployed VMs.

```bash
export TFGRID_MNEMONIC="your-mnemonic-phrase-here"
export SSH_KEY="your-ssh-key-here"
```

## Examples

The example below shows how to define and deploy a virtual machine, retrieve the deployment result, and delete the deployment.

```v
fn do()! {
mnemonic := os.getenv('TFGRID_MNEMONIC')
ssh_key := os.getenv('SSH_KEY')

// Create the Grid Client for deployment
mut grid := models.new_grid_client(mnemonic, .dev, ssh_key)!

// Define the VMs to be deployed
mut vms := models.GridMachinesModel{
name: "MachinesInterface",
node_id: 177,
network: models.NetworkModel{
name: "MyNetwork",
ip_range: '10.249.0.0/16',
subnet: '10.249.0.0/24',
},
machines: [
models.MachineModel{
name: "MyVM",
network_access: models.MachineNetworkAccessModel{
public_ip4: false,
public_ip6: false,
planetary: true,
},
capacity: models.ComputeCapacity{
cpu: 4,
memory: 2048,
},
}
]
}

// Deploy the VMs
grid.machines.deploy(vms)!

// Retrieve and print the deployments
deployments := grid.machines.get("MachinesInterface")!
println(deployments)

// Delete the deployment
grid.machines.delete("MachinesInterface")!
}

fn main() {
do()!
}
```
48 changes: 48 additions & 0 deletions crystallib/threefold/higher_grid/main.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module main

import freeflowuniverse.crystallib.threefold.higher_grid.models
import os

fn do()! {
mnemonic := os.getenv('TFGRID_MNEMONIC')
ssh_key := os.getenv('SSH_KEY')

// Create the GridConfig for deployment
// Assuming "dev" is the chain network
mut grid := models.new_grid_client(mnemonic, .dev, ssh_key)!

// Define the VM to be deployed
mut vms := models.GridMachinesModel{
name: "Machinesinterface",
node_id: 177
network: models.NetworkModel{
name: "Netselo",
ip_range: '10.249.0.0/16',
subnet: '10.249.0.0/24',
},
machines: [
models.MachineModel{
name: "testmachine",
network_access: models.MachineNetworkAccessModel{
public_ip4: false,
public_ip6: false,
planetary: true,
mycelium: true,
},
capacity: models.ComputeCapacity{
cpu: 5
memory: 2048
},
}
]
}

// grid.machines.deploy(vms)!
// grid.machines.list()!
// grid.machines.delete("Machinesinterface")!
grid.machines.get("Machinesinterface")!
}

fn main() {
do()!
}
Loading
Loading