test with docker-api (no oficial) lib

This commit is contained in:
2023-06-12 19:43:14 +02:00
commit 2b0d2b07d1
11 changed files with 2108 additions and 0 deletions

52
src/conf/mod.rs Normal file
View File

@@ -0,0 +1,52 @@
use std::net::{Ipv4Addr, SocketAddrV4};
use rusqlite::{Connection, Result};
const PATH: &str = "mdeploy.db";
pub struct ConfServer{
ip: String,
port: String,
ip_top_limit: u8,
ip_base_limit: u8,
}
pub struct MInstance {
id: i64,
ip: Ipv4Addr,
}
pub struct instance {
con: Connection,
}
impl instance {
pub fn new() -> Result<Self> {
let con = Connection::open(PATH)?;
let ret = Self {
con : con,
};
ret.create_table()?;
Ok(ret)
}
fn create_table(&self) -> Result<()> {
self.con.execute_batch(
"BEGIN;
CREATE TABLE IF NOT EXISTS images(
id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE);
CREATE TABLE IF NOT EXISTS l_instances(
id INTEGER PRIMARY KEY AUTOINCREMENT,
ip TEXT,
image INTEGER
FOREIGN KEY(INTEGER) REFERENCES images(id));
COMMIT;"
)
}
fn insert_image(&self, image: String){
}
}

45
src/create_container.rs Normal file
View File

@@ -0,0 +1,45 @@
use docker_api::opts::ContainerCreateOpts;
pub async fn list(docker: docker_api::Docker){
match docker.images().list(&Default::default()).await {
Ok(images) => {
for image in images {
println!("{0:?}", image.repo_tags);
}
},
Err(e) => println!("Something bad happened! {e}"),
}
}
pub async fn list_c(docker: docker_api::Docker){
match docker.containers().list(&Default::default()).await {
Ok(containers) => {
for container in containers {
println!("{0:?}", container.status);
}
},
Err(e) => println!("Something bad happened! {e}"),
}
}
pub async fn create(docker: docker_api::Docker){
let env = vec!["EULA=TRUE"];
let opts = ContainerCreateOpts::builder()
.image("itzg/minecraft-server:latest")
.env(env)
.build();
let cont = match docker.containers().create(&opts).await {
Ok(c) => {
println!("OK: {c:?}");
c
},
Err(e) => {eprintln!("Error: {e}"); panic!("{e}")},
};
match cont.start().await {
Ok(s) => {
println!("Ok {s:?}")
},
Err(e) => eprintln!("Error: {e}"),
};
}

19
src/main.rs Normal file
View File

@@ -0,0 +1,19 @@
use futures::executor::block_on;
use std::io;
use tokio::net::TcpListener;
use tokio::io::AsyncWriteExt;
mod create_container;
mod server;
mod conf;
#[tokio::main]
async fn main() {
let docker = match docker_api::Docker::new("unix:///var/run/docker.sock") {
Ok(d) => d,
Err(e) => {print!("{}",e.to_string()); panic!("{}", e.to_string())},
};
block_on(create_container::list(docker.clone()));
//block_on(create_container::create(docker.clone()));
//block_on(create_container::list_c(docker));
//server::start().await;
}

26
src/meson.build Normal file
View File

@@ -0,0 +1,26 @@
cargo_bin = find_program('cargo')
cargo_opt = [ '--manifest-path', meson.project_source_root() / 'Cargo.toml' ]
cargo_opt += [ '--target-dir', meson.project_build_root() / 'src' ]
cargo_env = [ 'CARGO_HOME=' + meson.project_build_root() / 'cargo-home' ]
if get_option('buildtype') == 'release'
cargo_options += [ '--release' ]
rust_target = 'release'
else
rust_target = 'debug'
endif
cargo_build = custom_target(
'cargo-build',
build_by_default: true,
build_always_stale: true,
output: meson.project_name(),
console: true,
install: true,
install_dir: get_option('bindir'),
command: [
'env', cargo_env,
cargo_bin, 'build',
cargo_opt, '&&', 'cp', 'src' / rust_target / meson.project_name(), '@OUTPUT@',
]
)

14
src/server.rs Normal file
View File

@@ -0,0 +1,14 @@
use tokio::net::TcpListener;
use tokio::io::AsyncWriteExt;
use std::io;
pub async fn start() -> io::Result<()> {
let listener = TcpListener::bind("127.0.0.1:8080").await?;
loop {
let (mut socket, _) = listener.accept().await?;
socket.write_all(b"algo").await.unwrap();
}
}