cargo-shuttle 0.57.3

CLI for the Shuttle platform (shuttle.dev)
Documentation
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
use std::{
    collections::HashMap, convert::Infallible, io::stderr, net::SocketAddr, process::exit,
    sync::Arc, time::Duration,
};

use anyhow::{bail, Context, Result};
use bollard::{
    container::{Config, CreateContainerOptions, StartContainerOptions},
    exec::{CreateExecOptions, CreateExecResults},
    image::CreateImageOptions,
    models::{CreateImageInfo, HostConfig, PortBinding, ProgressDetail},
    service::ContainerInspectResponse,
    Docker,
};
use crossterm::{
    cursor::{MoveDown, MoveUp},
    terminal::{Clear, ClearType},
    QueueableCommand,
};
use futures::StreamExt;
use http_body_util::{combinators::BoxBody, BodyExt, Empty, Full};
use hyper::{
    body::{self, Bytes},
    server::conn::http1,
    service::service_fn,
    Method, Request as HyperRequest, Response,
};
use hyper_util::rt::TokioIo;
use portpicker::pick_unused_port;
use shuttle_common::{
    models::resource::{
        self, ProvisionResourceRequest, ResourceResponse, ResourceState, ResourceType,
    },
    secrets::Secret,
    tables::get_resource_tables,
    ContainerRequest, ContainerResponse, DatabaseInfo, DbInput,
};
use tokio::{net::TcpListener, time::sleep};
use tracing::{debug, error, trace};

/// A provisioner for local runs
/// It uses Docker to create Databases
pub struct LocalProvisioner {
    docker: Docker,
}

impl LocalProvisioner {
    pub fn new() -> Result<Self> {
        // This only constructs the client and does not try to connect.
        // If the socket is not found, a "no such file" error will happen on the first request to Docker.
        Ok(Self {
            docker: Docker::connect_with_defaults()?,
        })
    }

    fn get_container_first_host_port(
        &self,
        container: &ContainerInspectResponse,
        port: &str,
    ) -> String {
        container
            .host_config
            .as_ref()
            .expect("container to have host config")
            .port_bindings
            .as_ref()
            .expect("port bindings on container")
            .get(port)
            .expect("a port bindings entry")
            .as_ref()
            .expect("a port bindings")
            .first()
            .expect("at least one port binding")
            .host_port
            .as_ref()
            .expect("a host port")
            .clone()
    }

    async fn start_container_if_not_running(
        &self,
        container: &ContainerInspectResponse,
        container_type: &str,
        name: &str,
    ) {
        if !container
            .state
            .as_ref()
            .expect("container to have a state")
            .running
            .expect("state to have a running key")
        {
            trace!("{container_type} container '{name}' not running, so starting it");
            self.docker
                .start_container(name, None::<StartContainerOptions<String>>)
                .await
                .expect("failed to start container");
        }
    }

    async fn get_container(
        &self,
        container_name: &str,
        image: &str,
        port: &str,
        env: Option<Vec<String>>,
    ) -> Result<ContainerInspectResponse> {
        match self.docker.inspect_container(container_name, None).await {
            Ok(container) => {
                trace!("found container {container_name}");
                Ok(container)
            }
            Err(bollard::errors::Error::DockerResponseServerError {
                status_code: 404, ..
            }) => {
                self.pull_image(image).await.expect("failed to pull image");
                trace!("will create container {container_name}");
                let options = Some(CreateContainerOptions {
                    name: container_name,
                    platform: None,
                });
                let mut port_bindings = HashMap::new();
                let host_port = pick_unused_port().expect("system to have a free port");
                port_bindings.insert(
                    port.to_string(),
                    Some(vec![PortBinding {
                        host_port: Some(host_port.to_string()),
                        ..Default::default()
                    }]),
                );
                let host_config = HostConfig {
                    port_bindings: Some(port_bindings),
                    ..Default::default()
                };

                let config: Config<String> = Config {
                    image: Some(image.to_string()),
                    env,
                    host_config: Some(host_config),
                    ..Default::default()
                };

                self.docker
                    .create_container(options, config)
                    .await
                    .expect("to be able to create container");

                Ok(self
                    .docker
                    .inspect_container(container_name, None)
                    .await
                    .expect("container to be created"))
            }
            Err(error) => {
                error!("Got unexpected error while inspecting docker container: {error}");
                error!(
                    "Make sure Docker is installed and running. For more help: https://docs.shuttle.dev/docs/local-run#docker-engines"
                );
                Err(anyhow::anyhow!("{}", error))
            }
        }
    }

    async fn get_db_connection_string(
        &self,
        project_name: &str,
        db_type: ResourceType,
        db_name: Option<String>,
    ) -> Result<DatabaseInfo> {
        trace!("getting sql string for project '{project_name}'");

        let database_name = match db_type {
            ResourceType::DatabaseAwsRdsPostgres
            | ResourceType::DatabaseAwsRdsMySql
            | ResourceType::DatabaseAwsRdsMariaDB => {
                db_name.unwrap_or_else(|| project_name.to_string())
            }
            _ => project_name.to_string(),
        };

        let EngineConfig {
            r#type,
            image,
            engine,
            username,
            password,
            port,
            env,
            is_ready_cmd,
        } = db_type_to_config(db_type, &database_name);
        let container_name = format!("shuttle_{project_name}_{type}");

        let container = self
            .get_container(&container_name, &image, &port, env)
            .await?;

        let host_port = self.get_container_first_host_port(&container, &port);

        self.start_container_if_not_running(&container, &r#type, &container_name)
            .await;

        self.wait_for_ready(&container_name, is_ready_cmd.clone())
            .await?;

        // The container enters the ready state, runs an init script and then reboots, so we sleep
        // a little and then check if it's ready again afterwards.
        sleep(Duration::from_millis(450)).await;
        self.wait_for_ready(&container_name, is_ready_cmd).await?;

        let res = DatabaseInfo::new(
            engine,
            username,
            password.expose().clone(),
            database_name,
            host_port,
            "localhost".to_string(),
            None,
        );

        Ok(res)
    }

    pub async fn start_container(&self, req: ContainerRequest) -> Result<ContainerResponse> {
        let ContainerRequest {
            project_name,
            container_name,
            env,
            image,
            port,
        } = req;

        let container_name = format!("shuttle_{project_name}_{container_name}");

        let container = self
            .get_container(&container_name, &image, &port, Some(env))
            .await?;

        let host_port = self.get_container_first_host_port(&container, &port);

        self.start_container_if_not_running(&container, &container_name, &container_name)
            .await;

        Ok(ContainerResponse { host_port })
    }

    async fn wait_for_ready(&self, container_name: &str, is_ready_cmd: Vec<String>) -> Result<()> {
        loop {
            trace!("waiting for '{container_name}' to be ready for connections");

            let config = CreateExecOptions {
                cmd: Some(is_ready_cmd.clone()),
                attach_stdout: Some(true),
                attach_stderr: Some(true),
                ..Default::default()
            };

            let CreateExecResults { id } = self
                .docker
                .create_exec(container_name, config)
                .await
                .expect("failed to create exec to check if container is ready");

            let ready_result = self
                .docker
                .start_exec(&id, None)
                .await
                .expect("failed to execute ready command");

            if let bollard::exec::StartExecResults::Attached { mut output, .. } = ready_result {
                while let Some(line) = output.next().await {
                    trace!("line: {:?}", line);

                    if let bollard::container::LogOutput::StdOut { .. } =
                        line.expect("output to have a log line")
                    {
                        return Ok(());
                    }
                }
            }

            sleep(Duration::from_millis(500)).await;
        }
    }

    async fn pull_image(&self, image: &str) -> Result<(), String> {
        trace!("pulling latest image for '{image}'");
        let mut layers = Vec::new();

        let create_image_options = Some(CreateImageOptions {
            from_image: image,
            ..Default::default()
        });
        let mut output = self.docker.create_image(create_image_options, None, None);

        while let Some(line) = output.next().await {
            let info = line.expect("failed to create image");

            if let Some(id) = info.id.as_ref() {
                match layers
                    .iter_mut()
                    .find(|item: &&mut CreateImageInfo| item.id.as_deref() == Some(id))
                {
                    Some(item) => *item = info,
                    None => layers.push(info),
                }
            } else {
                layers.push(info);
            }

            print_layers(&layers);
        }

        // Undo last MoveUps
        stderr()
            .queue(MoveDown(
                layers.len().try_into().expect("to convert usize to u16"),
            ))
            .expect("to reset cursor position");

        Ok(())
    }
}

fn print_layers(layers: &Vec<CreateImageInfo>) {
    for info in layers {
        stderr()
            .queue(Clear(ClearType::CurrentLine))
            .expect("to be able to clear line");

        if let Some(id) = info.id.as_ref() {
            let text = match (info.status.as_deref(), info.progress_detail.as_ref()) {
                (
                    Some("Downloading"),
                    Some(ProgressDetail {
                        current: Some(c),
                        total: Some(t),
                    }),
                ) => {
                    let percent = *c as f64 / *t as f64 * 100.0;
                    let progress = (percent as i64 / 10) as usize;
                    let remaining = 10 - progress;
                    format!("{:=<progress$}>{:remaining$}   {percent:.0}%", "", "")
                }
                (Some(status), _) => status.to_string(),
                _ => "Unknown".to_string(),
            };
            println!("[{id} {text}]");
        } else {
            println!(
                "{}",
                info.status.as_ref().expect("image info to have a status")
            )
        }
    }
    stderr()
        .queue(MoveUp(
            layers.len().try_into().expect("to convert usize to u16"),
        ))
        .expect("to reset cursor position");
}

struct EngineConfig {
    r#type: String,
    image: String,
    engine: String,
    username: String,
    password: Secret<String>,
    port: String,
    env: Option<Vec<String>>,
    is_ready_cmd: Vec<String>,
}

fn db_type_to_config(db_type: ResourceType, database_name: &str) -> EngineConfig {
    match db_type {
        ResourceType::DatabaseSharedPostgres => EngineConfig {
            r#type: "shared_postgres".to_string(),
            image: "docker.io/library/postgres:16".to_string(),
            engine: "postgres".to_string(),
            username: "postgres".to_string(),
            password: "postgres".to_string().into(),
            port: "5432/tcp".to_string(),
            env: Some(vec![
                "POSTGRES_PASSWORD=postgres".to_string(),
                format!("POSTGRES_DB={database_name}"),
            ]),
            is_ready_cmd: vec![
                "/bin/sh".to_string(),
                "-c".to_string(),
                "pg_isready | grep 'accepting connections'".to_string(),
            ],
        },
        ResourceType::DatabaseAwsRdsPostgres => EngineConfig {
            r#type: "aws_rds_postgres".to_string(),
            image: "docker.io/library/postgres:16".to_string(),
            engine: "postgres".to_string(),
            username: "postgres".to_string(),
            password: "postgres".to_string().into(),
            port: "5432/tcp".to_string(),
            env: Some(vec![
                "POSTGRES_PASSWORD=postgres".to_string(),
                format!("POSTGRES_DB={database_name}"),
            ]),
            is_ready_cmd: vec![
                "/bin/sh".to_string(),
                "-c".to_string(),
                "pg_isready | grep 'accepting connections'".to_string(),
            ],
        },
        ResourceType::DatabaseAwsRdsMariaDB => EngineConfig {
            r#type: "aws_rds_mariadb".to_string(),
            image: "docker.io/library/mariadb:10.6.7".to_string(),
            engine: "mariadb".to_string(),
            username: "root".to_string(),
            password: "mariadb".to_string().into(),
            port: "3306/tcp".to_string(),
            env: Some(vec![
                "MARIADB_ROOT_PASSWORD=mariadb".to_string(),
                format!("MARIADB_DATABASE={database_name}"),
            ]),
            is_ready_cmd: vec![
                "mysql".to_string(),
                "-pmariadb".to_string(),
                "--silent".to_string(),
                "-e".to_string(),
                "show databases;".to_string(),
            ],
        },
        ResourceType::DatabaseAwsRdsMySql => EngineConfig {
            r#type: "aws_rds_mysql".to_string(),
            image: "docker.io/library/mysql:8.0.28".to_string(),
            engine: "mysql".to_string(),
            username: "root".to_string(),
            password: "mysql".to_string().into(),
            port: "3306/tcp".to_string(),
            env: Some(vec![
                "MYSQL_ROOT_PASSWORD=mysql".to_string(),
                format!("MYSQL_DATABASE={database_name}"),
            ]),
            is_ready_cmd: vec![
                "mysql".to_string(),
                "-pmysql".to_string(),
                "--silent".to_string(),
                "-e".to_string(),
                "show databases;".to_string(),
            ],
        },
        _ => panic!("Non-database resource type provided: {db_type}"),
    }
}

#[derive(Clone)]
pub struct ProvApiState {
    pub project_name: String,
    pub secrets: HashMap<String, String>,
}

pub struct ProvisionerServer;

impl ProvisionerServer {
    pub async fn run(
        state: Arc<ProvApiState>,
        api_addr: &SocketAddr,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let listener = TcpListener::bind(api_addr).await?;
        loop {
            let (stream, _) = listener.accept().await?;
            let io = TokioIo::new(stream);

            let state = Arc::clone(&state);
            tokio::task::spawn(async move {
                if let Err(err) = http1::Builder::new()
                    .serve_connection(io, service_fn(|req| handler(Arc::clone(&state), req)))
                    .await
                {
                    eprintln!("Provisioner server error: {:?}", err);
                    exit(1);
                }
            });
        }
    }
}

pub async fn handler(
    state: Arc<ProvApiState>,
    req: HyperRequest<body::Incoming>,
) -> std::result::Result<Response<BoxBody<Bytes, Infallible>>, hyper::http::Error> {
    let method = req.method().clone();
    let uri = req.uri().clone();
    debug!("Received {method} {uri}");

    let body = req.into_body().collect().await.unwrap().to_bytes();

    match provision(state, method, uri.to_string().as_str(), body.to_vec()).await {
        Ok(bytes) => Response::builder()
            .status(200)
            .body(BoxBody::new(Full::new(Bytes::from(bytes)))),
        Err(e) => {
            eprintln!("Encountered error when provisioning: {e}");
            Response::builder().status(500).body(Empty::new().boxed())
        }
    }
}

async fn provision(
    state: Arc<ProvApiState>,
    method: Method,
    uri: &str,
    body: Vec<u8>,
) -> Result<Vec<u8>> {
    Ok(match (method, uri) {
        (Method::GET, "/projects/proj_LOCAL/resources/secrets") => {
            let response = ResourceResponse {
                r#type: ResourceType::Secrets,
                state: ResourceState::Ready,
                config: serde_json::Value::Null,
                output: serde_json::to_value(&state.secrets).unwrap(),
            };
            let table = get_resource_tables(
                std::slice::from_ref(&response),
                "local service",
                false,
                true,
            );
            println!("{table}");
            serde_json::to_vec(&response).unwrap()
        }
        (Method::POST, "/projects/proj_LOCAL/resources") => {
            let prov = LocalProvisioner::new().unwrap();
            let shuttle_resource: ProvisionResourceRequest =
                serde_json::from_slice(&body).context("deserializing resource request")?;

            let response = match shuttle_resource.r#type {
                ResourceType::DatabaseSharedPostgres
                | ResourceType::DatabaseAwsRdsMariaDB
                | ResourceType::DatabaseAwsRdsMySql
                | ResourceType::DatabaseAwsRdsPostgres => {
                    let config: DbInput = serde_json::from_value(shuttle_resource.config.clone())
                        .context("deserializing resource config")?;
                    let res = prov.get_db_connection_string(
                            &state.project_name,
                            shuttle_resource.r#type.clone(),
                            config.db_name,
                        )
                        .await
                        .context("Failed to start database container. Make sure that a Docker engine is running.")?;
                    ResourceResponse {
                        r#type: shuttle_resource.r#type,
                        state: resource::ResourceState::Ready,
                        config: shuttle_resource.config,
                        output: serde_json::to_value(res).unwrap(),
                    }
                }
                ResourceType::Container => {
                    let config = serde_json::from_value(shuttle_resource.config.clone())
                        .context("deserializing resource config")?;
                    let res = prov.start_container(config)
                            .await
                            .context("Failed to start Docker container. Make sure that a Docker engine is running.")?;
                    ResourceResponse {
                        r#type: shuttle_resource.r#type,
                        state: resource::ResourceState::Ready,
                        config: shuttle_resource.config,
                        output: serde_json::to_value(res).unwrap(),
                    }
                }
                ResourceType::Secrets => ResourceResponse {
                    r#type: shuttle_resource.r#type,
                    state: resource::ResourceState::Ready,
                    config: shuttle_resource.config,
                    output: serde_json::to_value(&state.secrets).unwrap(),
                },
                ResourceType::Unknown(s) => bail!("request for unknown resource type {s} recieved"),
            };

            let table = get_resource_tables(
                std::slice::from_ref(&response),
                "local service",
                false,
                true,
            );
            println!("{table}");

            serde_json::to_vec(&response).unwrap()
        }
        _ => bail!("Received unsupported resource request"),
    })
}