fix: Fix docker examples

- Moved `httpconnection` import from `clients` to `core`.
- Changed `tfgrid-sdk-ts` dashboard to playground.
- Added ipaddr to node_local().
- Added public keyword to OpenSSLGenerateArgs.
- Improved DockerEngine image and container loading.
- Added utils.contains_ssh_port.
- Improved error handling in DockerEngine.
- Improved Docker registry handling.

Co-authored-by: mariobassem12 <mariobassem12@gmail.com>
Co-authored-by: omda <mahmmoud.hassanein@gmail.com>
This commit is contained in:
2025-01-28 14:06:24 +01:00
parent ca8799af39
commit 8b0f692673
17 changed files with 70 additions and 51 deletions

View File

@@ -1,7 +1,7 @@
module dagu module dagu
// import os // import os
import freeflowuniverse.herolib.clients.httpconnection import freeflowuniverse.herolib.core.httpconnection
import os import os
struct GiteaClient[T] { struct GiteaClient[T] {

0
examples/virt/docker/docker_dev_tools.vsh Normal file → Executable file
View File

0
examples/virt/docker/docker_init.vsh Normal file → Executable file
View File

0
examples/virt/docker/docker_registry.vsh Normal file → Executable file
View File

0
examples/virt/docker/presearch_docker.vsh Normal file → Executable file
View File

12
examples/virt/docker/tf_dashboard.vsh Normal file → Executable file
View File

@@ -17,25 +17,25 @@ recipe.add_run(cmd: 'npm i -g yarn')!
recipe.add_run( recipe.add_run(
cmd: ' cmd: '
git clone https://github.com/threefoldtech/tfgrid-sdk-ts.git /app git clone https://github.com/threefoldtech/tfgrid-sdk-ts.git /app
cd /app/packages/dashboard cd /app/packages/playground
yarn install yarn install
yarn lerna run build --no-private yarn lerna run build --no-private
yarn workspace @threefold/dashboard build yarn workspace @threefold/playground build
' '
)! )!
recipe.add_run( recipe.add_run(
cmd: ' cmd: '
rm /etc/nginx/conf.d/default.conf rm /etc/nginx/conf.d/default.conf
cp /app/packages/dashboard/nginx.conf /etc/nginx/conf.d cp /app/packages/playground/nginx.conf /etc/nginx/conf.d
apk add --no-cache bash apk add --no-cache bash
chmod +x /app/packages/dashboard/scripts/build-env.sh chmod +x /app/packages/playground/scripts/build-env.sh
cp -r /app/packages/dashboard/dist /usr/share/nginx/html cp -r /app/packages/playground/dist /usr/share/nginx/html
' '
)! )!
recipe.add_run(cmd: 'echo "daemon off;" >> /etc/nginx/nginx.conf')! recipe.add_run(cmd: 'echo "daemon off;" >> /etc/nginx/nginx.conf')!
recipe.add_cmd(cmd: '/bin/bash -c /app/packages/dashboard/scripts/build-env.sh')! recipe.add_cmd(cmd: '/bin/bash -c /app/packages/playground/scripts/build-env.sh')!
recipe.add_entrypoint(cmd: 'nginx')! recipe.add_entrypoint(cmd: 'nginx')!
recipe.build(false)! recipe.build(false)!

View File

@@ -4,7 +4,10 @@ import freeflowuniverse.herolib.data.ipaddress
// get node connection to local machine // get node connection to local machine
pub fn (mut bldr BuilderFactory) node_local() !&Node { pub fn (mut bldr BuilderFactory) node_local() !&Node {
return bldr.node_new(name: 'localhost') return bldr.node_new(
name: 'localhost'
ipaddr: '127.0.0.1'
)
} }
// format ipaddr: localhost:7777 . // format ipaddr: localhost:7777 .
@@ -64,7 +67,6 @@ pub fn (mut bldr BuilderFactory) node_new(args_ NodeArguments) !&Node {
mut iadd := ipaddress.new(args.ipaddr)! mut iadd := ipaddress.new(args.ipaddr)!
node.name = iadd.toname()! node.name = iadd.toname()!
} }
wasincache := node.load()! wasincache := node.load()!
if wasincache && args.reload { if wasincache && args.reload {

View File

@@ -5,6 +5,7 @@ import json
@[params] @[params]
pub struct OpenSSLGenerateArgs { pub struct OpenSSLGenerateArgs {
pub:
name string = 'default' name string = 'default'
domain string = 'myregistry.domain.com' domain string = 'myregistry.domain.com'
reset bool reset bool
@@ -22,6 +23,7 @@ pub fn (mut ossl OpenSSL) generate(args OpenSSLGenerateArgs) !OpenSSLKey {
' '
mut b := builder.new()! mut b := builder.new()!
println('b: ${b}')
mut node := b.node_local()! mut node := b.node_local()!
node.exec(cmd: cmd)! node.exec(cmd: cmd)!

View File

@@ -4,7 +4,7 @@ import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.osal.screen import freeflowuniverse.herolib.osal.screen
import freeflowuniverse.herolib.ui.console import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.core.texttools import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.clients.httpconnection import freeflowuniverse.herolib.core.httpconnection
import os import os
@[params] @[params]

View File

@@ -1,6 +1,6 @@
module rmb module rmb
// import freeflowuniverse.herolib.clients.httpconnection // import freeflowuniverse.herolib.core.httpconnection
import freeflowuniverse.herolib.core.redisclient { RedisURL } import freeflowuniverse.herolib.core.redisclient { RedisURL }
import os import os

View File

@@ -2,7 +2,7 @@ module zerohub
import net.http import net.http
// import freeflowuniverse.herolib.clients.httpconnection // import freeflowuniverse.herolib.core.httpconnection
// TODO: curl -H "Authorization: bearer 6Pz6giOpHSaA3KdYI6LLpGSLmDmzmRkVdwvc7S-E5PVB0-iRfgDKW9Rb_ZTlj-xEW4_uSCa5VsyoRsML7DunA1sia3Jpc3RvZi4zYm90IiwgMTY3OTIxNTc3MF0=" https://hub.grid.tf/api/flist/ // TODO: curl -H "Authorization: bearer 6Pz6giOpHSaA3KdYI6LLpGSLmDmzmRkVdwvc7S-E5PVB0-iRfgDKW9Rb_ZTlj-xEW4_uSCa5VsyoRsML7DunA1sia3Jpc3RvZi4zYm90IiwgMTY3OTIxNTc3MF0=" https://hub.grid.tf/api/flist/

View File

@@ -1,6 +1,7 @@
module docker module docker
import freeflowuniverse.herolib.osal { exec } import freeflowuniverse.herolib.osal { exec }
import freeflowuniverse.herolib.virt.utils
pub fn (mut e DockerEngine) container_create(args DockerContainerCreateArgs) !&DockerContainer { pub fn (mut e DockerEngine) container_create(args DockerContainerCreateArgs) !&DockerContainer {
mut ports := '' mut ports := ''
@@ -33,7 +34,7 @@ pub fn (mut e DockerEngine) container_create(args DockerContainerCreateArgs) !&D
privileged := if args.privileged { '--privileged' } else { '' } privileged := if args.privileged { '--privileged' } else { '' }
// if forwarded ports passed in the args not containing mapping tp ssh (22) create one // if forwarded ports passed in the args not containing mapping tp ssh (22) create one
if !contains_ssh_port(args.forwarded_ports) { if !utils.contains_ssh_port(args.forwarded_ports) {
// find random free port in the node // find random free port in the node
mut port := e.get_free_port() or { panic('No free port.') } mut port := e.get_free_port() or { panic('No free port.') }
ports += '-p ${port}:22/tcp' ports += '-p ${port}:22/tcp'

View File

@@ -4,6 +4,7 @@ import freeflowuniverse.herolib.osal { exec }
import freeflowuniverse.herolib.core.texttools import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.virt.utils import freeflowuniverse.herolib.virt.utils
import freeflowuniverse.herolib.core import freeflowuniverse.herolib.core
import time
// import freeflowuniverse.herolib.installers.swarm // import freeflowuniverse.herolib.installers.swarm
@@ -54,6 +55,29 @@ pub fn (mut e DockerEngine) load() ! {
e.containers_load()! e.containers_load()!
} }
// load all images, they can be consulted in e.images
// see obj: DockerImage as result in e.images
pub fn (mut e DockerEngine) images_load() ! {
e.images = []DockerImage{}
mut lines := osal.execute_silent("docker images --format '{{.ID}}||{{.Repository}}||{{.Tag}}||{{.Digest}}||{{.Size}}||{{.CreatedAt}}'")!
for line in lines.split_into_lines() {
fields := line.split('||').map(utils.clear_str)
if fields.len != 6 {
panic('docker image needs to output 6 parts.\n${fields}')
}
mut image := DockerImage{
engine: &e
}
image.id = fields[0]
image.repo = fields[1]
image.tag = fields[2]
image.digest = utils.parse_digest(fields[3]) or { '' }
image.size = utils.parse_size_mb(fields[4]) or { 0 }
image.created = utils.parse_time(fields[5]) or { time.now() }
e.images << image
}
}
// load all containers, they can be consulted in e.containers // load all containers, they can be consulted in e.containers
// see obj: DockerContainer as result in e.containers // see obj: DockerContainer as result in e.containers
pub fn (mut e DockerEngine) containers_load() ! { pub fn (mut e DockerEngine) containers_load() ! {
@@ -65,7 +89,7 @@ pub fn (mut e DockerEngine) containers_load() ! {
stdout: false stdout: false
)! )!
lines := ljob.output lines := ljob.output
for line in lines { for line in lines.split_into_lines() {
if line.trim_space() == '' { if line.trim_space() == '' {
continue continue
} }
@@ -142,22 +166,23 @@ pub fn (err ContainerGetError) code() int {
// image_id string // image_id string
pub fn (mut e DockerEngine) containers_get(args_ ContainerGetArgs) ![]&DockerContainer { pub fn (mut e DockerEngine) containers_get(args_ ContainerGetArgs) ![]&DockerContainer {
mut args := args_ mut args := args_
args.name = texttools.name_fix(args.name) e.containers_load()!
mut res := []&DockerContainer{} mut res := []&DockerContainer{}
for _, c in e.containers { for i, c in e.containers {
container := c
if args.name.contains('*') || args.name.contains('?') || args.name.contains('[') { if args.name.contains('*') || args.name.contains('?') || args.name.contains('[') {
if c.name.match_glob(args.name) { if container.name.match_glob(args.name) {
res << &c res << &e.containers[i]
continue continue
} }
} else { } else {
if c.name == args.name || c.id == args.id { if container.name == args.name || container.id == args.id {
res << &c res << &e.containers[i]
continue continue
} }
} }
if args.image_id.len > 0 && c.image.id == args.image_id { if args.image_id.len > 0 && container.image.id == args.image_id {
res << &c res << &e.containers[i]
} }
} }
if res.len == 0 { if res.len == 0 {
@@ -172,8 +197,8 @@ pub fn (mut e DockerEngine) containers_get(args_ ContainerGetArgs) ![]&DockerCon
// get container from memory, can use match_glob see https://modules.vlang.io/index.html#string.match_glob // get container from memory, can use match_glob see https://modules.vlang.io/index.html#string.match_glob
pub fn (mut e DockerEngine) container_get(args_ ContainerGetArgs) !&DockerContainer { pub fn (mut e DockerEngine) container_get(args_ ContainerGetArgs) !&DockerContainer {
mut args := args_ mut args := args_
args.name = texttools.name_fix(args.name)
mut res := e.containers_get(args)! mut res := e.containers_get(args)!
if res.len > 1 { if res.len > 1 {
return ContainerGetError{ return ContainerGetError{
args: args args: args
@@ -211,7 +236,7 @@ pub fn (mut e DockerEngine) containers_delete(args ContainerGetArgs) ! {
// import a container into an image, run docker container with it // import a container into an image, run docker container with it
// image_repo examples ['myimage', 'myimage:latest'] // image_repo examples ['myimage', 'myimage:latest']
// if DockerContainerCreateArgs contains a name, container will be created and restarted // if DockerContainerCreateArgs contains a name, container will be created and restarted
pub fn (mut e DockerEngine) container_import(path string, mut args DockerContainerCreateArgs) !&DockerContainer { pub fn (mut e DockerEngine) container_import(path string, args DockerContainerCreateArgs) !&DockerContainer {
mut image := args.image_repo mut image := args.image_repo
if args.image_tag != '' { if args.image_tag != '' {
image = image + ':${args.image_tag}' image = image + ':${args.image_tag}'

View File

@@ -89,7 +89,7 @@ pub fn (err ImageGetError) code() int {
pub fn (mut e DockerEngine) image_get(args ImageGetArgs) !&DockerImage { pub fn (mut e DockerEngine) image_get(args ImageGetArgs) !&DockerImage {
mut counter := 0 mut counter := 0
mut result_digest := '' mut result_digest := ''
for i in e.images { for mut i in e.images {
if args.digest == args.digest { if args.digest == args.digest {
return &i return &i
} }

View File

@@ -19,13 +19,10 @@ pub mut:
pub fn (mut r DockerBuilderRecipe) add_codeget(args_ CodeGetArgs) ! { pub fn (mut r DockerBuilderRecipe) add_codeget(args_ CodeGetArgs) ! {
mut args := args_ mut args := args_
mut gs := gittools.get(coderoot: '${r.path()}/code')! mut gs := gittools.get(coderoot: '${r.path()}/code')!
mut gr := gs.get_repo(url: args.url, pull: args.pull, reset: args.reset)!
locator := gs.locator_new(args.url)!
mut gr := gs.repo_get(locator: locator, pull: args.pull, reset: args.reset)!
if args.name == '' { if args.name == '' {
args.name = gr.addr.name args.name = gr.name
} }
if args.dest == '' { if args.dest == '' {
@@ -43,7 +40,7 @@ pub fn (mut r DockerBuilderRecipe) add_codeget(args_ CodeGetArgs) ! {
return error("dest is to short (min 3): now '${args.dest}'") return error("dest is to short (min 3): now '${args.dest}'")
} }
commonpath := gr.path_relative() commonpath := gr.path()
if commonpath.contains('..') { if commonpath.contains('..') {
panic('bug should not be') panic('bug should not be')
} }

View File

@@ -1,8 +1,8 @@
module docker module docker
import freeflowuniverse.herolib.crypt.openssl import freeflowuniverse.herolib.crypt.openssl
import freeflowuniverse.herolib.clients.httpconnection import freeflowuniverse.herolib.core.httpconnection
import freeflowuniverse.herolib.osal { exec } import freeflowuniverse.herolib.osal
import os import os
import freeflowuniverse.herolib.ui.console import freeflowuniverse.herolib.ui.console
@@ -84,30 +84,22 @@ pub fn (mut e DockerEngine) registry_add(args DockerRegistryArgs) ! {
e.registries << registry e.registries << registry
// delete all previous containers, uses wildcards see https://modules.vlang.io/index.html#string.match_glob // delete all previous containers, uses wildcards see https://modules.vlang.io/index.html#string.match_glob
e.container_delete(name: 'docker_registry*')!
e.container_delete(name: 'docker_registry*') or {
if !(err as ContainerGetError).notfound {
return err
}
println('No containers to matching docker registry')
}
composer.start()! composer.start()!
exec(cmd: 'curl https://localhost:5000/v2/ -k', retry: 4) or {
return error('could not start docker registry, did not answer')
}
mut conn := httpconnection.new( mut conn := httpconnection.new(
name: 'localdockerhub' name: 'localdockerhub'
url: 'https://localhost:5000/v2/' url: 'https://localhost:5000/v2/'
retry: 10 retry: 10
)! )!
// r := conn.get_json_dict(mut prefix: 'errors')! res := conn.get()!
println(res)
// r := conn.get_json_dict(mut prefix: 'errors')!
r := conn.get(method: .get)!
console.print_debug('Sdsd')
console.print_debug(r)
if true {
panic('sdsd')
}
// now we need to check if we can connect
} }

View File

@@ -1,7 +1,7 @@
module hetzner module hetzner
import freeflowuniverse.herolib.data.paramsparser import freeflowuniverse.herolib.data.paramsparser
import freeflowuniverse.herolib.clients.httpconnection import freeflowuniverse.herolib.core.httpconnection
pub const version = '1.14.3' pub const version = '1.14.3'
const singleton = false const singleton = false