1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use std::env;
use std::fmt::Display;
use std::process::{Command, Output};

use log::LogLevel;

use juju::JujuError;

#[cfg(test)]
mod tests {

    mod relation_id {
        use super::super::relation_id;
        use std::env;

        #[test]
        fn it_returns_relation_id() {
            env::set_var("JUJU_RELATION_ID", "test");
            assert_eq!(relation_id(None, None).unwrap(), "test".to_string());
            env::remove_var("JUJU_RELATION_ID");
        }

        #[test]
        #[should_panic]
        fn it_panics_on_bad_first_arg() {
            relation_id(Some("Hello"), None);
        }

        #[test]
        #[should_panic]
        fn it_panics_on_bad_last_arg() {
            relation_id(None, Some("Hello"));
        }
    }

    mod hook_name {
        use super::super::hook_name;
        use std::env;

        #[test]
        fn it_gets_test_name_from_env() {
            env::set_var("JUJU_HOOK_NAME", "test");
            assert_eq!(hook_name().unwrap(), "test".to_string());
            env::remove_var("JUJU_HOOK_NAME");
        }

        #[test]
        fn it_gets_hook_name_from_args() {
            let name = hook_name().unwrap();
            assert!(name.contains("target/debug"), format!("Hook name is: {}", name));
        }
    }

    mod relation_type {
        use super::super::relation_type;
        use std::env;

        #[test]
        fn it_gets_relation_type() {
            env::set_var("JUJU_RELATION", "test");
            assert_eq!(relation_type().unwrap(), "test".to_string());
            env::remove_var("JUJU_RELATION");
        }
    }

    mod in_relation_hook {
        use super::super::in_relation_hook;
        use std::env;

        #[test]
        fn it_verifies_relation_presence() {
            env::set_var("JUJU_RELATION", "test");
            assert_eq!(in_relation_hook(), true);
            env::remove_var("JUJU_RELATION");
        }

        #[test]
        fn it_verifies_no_relation_presence() {
            assert_eq!(in_relation_hook(), false);
        }
    }

    mod local_unit {
        use super::super::local_unit;
        use std::env;

        #[test]
        fn it_retrieves_local_unit_name() {
            env::set_var("JUJU_UNIT_NAME", "test/1");
            assert_eq!(local_unit().unwrap(), "test/1".to_string());
            env::remove_var("JUJU_UNIT_NAME");
        }
    }

    mod remote_unit {
        use super::super::remote_unit;
        use std::env;

        #[test]
        fn it_retrieves_remote_unit_name() {
            env::set_var("JUJU_REMOTE_UNIT", "test/1");
            assert_eq!(remote_unit().unwrap(), "test/1".to_string());
            env::remove_var("JUJU_REMOTE_UNIT");
        }
    }

    mod service_name {
        use super::super::service_name;
        use std::env;

        #[test]
        fn it_retrieves_service_name() {
            env::set_var("JUJU_UNIT_NAME", "test/1");
            assert_eq!(service_name().unwrap(), "test".to_string());
            env::remove_var("JUJU_UNIT_NAME");
        }
    }

    mod charm_dir {
        use super::super::charm_dir;
        use std::env;

        fn it_knows_charm_dir() {
            env::set_var("CHARM_DIR", "/test/charmdir");
            assert_eq!(charm_dir().unwrap(), "/test/charmdir");
            env::remove_var("CHARM_DIR");
        }
    }

    mod execution_environment {
        use super::super::execution_environment;
        use std::env;

        #[test]
        #[should_panic]
        fn it_gets_execution_environment_with_no_relation() {
            let environment = execution_environment();
            assert_eq!(environment.conf, super::super::Config);
            assert!(environment.reltype.is_none());
            assert!(environment.relid.is_none());
            assert!(environment.rel.is_none());
            assert_eq!(environment.unit, "test/1");
        }
    }
}

/// Log a message, at an optional log::LogLevel, to the Juju log
pub fn log<T: Display>(message: T, level: Option<LogLevel>) {
    let mut cmd = vec![];
    if let Some(level) = level {
        // println!("loglevel = {}", level);
        cmd.push("-l".to_string());
        cmd.push(level.to_string());
    }
    cmd.push(message.to_string());
    let _ = run_command("juju-log", &cmd, false);
}

#[derive(PartialEq,Debug)]
pub struct Config;

#[derive(PartialEq,Debug)]
pub struct Relation;

// pub struct Env {
//     conf: Config,
//     reltype: Option<String>,
//     relid: Option<String>,
//     rel: Option<String>,
//     unit: String,
//     rels: Vec<Relation>,
//     env: Vec<(String, String)>,
// }

// pub fn execution_environment() -> Env {
//     unimplemented!()
// }

/// Are we currently in a hook
pub fn in_relation_hook() -> bool {
    env::var("JUJU_RELATION").is_ok()
}

/// Scope for the current relation hook
pub fn relation_type() -> Option<String> {
    match env::var("JUJU_RELATION") {
        Ok(s) => Some(s),
        Err(_) => None,
    }
}

/// Local unit ID
pub fn local_unit() -> Option<String> {
    match env::var("JUJU_UNIT_NAME") {
        Ok(s) => Some(s),
        Err(_) => None
    }
}

/// Charm directory
pub fn charm_dir() -> Option<String> {
    match env::var("CHARM_DIR") {
        Ok(s) => Some(s),
        Err(_) => None
    }
}

/// The remote unit for the current relation hook
pub fn remote_unit() -> Option<String> {
    match env::var("JUJU_REMOTE_UNIT") {
        Ok(s) => Some(s),
        Err(_) => None
    }
}

/// The name of the service tha this unit belongs to
pub fn service_name() -> Option<String> {
    match local_unit() {
        Some(s) => {
            s.split('/').next().map(|s| s.to_string())
        },
        None => None
    }
}

/// Currently executing hook name
pub fn hook_name() -> Option<String> {
    match env::var("JUJU_HOOK_NAME") {
        Ok(s) => Some(s),
        Err(_) => env::args().next()
    }
}

/// id for the current relation
pub fn relation_id(relation_name: Option<&str>, service_or_unit: Option<&str>) -> Option<String> {
    if relation_name.is_none() && service_or_unit.is_none() {
        match env::var("JUJU_RELATION_ID") {
            Ok(val) => Some(val),
            Err(_) => None,
        }
    } else {
        if relation_name.is_some() && service_or_unit.is_some() {
            unimplemented!()
        } else {
            panic!("both relation name or service_or_unit or neither must be passed")
        }
    }
}

fn run_command(command: &str, arg_list: &Vec<String>, as_root: bool) -> Result<Output, JujuError>{
    if as_root{
        let mut cmd = Command::new("sudo");
        cmd.arg(command);
        for arg in arg_list{
            cmd.arg(&arg);
        }
        let output = try!(cmd.output());
        return Ok(output);
    } else {
       let mut cmd = Command::new(command);
        for arg in arg_list{
            cmd.arg(&arg);
        }
        let output = try!(cmd.output());
        return Ok(output);
    }
}