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
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
//! A library to interface with Juju.  For more information about Juju see
//! [Juju](https://jujucharms.com/docs/stable/about-juju)
//!
//! A hello world Juju charm example in Rust:
//! You will need a working Juju environment for this to function properly.  See [Setting up Juju]
//! (https://jujucharms.com/docs/stable/getting-started).  After Juju is functioning see
//! [What makes a Charm](https://jujucharms.com/docs/stable/authors-charm-components) for the base
//! components of a charm.
//!
//! Our src/main.rs will contain the following:
//! # Examples
//! ```
//! #[macro_use]
//! extern crate juju;
//! extern crate log;
//! use log::LogLevel;
//!
//! fn config_changed()->Result<(), String>{
//!     juju::log("Hello Juju from Rust!", Some(LogLevel::Debug));
//!     return Ok(());
//! }
//!
//! fn main(){
//!     let hook_registry: Vec<juju::Hook> = vec![
//!         hook!("config-changed", config_changed)
//!     ];
//!     let result =  juju::process_hooks(hook_registry);
//!
//!     if result.is_err(){
//!         juju::log(&format!("Hook failed with error: {:?}", result.err()),
//!             Some(LogLevel::Error));
//!     }else{
//!         juju::log("Hook call was successful!", Some(LogLevel::Debug));
//!     }
//! }
//! ```
//! Now you can build with `cargo build ` and install the binary in the hooks directory.
//!
//! Create a symlink in the hooks directory with `ln -s hello-world config-changed`.  Juju will
//! attempt to run that symlink and our Juju library will map that to our config_changed function.
//!
//! We can test our hello-world charm by deploying with juju and watching the debug logs. See
//! [Deploying a Charm](https://jujucharms.com/docs/stable/charms-deploying) for more information.
//!
//! You should see a message in juju debug-log like this `unit-hello-world-0[6229]: 2015-08-21
//! 16:16:05 INFO unit.hello-world/0.juju-log server.go:254 Hello Juju from Rust!`
//!

extern crate charmhelpers;
extern crate log;
extern crate memchr;
extern crate rusqlite;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;

use std::collections::HashMap;
use std::env;
use std::error::Error;
use std::fs::OpenOptions;
use std::fmt;
use std::io;
use std::io::prelude::*;
use std::net::IpAddr;
use std::path::Path;
use std::str::FromStr;

pub use log::LogLevel;
use memchr::memchr;

pub use charmhelpers::core::hookenv::log;

pub mod macros;
pub mod unitdata;

// Custom error handling for the library
#[derive(Debug)]
pub enum JujuError {
    AddrParseError(std::net::AddrParseError),
    FromUtf8Error(std::string::FromUtf8Error),
    IoError(io::Error),
    ParseIntError(std::num::ParseIntError),
    RusqliteError(rusqlite::Error),
    SerdeError(serde_json::Error),
    VarError(std::env::VarError),
}

impl JujuError {
    fn new(err: String) -> JujuError {
        JujuError::IoError(io::Error::new(std::io::ErrorKind::Other, err))
    }

    pub fn to_string(&self) -> String {
        match *self {
            JujuError::AddrParseError(ref err) => err.description().to_string(),
            JujuError::FromUtf8Error(ref err) => err.description().to_string(),
            JujuError::IoError(ref err) => err.description().to_string(),
            JujuError::ParseIntError(ref err) => err.description().to_string(),
            JujuError::RusqliteError(ref err) => err.description().to_string(),
            JujuError::SerdeError(ref err) => err.description().to_string(),
            JujuError::VarError(ref err) => err.description().to_string(),
        }
    }
}
impl fmt::Display for JujuError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(self.description())
    }
}

impl Error for JujuError {
    fn description(&self) -> &str {
        match *self {
            JujuError::AddrParseError(ref err) => err.description(),
            JujuError::FromUtf8Error(ref err) => err.description(),
            JujuError::IoError(ref err) => err.description(),
            JujuError::ParseIntError(ref err) => err.description(),
            JujuError::RusqliteError(ref err) => err.description(),
            JujuError::SerdeError(ref err) => err.description(),
            JujuError::VarError(ref err) => err.description(),
        }
    }
    fn cause(&self) -> Option<&Error> {
        match *self {
            JujuError::AddrParseError(ref err) => err.cause(),
            JujuError::FromUtf8Error(ref err) => err.cause(),
            JujuError::IoError(ref err) => err.cause(),
            JujuError::ParseIntError(ref err) => err.cause(),
            JujuError::SerdeError(ref err) => err.cause(),
            JujuError::RusqliteError(ref err) => err.cause(),
            JujuError::VarError(ref err) => err.cause(),
        }
    }
}


impl From<io::Error> for JujuError {
    fn from(err: io::Error) -> JujuError {
        JujuError::IoError(err)
    }
}

impl From<std::string::FromUtf8Error> for JujuError {
    fn from(err: std::string::FromUtf8Error) -> JujuError {
        JujuError::FromUtf8Error(err)
    }
}

impl From<std::num::ParseIntError> for JujuError {
    fn from(err: std::num::ParseIntError) -> JujuError {
        JujuError::ParseIntError(err)
    }
}

impl From<std::env::VarError> for JujuError {
    fn from(err: std::env::VarError) -> JujuError {
        JujuError::VarError(err)
    }
}

impl From<rusqlite::Error> for JujuError {
    fn from(err: rusqlite::Error) -> JujuError {
        JujuError::RusqliteError(err)
    }
}

impl From<serde_json::Error> for JujuError {
    fn from(err: serde_json::Error) -> JujuError {
        JujuError::SerdeError(err)
    }
}

impl From<std::net::AddrParseError> for JujuError {
    fn from(err: std::net::AddrParseError) -> JujuError {
        JujuError::AddrParseError(err)
    }
}


#[derive(Debug)]
pub enum Transport {
    Tcp,
    Udp,
}

impl Transport {
    /// Returns a String representation of the enum variant
    fn to_string(self) -> String {
        match self {
            Transport::Tcp => "tcp".to_string(),
            Transport::Udp => "udp".to_string(),
        }
    }
}

#[derive(Debug)]
/// For information about what these StatusType variants mean see: [Status reference]
/// (https://jujucharms.com/docs/stable/reference-status)
pub enum StatusType {
    Maintenance,
    Waiting,
    Active,
    Blocked,
}

impl StatusType {
    /// Returns a String representation of the enum variant
    pub fn to_string(self) -> String {
        match self {
            StatusType::Maintenance => "maintenance".to_string(),
            StatusType::Waiting => "waiting".to_string(),
            StatusType::Active => "active".to_string(),
            StatusType::Blocked => "blocked".to_string(),
        }
    }
}

#[derive(Debug)]
pub struct Status {
    /// The type of status
    pub status_type: StatusType,
    /// A message to show alongside the status
    pub message: String,
}

#[derive(Debug)]
pub struct Context {
    /// The scope for the current relation hook
    pub relation_type: String,
    /// The relation ID for the current relation hook
    pub relation_id: usize,
    /// Local unit ID
    pub unit: String,
    /// relation data for all related units
    pub relations: HashMap<String, String>,
}

impl Context {
    /// Constructs a new `Context`
    /// Creates a context that's filled out from the env variables
    /// # Example usage
    /// ```
    /// extern crate juju;
    /// let context = juju::Context::new_from_env();
    /// ```

    pub fn new_from_env() -> Context {
        let relations: HashMap<String, String> = HashMap::new();

        // This variable is useless.  It only shows "server" for everything
        let relation_type = env::var("JUJU_RELATION").unwrap_or("".to_string());
        let relation_id_str = env::var("JUJU_RELATION_ID").unwrap_or("".to_string());
        let parts: Vec<&str> = relation_id_str.split(":").collect();
        let relation_id: usize;
        if parts.len() > 1 {
            relation_id = parts[1].parse::<usize>().unwrap_or(0);
        } else {
            relation_id = 0;
        }
        let unit = env::var("JUJU_UNIT_NAME").unwrap_or("".to_string());

        Context {
            relation_type: relation_type,
            relation_id: relation_id,
            unit: unit,
            relations: relations,
        }
    }
}

/// A HashMap representation of the charm's config.yaml, with some
/// extra features:
/// - See which values in the HashMap have changed since the previous hook.
/// - For values that have changed, see what the previous value was.
/// - Store arbitrary data for use in a later hook.
#[derive(Debug)]
pub struct Config {
    values: HashMap<String, String>,
}

impl Config {
    /// Create a new Config and automatically load the previous config values if the
    /// .juju-persistent-config is present
    /// The .juju-persistent-config is also saved automatically when this struct is dropped.
    pub fn new() -> Result<Self, JujuError> {
        if Path::new(".juju-persistent-config").exists() {
            // Load the values from disk
            let mut file = OpenOptions::new().read(true).open(".juju-persistent-config")?;
            let mut s = String::new();
            file.read_to_string(&mut s)?;
            let previous_values: HashMap<String, String> = serde_json::from_str(&s)?;
            Ok(Config { values: previous_values })
        } else {
            // Initalize with all current values
            let current_values = config_get_all()?;
            Ok(Config { values: current_values })
        }
    }

    /// Return the current value for this key
    pub fn get(self, key: &str) -> Result<Option<String>, JujuError> {
        let current_value = config_get(key)?;
        Ok(current_value)
    }

    /// Return true if the current value for this key is different from
    /// the previous value.
    pub fn changed(self, key: &str) -> Result<bool, JujuError> {
        match self.values.get(key) {
            Some(previous_value) => {
                let current_value = config_get(key)?;
                match current_value {
                    Some(value) => Ok(&value != previous_value),
                    None => Ok(true),
                }
                //Ok(&current_value != previous_value)
            }
            // No previous key
            None => Ok(true),
        }
    }

    /// Return previous value for this key, or None if there
    /// is no previous value.
    pub fn previous(self, key: &str) -> Option<String> {
        match self.values.get(key) {
            Some(previous_value) => Some(previous_value.clone()),
            None => None,
        }
    }
}

impl Drop for Config {
    // Automatic saving of the .juju-persistent-config file when this struct is dropped
    fn drop(&mut self) {
        let mut file = match OpenOptions::new()
                  .write(true)
                  .truncate(true)
                  .create(true)
                  .open(".juju-persistent-config") {
            Ok(f) => f,
            Err(e) => {
                log(&format!("Unable to open .juju-persistent-config file for writing. Err: {}",
                             e),
                    Some(LogLevel::Error));
                return;
            }
        };
        let serialized = match serde_json::to_string(&self.values) {
            Ok(f) => f,
            Err(e) => {
                log(&format!("Unable to serialize Config values: {:?}.  Err: {}",
                             &self.values,
                             e),
                    Some(LogLevel::Error));
                return;
            }
        };
        match file.write(&serialized.as_bytes()) {
            Ok(bytes_written) => {
                log(&format!(".juju-persistent-config saved.  Wrote {} bytes",
                             bytes_written),
                    Some(LogLevel::Debug));
            }
            Err(e) => {
                log(&format!("Unable to write to .juju-persistent-config Err: {}", e),
                    Some(LogLevel::Error));
                return;
            }
        }
    }
}

#[derive(Debug)]
pub struct Relation {
    /// The name of a unit related to your service
    pub name: String,
    /// The id of the unit related to your service
    pub id: usize,
}

#[derive(Debug,PartialEq)]
pub struct Hook {
    /// The name of the hook to call
    pub name: String,
    /// A function to call when Juju calls this hook
    /// # Failures
    /// Your function passed in needs to return a String on error so that users will
    /// know what happened.  Ideally this should also be logged with juju::log
    pub callback: fn() -> Result<(), String>,
}

/// Returns 0 if the process completed successfully.
/// #Failures
/// Returns a String of the stderr if the process failed to execute
fn process_output(output: std::process::Output) -> Result<i32, JujuError> {
    let status = output.status;

    if status.success() {
        return Ok(0);
    } else {
        return Err(JujuError::new(String::from_utf8(output.stderr)?));
    }
}

/// Add metric values
/// See [Juju Metrics](https://jujucharms.com/docs/2.0/developer-metrics) for more
/// information
/// May only be called from the collect-metrics hook
/// # Failures
/// Returns stderr if the add_metric command fails
pub fn add_metric(key: &str, value: &str) -> Result<i32, JujuError> {
    let mut arg_list: Vec<String> = Vec::new();
    arg_list.push(format!("{}={}", key, value));

    let output = run_command("add-metric", &arg_list, false)?;
    return process_output(output);
}

/// Get the availability zone
/// # Failures
/// Returns stderr if the meter_status command fails
pub fn az_info() -> Result<String, JujuError> {
    let az = env::var("JUJU_AVAILABILITY_ZONE")?;
    return Ok(az);
}

/// Get the meter status, if running in the meter-status-changed hook
/// # Failures
/// Returns stderr if the meter_status command fails
pub fn meter_status() -> Result<String, JujuError> {
    let status = env::var("JUJU_METER_STATUS")?;
    return Ok(status);
}


/// Get the meter status information, if running in the meter-status-changed hook
/// # Failures
/// Returns stderr if the meter_info command fails
pub fn meter_info() -> Result<String, JujuError> {
    let info = env::var("JUJU_METER_INFO")?;
    return Ok(info);
}

/// This will reboot your juju instance.  Examples of using this are when a new kernel is installed
/// and the virtual machine or server needs to be rebooted to use it.
/// # Failures
/// Returns stderr if the reboot command fails
pub fn reboot() -> Result<i32, JujuError> {
    let output = run_command_no_args("juju-reboot", true)?;
    return process_output(output);
}
/// Charm authors may trigger this command from any hook to output what
/// version of the application is running. This could be a package version,
/// for instance postgres version 9.5. It could also be a build number or
/// version control revision identifier, for instance git sha 6fb7ba68.
/// # Failures
/// Returns stderr if the action_get command fails
pub fn application_version_set(version: &str) -> Result<i32, JujuError> {
    let mut arg_list: Vec<String> = Vec::new();
    arg_list.push(version.to_string());
    let output = run_command("application-version-set", &arg_list, false)?;
    return process_output(output);
}

/// action_get_all gets all values that are set
/// See [Juju Actions](https://jujucharms.com/docs/devel/authors-charm-actions) for more information
/// # Failures
/// Returns stderr if the action_get command fails
pub fn action_get_all() -> Result<HashMap<String, String>, JujuError> {
    let output = run_command_no_args("action-get", false)?;
    let values = String::from_utf8(output.stdout)?;
    let mut map: HashMap<String, String> = HashMap::new();

    for line in values.lines() {
        let parts: Vec<&str> = line.split(":").collect();
        if parts.len() == 2 {
            map.insert(parts[0].to_string(), parts[1].trim().to_string());
        }
    }
    return Ok(map);
}

/// action_get gets the value of the parameter at the given key
/// See [Juju Actions](https://jujucharms.com/docs/devel/authors-charm-actions) for more information
/// # Failures
/// Returns stderr if the action_get command fails
pub fn action_get(key: &str) -> Result<Option<String>, JujuError> {
    let mut arg_list: Vec<String> = Vec::new();
    arg_list.push(key.to_string());

    let output = run_command("action-get", &arg_list, false)?;
    let value = String::from_utf8(output.stdout)?.trim().to_string();
    if value.is_empty() {
        Ok(None)
    } else {
        Ok(Some(value))
    }
}

/// Get the name of the currently executing action
/// # Failures
/// Returns JujuError if the environment variable JUJU_ACTION_NAME does not exist
pub fn action_name() -> Result<String, JujuError> {
    let name = env::var("JUJU_ACTION_NAME")?;
    return Ok(name);
}

/// Get the uuid of the currently executing action
/// # Failures
/// Returns JujuError if the environment variable JUJU_ACTION_UUID does not exist
pub fn action_uuid() -> Result<String, JujuError> {
    let uuid = env::var("JUJU_ACTION_UUID")?;
    return Ok(uuid);
}

/// Get the tag of the currently executing action
/// # Failures
/// Returns JujuError if the environment variable JUJU_ACTION_TAG does not exist
pub fn action_tag() -> Result<String, JujuError> {
    let tag = env::var("JUJU_ACTION_TAG")?;
    return Ok(tag);
}

/// action_set permits the Action to set results in a map to be returned at completion of
/// the Action.
/// See [Juju Actions](https://jujucharms.com/docs/devel/authors-charm-actions) for more
/// information
/// # Failures
/// Returns stderr if the action_set command fails
pub fn action_set(key: &str, value: &str) -> Result<i32, JujuError> {
    let mut arg_list: Vec<String> = Vec::new();
    arg_list.push(format!("{}={}", key, value));

    let output = run_command("action-set", &arg_list, false)?;
    return process_output(output);
}

/// See [Juju Actions](https://jujucharms.com/docs/devel/authors-charm-actions) for more
/// information
/// # Failures
/// Returns stderr if the action_fail command fails
pub fn action_fail(msg: &str) -> Result<i32, JujuError> {
    let mut arg_list: Vec<String> = Vec::new();
    arg_list.push(msg.to_string());

    let output = run_command("action-fail", &arg_list, false)?;
    return process_output(output);
}

/// This will return the private IP address associated with the unit.
/// It can be very useful for services that require communicating with the other units related
/// to it.
/// # Failures
/// Will return a String of the stderr if the call fails
pub fn unit_get_private_addr() -> Result<IpAddr, JujuError> {
    let mut arg_list: Vec<String> = Vec::new();
    arg_list.push("private-address".to_string());

    let output = run_command("unit-get", &arg_list, false)?;
    let private_addr: String = String::from_utf8(output.stdout)?;
    let ip = IpAddr::from_str(private_addr.trim())?;
    return Ok(ip);
}

/// This will return the public IP address associated with the unit.
/// # Failures
/// Will return a String of the stderr if the call fails
pub fn unit_get_public_addr() -> Result<IpAddr, JujuError> {
    let mut arg_list: Vec<String> = Vec::new();
    arg_list.push("public-address".to_string());

    let output = run_command("unit-get", &arg_list, false)?;
    let public_addr = String::from_utf8(output.stdout)?;
    let ip = IpAddr::from_str(public_addr.trim())?;
    return Ok(ip);
}

/// This will return a configuration item that corresponds to the key passed in
/// # Failures
/// Will return a String of the stderr if the call fails
pub fn config_get(key: &str) -> Result<Option<String>, JujuError> {
    let mut arg_list: Vec<String> = Vec::new();
    arg_list.push(key.to_string());

    let output = run_command("config-get", &arg_list, false)?;
    let value = String::from_utf8(output.stdout)?.trim().to_string();
    if value.is_empty() {
        Ok(None)
    } else {
        Ok(Some(value))
    }
}

/// config_get_all will return all configuration options as a HashMap<String,String>
/// # Failures
/// Returns a String of if the configuration options are not able to be transformed into a HashMap
pub fn config_get_all() -> Result<HashMap<String, String>, JujuError> {
    let mut values: HashMap<String, String> = HashMap::new();

    let arg_list: Vec<String> = vec!["--all".to_string()];
    let output = run_command("config-get", &arg_list, false)?;
    let output_str = String::from_utf8(output.stdout)?;
    //  Example output:
    // "brick_paths: /mnt/brick1 /mnt/brick2\ncluster_type: Replicate\n"
    //
    // For each line split at : and load the parts into the HashMap
    for line in output_str.lines() {
        if let Some(position) = memchr(b':', &line.as_bytes()) {
            values.insert(line[0..position].trim().to_string(),
                          line[position + 1..].trim().to_string());
        }
        // Skip blank lines or failed splits
    }

    return Ok(values);
}

/// This will expose a port on the unit.  The transport argument will indicate whether tcp or udp
/// should be exposed
/// # Failures
/// Will return a String of the stderr if the call fails
pub fn open_port(port: usize, transport: Transport) -> Result<i32, JujuError> {
    let mut arg_list: Vec<String> = Vec::new();
    let port_string = format!("{}/{}", port.to_string(), transport.to_string());

    arg_list.push(port_string);
    let output = run_command("open-port", &arg_list, false)?;
    return process_output(output);
}

/// This will hide a port on the unit.  The transport argument will indicate whether tcp or udp
/// should be exposed
/// # Failures
/// Will return a String of the stderr if the call fails
pub fn close_port(port: usize, transport: Transport) -> Result<i32, JujuError> {
    let mut arg_list: Vec<String> = Vec::new();
    let port_string = format!("{}/{}", port.to_string(), transport.to_string());

    arg_list.push(port_string);
    let output = run_command("close-port", &arg_list, false)?;
    return process_output(output);
}

/// Set relation information for the current unit
/// # Failures
/// Will return a String of the stderr if the call fails
pub fn relation_set(key: &str, value: &str) -> Result<i32, JujuError> {
    let mut arg_list: Vec<String> = Vec::new();
    let arg = format!("{}={}", key.clone(), value);

    arg_list.push(arg);
    let output = run_command("relation-set", &arg_list, false)?;
    return process_output(output);
}
/// Sets relation information using a specific relation ID. Used outside of relation hooks
/// # Failures
/// Will return a String of the stderr if the call fails
pub fn relation_set_by_id(key: &str, value: &str, id: &Relation) -> Result<String, JujuError> {
    let mut arg_list: Vec<String> = Vec::new();

    arg_list.push(format!("-r {}:{}", id.name, id.id.to_string()));
    arg_list.push(format!("{}={}", key, value).to_string());

    let output = run_command("relation-set", &arg_list, false)?;
    let relation = String::from_utf8(output.stdout)?;
    return Ok(relation);
}

/// Get relation information for the current unit
/// # Failures
/// Will return a String of the stderr if the call fails
pub fn relation_get(key: &str) -> Result<Option<String>, JujuError> {
    let mut arg_list: Vec<String> = Vec::new();
    arg_list.push(key.to_string());
    let output = run_command("relation-get", &arg_list, false)?;
    let value = String::from_utf8(output.stdout)?.trim().to_string();
    if value.is_empty() {
        Ok(None)
    } else {
        Ok(Some(value))
    }
}

/// Get relation information for a specific unit
/// # Failures
/// Will return a String of the stderr if the call fails
pub fn relation_get_by_unit(key: &str, unit: &Relation) -> Result<Option<String>, JujuError> {
    let mut arg_list: Vec<String> = Vec::new();
    arg_list.push(key.to_string());
    arg_list.push(format!("{}/{}", unit.name, unit.id.to_string()));

    let output = run_command("relation-get", &arg_list, false)?;
    let relation = String::from_utf8(output.stdout)?.trim().to_string();
    if relation.is_empty() {
        Ok(None)
    } else {
        Ok(Some(relation))
    }
}

/// Get relation information using a specific relation ID. Used outside of relation hooks
/// # Failures
/// Will return a String of the stderr if the call fails
pub fn relation_get_by_id(key: &str,
                          id: &Relation,
                          unit: &Relation)
                          -> Result<Option<String>, JujuError> {
    let mut arg_list: Vec<String> = Vec::new();

    arg_list.push(format!("-r {}:{}", id.name, id.id.to_string()));
    arg_list.push(format!("{}", key.to_string()));
    arg_list.push(format!("{}/{}", unit.name, unit.id.to_string()));

    let output = run_command("relation-get", &arg_list, false)?;
    let relation = String::from_utf8(output.stdout)?.trim().to_string();
    if relation.is_empty() {
        Ok(None)
    } else {
        Ok(Some(relation))
    }
}

/// Returns a list of all related units
/// # Failures
/// Will return a String of the stderr if the call fails
pub fn relation_list() -> Result<Vec<Relation>, JujuError> {
    let mut related_units: Vec<Relation> = Vec::new();

    let output = run_command_no_args("relation-list", false)?;
    let output_str = String::from_utf8(output.stdout)?;

    log(&format!("relation-list output: {}", output_str),
        Some(LogLevel::Debug));

    for line in output_str.lines() {
        let v: Vec<&str> = line.split('/').collect();
        let id: usize = v[1].parse::<usize>()?;
        let r: Relation = Relation {
            name: v[0].to_string(),
            id: id,
        };
        related_units.push(r);
    }
    return Ok(related_units);
}

/// Returns a list of all related units for the supplied identifier
/// # Failures
/// Will return a String of the stderr if the call fails
pub fn relation_list_by_id(id: &Relation) -> Result<Vec<Relation>, JujuError> {
    let mut related_units: Vec<Relation> = Vec::new();
    let mut arg_list: Vec<String> = Vec::new();

    arg_list.push(format!("-r {}:{}", id.name, id.id.to_string()));

    let output = run_command("relation-list", &arg_list, false)?;
    let output_str = String::from_utf8(output.stdout)?;

    log(&format!("relation-list output: {}", output_str),
        Some(LogLevel::Debug));

    for line in output_str.lines() {
        let v: Vec<&str> = line.split('/').collect();
        let id: usize = v[1].parse::<usize>()?;
        let r: Relation = Relation {
            name: v[0].to_string(),
            id: id,
        };
        related_units.push(r);
    }
    return Ok(related_units);
}

pub fn relation_ids() -> Result<Vec<Relation>, JujuError> {
    let mut related_units: Vec<Relation> = Vec::new();
    let output = run_command_no_args("relation-ids", false)?;
    let output_str: String = String::from_utf8(output.stdout)?;
    log(&format!("relation-ids output: {}", output_str),
        Some(LogLevel::Debug));

    for line in output_str.lines() {
        let v: Vec<&str> = line.split(':').collect();
        let id: usize = v[1].parse::<usize>()?;
        let r: Relation = Relation {
            name: v[0].to_string(),
            id: id,
        };
        related_units.push(r);
    }
    return Ok(related_units);
}

/// Gets the relation IDs by their identifier
/// # Failures
/// Will return a String of the stderr if the call fails

pub fn relation_ids_by_identifier(id: &str) -> Result<Vec<Relation>, JujuError> {
    let mut related_units: Vec<Relation> = Vec::new();
    let mut arg_list: Vec<String> = Vec::new();

    arg_list.push(id.to_string());

    let output = run_command("relation-ids", &arg_list, false)?;
    let output_str: String = String::from_utf8(output.stdout)?;
    log(&format!("relation-ids output: {}", output_str),
        Some(LogLevel::Debug));

    for line in output_str.lines() {
        let v: Vec<&str> = line.split(':').collect();
        let id: usize = v[1].parse::<usize>()?;
        let r: Relation = Relation {
            name: v[0].to_string(),
            id: id,
        };
        related_units.push(r);
    }
    return Ok(related_units);
}

/// Set the status of your unit to indicate to the Juju if everything is ok or something is wrong.
/// See the Status enum for information about what can be set.
/// # Failures
/// Will return a String of the stderr if the call fails
pub fn status_set(status: Status) -> Result<i32, JujuError> {
    let mut arg_list: Vec<String> = Vec::new();
    arg_list.push(status.status_type.to_string());
    arg_list.push(status.message);

    let output = run_command("status-set", &arg_list, false)?;
    return process_output(output);
}

/// Retrieve the previously set juju workload state
/// # Failures
/// Will return a String of the stderr if the call fails
pub fn status_get() -> Result<String, JujuError> {
    let output = run_command_no_args("status-get", false)?;
    return Ok(String::from_utf8(output.stdout)?);
}

/// If storage drives were allocated to your unit this will get the path of them.
/// In the storage-attaching hook this will tell you the location where the storage
/// is attached to.  IE: /dev/xvdf for block devices or /mnt/{name} for filesystem devices
/// # Failures
/// Will return a String of the stderr if the call fails
pub fn storage_get_location() -> Result<Option<String>, JujuError> {
    let mut arg_list: Vec<String> = Vec::new();
    arg_list.push("location".to_string());
    let output = run_command("storage-get", &arg_list, false)?;
    let stdout = String::from_utf8(output.stdout)?.trim().to_string();
    if stdout.is_empty() {
        Ok(None)
    } else {
        Ok(Some(stdout))
    }
}

/// Return the location of the mounted storage device.  The mounted
/// storage devices can be gotten by calling storage_list() and
/// then passed into this function to get their mount location.
/// # Failures
/// Will return a String of the stderr if the call fails
pub fn storage_get(name: &str) -> Result<Option<String>, JujuError> {
    let mut arg_list: Vec<String> = Vec::new();
    arg_list.push("-s".to_string());
    arg_list.push(name.to_string());
    arg_list.push("location".to_string());
    let output = run_command("storage-get", &arg_list, false)?;
    let stdout = String::from_utf8(output.stdout)?.trim().to_string();
    if stdout.is_empty() {
        Ok(None)
    } else {
        Ok(Some(stdout))
    }
}

/// Used to list storage instances that are attached to the unit.
/// The names returned may be passed through to storage_get
/// # Failures
/// Will return a String of the stderr if the call fails
pub fn storage_list() -> Result<String, JujuError> {
    let output = run_command_no_args("storage-list", false)?;
    return Ok(String::from_utf8(output.stdout)?);
}

/// Call this to process your cmd line arguments and call any needed hooks
/// # Examples
/// ```
///     extern crate juju;
///     extern crate log;
///
///     fn config_changed()->Result<(), String>{
///         //Do nothing
///         return Ok(());
///    }
///
///     let mut hook_registry: Vec<juju::Hook> = Vec::new();
///
///     //Register our hooks with the Juju library
///     hook_registry.push(juju::Hook{
///         name: "config-changed".to_string(),
///         callback: config_changed,
///     });
///     let result =  juju::process_hooks(hook_registry);
///
///     if result.is_err(){
///         juju::log(&format!("Hook failed with error: {:?}", result.err()),
///         Some(log::LogLevel::Error));
///     }
/// ```
///
pub fn process_hooks(registry: Vec<Hook>) -> Result<(), String> {
    let hook_name = match charmhelpers::core::hookenv::hook_name() {
        Some(s) => s,
        _ => "".to_string(),
    };

    for hook in registry {
        if hook_name.contains(&hook.name) {
            return (hook.callback)();
        }
    }
    return Err(format!("Warning: Unknown callback for hook {}", hook_name));
}

/// Juju leader get value(s)
/// # Failures
/// Will return stderr as a String if the function fails to run
pub fn leader_get(attribute: Option<String>) -> Result<Option<String>, JujuError> {
    let arg_list: Vec<String>;
    match attribute {
        Some(a) => arg_list = vec![a],
        None => arg_list = vec!['-'.to_string()],
    };
    let output = run_command("leader-get", &arg_list, false)?;
    let value = String::from_utf8(output.stdout)?.trim().to_string();
    if value.is_empty() {
        Ok(None)
    } else {
        Ok(Some(value))
    }
}


/// Juju leader set value(s)
/// # Failures
/// Will return stderr as a String if the function fails to run
pub fn leader_set(settings: HashMap<String, String>) -> Result<i32, JujuError> {
    let mut arg_list: Vec<String> = Vec::new();
    for (key, value) in settings {
        arg_list.push(format!("{}={}", key, value));
    }

    let output = run_command("leader-set", &arg_list, false)?;
    return process_output(output);
}

/// Returns true/false if this unit is the leader
/// # Failures
/// Will return stderr as a String if the function fails to run
/// # Examples
/// ```
/// extern crate juju;
/// let leader = match juju::is_leader(){
///   Ok(l) => l,
///   Err(e) => {
///     println!("Failed to run.  Error was {:?}", e);
///     //Bail
///     return;
///   },
/// };
/// if leader{
///   println!("I am the leader!");
/// }else{
///   println!("I am not the leader.  Maybe later I will be promoted");
/// }
/// ```
///
pub fn is_leader() -> Result<bool, JujuError> {
    let output = run_command_no_args("is-leader", false)?;
    let output_str: String = String::from_utf8(output.stdout)?;
    match output_str.trim().as_ref() {
        "True" => Ok(true),
        "False" => Ok(false),
        _ => Ok(false),
    }
}

fn run_command_no_args(command: &str, as_root: bool) -> Result<std::process::Output, JujuError> {
    if as_root {
        let mut cmd = std::process::Command::new("sudo");
        let output = cmd.output()?;
        return Ok(output);
    } else {
        let mut cmd = std::process::Command::new(command);
        let output = cmd.output()?;
        return Ok(output);
    }
}

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