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
use clap::{App, Arg, ArgMatches};
use log::{debug, error, warn};
use std::fs::{create_dir_all, File};
use std::io::{Error, Write};
use std::path::{Path, PathBuf};
use super::Archive;
use crate::scheduler::job::JobInfo;
pub fn clap_subcommand(command: &str) -> App {
App::new(command)
.about("Archive to the filesystem")
.arg(
Arg::new("archive")
.long("archive")
.short('a')
.takes_value(true)
.about("Location of the job scripts' archive."),
)
.arg(
Arg::new("period")
.long("period")
.short('p')
.takes_value(true)
.possible_value("yearly")
.possible_value("monthly")
.possible_value("daily")
.about(
"Archive under a YYYY subdirectory (yearly), YYYYMM (monthly), or YYYYMMDD (daily)."
)
)
}
pub enum Period {
Daily,
Monthly,
Yearly,
None,
}
pub struct FileArchive {
archive_path: PathBuf,
period: Period,
}
impl FileArchive {
pub fn new(archive_path: &PathBuf, p: Period) -> Self {
FileArchive {
archive_path: archive_path.clone(),
period: p,
}
}
pub fn build(matches: &ArgMatches) -> Result<Self, Error> {
let archive = Path::new(
matches
.value_of("archive")
.expect("You must provide the location of the archive"),
);
if !archive.is_dir() {
warn!(
"Provided archive {:?} is not a valid directory, creating it.",
&archive
);
if let Err(e) = create_dir_all(&archive) {
error!("Unable to create archive at {:?}. {}", &archive, e);
return Err(e);
}
};
let period = match matches.value_of("period") {
Some("yearly") => Period::Yearly,
Some("monthly") => Period::Monthly,
Some("daily") => Period::Daily,
_ => Period::None,
};
Ok(FileArchive::new(&archive.to_path_buf(), period))
}
}
impl Archive for FileArchive {
fn archive(&self, job_entry: &Box<dyn JobInfo>) -> Result<(), Error> {
let archive_path = &self.archive_path;
let target_path = determine_target_path(&archive_path, &self.period);
debug!("Target path: {:?}", target_path);
for (fname, fcontents) in job_entry.files().iter() {
debug!("Creating an entry for {}", fname);
let mut f = File::create(target_path.join(&fname))?;
f.write_all(&fcontents)?;
}
Ok(())
}
}
fn determine_target_path(archive_path: &Path, p: &Period) -> PathBuf {
let archive_subdir = match p {
Period::Yearly => Some(format!("{}", chrono::Local::now().format("%Y"))),
Period::Monthly => Some(format!("{}", chrono::Local::now().format("%Y%m"))),
Period::Daily => Some(format!("{}", chrono::Local::now().format("%Y%m%d"))),
_ => None,
};
debug!("Archive subdir is {:?}", &archive_subdir);
match archive_subdir {
Some(d) => {
let archive_subdir_path = archive_path.join(&d);
if !Path::exists(&archive_subdir_path) {
debug!("Archive subdir {:?} does not yet exist, creating", &d);
create_dir_all(&archive_subdir_path).unwrap();
}
archive_subdir_path
}
None => archive_path.to_path_buf(),
}
}
#[cfg(test)]
mod tests {
extern crate tempfile;
use std::fs::{create_dir, read_to_string, File};
use std::io::Write;
use std::path::Path;
use tempfile::tempdir;
use super::super::*;
use super::*;
use crate::scheduler::job::JobInfo;
use crate::scheduler::slurm::SlurmJobEntry;
#[test]
fn test_determine_target_path() {
let tdir = tempdir().unwrap();
let archive_dir = tdir.path();
let _dir = create_dir(&archive_dir);
let p = Period::None;
let target_path = determine_target_path(&archive_dir, &p);
assert_eq!(target_path, archive_dir);
let d = format!("{}", chrono::Local::now().format("%Y"));
let p = Period::Yearly;
let target_path = determine_target_path(&archive_dir, &p);
assert_eq!(target_path, archive_dir.join(d));
let d = format!("{}", chrono::Local::now().format("%Y%m"));
let p = Period::Monthly;
let target_path = determine_target_path(&archive_dir, &p);
assert_eq!(target_path, archive_dir.join(d));
let d = format!("{}", chrono::Local::now().format("%Y%m%d"));
let p = Period::Daily;
let target_path = determine_target_path(&archive_dir, &p);
assert_eq!(target_path, archive_dir.join(d));
}
#[test]
fn test_file_archive() {
let tdir = tempdir().unwrap();
let archive_dir = tdir.path().join("archive");
let _dir = create_dir(&archive_dir);
let job_dir = tdir.path().join("job.1234");
let _dir = create_dir(&job_dir);
let env_path = job_dir.join("environment");
let mut env = File::create(env_path).unwrap();
env.write(b"environment").unwrap();
let job_path = job_dir.join("script");
let mut job = File::create(&job_path).unwrap();
job.write(b"job script").unwrap();
let mut slurm_job_entry = SlurmJobEntry::new(&job_dir, "1234", "mycluster");
if let Err(_) = slurm_job_entry.read_job_info() {
assert!(false);
}
let file_archiver = FileArchive::new(&archive_dir, Period::None);
let jobinfo: Box<dyn JobInfo> = Box::new(slurm_job_entry);
file_archiver.archive(&jobinfo).unwrap();
assert!(Path::is_file(&archive_dir.join("job.1234_environment")));
assert!(Path::is_file(&archive_dir.join("job.1234_script")));
let archive_env_contents =
read_to_string(&archive_dir.join("job.1234_environment")).unwrap();
assert_eq!(&archive_env_contents, "environment");
let archive_script_contents = read_to_string(&archive_dir.join("job.1234_script")).unwrap();
assert_eq!(&archive_script_contents, "job script");
}
}