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
use crate::{yes_or_no, Cmd, File, MasterConfig, Worker, AUTH_TOKEN_PATH};
use std::path::PathBuf;
use std::process::exit;
pub trait BuildSystem {
fn preinstall(&mut self) -> Result<(), String> {
Ok(())
}
fn install(&mut self) -> Result<(), String> {
self.preinstall()?;
info!("Installing Python...");
self.install_python()?;
info!("Installing Buildbot...");
self.install_buildbot()?;
info!(
"Next, write your VCS's api token to '{}', and then run the `build` subcommand",
AUTH_TOKEN_PATH
);
Ok(())
}
fn prebuild(&mut self) -> Result<(), String> {
Ok(())
}
fn rebuild(&mut self, master: MasterConfig) -> Result<(), String> {
self.prebuild()?;
if !yes_or_no("Have you already run the install subcommand and activated your virtual env in this shell? (y/n) ") {
error!("You must run the install subcommand and activate your venv before the running the build subcommand!");
exit(0);
}
info!("Creating master...");
self.create_master()?;
let workers = master.get_workers();
info!("Creating workers...");
self.create_workers(&workers)?;
info!("Writing to master/master.cfg...");
self.write_master_config(&master)?;
info!("Writing to worker configs...");
self.write_worker_configs(&workers)?;
info!("Reconfiguring master...");
self.reconfigure_master()?;
Ok(())
}
fn build(&mut self, master: MasterConfig) -> Result<(), String> {
self.prebuild()?;
if !yes_or_no("Have you already run the install subcommand and activated your virtual env in this shell? (y/n) ") {
error!("You must run the install subcommand and activate your venv before the running the build subcommand!");
exit(0);
}
info!("Creating master...");
self.create_master()?;
let workers = master.get_workers();
info!("Creating workers...");
self.create_workers(&workers)?;
info!("Writing to master/master.cfg...");
self.write_master_config(&master)?;
info!("Writing to worker configs...");
self.write_worker_configs(&workers)?;
info!("Next, run the `start` subcommand to execute the master and the workers");
Ok(())
}
fn start(&mut self, workers: &[Worker]) -> Result<(), String> {
if !yes_or_no("Have you already run the build subcommand and activated your virtual env in this shell? (y/n) ") {
error!("You must run the build subcommand and activate your venv before the running the start subcommand!");
exit(0);
}
info!("Starting workers and masters...");
self.start_master()?;
self.start_workers(workers)?;
Ok(())
}
fn stop(&mut self) -> Result<(), String> {
if !yes_or_no("Are you sure you want to kill rusty-ci? This will also kill all python processes! (y/n) ") {
error!("You weren't sure!");
exit(0);
}
info!("Killing python...");
Cmd::new("killall").arg("python").run();
info!("Killing python3...");
Cmd::new("killall").arg("python3").run();
info!("Killing workers...");
Cmd::new("killall").arg("buildbot-worker").run();
Ok(())
}
fn reconfigure_master(&mut self) -> Result<(), String> {
let buildbot = |sub_command| -> Result<(), String> {
Cmd::new("buildbot").arg(sub_command).arg("master").run();
Ok(())
};
buildbot("reconfig")?;
buildbot("cleanupdb")?;
Ok(())
}
fn start_master(&mut self) -> Result<(), String> {
let buildbot = |sub_command| -> Result<(), String> {
Cmd::new("buildbot").arg(sub_command).arg("master").run();
Ok(())
};
buildbot("stop")?;
buildbot("reconfig")?;
buildbot("cleanupdb")?;
buildbot("start")?;
Ok(())
}
fn start_workers(&mut self, workers: &[Worker]) -> Result<(), String> {
let start_worker = |dir| -> Result<(), String> {
Cmd::new("buildbot-worker").arg("restart").arg(&dir).run();
Ok(())
};
for worker in workers {
start_worker(worker.get_dir())?;
}
Ok(())
}
fn create_workers(&mut self, workers: &[Worker]) -> Result<(), String> {
let make_worker = |dir| -> Result<(), String> {
Cmd::new("buildbot-worker")
.arg("create-worker")
.arg(&dir)
.arg("localhost")
.arg(&dir)
.arg("pass")
.run();
Ok(())
};
for worker in workers {
make_worker(worker.get_dir())?;
}
Ok(())
}
fn write_worker_configs(&mut self, workers: &[Worker]) -> Result<(), String> {
for worker in workers {
let mut path = PathBuf::new();
path.push(worker.get_dir());
path.push("buildbot.tac");
match File::write(path, worker.to_string()) {
Err(e) => Err(e + &format!(
"\nDid you enter a valid basedir for the \"{}\" worker?",
worker.get_name()
)),
Ok(()) => Ok(()),
}?;
}
Ok(())
}
fn create_master(&mut self) -> Result<(), String> {
Cmd::new("buildbot")
.arg("create-master")
.arg("master")
.run();
Ok(())
}
fn write_master_config(&mut self, master: &MasterConfig) -> Result<(), String> {
match File::write("master/master.cfg", master.to_string()) {
Err(e) => Err(e + "\nDid you enter your venv by running `. venv/bin/activate`?"),
Ok(()) => Ok(()),
}
}
fn install_python(&mut self) -> Result<(), String> {
let apt_install = |package| -> Result<(), String> {
Cmd::new("apt-get")
.arg("install")
.arg(package)
.arg("-y")
.run();
Ok(())
};
apt_install("python3-dev")?;
apt_install("python3-pip")?;
apt_install("python3-venv")?;
Ok(())
}
fn install_buildbot(&mut self) -> Result<(), String> {
Cmd::new("python3")
.arg("-m")
.arg("pip")
.arg("install")
.arg("-U")
.arg("pip")
.run();
Cmd::new("python3")
.arg("-m")
.arg("pip")
.arg("install")
.arg("-U")
.arg("buildbot[bundle]")
.run();
Cmd::new("python3")
.arg("-m")
.arg("pip")
.arg("install")
.arg("-U")
.arg("txrequest")
.run();
Cmd::new("python3")
.arg("-m")
.arg("pip")
.arg("install")
.arg("-U")
.arg("treq")
.run();
Cmd::new("python3")
.arg("-m")
.arg("pip")
.arg("install")
.arg("buildbot-worker")
.arg("setuptools-trial")
.run();
Ok(())
}
}