1
0
Fork 0
mirror of https://codeberg.org/icewind/haze.git synced 2026-06-03 09:04:12 +02:00

db and image pull

This commit is contained in:
Robin Appelman 2021-03-13 22:20:11 +01:00
commit 2a4a2b9d17
9 changed files with 180 additions and 38 deletions

View file

@ -15,11 +15,12 @@ RUN DEBIAN_FRONTEND=noninteractive ;\
attr \ attr \
git \ git \
neovim \ neovim \
nano nano \
sqlite
RUN wget https://phar.phpunit.de/phpunit-8.phar -O /usr/local/bin/phpunit.phar RUN wget https://phar.phpunit.de/phpunit-8.phar -O /usr/local/bin/phpunit.phar
ADD configs/autoconfig_mysql.php configs/autoconfig_pgsql.php configs/autoconfig_oci.php configs/s3.php configs/swift.php configs/swiftv3.php configs/azure.php configs/config.php /root/ ADD configs/autoconfig_mariadb.php configs/autoconfig_mysql.php configs/autoconfig_pgsql.php configs/autoconfig_oci.php configs/s3.php configs/swift.php configs/swiftv3.php configs/azure.php configs/config.php /root/
ADD configs/nginx-app.conf /etc/nginx/ ADD configs/nginx-app.conf /etc/nginx/
RUN mkdir --parent /var/log/cron RUN mkdir --parent /var/log/cron

View file

@ -0,0 +1,9 @@
<?php
$AUTOCONFIG = [
'dbname' => 'haze',
'dbhost' => 'mariadb',
'dbuser' => 'haze',
'dbpass' => 'haze',
'dbtype' => 'mysql'
];

View file

@ -0,0 +1,9 @@
<?php
$AUTOCONFIG = [
'dbname' => 'haze',
'dbhost' => 'mysql',
'dbuser' => 'haze',
'dbpass' => 'haze',
'dbtype' => 'mysql'
];

View file

@ -11,6 +11,11 @@ then
cp /root/autoconfig_mysql.php /var/www/html/config/autoconfig.php cp /root/autoconfig_mysql.php /var/www/html/config/autoconfig.php
fi fi
if [ "$SQL" = "mariadb" ]
then
cp /root/autoconfig_mariadb.php /var/www/html/config/autoconfig.php
fi
if [ "$SQL" = "pgsql" ] if [ "$SQL" = "pgsql" ]
then then
cp /root/autoconfig_pgsql.php /var/www/html/config/autoconfig.php cp /root/autoconfig_pgsql.php /var/www/html/config/autoconfig.php

View file

@ -288,7 +288,7 @@ impl Cloud {
} }
pub async fn exec(&self, docker: &mut Docker, cmd: Vec<String>) -> Result<()> { pub async fn exec(&self, docker: &mut Docker, cmd: Vec<String>) -> Result<()> {
exec_tty(docker, &self.id, "haze", cmd).await exec_tty(docker, &self.id, "haze", cmd, vec![]).await
} }
} }

View file

@ -1,3 +1,5 @@
use crate::image::pull_image;
use crate::tty::exec_tty;
use bollard::container::{Config, CreateContainerOptions, NetworkingConfig}; use bollard::container::{Config, CreateContainerOptions, NetworkingConfig};
use bollard::models::{EndpointSettings, HostConfig}; use bollard::models::{EndpointSettings, HostConfig};
use bollard::Docker; use bollard::Docker;
@ -5,6 +7,24 @@ use color_eyre::{Report, Result};
use maplit::hashmap; use maplit::hashmap;
use std::str::FromStr; use std::str::FromStr;
pub enum DatabaseFamily {
Sqlite,
Mysql,
MariaDB,
Postgres,
}
impl DatabaseFamily {
pub fn name(&self) -> &'static str {
match self {
DatabaseFamily::Sqlite => "sqlite",
DatabaseFamily::Mysql => "mysql",
DatabaseFamily::MariaDB => "mariadb",
DatabaseFamily::Postgres => "pgsql",
}
}
}
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
#[allow(dead_code)] #[allow(dead_code)]
pub enum Database { pub enum Database {
@ -57,6 +77,18 @@ impl FromStr for Database {
"pgsql:11" => Ok(Database::Postgres11), "pgsql:11" => Ok(Database::Postgres11),
"pgsql:12" => Ok(Database::Postgres12), "pgsql:12" => Ok(Database::Postgres12),
"pgsql:13" => Ok(Database::Postgres13), "pgsql:13" => Ok(Database::Postgres13),
"postgres" => Ok(Database::Postgres),
"postgres:9" => Ok(Database::Postgres9),
"postgres:10" => Ok(Database::Postgres10),
"postgres:11" => Ok(Database::Postgres11),
"postgres:12" => Ok(Database::Postgres12),
"postgres:13" => Ok(Database::Postgres13),
"postgresql" => Ok(Database::Postgres),
"postgresql:9" => Ok(Database::Postgres9),
"postgresql:10" => Ok(Database::Postgres10),
"postgresql:11" => Ok(Database::Postgres11),
"postgresql:12" => Ok(Database::Postgres12),
"postgresql:13" => Ok(Database::Postgres13),
_ => Err(Report::msg("Unknown db type")), _ => Err(Report::msg("Unknown db type")),
} }
} }
@ -66,17 +98,17 @@ impl Database {
pub fn image(&self) -> &'static str { pub fn image(&self) -> &'static str {
match self { match self {
Database::Sqlite => "", Database::Sqlite => "",
Database::Mysql => "mysql", Database::Mysql => "mysql:8",
Database::Mysql80 => "mysql:8", Database::Mysql80 => "mysql:8",
Database::Mysql57 => "mysql:5.7", Database::Mysql57 => "mysql:5.7",
Database::Mysql56 => "mysql:5.6", Database::Mysql56 => "mysql:5.6",
Database::MariaDB => "mariadb", Database::MariaDB => "mariadb:10",
Database::MariaDB101 => "mariadb:10.1", Database::MariaDB101 => "mariadb:10.1",
Database::MariaDB102 => "mariadb:10.2", Database::MariaDB102 => "mariadb:10.2",
Database::MariaDB103 => "mariadb:10.3", Database::MariaDB103 => "mariadb:10.3",
Database::MariaDB104 => "mariadb:10.4", Database::MariaDB104 => "mariadb:10.4",
Database::MariaDB105 => "mariadb:10.5", Database::MariaDB105 => "mariadb:10.5",
Database::Postgres => "postgres", Database::Postgres => "postgres:9",
Database::Postgres9 => "postgres:9", Database::Postgres9 => "postgres:9",
Database::Postgres10 => "postgres:10", Database::Postgres10 => "postgres:10",
Database::Postgres11 => "postgres:11", Database::Postgres11 => "postgres:11",
@ -85,52 +117,41 @@ impl Database {
} }
} }
pub fn name(&self) -> &'static str { pub fn name(&self) -> &str {
self.family().name()
}
pub fn family(&self) -> DatabaseFamily {
match self { match self {
Database::Sqlite => "sqlite", Database::Sqlite => DatabaseFamily::Sqlite,
Database::Mysql Database::Mysql | Database::Mysql80 | Database::Mysql57 | Database::Mysql56 => {
| Database::Mysql80 DatabaseFamily::Mysql
| Database::Mysql57 }
| Database::Mysql56 Database::MariaDB
| Database::MariaDB
| Database::MariaDB101 | Database::MariaDB101
| Database::MariaDB102 | Database::MariaDB102
| Database::MariaDB103 | Database::MariaDB103
| Database::MariaDB104 | Database::MariaDB104
| Database::MariaDB105 => "mysql", | Database::MariaDB105 => DatabaseFamily::MariaDB,
Database::Postgres Database::Postgres
| Database::Postgres9 | Database::Postgres9
| Database::Postgres10 | Database::Postgres10
| Database::Postgres11 | Database::Postgres11
| Database::Postgres12 | Database::Postgres12
| Database::Postgres13 => "pgsql", | Database::Postgres13 => DatabaseFamily::Postgres,
} }
} }
pub fn env(&self) -> Vec<&'static str> { pub fn env(&self) -> Vec<&'static str> {
match self { match self.family() {
Database::Sqlite => Vec::new(), DatabaseFamily::Sqlite => Vec::new(),
Database::Mysql DatabaseFamily::Mysql | DatabaseFamily::MariaDB => vec![
| Database::Mysql80
| Database::Mysql57
| Database::Mysql56
| Database::MariaDB
| Database::MariaDB101
| Database::MariaDB102
| Database::MariaDB103
| Database::MariaDB104
| Database::MariaDB105 => vec![
"MYSQL_ROOT_PASSWORD=haze", "MYSQL_ROOT_PASSWORD=haze",
"MYSQL_PASSWORD=haze", "MYSQL_PASSWORD=haze",
"MYSQL_USER=haze", "MYSQL_USER=haze",
"MYSQL_DATABASE=haze", "MYSQL_DATABASE=haze",
], ],
Database::Postgres DatabaseFamily::Postgres => vec![
| Database::Postgres9
| Database::Postgres10
| Database::Postgres11
| Database::Postgres12
| Database::Postgres13 => vec![
"POSTGRES_PASSWORD=haze", "POSTGRES_PASSWORD=haze",
"POSTGRES_USER=haze", "POSTGRES_USER=haze",
"POSTGRES_DATABASE=haze", "POSTGRES_DATABASE=haze",
@ -147,6 +168,7 @@ impl Database {
if matches!(self, Database::Sqlite) { if matches!(self, Database::Sqlite) {
return Ok(None); return Ok(None);
} }
pull_image(docker, self.image()).await?;
let options = Some(CreateContainerOptions { let options = Some(CreateContainerOptions {
name: format!("{}-db", cloud_id), name: format!("{}-db", cloud_id),
}); });
@ -175,4 +197,39 @@ impl Database {
docker.start_container::<String>(&id, None).await?; docker.start_container::<String>(&id, None).await?;
Ok(Some(id)) Ok(Some(id))
} }
pub async fn exec(&self, docker: &mut Docker, cloud_id: &str) -> Result<()> {
match self.family() {
DatabaseFamily::Sqlite => {
exec_tty(
docker,
cloud_id,
"haze",
vec!["sqlite3", "/var/www/html/data/owncloud.db"],
vec![],
)
.await
}
DatabaseFamily::MariaDB | DatabaseFamily::Mysql => {
exec_tty(
docker,
format!("{}-db", cloud_id),
"mysql",
vec!["mysql", "-u", "haze", "-phaze", "haze"],
vec![],
)
.await
}
DatabaseFamily::Postgres => {
exec_tty(
docker,
format!("{}-db", cloud_id),
"root",
vec!["psql", "haze", "haze"],
vec!["PGPASSWORD=haze"],
)
.await
}
}
}
} }

55
src/image.rs Normal file
View file

@ -0,0 +1,55 @@
use bollard::image::CreateImageOptions;
use bollard::models::CreateImageInfo;
use bollard::Docker;
use color_eyre::Result;
use futures_util::StreamExt;
use std::collections::HashMap;
use std::io::stdout;
use std::io::Write;
use termion::cursor;
pub async fn pull_image(docker: &mut Docker, image: &str) -> Result<()> {
if let Err(_) = docker.inspect_image(image).await {
let mut info_stream = docker.create_image(
Some(CreateImageOptions {
from_image: image,
..Default::default()
}),
None,
None,
);
let mut bars: HashMap<String, u16> = HashMap::new();
let stdout = stdout();
let mut stdout = stdout.lock();
while let Some(info) = info_stream.next().await {
let info: CreateImageInfo = info?;
// dbg!(&info);
if let (Some(id), Some(status), Some(progress)) = (info.id, info.status, info.progress)
{
match bars.get(&id) {
Some(pos) => {
let offset = bars.len() as u16 - pos;
write!(
stdout,
"{}{}{} - {:12} {}{}",
cursor::Save,
cursor::Up(offset),
id,
status,
progress,
cursor::Restore
)?;
}
None => {
writeln!(stdout, "{} - {:12} {}", id, status, progress)?;
bars.insert(id, bars.len() as u16);
}
}
stdout.flush()?;
}
}
}
Ok(())
}

View file

@ -8,6 +8,7 @@ mod args;
mod cloud; mod cloud;
mod config; mod config;
mod database; mod database;
mod image;
mod php; mod php;
mod tty; mod tty;
@ -95,7 +96,8 @@ async fn main() -> Result<()> {
cloud.exec(&mut docker, options).await?; cloud.exec(&mut docker, options).await?;
} }
HazeCommand::Db => { HazeCommand::Db => {
todo!(); let cloud = get_by_filter(&mut docker, None, &config).await?;
cloud.db.exec(&mut docker, &cloud.id).await?;
} }
HazeCommand::Test => { HazeCommand::Test => {
todo!(); todo!();

View file

@ -9,12 +9,15 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::task::spawn; use tokio::task::spawn;
use tokio::time::sleep; use tokio::time::sleep;
pub async fn exec_tty( pub async fn exec_tty<S1: AsRef<str>, S2: Into<String>>(
docker: &mut Docker, docker: &mut Docker,
container: &str, container: S1,
user: &str, user: &str,
cmd: Vec<String>, cmd: Vec<S2>,
env: Vec<&str>,
) -> Result<()> { ) -> Result<()> {
let cmd = cmd.into_iter().map(S2::into).collect();
let env = env.into_iter().map(String::from).collect();
let config = CreateExecOptions { let config = CreateExecOptions {
cmd: Some(cmd), cmd: Some(cmd),
user: Some(user.to_string()), user: Some(user.to_string()),
@ -22,10 +25,11 @@ pub async fn exec_tty(
attach_stderr: Some(true), attach_stderr: Some(true),
attach_stdin: Some(true), attach_stdin: Some(true),
tty: Some(true), tty: Some(true),
env: Some(env),
..Default::default() ..Default::default()
}; };
let message = docker let message = docker
.create_exec(container, config) .create_exec(container.as_ref(), config)
.await .await
.wrap_err("Failed to setup exec")?; .wrap_err("Failed to setup exec")?;
if let StartExecResults::AttachedTTY { if let StartExecResults::AttachedTTY {