view src/main.rs @ 17:5477c26e6984

impl sub commands that use uid
author AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
date Mon, 02 Nov 2020 16:09:53 +0900
parents 56e9763abeef
children 9b24d6767a2f
line wrap: on
line source

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

#[derive(Clap)]
#[clap(version = "1.0", 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),
    DefineGDB(DefineGDB),
}

/// define the domain in which the gdb port is opened from the template XML file
#[derive(Clap)]
struct DefineGDB {
    name: String,
}

/// define (but don't start) a domain from an template XML file
#[derive(Clap)]
struct Define {
    name: String,
}

/// domain information in XML
#[derive(Clap)]
struct DumpXML {
    name_or_id: 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 {}

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

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

/*
struct VM {
    id: u32,
    name: String,
    is_vm_running: bool,
}
*/

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(name) => {}

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

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

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

        SubCommand::VNCDisplay(arg) => {
            user::set_root_id();
            command::vncdisplay(user_name, arg.name);
        }

        _ => {}
    }

    //set_root_id();
    //list_command(user_name);
}

fn create_new_vm(user_name: &'static str, vm_name: &'static str, debug_kernel: bool) {}