..deployments
This commit is contained in:
@@ -3,10 +3,8 @@
|
||||
import freeflowuniverse.herolib.threefold.gridproxy
|
||||
import freeflowuniverse.herolib.threefold.tfgrid3deployer
|
||||
import freeflowuniverse.herolib.ui.console
|
||||
import freeflowuniverse.herolib.installers.threefold.griddriver
|
||||
|
||||
fn main() {
|
||||
griddriver.install()!
|
||||
|
||||
v := tfgrid3deployer.get()!
|
||||
println('cred: ${v}')
|
||||
@@ -19,19 +17,19 @@ fn main() {
|
||||
cpu: 1
|
||||
memory: 2
|
||||
planetary: false
|
||||
public_ip4: true
|
||||
public_ip4: false
|
||||
mycelium: tfgrid3deployer.Mycelium{}
|
||||
nodes: [u32(167)]
|
||||
)
|
||||
deployment.add_machine(
|
||||
name: 'my_vm2'
|
||||
cpu: 1
|
||||
memory: 2
|
||||
planetary: false
|
||||
public_ip4: true
|
||||
mycelium: tfgrid3deployer.Mycelium{}
|
||||
// nodes: [u32(164)]
|
||||
)
|
||||
// deployment.add_machine(
|
||||
// name: 'my_vm2'
|
||||
// cpu: 1
|
||||
// memory: 2
|
||||
// planetary: false
|
||||
// public_ip4: true
|
||||
// mycelium: tfgrid3deployer.Mycelium{}
|
||||
// // nodes: [u32(164)]
|
||||
// )
|
||||
|
||||
deployment.add_zdb(name: 'my_zdb', password: 'my_passw&rd', size: 2)
|
||||
deployment.add_webname(name: 'mywebname2', backend: 'http://37.27.132.47:8000')
|
||||
|
||||
@@ -7,7 +7,10 @@ import freeflowuniverse.herolib.installers.threefold.griddriver
|
||||
import os
|
||||
import time
|
||||
|
||||
griddriver.install()!
|
||||
|
||||
res2:=tfgrid3deployer.filter_nodes()!
|
||||
println(res2)
|
||||
exit(0)
|
||||
|
||||
v := tfgrid3deployer.get()!
|
||||
println('cred: ${v}')
|
||||
@@ -18,7 +21,7 @@ deployment.add_machine(
|
||||
cpu: 1
|
||||
memory: 2
|
||||
planetary: false
|
||||
public_ip4: true
|
||||
public_ip4: false
|
||||
size: 10 // 10 gig
|
||||
mycelium: tfgrid3deployer.Mycelium{}
|
||||
)
|
||||
|
||||
@@ -8,29 +8,11 @@ To get started
|
||||
|
||||
|
||||
|
||||
import freeflowuniverse.herolib.installers.something. griddriver
|
||||
import freeflowuniverse.herolib.installers.threefold.griddriver
|
||||
|
||||
mut installer:= griddriver.get()!
|
||||
|
||||
installer.start()!
|
||||
|
||||
|
||||
|
||||
|
||||
```
|
||||
|
||||
## example heroscript
|
||||
|
||||
|
||||
```hero
|
||||
!!griddriver.install
|
||||
homedir: '/home/user/griddriver'
|
||||
username: 'admin'
|
||||
password: 'secretpassword'
|
||||
title: 'Some Title'
|
||||
host: 'localhost'
|
||||
port: 8888
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
36
lib/threefold/tfgrid3deployer/filter.v
Normal file
36
lib/threefold/tfgrid3deployer/filter.v
Normal file
@@ -0,0 +1,36 @@
|
||||
module tfgrid3deployer
|
||||
|
||||
import freeflowuniverse.herolib.threefold.gridproxy
|
||||
import freeflowuniverse.herolib.threefold.gridproxy.model as gridproxy_models
|
||||
|
||||
|
||||
//TODO: put all code in relation to filtering in file filter.v
|
||||
@[params]
|
||||
pub struct FilterNodesArgs {
|
||||
gridproxy_models.NodeFilter
|
||||
pub:
|
||||
on_hetzner bool
|
||||
}
|
||||
|
||||
pub fn filter_nodes(args FilterNodesArgs) ![]gridproxy_models.Node {
|
||||
// Resolve the network configuration
|
||||
net := resolve_network()!
|
||||
|
||||
// Create grid proxy client and retrieve the matching nodes
|
||||
mut gp_client := gridproxy.new(net: net, cache: true)!
|
||||
|
||||
mut filter := args.NodeFilter
|
||||
if args.on_hetzner {
|
||||
filter.features << ['zmachine-light']
|
||||
}
|
||||
|
||||
nodes := gp_client.get_nodes(filter)!
|
||||
return nodes
|
||||
}
|
||||
|
||||
// fn get_hetzner_node_ids(nodes []gridproxy_models.Node) ![]u64 {
|
||||
// // get farm ids that are know to be hetzner's
|
||||
// // if we need to iterate over all nodes, maybe we should use multi-threading
|
||||
// panic('Not Implemented')
|
||||
// return []
|
||||
// }
|
||||
@@ -2,6 +2,9 @@ module tfgrid3deployer
|
||||
|
||||
import freeflowuniverse.herolib.core.base
|
||||
import freeflowuniverse.herolib.core.playbook
|
||||
import freeflowuniverse.herolib.ui.console
|
||||
import freeflowuniverse.herolib.installers.threefold.griddriver
|
||||
|
||||
|
||||
__global (
|
||||
tfgrid3deployer_global map[string]&TFGridDeployer
|
||||
@@ -18,9 +21,6 @@ pub mut:
|
||||
|
||||
fn args_get(args_ ArgsGet) ArgsGet {
|
||||
mut args := args_
|
||||
if args.name == '' {
|
||||
args.name = tfgrid3deployer_default
|
||||
}
|
||||
if args.name == '' {
|
||||
args.name = 'default'
|
||||
}
|
||||
@@ -28,43 +28,55 @@ fn args_get(args_ ArgsGet) ArgsGet {
|
||||
}
|
||||
|
||||
pub fn get(args_ ArgsGet) !&TFGridDeployer {
|
||||
|
||||
mut installer:=griddriver.get()!
|
||||
installer.install()!
|
||||
|
||||
mut context := base.context()!
|
||||
mut args := args_get(args_)
|
||||
mut obj := TFGridDeployer{}
|
||||
if args.name !in tfgrid3deployer_global {
|
||||
if args.name == 'default' {
|
||||
if !config_exists(args) {
|
||||
if default {
|
||||
config_save(args)!
|
||||
}
|
||||
}
|
||||
config_load(args)!
|
||||
if !exists(args)! {
|
||||
set(obj)!
|
||||
} else {
|
||||
heroscript := context.hero_config_get('tfgrid3deployer', args.name)!
|
||||
mut obj_ := heroscript_loads(heroscript)!
|
||||
set_in_mem(obj_)!
|
||||
}
|
||||
}
|
||||
return tfgrid3deployer_global[args.name] or {
|
||||
println(tfgrid3deployer_global)
|
||||
panic('could not get config for tfgrid3deployer with name:${args.name}')
|
||||
// bug if we get here because should be in globals
|
||||
panic('could not get config for tfgrid3deployer with name, is bug:${args.name}')
|
||||
}
|
||||
}
|
||||
|
||||
fn config_exists(args_ ArgsGet) bool {
|
||||
// register the config for the future
|
||||
pub fn set(o TFGridDeployer) ! {
|
||||
set_in_mem(o)!
|
||||
mut context := base.context()!
|
||||
heroscript := heroscript_dumps(o)!
|
||||
context.hero_config_set('tfgrid3deployer', o.name, heroscript)!
|
||||
}
|
||||
|
||||
// does the config exists?
|
||||
pub fn exists(args_ ArgsGet) !bool {
|
||||
mut context := base.context()!
|
||||
mut args := args_get(args_)
|
||||
mut context := base.context() or { panic('bug') }
|
||||
return context.hero_config_exists('tfgrid3deployer', args.name)
|
||||
}
|
||||
|
||||
fn config_load(args_ ArgsGet) ! {
|
||||
pub fn delete(args_ ArgsGet) ! {
|
||||
mut args := args_get(args_)
|
||||
mut context := base.context()!
|
||||
mut heroscript := context.hero_config_get('tfgrid3deployer', args.name)!
|
||||
play(heroscript: heroscript)!
|
||||
context.hero_config_delete('tfgrid3deployer', args.name)!
|
||||
if args.name in tfgrid3deployer_global {
|
||||
// del tfgrid3deployer_global[args.name]
|
||||
}
|
||||
}
|
||||
|
||||
fn config_save(args_ ArgsGet) ! {
|
||||
mut args := args_get(args_)
|
||||
mut context := base.context()!
|
||||
context.hero_config_set('tfgrid3deployer', args.name, heroscript_default()!)!
|
||||
}
|
||||
|
||||
fn set(o TFGridDeployer) ! {
|
||||
// only sets in mem, does not set as config
|
||||
fn set_in_mem(o TFGridDeployer) ! {
|
||||
mut o2 := obj_init(o)!
|
||||
tfgrid3deployer_global[o.name] = &o2
|
||||
tfgrid3deployer_default = o.name
|
||||
@@ -81,16 +93,14 @@ pub mut:
|
||||
pub fn play(args_ PlayArgs) ! {
|
||||
mut args := args_
|
||||
|
||||
if args.heroscript == '' {
|
||||
args.heroscript = heroscript_default()!
|
||||
}
|
||||
mut plbook := args.plbook or { playbook.new(text: args.heroscript)! }
|
||||
|
||||
mut install_actions := plbook.find(filter: 'tfgrid3deployer.configure')!
|
||||
if install_actions.len > 0 {
|
||||
for install_action in install_actions {
|
||||
mut p := install_action.params
|
||||
cfg_play(p)!
|
||||
heroscript := install_action.heroscript()
|
||||
mut obj2 := heroscript_loads(heroscript)!
|
||||
set(obj2)!
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -99,3 +109,10 @@ pub fn play(args_ PlayArgs) ! {
|
||||
pub fn switch(name string) {
|
||||
tfgrid3deployer_default = name
|
||||
}
|
||||
|
||||
// helpers
|
||||
|
||||
@[params]
|
||||
pub struct DefaultConfigArgs {
|
||||
instance string = 'default'
|
||||
}
|
||||
|
||||
@@ -1,28 +1,15 @@
|
||||
module tfgrid3deployer
|
||||
|
||||
import freeflowuniverse.herolib.data.paramsparser
|
||||
import freeflowuniverse.herolib.data.encoderhero
|
||||
import os
|
||||
|
||||
pub const version = '1.0.0'
|
||||
const singleton = false
|
||||
const default = true
|
||||
|
||||
pub fn heroscript_default() !string {
|
||||
ssh_key := os.getenv_opt('SSH_KEY') or { '' }
|
||||
mnemonic := os.getenv_opt('TFGRID_MNEMONIC') or { '' }
|
||||
network := os.getenv_opt('TFGRID_NETWORK') or { 'main' } // main,test,dev,qa
|
||||
heroscript := "
|
||||
!!tfgrid3deployer.configure name:'default'
|
||||
ssh_key: '${ssh_key}'
|
||||
mnemonic: '${mnemonic}'
|
||||
network: ${network}
|
||||
// THIS THE THE SOURCE OF THE INFORMATION OF THIS FILE, HERE WE HAVE THE CONFIG OBJECT CONFIGURED AND MODELLED
|
||||
|
||||
"
|
||||
if ssh_key.len == 0 || mnemonic.len == 0 || network.len == 0 {
|
||||
return error('please configure the tfgrid deployer or set SSH_KEY, TFGRID_MNEMONIC, and TFGRID_NETWORK.')
|
||||
}
|
||||
return heroscript
|
||||
}
|
||||
|
||||
pub enum Network {
|
||||
dev
|
||||
@@ -31,6 +18,7 @@ pub enum Network {
|
||||
qa
|
||||
}
|
||||
|
||||
@[heap]
|
||||
pub struct TFGridDeployer {
|
||||
pub mut:
|
||||
name string = 'default'
|
||||
@@ -39,25 +27,51 @@ pub mut:
|
||||
network Network
|
||||
}
|
||||
|
||||
fn cfg_play(p paramsparser.Params) ! {
|
||||
network_str := p.get_default('network', 'main')!
|
||||
network := match network_str {
|
||||
'dev' { Network.dev }
|
||||
'test' { Network.test }
|
||||
'qa' { Network.qa }
|
||||
else { Network.main }
|
||||
}
|
||||
|
||||
mut mycfg := TFGridDeployer{
|
||||
ssh_key: p.get_default('ssh_key', '')!
|
||||
mnemonic: p.get_default('mnemonic', '')!
|
||||
network: network
|
||||
// your checking & initialization code if needed
|
||||
fn obj_init(mycfg_ TFGridDeployer) !TFGridDeployer {
|
||||
mut mycfg := mycfg_
|
||||
ssh_key := os.getenv_opt('SSH_KEY') or { '' }
|
||||
if ssh_key.len>0{
|
||||
mycfg.ssh_key = ssh_key
|
||||
}
|
||||
set(mycfg)!
|
||||
mnemonic := os.getenv_opt('TFGRID_MNEMONIC') or { '' }
|
||||
if mnemonic.len>0{
|
||||
mycfg.mnemonic = mnemonic
|
||||
}
|
||||
network := os.getenv_opt('TFGRID_NETWORK') or { 'main' } //
|
||||
if network.len>0{
|
||||
match network {
|
||||
"main"{
|
||||
mycfg.network = .main
|
||||
} "dev" {
|
||||
mycfg.network = .dev
|
||||
} "test" {
|
||||
mycfg.network = .test
|
||||
} "qa" {
|
||||
mycfg.network = .qa
|
||||
}else{
|
||||
return error("can't find network with type; ${network}")
|
||||
}
|
||||
}
|
||||
}
|
||||
if mycfg.ssh_key.len == 0 {
|
||||
return error('ssh_key cannot be empty')
|
||||
}
|
||||
if mycfg.mnemonic.len == 0 {
|
||||
return error('mnemonic cannot be empty')
|
||||
}
|
||||
// println(mycfg)
|
||||
return mycfg
|
||||
}
|
||||
|
||||
fn obj_init(obj_ TFGridDeployer) !TFGridDeployer {
|
||||
// never call get here, only thing we can do here is work on object itself
|
||||
mut obj := obj_
|
||||
/////////////NORMALLY NO NEED TO TOUCH
|
||||
|
||||
pub fn heroscript_dumps(obj TFGridDeployer) !string {
|
||||
return encoderhero.encode[TFGridDeployer](obj)!
|
||||
}
|
||||
|
||||
pub fn heroscript_loads(heroscript string) !TFGridDeployer {
|
||||
mut obj := encoderhero.decode[TFGridDeployer](heroscript)!
|
||||
return obj
|
||||
}
|
||||
|
||||
@@ -32,46 +32,14 @@ fn wireguard_routing_ip(ip string) string {
|
||||
return '100.64.${parts[1]}.${parts[2]}/32'
|
||||
}
|
||||
|
||||
/*
|
||||
* Just generate a hex key for the mycelium network
|
||||
*/
|
||||
fn get_mycelium() grid_models.Mycelium {
|
||||
// Creates a new mycelium address with a randomly generated hex key
|
||||
pub fn (mut deployer TFGridDeployer) mycelium_address_create() grid_models.Mycelium {
|
||||
return grid_models.Mycelium{
|
||||
hex_key: rand.string(32).bytes().hex()
|
||||
peers: []
|
||||
peers: []
|
||||
}
|
||||
}
|
||||
|
||||
@[params]
|
||||
pub struct FilterNodesArgs {
|
||||
gridproxy_models.NodeFilter
|
||||
pub:
|
||||
on_hetzner bool
|
||||
}
|
||||
|
||||
pub fn filter_nodes(args FilterNodesArgs) ![]gridproxy_models.Node {
|
||||
// Resolve the network configuration
|
||||
net := resolve_network()!
|
||||
|
||||
// Create grid proxy client and retrieve the matching nodes
|
||||
mut gp_client := gridproxy.new(net: net, cache: true)!
|
||||
|
||||
mut filter := args.NodeFilter
|
||||
if args.on_hetzner {
|
||||
filter.features << ['zmachine-light']
|
||||
}
|
||||
|
||||
nodes := gp_client.get_nodes(filter)!
|
||||
return nodes
|
||||
}
|
||||
|
||||
// fn get_hetzner_node_ids(nodes []gridproxy_models.Node) ![]u64 {
|
||||
// // get farm ids that are know to be hetzner's
|
||||
// // if we need to iterate over all nodes, maybe we should use multi-threading
|
||||
// panic('Not Implemented')
|
||||
// return []
|
||||
// }
|
||||
|
||||
fn convert_to_gigabytes(bytes u64) u64 {
|
||||
return bytes * 1024 * 1024 * 1024
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ pub mut:
|
||||
description string
|
||||
mode grid_models.ZdbMode = 'user'
|
||||
public bool
|
||||
use_hetzner_node bool
|
||||
}
|
||||
|
||||
pub struct ZDB {
|
||||
|
||||
Reference in New Issue
Block a user