view src/main.rs @ 75:b4d2ef1897f9 default tip master

update README
author AnaTofuZ <anatofuz@gmail.com>
date Sun, 20 Dec 2020 23:04:41 +0900
parents 1939d3b5722d
children
line wrap: on
line source

use clap::{crate_version, Clap};
use ie_virsh::{command, user};

#[derive(Clap)]
#[clap(version = crate_version!(), author = "AnaTofuZ <anatofuz@cr.ie.u-ryukyu.ac.jp>")]
struct Opts {
    #[clap(subcommand)]
    subcmd: SubCommand,
}

#[derive(Clap)]
enum SubCommand {
    List(List),
    Undefine(Undefine),
    Define(Define),
    Shutdown(Shutdown),
    Destroy(Destroy),
    Console(Console),
    Start(Start),
    Ttyconsole(TTyconsole),
    Vncdisplay(Vncdisplay),
    Dumpxml(Dumpxml),
    Domiflist(Domiflist),
    Dominfo(Dominfo),
    Templates(Templates),
    Edit(Edit),
}

/// show templates vm
#[derive(Clap)]
struct Templates {}

/// define (but don't start) a domain from an template XML file
#[derive(Clap)]
struct Define {
    name: String,
    /// create a VM with a differential from the template VM registered in $ie-virsh templates
    #[clap(short, long, parse(from_str))]
    template: Option<String>,

    /// define the domain in which the gdb port is opened from the template XML file
    #[clap(short, long)]
    gdb: bool,
}

/// domain information in XML
#[derive(Clap)]
struct Dumpxml {
    name: String,
}

/// vncdisplay
#[derive(Clap)]
struct Vncdisplay {
    name: String,
}

/// undefine a domain
#[derive(Clap)]
struct Undefine {
    name: String,
}

/// start a (previously defined) inactive domain
#[derive(Clap)]
struct Start {
    name: String,
}

/// tty console
#[derive(Clap)]
struct TTyconsole {
    name: String,
}

/// gracefully shutdown a domain
#[derive(Clap)]
struct Shutdown {
    name: String,
}

/// list domains
#[derive(Clap)]
struct List {}

/// edit XML configuration for a domain
#[derive(Clap)]
struct Edit {
    name: String,
}

/// destroy (stop) a domain
#[derive(Clap)]
struct Destroy {
    name: String,
}

/// connect to the guest console
#[derive(Clap)]
struct Console {
    name: String,
}

/// list all domain virtual interfaces
#[derive(Clap)]
struct Domiflist {
    name: String,
}

/// domain information
#[derive(Clap)]
struct Dominfo {
    name: String,
}

fn main() {
    let opts: Opts = Opts::parse();

    let uid = user::getuid();
    let gid = user::getgid();
    let user_name = user::getlogin(uid);
    println!("uid: {} gid: {} name: {}", uid, gid, user_name);

    match opts.subcmd {
        SubCommand::List(_) => {
            user::set_root_id();
            command::list(&user_name);
        }

        SubCommand::Start(arg) => {
            user::set_root_id();
            command::start(&user_name, &arg.name);
        }

        SubCommand::Define(arg) => {
            user::set_root_id();
            let user = user::User {
                uid,
                gid,
                name: user_name,
            };

            let userdetail = user::UserDetail::new(&user);
            if let Some(template) = arg.template {
                command::define_from_template(&userdetail, &arg.name, &template);
                return;
            }
            if arg.gdb {
                command::define_gdb(&userdetail, &arg.name);
                return;
            }
            command::define(&userdetail, &arg.name);
        }

        SubCommand::Shutdown(arg) => {
            user::set_root_id();
            command::subshell(&user_name, &arg.name, "shutdown");
        }

        SubCommand::Destroy(arg) => {
            user::set_root_id();
            command::subshell(&user_name, &arg.name, "destroy");
        }

        SubCommand::Vncdisplay(arg) => {
            user::set_root_id();
            command::subshell(&user_name, &arg.name, "vncdisplay");
        }

        SubCommand::Ttyconsole(arg) => {
            user::set_root_id();
            command::subshell(&user_name, &arg.name, "ttyconsole");
        }

        SubCommand::Dumpxml(arg) => {
            user::set_root_id();
            command::subshell(&user_name, &arg.name, "dumpxml");
        }

        SubCommand::Undefine(arg) => {
            user::set_root_id();
            command::subshell(&user_name, &arg.name, "undefine");
            //command::vol_delete(&user_name, &arg.name, "vol-delete");
        }

        SubCommand::Domiflist(arg) => {
            user::set_root_id();
            command::subshell(&user_name, &arg.name, "domiflist");
        }

        SubCommand::Dominfo(arg) => {
            user::set_root_id();
            command::subshell(&user_name, &arg.name, "dominfo");
        }

        SubCommand::Console(arg) => {
            user::set_root_id();
            command::exec(&user_name, &arg.name, "console");
        }

        SubCommand::Edit(arg) => {
            user::set_root_id();
            command::exec(&user_name, &arg.name, "edit");
        }

        SubCommand::Templates(_) => {
            if let Err(e) = command::templates() {
                println!("{}", e);
            }
        }
    }
}