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
use tokio::process::Command;
use crate::style_print::*;
use console::style;
use std::io;
use regex::Regex;
use std::str;
use std::fs;
use std::io::Write;
use crate::gh::process_setup_secret;
#[derive(Debug)]
pub struct EnvProduction {
pub database_url: String,
pub zapp_gcloudsql_instance: String,
pub zapp_gcp_project_id: String,
pub zapp_service_name: String,
pub zapp_gcp_region: String
}
fn regex(re_str: &str) -> Regex {
Regex::new(re_str).unwrap()
}
pub async fn process_create_sql(project_id: &str, service_name: &str, region: &str) {
log_input("Please input your DB Root Password:").await;
let mut db_password = String::new();
io::stdin()
.read_line(&mut db_password)
.expect("Failed to read line");
let db_password: String = db_password
.trim()
.parse()
.expect("Please input DB Root Password:");
let zone = String::from(region) + "-b";
println!(
"⏰ {}",
style("Creating Cloud SQL ...\nThis process takes 5 to 10 min.").white().bold()
);
let instance_name = String::from(service_name) + "-db";
let internal_ip = get_instance_ip(project_id, service_name, 1).await;
let database_url = String::from("DATABASE_URL='postgres://postgres:") + &db_password + "@" + &internal_ip + ":5432/" + &instance_name + "\n";
let zapp_gcloudsql_instance = String::from("ZAPP_GCLOUDSQL_INSTANCE=/cloudsql/") + &project_id + ":" + ®ion + ":" + &instance_name + "\n";
let zapp_gcp_project_id = String::from("ZAPP_GCP_PROJECT_ID=") + &project_id + "\n";
let zapp_service_name = String::from("ZAPP_SERVICE_NAME=") + &service_name + "\n";
let zapp_gcp_region = String::from("ZAPP_GCP_REGION=") + ®ion + "\n";
let env_production = EnvProduction {
database_url,
zapp_gcloudsql_instance,
zapp_gcp_project_id,
zapp_service_name,
zapp_gcp_region
};
let filename = ".env.production";
let mut file = fs::File::create(filename).unwrap();
file.write_all(env_production.database_url.as_bytes()).unwrap();
file.write_all(env_production.zapp_gcloudsql_instance.as_bytes()).unwrap();
file.write_all(env_production.zapp_gcp_project_id.as_bytes()).unwrap();
file.write_all(env_production.zapp_service_name.as_bytes()).unwrap();
file.write_all(env_production.zapp_gcp_region.as_bytes()).unwrap();
process_setup_secret().await;
let db_version = String::from("--database-version=POSTGRES_14");
let output = Command::new("gcloud")
.args(&[
"sql",
"instances",
"create",
&instance_name,
&db_version,
"--cpu=1",
"--memory=4096MB",
"--zone",
&zone,
"--root-password",
&db_password,
"--database-flags",
"cloudsql.iam_authentication=on",
"--project",
project_id
])
.output()
.await;
match &output {
Ok(val) => {
let err = str::from_utf8(&val.stderr);
let rt = regex("ERROR:");
match rt.is_match(err.unwrap()) {
true => {
panic!("{:?}", err.unwrap())
}
false => {
println!(
"✅ {}",
style("Successfully created Cloud SQL!").white().bold()
);
}
}
},
Err(err) => println!("error = {:?}", err)
}
}
pub async fn process_patch_sql(project_id: &str, service_name: &str, action: &str) {
let instance_name = String::from(service_name) + "-db";
let activation_policy = match action {
"start" => {
"ALWAYS"
}
"stop" => {
"NEVER"
}
_ => {
panic!("No action name!");
}
};
let output = Command::new("gcloud")
.args(&[
"sql",
"instances",
"patch",
&instance_name,
"--activation-policy",
activation_policy,
"--project",
project_id
])
.output()
.await;
match &output {
Ok(val) => {
let err = str::from_utf8(&val.stderr);
let rt = regex("ERROR:");
match rt.is_match(err.unwrap()) {
true => {
panic!("{:?}", err.unwrap())
}
false => {
println!(
"✅ {}",
style("Successfully patched Cloud SQL!").white().bold()
);
}
}
},
Err(err) => println!("error = {:?}", err)
}
}
pub async fn process_restart_sql(project_id: &str, service_name: &str) {
let instance_name = String::from(service_name) + "-db";
let output = Command::new("gcloud")
.args(&[
"sql",
"instances",
"restart",
&instance_name,
"--project",
project_id
])
.output()
.await;
match &output {
Ok(val) => {
let err = str::from_utf8(&val.stderr);
let rt = regex("ERROR:");
match rt.is_match(err.unwrap()) {
true => {
panic!("{:?}", err.unwrap())
}
false => {
log_success("Successfully restart Cloud SQL!").await
}
}
},
Err(err) => println!("error = {:?}", err)
}
}
pub async fn process_create_ip_range(project_id: &str, service_name: &str) {
println!(
"⏰ {}",
style("Creating IP range ...\nThis process takes 5 to 10 min.").white().bold()
);
let ip_range_name = String::from(service_name) + "-ip-range";
let network = String::from("--network=") + service_name;
let output = Command::new("gcloud")
.args(&[
"compute",
"addresses",
"create",
&ip_range_name,
"--global",
"--purpose=VPC_PEERING",
"--prefix-length=16",
"--description='peering range for Epics'",
&network,
"--project",
project_id
])
.output()
.await;
match &output {
Ok(val) => {
let err = str::from_utf8(&val.stderr);
let rt = regex("ERROR:");
match rt.is_match(err.unwrap()) {
true => {
panic!("{:?}", err.unwrap())
}
false => {
println!(
"✅ {}",
style("Successfully created IP range!").white().bold()
);
}
}
},
Err(err) => println!("error = {:?}", err)
}
}
pub async fn process_connect_vpc_connector(project_id: &str, service_name: &str) {
println!(
"⏰ {}",
style("Connecting to VPC Connector ...\nThis process takes 5 to 10 min.").white().bold()
);
let ip_range_name = String::from(service_name) + "-ip-range";
let network = String::from("--network=") + service_name;
let output = Command::new("gcloud")
.args(&[
"services",
"vpc-peerings",
"connect",
"--service=servicenetworking.googleapis.com",
"--ranges",
&ip_range_name,
&network,
"--project",
project_id
])
.output()
.await;
match &output {
Ok(val) => {
let err = str::from_utf8(&val.stderr);
let rt = regex("ERROR:");
match rt.is_match(err.unwrap()) {
true => {
panic!("{:?}", err.unwrap())
}
false => {
println!(
"✅ {}",
style("Successfully connected to VPC!").white().bold()
);
}
}
},
Err(err) => println!("error = {:?}", err)
}
}
pub async fn process_assign_network(project_id: &str, service_name: &str) {
println!(
"⏰ {}",
style("Assign network ...\nThis process takes 5 to 10 min.").white().bold()
);
let instance_name = String::from(service_name) + "-db";
let network = String::from("--network=") + service_name;
let output = Command::new("gcloud")
.args(&[
"beta",
"sql",
"instances",
"patch",
&instance_name,
&network,
"--project",
project_id
])
.output()
.await;
match &output {
Ok(val) => {
let err = str::from_utf8(&val.stderr);
let rt = regex("ERROR:");
match rt.is_match(err.unwrap()) {
true => {
panic!("{:?}", err.unwrap())
}
false => {
println!(
"✅ {}",
style("Successfully setup your database!").white().bold()
);
}
}
},
Err(err) => println!("error = {:?}", err)
}
}
pub async fn get_instance_ip(project_id: &str, service_name: &str, ip_type: usize) -> String {
let instance_name = String::from(service_name) + "-db";
let mut _internal_ip = String::new();
let output = Command::new("gcloud")
.args(&[
"sql",
"instances",
"describe",
&instance_name,
"--project",
project_id,
"--format",
"value(ipAddresses.ipAddress)"
])
.output()
.await;
let _internal_ip = match &output {
Ok(val) => {
let ips = str::from_utf8(&val.stdout).unwrap().trim();
let ip_array = ips.split(';').fold(Vec::new(), |mut s, i| {
s.push(i.to_string());
s
});
if ip_type == 0 {
ip_array.first().unwrap().clone()
} else {
ip_array.last().unwrap().clone()
}
},
Err(err) => {
panic!("{:?}", err)
}
};
_internal_ip
}