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
mod builder;
mod local;
pub mod util;
use std::env::current_dir;
use std::path::PathBuf;
use anyhow::Result;
use clap::Parser;
use self::util::env_file_to_map;
use crate::commands::containers::types::ContainerOptions;
use crate::commands::containers::utils::create_containers;
use crate::commands::gateways::create::GatewayOptions;
use crate::commands::gateways::types::{GatewayConfig, GatewayType};
use crate::commands::gateways::util::{create_gateway, update_gateway_config};
use crate::commands::ignite::create::{
DeploymentConfig, Options as CreateOptions, WEB_DEPLOYMENTS_URL,
};
use crate::commands::ignite::types::{
CreateDeployment, Deployment, ScalingStrategy, SingleDeployment,
};
use crate::commands::ignite::util::{create_deployment, rollout, update_deployment_config};
use crate::commands::projects::util::format_project;
use crate::state::State;
use crate::store::hopfile::HopFile;
use crate::util::urlify;
const HOP_BUILD_BASE_URL: &str = "https://builder.hop.io/v1";
const HOP_REGISTRY_URL: &str = "registry.hop.io";
#[derive(Debug, Parser)]
#[clap(about = "Deploy a new container")]
pub struct Options {
#[clap(
name = "dir",
help = "Directory to deploy, defaults to current directory"
)]
path: Option<PathBuf>,
#[clap(flatten)]
config: DeploymentConfig,
#[clap(
short = 'E',
long = "env-file",
help = "Load environment variables from a .env file in the current directory, in the form of KEY=VALUE"
)]
envfile: bool,
#[clap(
short = 'y',
long = "yes",
help = "Use the default yes answer to all prompts"
)]
yes: bool,
#[clap(
short = 'l',
long = "local",
help = "Build the container locally using nixpacks or docker instead of using the builder"
)]
local: bool,
}
pub async fn handle(options: Options, state: State) -> Result<()> {
let mut dir = current_dir().expect("Could not get current directory");
if let Some(path) = options.path {
dir = dir
.join(path)
.canonicalize()
.expect("Could not get canonical path");
}
assert!(dir.is_dir(), "{} is not a directory", dir.display());
log::info!("Attempting to deploy {}", dir.display());
let is_not_guided = options.config != DeploymentConfig::default();
let (project, deployment, container_options, existing) = match HopFile::find(dir.clone()).await
{
Some(hopfile) => {
log::info!("Found hopfile: {}", hopfile.path.display());
let deployment = state
.http
.request::<SingleDeployment>(
"GET",
&format!("/ignite/deployments/{}", hopfile.config.deployment_id),
None,
)
.await
.expect("Failed to get deployment")
.unwrap()
.deployment;
let project = state
.ctx
.find_project_by_id_or_namespace(hopfile.config.project_id)
.unwrap();
if is_not_guided {
log::warn!("Deployment exists, skipping arguments");
}
log::info!("Deploying to project {}", format_project(&project));
let container_options = ContainerOptions {
containers: Some(deployment.container_count),
min_containers: None,
max_containers: None,
};
(project, deployment, container_options, true)
}
None => {
log::info!("No hopfile found, creating one");
let project = state.ctx.clone().current_project_error();
log::info!("Deploying to project {}", format_project(&project));
let default_name = dir
.clone()
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_string()
.replace('_', "-")
.replace(' ', "-")
.replace('.', "-")
.to_lowercase();
let (mut deployment_config, container_options) = if options.yes {
log::warn!("Using default config, skipping arguments");
(
CreateDeployment {
name: Some(default_name),
container_strategy: ScalingStrategy::Manual,
..Default::default()
},
ContainerOptions {
containers: Some(1),
min_containers: None,
max_containers: None,
},
)
} else {
update_deployment_config(
CreateOptions {
config: options.config.clone(),
image: Some("".to_string()),
},
is_not_guided,
&Deployment::default(),
&Some(default_name),
)
};
deployment_config.image.name = format!(
"{}/{}/{}",
HOP_REGISTRY_URL,
project.namespace,
deployment_config.name.clone().unwrap()
);
if options.envfile {
deployment_config
.env
.extend(env_file_to_map(dir.join(".env")).await);
}
let deployment =
create_deployment(&state.http, &project.id, &deployment_config).await?;
if !options.yes
&& !is_not_guided
&& dialoguer::Confirm::new()
.with_prompt("Do you want to create a Gateway? (You can always add one later)")
.interact()?
{
let gateway_config = update_gateway_config(
&GatewayOptions::default(),
false,
&GatewayConfig::default(),
)?;
let gateway = create_gateway(&state.http, &deployment.id, &gateway_config).await?;
log::info!("Created Gateway `{}`", gateway.id);
if gateway.type_ == GatewayType::External {
log::info!(
"Your deployment will be accesible via {}",
urlify(&gateway.full_url())
);
}
}
HopFile::new(
dir.clone().join("hop.yml"),
project.id.clone(),
deployment.id.clone(),
)
.save()
.await?;
(project, deployment, container_options, false)
}
};
if !options.local {
builder::build(&state, &project.id, &deployment.id, dir.clone()).await?;
} else {
local::build(&state, &deployment.config.image.name, dir.clone()).await?;
}
if existing {
if deployment.container_count > 0 {
log::info!("Rolling out new containers");
rollout(&state.http, &deployment.id).await?;
}
} else if let Some(containers) = container_options
.containers
.or(container_options.min_containers)
{
create_containers(&state.http, &deployment.id, containers).await?;
}
log::info!(
"Deployed successfuly, you can find it at: {}",
urlify(&format!(
"{}{}?project={}",
WEB_DEPLOYMENTS_URL, deployment.id, project.namespace
))
);
Ok(())
}