Skip to content

Commit

Permalink
impl extends res Link #9
Browse files Browse the repository at this point in the history
  • Loading branch information
zuowenjian committed Apr 23, 2018
1 parent e9b945d commit c598fba
Show file tree
Hide file tree
Showing 10 changed files with 310 additions and 10 deletions.
1 change: 1 addition & 0 deletions core/src/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ macro_rules! map {
//StrMap(map) ;
})
}

2 changes: 1 addition & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub mod def ;
pub mod model ;
pub mod creator ;
pub mod parser ;
mod res ;
pub mod res ;
pub mod inner ;


Expand Down
1 change: 1 addition & 0 deletions core/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub trait Res
fn check(&self , context : &mut Context ) ->BoolR;
fn clean(&self , context : &mut Context ) ->BoolR;
fn info(&self) ->String ;
fn name(&self) ->String ;
}
pub type ResBox = Box<Res> ;
pub type ResVec = Vec<Box<Res>> ;
Expand Down
29 changes: 23 additions & 6 deletions core/src/res.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,32 @@ where T: ResDesp + InvokeStart + InvokeStop + InvokeHook + Interceptor
{
self.res_info()
}
fn name(&self) ->String
{
self.res_name()
}
}
#[macro_export]
macro_rules! res_info {
{ $res:expr } => {
info!("res {}: {:?}", $res.name(),$res.info()) ;
};
}
#[macro_export]
macro_rules! res_assert {
{ $ret : expr } => {
assert!($ret.is_ok(),"err:{:?}",$ret);
};
}

pub fn res_check<T>( res :&T) where T : Res
pub fn res_check<T>( res :&Box<T>) where T : Res + ?Sized
{
let mut c = Context::new() ;
assert!(res.conf( &mut c).is_ok()) ;
assert!(res.start( &mut c).is_ok());
assert!(res.stop( &mut c).is_ok()) ;
assert!(res.clean( &mut c).is_ok()) ;
res_info!(res);
res_assert!(res.conf( &mut c)) ;
res_assert!(res.start( &mut c));
res_assert!(res.stop( &mut c)) ;
res_assert!(res.clean( &mut c)) ;
}


Expand Down Expand Up @@ -156,7 +173,7 @@ mod tests
fn useres_stub()
{

let res = StubRes::new();
let res = Box::new(StubRes::new());
res_check(&res) ;
}
}
8 changes: 8 additions & 0 deletions extends/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,12 @@ name = "rg_exts"
version = "0.1.0"
authors = ["zuowenjian <[email protected]>"]


[dependencies]
log = "0.4.1"
pretty_env_logger = "0.2"
rg_core = { path = "../core", version = "*" }
shells = "0.2"



142 changes: 142 additions & 0 deletions extends/meterial/run_tpl.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# from XCodeCraft Template
_env:
- !R.env
_name : "_local_deploy"
_res :
- !R.project
root : "${HOME}/devspace/%{PRJ_NAME}"
name : "%{PRJ_NAME}"
- !R.vars
PHP_ERROR : "E_ALL & ~E_NOTICE"
FPM_USER : "${USER}"

# 数据库
# DB_HOST : "127.0.0.1"
# DB_NAME : "db_name"
# DB_USER : "db_user"
# DB_PWD : ""

- !R.env
_name : "_safe_deploy"
_res :
- !R.project
root : "/data/x/projects/%{PRJ_NAME}"
name : "%{PRJ_NAME}"
- !R.vars
PHP_ERROR : "E_ALL & ~E_NOTICE"
FPM_USER : "%{FPM_USER}"
- !R.env
_name : "_dev"
_res :
- !R.vars
BASE_DOMAIN : "${USER}.%{MAIN_DOMAIN}"

- !R.env
_name : "_demo"
_res :
- !R.vars
BASE_DOMAIN : "demo.%{MAIN_DOMAIN}"

- !R.env
_name : "base"
_res :
- !R.vars
PYLON : "/data/x/framework/pylon-ng/"
RIGGER : "/data/x/tools/rigger-ng/"
BASE_INCLUDE : "${PRJ_ROOT}/src/bizlogic:${PRJ_ROOT}/src/foundation"
- !R.path
dst : "${PRJ_ROOT}/conf/used "

- !R.env
_name : "speed_max"
_res :
- !R.vars
MAX_CHILDREN : "20"
START_SERVERS : "5"
MIN_SPARE_SERVERS : "5"
MAX_SPARE_SERVERS : "10"
- !R.env
_name : "speed_min"
_res :
- !R.vars
MAX_CHILDREN : "5"
START_SERVERS : "2"
MIN_SPARE_SERVERS : "1"
MAX_SPARE_SERVERS : "3"

- !R.env
_name : "debug"
_res :
- !R.vars
PHP_ERROR : "E_ALL & ~E_NOTICE"
DEBUG : "ON"
LOG_MODE : "DEBUG"
- !R.env
_name : "release"
_res :
- !R.vars
PHP_ERROR : "E_ERROR"
DEBUG : "XOFF"
LOG_MODE : "ONLINE"


- !R.env
_name : "dev"
_mix : "_local_deploy,_dev,base,speed_min,debug"

- !R.env
_name : "demo"
_mix : "_safe_deploy,_demo,base,speed_min,debug"

- !R.env
_name : "test"
_mix : "_safe_deploy,_test,base,speed_max,release"

_sys:
- !R.system
_name : "init"
_res :
# - !R.vars
# YOU_X : "x"
# - !R.mysql
# host: "${DB_HOST}"
# name: "${DB_NAME}"
# user: "${DB_USER}"
# password: "${DB_PWD}"
# sql: "${PRJ_ROOT}/src/init/create_db.sql"
- !R.using
path : "${RIGGER}/extends/moduls/pylon.yaml"
modul : "pylon_php"
args : !R.vars
MOD_TAG : "init"
MOD_INCLUDE : "${PRJ_ROOT}/src/init:${BASE_INCLUDE}"
# - !R.php
# ini : "${PRJ_ROOT}/conf/used/init_php.ini"
# script : "${PRJ_ROOT}/src/init/sys_init.php"

- !R.system
_name : "api"
_res :
- !R.vars
DOMAIN : "api.${BASE_DOMAIN}"
SOCK_FILE : "${RUN_PATH}/api_fpm.sock"
- !R.using
path : "${RIGGER}/extends/moduls/pylon.yaml"
modul : "pylon_web"
args : !R.vars
MOD_TAG : "api"
MOD_ENTRY : "${PRJ_ROOT}/src/sys_${SYS_NAME}"
MOD_INCLUDE : "${PRJ_ROOT}/src/sys_${SYS_NAME}:${BASE_INCLUDE}"

- !R.system
_name : "test"
_res :
- !R.using
path : "${RIGGER}/extends/moduls/pylon.yaml"
modul : "pylon_php"
args : !R.vars
MOD_TAG : "test"
MOD_INCLUDE : "${PRJ_ROOT}/test:${PRJ_ROOT}/src"
- !R.shell
script : "${PRJ_ROOT}/test/phpunit.sh"

7 changes: 7 additions & 0 deletions extends/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@

#[macro_use] extern crate log;
extern crate pretty_env_logger;
#[macro_use] extern crate rg_core ;
#[macro_use] extern crate shells ;

pub mod unix ;
pub mod service ;
3 changes: 0 additions & 3 deletions extends/src/service/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
fn main() {
println!("Hello, world!");
}
125 changes: 125 additions & 0 deletions extends/src/unix/link.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
use rg_core::err::* ;
use rg_core::def::* ;
use rg_core::model::* ;
use rg_core::res::* ;
use rg_core::creator::* ;
use std::path::Path ;

#[derive(Debug)]
pub struct Link
{
src: String,
dst: String,

}
impl Loader<Link> for Link
{
fn load( data : &StrMap) -> Link
{
let dst = data.must_get(&String::from("dst")).clone() ;
let src = data.must_get(&String::from("src")).clone() ;
Link{ src, dst}
}
fn key() -> String { String::from("Link") }
}



impl InvokeHook for Link{}
impl InvokeStop for Link{

}


impl ResDesp for Link
{
fn res_info(&self) -> String {
format!("{:?}",self)
}
fn res_name(&self) -> String {
Link::key()
}
}


#[macro_export]
macro_rules! ERR{
($args:expr) => ({ return Error::Runtime(String::from($args)) });
($fmt:expr, $($args:tt)*) => ({
return Err(Error::Runtime(format!($fmt,$($args)*)))
});
}


impl InvokeStart for Link
{
fn res_conf(&self,_context : &mut Context) ->BoolR
{
let dst_path = Path::new(self.dst.as_str()) ;
let src_path = Path::new(self.src.as_str()) ;
if dst_path.exists()
{
if dst_path.read_link().is_ok()
{
let (code,stdout,stderr )= sh!("unlink {}",self.dst);
if code != 0 {
ERR!("{:?} {:?} ",stdout,stderr);
}
}
else
{
ERR!("dst exists {:?}",self.dst);
}
}
//TODO:
/*
if src_path.exists()
{
let parent = dst_path.parent();
let f_name = dst_path.file_name() ;
if let Some(p) = parent {
if let Some(f) = f_name
{
let (code,stdout,stderr )= sh!("cd {:?} ; ln -s {} {:?}", p.as_os_str(),self.src, f) ;
if code != 0 {
ERR!("{:?} {:?} ",stdout,stderr);
}
return Ok(()) ;
}
}
}
ERR!("{} not exists",self.src);
*/
Ok(())

}
}



pub fn res_regist(f : &mut ResFatory)
{
regist_creator::<Link>(f) ;
}

#[cfg(test)]
mod tests
{
use super::* ;
//use rg_core::res::* ;
use rg_core::model::* ;
use rg_core::parser::* ;
use rg_core::creator::* ;
use pretty_env_logger ;
#[test]
fn creat_link()
{
pretty_env_logger::init();
debug!("hello") ;
let mut god = ResFatory::new() ;
res_regist(&mut god);
let data = map!("dst" =>"${HOME}/devspace/rigger-nx/extends/meterial/run_ngx.yaml","src" => "${HOME}/devspace/rigger-nx/extends/meterial/run_tpl.yaml") ;
let obj = god.create(&Link::key(),&data ).unwrap();
res_check(&obj);
}
}
2 changes: 2 additions & 0 deletions extends/src/unix/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

pub mod link ;

0 comments on commit c598fba

Please sign in to comment.