Merge pull request #41 from freeflowuniverse/development_docker

Fix docker examples
This commit is contained in:
2025-01-28 17:44:55 +02:00
committed by GitHub
24 changed files with 412 additions and 115 deletions

View File

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

11
examples/installers/docker.vsh Executable file
View File

@@ -0,0 +1,11 @@
#!/usr/bin/env -S v -n -w -gc none -no-retry-compilation -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.installers.virt.docker as docker_installer
mut docker := docker_installer.get()!
// To install
docker.install()!
// To remove
docker.destroy()!

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(
cmd: '
git clone https://github.com/threefoldtech/tfgrid-sdk-ts.git /app
cd /app/packages/dashboard
cd /app/packages/playground
yarn install
yarn lerna run build --no-private
yarn workspace @threefold/dashboard build
yarn workspace @threefold/playground build
'
)!
recipe.add_run(
cmd: '
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
chmod +x /app/packages/dashboard/scripts/build-env.sh
cp -r /app/packages/dashboard/dist /usr/share/nginx/html
chmod +x /app/packages/playground/scripts/build-env.sh
cp -r /app/packages/playground/dist /usr/share/nginx/html
'
)!
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.build(false)!

View File

@@ -4,7 +4,10 @@ import freeflowuniverse.herolib.data.ipaddress
// get node connection to local machine
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 .
@@ -64,7 +67,6 @@ pub fn (mut bldr BuilderFactory) node_new(args_ NodeArguments) !&Node {
mut iadd := ipaddress.new(args.ipaddr)!
node.name = iadd.toname()!
}
wasincache := node.load()!
if wasincache && args.reload {

View File

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

View File

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

View File

@@ -0,0 +1,13 @@
!!hero_code.generate_installer
name:'docker'
classname:'DockerInstaller'
singleton:0
templates:0
default:1
title:''
supported_platforms:''
reset:0
startupmanager:1
hasconfig:0
build:0

View File

@@ -1,64 +0,0 @@
module docker
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.installers.base
import freeflowuniverse.herolib.ui.console
// install docker will return true if it was already installed
pub fn install_() ! {
console.print_header('package install install docker')
if core.platform() != .ubuntu {
return error('only support ubuntu for now')
}
base.install()!
if !osal.done_exists('install_docker') && !osal.cmd_exists('docker') {
// osal.upgrade()!
osal.package_install('mc,wget,htop,apt-transport-https,ca-certificates,curl,software-properties-common')!
cmd := '
rm -f /usr/share/keyrings/docker-archive-keyring.gpg
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
apt update
apt-cache policy docker-ce
#systemctl status docker
'
osal.execute_silent(cmd)!
osal.package_install('docker-ce')!
check()!
osal.done_set('install_docker', 'OK')!
}
console.print_header('docker already done')
}
pub fn check() ! {
// todo: do a monitoring check to see if it works
cmd := '
# Check if docker command exists
if ! command -v docker &> /dev/null; then
echo "Error: Docker command-line tool is not installed."
exit 1
fi
# Check if Docker daemon is running
if ! pgrep -f "dockerd" &> /dev/null; then
echo "Error: Docker daemon is not running."
exit 1
fi
# Run the hello-world Docker container
output=$(docker run hello-world 2>&1)
if [[ "\$output" == *"Hello from Docker!"* ]]; then
echo "Docker is installed and running properly."
else
echo "Error: Failed to run the Docker hello-world container."
echo "Output: \$output"
exit 1
fi
'
r := osal.execute_silent(cmd)!
console.print_debug(r)
}

View File

@@ -0,0 +1,106 @@
module docker
import freeflowuniverse.herolib.core
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.osal.zinit
import freeflowuniverse.herolib.installers.ulist
fn startupcmd() ![]zinit.ZProcessNewArgs {
mut res := []zinit.ZProcessNewArgs{}
res << zinit.ZProcessNewArgs{
name: 'docker'
cmd: 'dockerd'
}
return res
}
fn running() !bool {
console.print_header('Checking if Docker is running')
is_installed := installed() or {
return error('Cannot execute command docker, check if the docker is installed or call the `install()` method: ${err}')
}
if !is_installed {
return false
}
// Checking if the docker server responed
cmd := 'docker ps'
osal.execute_stdout(cmd) or { return false }
console.print_header('Docker is running')
return true
}
fn start_pre() ! {
}
fn start_post() ! {
}
fn stop_pre() ! {
}
fn stop_post() ! {
}
//////////////////// following actions are not specific to instance of the object
// checks if a certain version or above is installed
fn installed() !bool {
console.print_header('Checking if Docker is installed')
cmd := 'docker -v'
osal.execute_stdout(cmd) or { return false }
console.print_header('Docker is installed')
return true
}
// get the Upload List of the files
fn ulist_get() !ulist.UList {
return ulist.UList{}
}
// uploads to S3 server if configured
fn upload() ! {}
fn install() ! {
console.print_header('Installing Docker')
if core.platform()! != .ubuntu {
return error('only support ubuntu for now')
}
mut cmd := '
sudo apt-get update -y
sudo apt-get install -y ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "\$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update -y
'
osal.execute_stdout(cmd) or { return error('Cannot install docker due to: ${err}') }
cmd = 'sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin'
osal.execute_stdout(cmd) or { return error('Cannot install docker due to: ${err}') }
console.print_header('Docker installed sucessfully')
}
fn destroy() ! {
console.print_header('Removing Docker')
// Uninstall the Docker Engine, CLI, containerd, and Docker Compose packages:
mut cmd := 'sudo apt-get purge -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras'
osal.execute_stdout(cmd) or { return error('Cannot uninstall docker due to: ${err}') }
// Images, containers, volumes, or custom configuration files on your host aren't automatically removed. To delete all images, containers, and volumes:
cmd = 'sudo rm -rf /var/lib/docker && sudo rm -rf /var/lib/containerd'
osal.execute_stdout(cmd) or { return error('Cannot uninstall docker due to: ${err}') }
// Remove source list and keyrings
cmd = 'sudo rm /etc/apt/sources.list.d/docker.list && sudo rm /etc/apt/keyrings/docker.asc'
osal.execute_stdout(cmd) or { return error('Cannot uninstall docker due to: ${err}') }
console.print_header('Docker is removed')
}

View File

@@ -0,0 +1,147 @@
module docker
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.sysadmin.startupmanager
import freeflowuniverse.herolib.osal.zinit
import time
__global (
docker_global map[string]&DockerInstaller
docker_default string
)
/////////FACTORY
@[params]
pub struct ArgsGet {
pub mut:
name string
}
pub fn get(args_ ArgsGet) !&DockerInstaller {
return &DockerInstaller{}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS ///////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
fn startupmanager_get(cat zinit.StartupManagerType) !startupmanager.StartupManager {
// unknown
// screen
// zinit
// tmux
// systemd
match cat {
.zinit {
console.print_debug('startupmanager: zinit')
return startupmanager.get(cat: .zinit)!
}
.systemd {
console.print_debug('startupmanager: systemd')
return startupmanager.get(cat: .systemd)!
}
else {
console.print_debug('startupmanager: auto')
return startupmanager.get()!
}
}
}
pub fn (mut self DockerInstaller) start() ! {
switch(self.name)
if self.running()! {
return
}
console.print_header('docker start')
if !installed()! {
install()!
}
configure()!
start_pre()!
for zprocess in startupcmd()! {
mut sm := startupmanager_get(zprocess.startuptype)!
console.print_debug('starting docker with ${zprocess.startuptype}...')
sm.new(zprocess)!
sm.start(zprocess.name)!
}
start_post()!
for _ in 0 .. 50 {
if self.running()! {
return
}
time.sleep(100 * time.millisecond)
}
return error('docker did not install properly.')
}
pub fn (mut self DockerInstaller) install_start(args InstallArgs) ! {
switch(self.name)
self.install(args)!
self.start()!
}
pub fn (mut self DockerInstaller) stop() ! {
switch(self.name)
stop_pre()!
for zprocess in startupcmd()! {
mut sm := startupmanager_get(zprocess.startuptype)!
sm.stop(zprocess.name)!
}
stop_post()!
}
pub fn (mut self DockerInstaller) restart() ! {
switch(self.name)
self.stop()!
self.start()!
}
pub fn (mut self DockerInstaller) running() !bool {
switch(self.name)
// walk over the generic processes, if not running return
for zprocess in startupcmd()! {
mut sm := startupmanager_get(zprocess.startuptype)!
r := sm.running(zprocess.name)!
if r == false {
return false
}
}
return running()!
}
@[params]
pub struct InstallArgs {
pub mut:
reset bool
}
pub fn (mut self DockerInstaller) install(args InstallArgs) ! {
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
}
pub fn (mut self DockerInstaller) destroy() ! {
switch(self.name)
self.stop() or {}
destroy()!
}
// switch instance to be used for docker
pub fn switch(name string) {
docker_default = name
}

View File

@@ -0,0 +1,23 @@
module docker
pub const version = '1.14.3'
const singleton = false
const default = true
// THIS THE THE SOURCE OF THE INFORMATION OF THIS FILE, HERE WE HAVE THE CONFIG OBJECT CONFIGURED AND MODELLED
@[heap]
pub struct DockerInstaller {
pub mut:
name string = 'default'
}
fn obj_init(obj_ DockerInstaller) !DockerInstaller {
// never call get here, only thing we can do here is work on object itself
mut obj := obj_
return obj
}
// called before start if done
fn configure() ! {
// mut installer := get()!
}

View File

@@ -0,0 +1,42 @@
# docker
To get started
```vlang
import freeflowuniverse.herolib.installers.something.docker as docker_installer
heroscript:="
!!docker.configure name:'test'
password: '1234'
port: 7701
!!docker.start name:'test' reset:1
"
docker_installer.play(heroscript=heroscript)!
//or we can call the default and do a start with reset
//mut installer:= docker_installer.get()!
//installer.start(reset:true)!
```
## example heroscript
```hero
!!docker.configure
homedir: '/home/user/docker'
username: 'admin'
password: 'secretpassword'
title: 'Some Title'
host: 'localhost'
port: 8888
```

View File

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

View File

@@ -2,7 +2,7 @@ module zerohub
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/

View File

@@ -1,6 +1,7 @@
module docker
import freeflowuniverse.herolib.osal { exec }
import freeflowuniverse.herolib.virt.utils
pub fn (mut e DockerEngine) container_create(args DockerContainerCreateArgs) !&DockerContainer {
mut ports := ''
@@ -33,7 +34,7 @@ pub fn (mut e DockerEngine) container_create(args DockerContainerCreateArgs) !&D
privileged := if args.privileged { '--privileged' } else { '' }
// 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
mut port := e.get_free_port() or { panic('No free port.') }
ports += '-p ${port}:22/tcp'

View File

@@ -4,6 +4,7 @@ import freeflowuniverse.herolib.osal { exec }
import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.virt.utils
import freeflowuniverse.herolib.core
import time
// import freeflowuniverse.herolib.installers.swarm
@@ -54,6 +55,29 @@ pub fn (mut e DockerEngine) 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
// see obj: DockerContainer as result in e.containers
pub fn (mut e DockerEngine) containers_load() ! {
@@ -65,7 +89,7 @@ pub fn (mut e DockerEngine) containers_load() ! {
stdout: false
)!
lines := ljob.output
for line in lines {
for line in lines.split_into_lines() {
if line.trim_space() == '' {
continue
}
@@ -142,22 +166,23 @@ pub fn (err ContainerGetError) code() int {
// image_id string
pub fn (mut e DockerEngine) containers_get(args_ ContainerGetArgs) ![]&DockerContainer {
mut args := args_
args.name = texttools.name_fix(args.name)
e.containers_load()!
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 c.name.match_glob(args.name) {
res << &c
if container.name.match_glob(args.name) {
res << &e.containers[i]
continue
}
} else {
if c.name == args.name || c.id == args.id {
res << &c
if container.name == args.name || container.id == args.id {
res << &e.containers[i]
continue
}
}
if args.image_id.len > 0 && c.image.id == args.image_id {
res << &c
if args.image_id.len > 0 && container.image.id == args.image_id {
res << &e.containers[i]
}
}
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
pub fn (mut e DockerEngine) container_get(args_ ContainerGetArgs) !&DockerContainer {
mut args := args_
args.name = texttools.name_fix(args.name)
mut res := e.containers_get(args)!
if res.len > 1 {
return ContainerGetError{
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
// image_repo examples ['myimage', 'myimage:latest']
// 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
if 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 {
mut counter := 0
mut result_digest := ''
for i in e.images {
for mut i in e.images {
if args.digest == args.digest {
return &i
}

View File

@@ -19,13 +19,10 @@ pub mut:
pub fn (mut r DockerBuilderRecipe) add_codeget(args_ CodeGetArgs) ! {
mut args := args_
mut gs := gittools.get(coderoot: '${r.path()}/code')!
locator := gs.locator_new(args.url)!
mut gr := gs.repo_get(locator: locator, pull: args.pull, reset: args.reset)!
mut gr := gs.get_repo(url: args.url, pull: args.pull, reset: args.reset)!
if args.name == '' {
args.name = gr.addr.name
args.name = gr.name
}
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}'")
}
commonpath := gr.path_relative()
commonpath := gr.path()
if commonpath.contains('..') {
panic('bug should not be')
}

View File

@@ -1,8 +1,8 @@
module docker
import freeflowuniverse.herolib.crypt.openssl
import freeflowuniverse.herolib.clients.httpconnection
import freeflowuniverse.herolib.osal { exec }
import freeflowuniverse.herolib.core.httpconnection
import freeflowuniverse.herolib.osal
import os
import freeflowuniverse.herolib.ui.console
@@ -84,30 +84,22 @@ pub fn (mut e DockerEngine) registry_add(args DockerRegistryArgs) ! {
e.registries << registry
// 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()!
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(
name: 'localdockerhub'
url: 'https://localhost:5000/v2/'
retry: 10
)!
// r := conn.get_json_dict(mut prefix: 'errors')!
// 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
res := conn.get()!
println(res)
}

View File

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