mirror of
https://codeberg.org/icewind/haze.git
synced 2026-06-03 17:14:08 +02:00
db and image pull
This commit is contained in:
parent
2ca635603b
commit
2a4a2b9d17
9 changed files with 180 additions and 38 deletions
|
|
@ -15,11 +15,12 @@ RUN DEBIAN_FRONTEND=noninteractive ;\
|
|||
attr \
|
||||
git \
|
||||
neovim \
|
||||
nano
|
||||
nano \
|
||||
sqlite
|
||||
|
||||
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/
|
||||
|
||||
RUN mkdir --parent /var/log/cron
|
||||
|
|
|
|||
9
images/haze/configs/autoconfig_mariadb.php
Normal file
9
images/haze/configs/autoconfig_mariadb.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
$AUTOCONFIG = [
|
||||
'dbname' => 'haze',
|
||||
'dbhost' => 'mariadb',
|
||||
'dbuser' => 'haze',
|
||||
'dbpass' => 'haze',
|
||||
'dbtype' => 'mysql'
|
||||
];
|
||||
9
images/haze/configs/ldap/autoconfig_mariadb.php
Normal file
9
images/haze/configs/ldap/autoconfig_mariadb.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
$AUTOCONFIG = [
|
||||
'dbname' => 'haze',
|
||||
'dbhost' => 'mysql',
|
||||
'dbuser' => 'haze',
|
||||
'dbpass' => 'haze',
|
||||
'dbtype' => 'mysql'
|
||||
];
|
||||
|
|
@ -11,6 +11,11 @@ then
|
|||
cp /root/autoconfig_mysql.php /var/www/html/config/autoconfig.php
|
||||
fi
|
||||
|
||||
if [ "$SQL" = "mariadb" ]
|
||||
then
|
||||
cp /root/autoconfig_mariadb.php /var/www/html/config/autoconfig.php
|
||||
fi
|
||||
|
||||
if [ "$SQL" = "pgsql" ]
|
||||
then
|
||||
cp /root/autoconfig_pgsql.php /var/www/html/config/autoconfig.php
|
||||
|
|
|
|||
|
|
@ -288,7 +288,7 @@ impl Cloud {
|
|||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
117
src/database.rs
117
src/database.rs
|
|
@ -1,3 +1,5 @@
|
|||
use crate::image::pull_image;
|
||||
use crate::tty::exec_tty;
|
||||
use bollard::container::{Config, CreateContainerOptions, NetworkingConfig};
|
||||
use bollard::models::{EndpointSettings, HostConfig};
|
||||
use bollard::Docker;
|
||||
|
|
@ -5,6 +7,24 @@ use color_eyre::{Report, Result};
|
|||
use maplit::hashmap;
|
||||
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)]
|
||||
#[allow(dead_code)]
|
||||
pub enum Database {
|
||||
|
|
@ -57,6 +77,18 @@ impl FromStr for Database {
|
|||
"pgsql:11" => Ok(Database::Postgres11),
|
||||
"pgsql:12" => Ok(Database::Postgres12),
|
||||
"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")),
|
||||
}
|
||||
}
|
||||
|
|
@ -66,17 +98,17 @@ impl Database {
|
|||
pub fn image(&self) -> &'static str {
|
||||
match self {
|
||||
Database::Sqlite => "",
|
||||
Database::Mysql => "mysql",
|
||||
Database::Mysql => "mysql:8",
|
||||
Database::Mysql80 => "mysql:8",
|
||||
Database::Mysql57 => "mysql:5.7",
|
||||
Database::Mysql56 => "mysql:5.6",
|
||||
Database::MariaDB => "mariadb",
|
||||
Database::MariaDB => "mariadb:10",
|
||||
Database::MariaDB101 => "mariadb:10.1",
|
||||
Database::MariaDB102 => "mariadb:10.2",
|
||||
Database::MariaDB103 => "mariadb:10.3",
|
||||
Database::MariaDB104 => "mariadb:10.4",
|
||||
Database::MariaDB105 => "mariadb:10.5",
|
||||
Database::Postgres => "postgres",
|
||||
Database::Postgres => "postgres:9",
|
||||
Database::Postgres9 => "postgres:9",
|
||||
Database::Postgres10 => "postgres:10",
|
||||
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 {
|
||||
Database::Sqlite => "sqlite",
|
||||
Database::Mysql
|
||||
| Database::Mysql80
|
||||
| Database::Mysql57
|
||||
| Database::Mysql56
|
||||
| Database::MariaDB
|
||||
Database::Sqlite => DatabaseFamily::Sqlite,
|
||||
Database::Mysql | Database::Mysql80 | Database::Mysql57 | Database::Mysql56 => {
|
||||
DatabaseFamily::Mysql
|
||||
}
|
||||
Database::MariaDB
|
||||
| Database::MariaDB101
|
||||
| Database::MariaDB102
|
||||
| Database::MariaDB103
|
||||
| Database::MariaDB104
|
||||
| Database::MariaDB105 => "mysql",
|
||||
| Database::MariaDB105 => DatabaseFamily::MariaDB,
|
||||
Database::Postgres
|
||||
| Database::Postgres9
|
||||
| Database::Postgres10
|
||||
| Database::Postgres11
|
||||
| Database::Postgres12
|
||||
| Database::Postgres13 => "pgsql",
|
||||
| Database::Postgres13 => DatabaseFamily::Postgres,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn env(&self) -> Vec<&'static str> {
|
||||
match self {
|
||||
Database::Sqlite => Vec::new(),
|
||||
Database::Mysql
|
||||
| Database::Mysql80
|
||||
| Database::Mysql57
|
||||
| Database::Mysql56
|
||||
| Database::MariaDB
|
||||
| Database::MariaDB101
|
||||
| Database::MariaDB102
|
||||
| Database::MariaDB103
|
||||
| Database::MariaDB104
|
||||
| Database::MariaDB105 => vec![
|
||||
match self.family() {
|
||||
DatabaseFamily::Sqlite => Vec::new(),
|
||||
DatabaseFamily::Mysql | DatabaseFamily::MariaDB => vec![
|
||||
"MYSQL_ROOT_PASSWORD=haze",
|
||||
"MYSQL_PASSWORD=haze",
|
||||
"MYSQL_USER=haze",
|
||||
"MYSQL_DATABASE=haze",
|
||||
],
|
||||
Database::Postgres
|
||||
| Database::Postgres9
|
||||
| Database::Postgres10
|
||||
| Database::Postgres11
|
||||
| Database::Postgres12
|
||||
| Database::Postgres13 => vec![
|
||||
DatabaseFamily::Postgres => vec![
|
||||
"POSTGRES_PASSWORD=haze",
|
||||
"POSTGRES_USER=haze",
|
||||
"POSTGRES_DATABASE=haze",
|
||||
|
|
@ -147,6 +168,7 @@ impl Database {
|
|||
if matches!(self, Database::Sqlite) {
|
||||
return Ok(None);
|
||||
}
|
||||
pull_image(docker, self.image()).await?;
|
||||
let options = Some(CreateContainerOptions {
|
||||
name: format!("{}-db", cloud_id),
|
||||
});
|
||||
|
|
@ -175,4 +197,39 @@ impl Database {
|
|||
docker.start_container::<String>(&id, None).await?;
|
||||
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
55
src/image.rs
Normal 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(())
|
||||
}
|
||||
|
|
@ -8,6 +8,7 @@ mod args;
|
|||
mod cloud;
|
||||
mod config;
|
||||
mod database;
|
||||
mod image;
|
||||
mod php;
|
||||
mod tty;
|
||||
|
||||
|
|
@ -95,7 +96,8 @@ async fn main() -> Result<()> {
|
|||
cloud.exec(&mut docker, options).await?;
|
||||
}
|
||||
HazeCommand::Db => {
|
||||
todo!();
|
||||
let cloud = get_by_filter(&mut docker, None, &config).await?;
|
||||
cloud.db.exec(&mut docker, &cloud.id).await?;
|
||||
}
|
||||
HazeCommand::Test => {
|
||||
todo!();
|
||||
|
|
|
|||
12
src/tty.rs
12
src/tty.rs
|
|
@ -9,12 +9,15 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
|||
use tokio::task::spawn;
|
||||
use tokio::time::sleep;
|
||||
|
||||
pub async fn exec_tty(
|
||||
pub async fn exec_tty<S1: AsRef<str>, S2: Into<String>>(
|
||||
docker: &mut Docker,
|
||||
container: &str,
|
||||
container: S1,
|
||||
user: &str,
|
||||
cmd: Vec<String>,
|
||||
cmd: Vec<S2>,
|
||||
env: Vec<&str>,
|
||||
) -> Result<()> {
|
||||
let cmd = cmd.into_iter().map(S2::into).collect();
|
||||
let env = env.into_iter().map(String::from).collect();
|
||||
let config = CreateExecOptions {
|
||||
cmd: Some(cmd),
|
||||
user: Some(user.to_string()),
|
||||
|
|
@ -22,10 +25,11 @@ pub async fn exec_tty(
|
|||
attach_stderr: Some(true),
|
||||
attach_stdin: Some(true),
|
||||
tty: Some(true),
|
||||
env: Some(env),
|
||||
..Default::default()
|
||||
};
|
||||
let message = docker
|
||||
.create_exec(container, config)
|
||||
.create_exec(container.as_ref(), config)
|
||||
.await
|
||||
.wrap_err("Failed to setup exec")?;
|
||||
if let StartExecResults::AttachedTTY {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue