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
pub mod project;
use std::{
collections::BTreeMap,
fmt::{Display, Formatter},
};
use chrono::{DateTime, Utc};
use log::Level;
use rocket::Responder;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::project::ProjectName;
pub const SHUTTLE_PROJECT_HEADER: &str = "Shuttle-Project";
#[cfg(debug_assertions)]
pub const API_URL_DEFAULT: &str = "http://localhost:8001";
#[cfg(not(debug_assertions))]
pub const API_URL_DEFAULT: &str = "https://api.shuttle.rs";
pub type ApiKey = String;
pub type ApiUrl = String;
pub type Host = String;
pub type DeploymentId = Uuid;
pub type Port = u16;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeploymentMeta {
pub id: DeploymentId,
pub project: ProjectName,
pub state: DeploymentStateMeta,
pub host: String,
pub build_logs: Option<String>,
pub runtime_logs: BTreeMap<DateTime<Utc>, LogItem>,
pub database_deployment: Option<DatabaseReadyInfo>,
pub created_at: DateTime<Utc>,
}
impl DeploymentMeta {
pub fn queued(fqdn: &str, project: ProjectName) -> Self {
Self::new(fqdn, project, DeploymentStateMeta::Queued)
}
pub fn built(fqdn: &str, project: ProjectName) -> Self {
Self::new(fqdn, project, DeploymentStateMeta::Built)
}
fn new(fqdn: &str, project: ProjectName, state: DeploymentStateMeta) -> Self {
let host = Self::create_host(fqdn, &project);
Self {
id: Uuid::new_v4(),
project,
state,
host,
build_logs: None,
runtime_logs: BTreeMap::new(),
database_deployment: None,
created_at: Utc::now(),
}
}
pub fn create_host(fqdn: &str, project_name: &ProjectName) -> Host {
format!("{}.{}", project_name, fqdn)
}
}
#[cfg(debug_assertions)]
const PUBLIC_IP: &str = "localhost";
#[cfg(not(debug_assertions))]
const PUBLIC_IP: &'static str = "pg.shuttle.rs";
impl Display for DeploymentMeta {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let db = {
if let Some(info) = &self.database_deployment {
format!(
"\n Database URI: {}",
info.connection_string(PUBLIC_IP)
)
} else {
"".to_string()
}
};
write!(
f,
r#"
Project: {}
Deployment Id: {}
Deployment Status: {}
Host: {}
Created At: {}{}
"#,
self.project, self.id, self.state, self.host, self.created_at, db
)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatabaseReadyInfo {
pub role_name: String,
pub role_password: String,
pub database_name: String,
}
impl DatabaseReadyInfo {
pub fn new(role_name: String, role_password: String, database_name: String) -> Self {
Self {
role_name,
role_password,
database_name,
}
}
pub fn connection_string(&self, ip: &str) -> String {
format!(
"postgres://{}:{}@{}/{}",
self.role_name, self.role_password, ip, self.database_name
)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DeploymentStateMeta {
Queued,
Built,
Loaded,
Deployed,
Error(String),
Deleted,
}
impl Display for DeploymentStateMeta {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let s = match self {
DeploymentStateMeta::Queued => "QUEUED".to_string(),
DeploymentStateMeta::Built => "BUILT".to_string(),
DeploymentStateMeta::Loaded => "LOADED".to_string(),
DeploymentStateMeta::Deployed => "DEPLOYED".to_string(),
DeploymentStateMeta::Error(msg) => format!("ERROR: {}", &msg),
DeploymentStateMeta::Deleted => "DELETED".to_string(),
};
write!(f, "{}", s)
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Responder)]
#[response(content_type = "json")]
pub enum DeploymentApiError {
#[response(status = 500)]
Internal(String),
#[response(status = 503)]
Unavailable(String),
#[response(status = 404)]
NotFound(String),
#[response(status = 400)]
BadRequest(String),
#[response(status = 409)]
ProjectAlreadyExists(String),
}
impl Display for DeploymentApiError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
DeploymentApiError::Internal(s) => write!(f, "internal: {}", s),
DeploymentApiError::Unavailable(s) => write!(f, "unavailable: {}", s),
DeploymentApiError::NotFound(s) => write!(f, "not found: {}", s),
DeploymentApiError::BadRequest(s) => write!(f, "bad request: {}", s),
DeploymentApiError::ProjectAlreadyExists(s) => write!(f, "conflict: {}", s),
}
}
}
impl std::error::Error for DeploymentApiError {}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct LogItem {
pub body: String,
pub level: Level,
pub target: String,
}