start to use sqlite

This commit is contained in:
2023-07-15 09:27:16 +02:00
parent 509ec65a62
commit e3422ed255
3 changed files with 97 additions and 13 deletions

View File

@@ -1,5 +1,6 @@
use std::net::{Ipv4Addr, SocketAddrV4};
use rusqlite::{Connection, Result};
use std::collections::hash_map::HashMap;
const PATH: &str = "mdeploy.db";
@@ -12,19 +13,23 @@ pub struct ConfServer{
pub struct MInstance {
id: i64,
ip: Ipv4Addr,
ip: String,
image: i64,
}
pub struct instance {
pub struct InstanceStorage {
con: Connection,
instances: HashMap<String, MInstance>,
images: HashMap<String, i64>,
}
impl instance {
impl InstanceStorage {
pub fn new() -> Result<Self> {
let con = Connection::open(PATH)?;
let ret = Self {
con : con,
instances: HashMap::new(),
images: HashMap::new(),
};
ret.create_table()?;
Ok(ret)
@@ -38,15 +43,82 @@ impl instance {
CREATE TABLE IF NOT EXISTS l_instances(
id INTEGER PRIMARY KEY AUTOINCREMENT,
ip TEXT,
image INTEGER
domain TEXT,
image INTEGER,
FOREIGN KEY(INTEGER) REFERENCES images(id));
COMMIT;"
)
}
fn insert_image(&self, image: String){
fn insert_image(&self, image: String) -> Result<()>{
let mut stmt = self.con.prepare("INSERT INTO images values(?1)")?;
stmt.execute([image])?;
Ok(())
}
fn load_instances(&mut self) -> Result<()> {
let mut stmt = self.con.prepare("select id, ip, domain, image from l_instances")?;
let mut rows = stmt.query([])?;
for row in rows.next()? {
let mut domain = row.get::<_, String>(2)?;
self.instances.insert(domain, MInstance {
id: row.get::<_, i64>(0)?,
ip: row.get::<_, String>(1)?,
image: row.get::<_, i64>(3)?
,
});
}
Ok(())
}
fn load_images(&mut self) -> Result<()> {
let mut stmt = self.con.prepare("select id, name from images")?;
let mut rows = stmt.query([])?;
for row in rows.next()? {
self.images.insert(row.get::<_, String>(1)?, row.get::<_, i64>(0)?);
}
Ok(())
}
fn get_image_id(&self, image: String) -> Result<i64> {
let mut stmt = self.con.prepare("select id from images where name = ?1")?;
let mut rows = stmt.query([image])?;
let id = rows.next()?.unwrap().get::<_, i64>(0)?;
Ok(id)
}
fn get_instance_id(&self, domain: String) -> Result<i64> {
let mut stmt = self.con.prepare("select id from l_instances where domain = ?1")?;
let mut rows = stmt.query([domain])?;
let id = rows.next()?.unwrap().get::<_, i64>(0)?;
Ok(id)
}
fn new_instance(&mut self, domain: String, image: String, ip: String) -> Result<i64>{
let image_id = self.create_or_get_image_id(image.clone(), 0)?;
let mut stmt = self.con.prepare("INSERT INTO l_instances values(?1, ?2, ?3)")?;
stmt.execute([ip.clone(), image, image_id.to_string()])?;
let instance_id = self.get_instance_id(domain.clone())?;
self.instances.insert(domain.clone(), MInstance{
id: instance_id,
ip: ip,
image: image_id
});
Ok(instance_id)
}
fn create_or_get_image_id(&self, image: String, mut depth: i64) -> Result<i64> {
if depth > 0 {
return Err(rusqlite::Error::QueryReturnedNoRows);
}
depth+=1;
match self.get_image_id(image.clone()) {
Ok(id) => Ok(id),
Err(_e) => {
self.insert_image(image.clone())?;
self.create_or_get_image_id(image, depth)
}
}
}
}

View File

@@ -15,7 +15,7 @@ async fn main() {
let net_name = String::from("customnetwork");
let net_range = String::from("172.20.0.0/24");
match network_docker::Network::create_network(docker.clone(), net_name, net_range).await {
match network_docker::Network::new(docker.clone(), net_name, net_range).await {
Ok(_n)=> println!("va"),
Err(e)=> println!("err: {}", e),
};

View File

@@ -1,7 +1,8 @@
use bollard::Docker;
use bollard::errors::Error;
use bollard::errors::Error::DockerResponseServerError;
use bollard::models::{IpamConfig,Ipam};
use bollard::network::CreateNetworkOptions;
use bollard::network::{CreateNetworkOptions, ListNetworksOptions};
use std::collections::hash_map::HashMap;
pub struct Network {
name: String,
@@ -10,9 +11,20 @@ pub struct Network {
}
impl Network {
async fn check_network(docker: bollard::Docker, name) -> bool {
pub async fn new(docker: bollard::Docker, name: String, range: String) -> Result<Self, Error> {
if Self::check_network(docker.clone(), name.clone()).await {
Ok(Network {
name: name,
range: range,
})
} else {
Self::create_network(docker, name, range).await
}
}
async fn check_network(docker: bollard::Docker, name: String) -> bool {
let mut list_networks_filters = HashMap::new();
list_networks_filters.insert("name", name);
list_networks_filters.insert("name", vec![name.as_str()]);
match docker.list_networks(Some(ListNetworksOptions {
filters: list_networks_filters,
})).await {
@@ -21,7 +33,7 @@ impl Network {
}
}
pub async fn create_network(docker: bollard::Docker, name: String, range: String) -> Result<Self, Error>{
async fn create_network(docker: bollard::Docker, name: String, range: String) -> Result<Self, Error>{
let ipam_config = IpamConfig {
subnet: Some(range.clone()),
..Default::default()