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
#[cfg(feature = "cluster_components")]
mod local;
mod k8;
mod tls;

use fmt::Display;
use structopt::StructOpt;
use std::{fmt, str::FromStr};

use crate::Terminal;
use crate::CliError;
use tls::TlsOpt;

use super::util::*;

#[cfg(target_os = "macos")]
fn get_log_directory() -> &'static str {
    "/usr/local/var/log/fluvio"
}

#[cfg(not(target_os = "macos"))]
fn get_log_directory() -> &'static str {
    "/tmp"
}

#[derive(Debug)]
pub struct DefaultVersion(String);

impl Default for DefaultVersion {
    fn default() -> Self {
        Self(crate::VERSION.to_string())
    }
}

impl Display for DefaultVersion {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl FromStr for DefaultVersion {
    type Err = std::io::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self(s.to_string()))
    }
}

#[derive(Debug)]
pub struct DefaultLogDirectory(String);

impl Default for DefaultLogDirectory {
    fn default() -> Self {
        Self(get_log_directory().to_string())
    }
}

impl Display for DefaultLogDirectory {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl FromStr for DefaultLogDirectory {
    type Err = std::io::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self(s.to_string()))
    }
}

#[derive(Debug, StructOpt)]
pub struct K8Install {
    /// k8: use specific chart version
    #[structopt(long, default_value)]
    pub chart_version: DefaultVersion,

    /// k8: use specific image version
    #[structopt(long)]
    pub image_version: Option<String>,

    /// k8: use custom docker registry
    #[structopt(long)]
    pub registry: Option<String>,

    /// k8
    #[structopt(long, default_value = "default")]
    pub namespace: String,

    /// k8
    #[structopt(long, default_value = "main")]
    pub group_name: String,

    /// helm chart installation name
    #[structopt(long, default_value = "fluvio")]
    pub install_name: String,

    /// Local path to a helm chart to install
    #[structopt(long)]
    pub chart_location: Option<String>,

    /// k8
    #[structopt(long, default_value = "minikube")]
    pub cloud: String,
}

#[derive(Debug, StructOpt)]
pub struct InstallCommand {
    /// use local image
    #[structopt(long)]
    pub develop: bool,

    #[structopt(flatten)]
    pub k8_config: K8Install,

    #[structopt(long)]
    pub skip_profile_creation: bool,

    /// number of SPU
    #[structopt(long, default_value = "1")]
    pub spu: u16,

    /// RUST_LOG options
    #[structopt(long)]
    pub rust_log: Option<String>,

    /// log dir
    #[structopt(long, default_value)]
    log_dir: DefaultLogDirectory,

    #[structopt(long)]
    /// installing sys
    sys: bool,

    /// install local spu/sc(custom)
    #[structopt(long)]
    local: bool,

    #[structopt(flatten)]
    tls: TlsOpt,
}

pub async fn process_install<O>(
    _out: std::sync::Arc<O>,
    command: InstallCommand,
) -> Result<String, CliError>
where
    O: Terminal,
{
    use k8::install_sys;
    use k8::install_core;

    let spu = command.spu;

    #[cfg(feature = "cluster_components")]
    use local::install_local;

    if command.sys {
        install_sys(command)?;
    } else if command.local {
        #[cfg(feature = "cluster_components")]
        install_local(command).await?;
        confirm_spu(spu).await?;
    } else {
        install_core(command).await?;
        confirm_spu(spu).await?;
    }

    Ok("".to_owned())
}

/// check to ensure spu are all running
async fn confirm_spu(spu: u16) -> Result<(), CliError> {
    use std::time::Duration;

    use fluvio_future::timer::sleep;
    use fluvio::Fluvio;
    use fluvio_cluster::ClusterError;
    use fluvio_controlplane_metadata::spu::SpuSpec;

    println!("waiting for spu to be provisioned");

    let client = Fluvio::connect().await.expect("sc ");
    let mut admin = client.admin().await;

    // wait for list of spu
    for _ in 0..30u16 {
        let spus = admin.list::<SpuSpec, _>(vec![]).await.expect("no spu list");
        let live_spus = spus
            .iter()
            .filter(|spu| spu.status.is_online() && !spu.spec.public_endpoint.ingress.is_empty())
            .count();
        if live_spus == spu as usize {
            println!("{} spus provisioned", spus.len());
            return Ok(());
        } else {
            println!("{} out of spu: {} up, waiting 1 sec", live_spus, spu);
            sleep(Duration::from_secs(1)).await;
        }
    }

    println!("waited too long,bailing out");
    Err(ClusterError::Other(format!("not able to provision:{} spu", spu)).into())
}