-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathlib.rs
More file actions
184 lines (169 loc) · 6.34 KB
/
lib.rs
File metadata and controls
184 lines (169 loc) · 6.34 KB
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
/*
* SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use std::cmp::min;
use std::io::Write;
use std::time::Duration;
use carbide_uuid::machine::MachineId;
use errors::MachineValidationError;
use futures_util::StreamExt;
use serde::{Deserialize, Serialize};
mod errors;
mod machine_validation;
pub const MACHINE_VALIDATION_SERVER: &str = "carbide-pxe.forge";
pub const SCHME: &str = "http";
pub const MACHINE_VALIDATION_IMAGE_PATH: &str = "/public/blobs/internal/machine-validation/images/";
pub const MACHINE_VALIDATION_IMAGE_FILE: &str = "/tmp/machine_validation.tar";
pub const MACHINE_VALIDATION_RUNNER_BASE_PATH: &str = "nvcr.io/nvidian/nvforge/";
pub const MACHINE_VALIDATION_RUNNER_TAG: &str = "latest";
pub const IMAGE_LIST_FILE: &str = "/tmp/list.json";
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct MachineValidationOptions {
pub api: String,
pub root_ca: String,
pub client_cert: String,
pub client_key: String,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct MachineValidation {
options: MachineValidationOptions,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct MachineValidationFilter {
pub tags: Vec<String>,
pub allowed_tests: Vec<String>,
pub run_unverfied_tests: Option<bool>,
pub contexts: Option<Vec<String>>,
}
pub struct MachineValidationManager {}
impl MachineValidationManager {
pub async fn download_file(url: &str, output_file: &str) -> Result<(), MachineValidationError> {
let client = reqwest::ClientBuilder::new()
.timeout(Duration::from_secs(30))
.build()
.map_err(|e| MachineValidationError::Generic(format!("Client builder error: {e}")))?;
let res = client
.get(url)
.send()
.await
.or(Err(MachineValidationError::Generic(format!(
"Failed to GET from '{}'",
&url
))))?;
let total_size = res
.content_length()
.ok_or(MachineValidationError::Generic(format!(
"Failed to get content length from '{}'",
&url
)))?;
let _ = std::fs::remove_file(output_file).or(Err(MachineValidationError::Generic(
format!("Failed to delete file '{output_file}'"),
)));
let mut file = std::fs::File::create(output_file).or(Err(
MachineValidationError::Generic(format!("Failed to create file '{output_file}'")),
))?;
let mut buffer: u64 = 0;
let mut stream = res.bytes_stream();
while let Some(item) = stream.next().await {
let chunk = item.or(Err(MachineValidationError::Generic(
"Error while reading stream".to_string(),
)))?;
file.write_all(&chunk)
.or(Err(MachineValidationError::Generic(
"Error while writing to file".to_string(),
)))?;
let new = min(buffer + (chunk.len() as u64), total_size);
buffer = new;
}
Ok(())
}
pub async fn run(
machine_id: &MachineId,
platform_name: String,
options: MachineValidationOptions,
context: String,
uuid: String,
machine_validation_filter: MachineValidationFilter,
) -> Result<(), MachineValidationError> {
let mc = MachineValidation::new(options);
let tests = mc
.clone()
.get_machine_validation_tests(rpc::forge::MachineValidationTestsGetRequest {
supported_platforms: vec![platform_name],
contexts: if machine_validation_filter
.clone()
.contexts
.unwrap_or_default()
.is_empty()
{
vec![context.clone()]
} else {
machine_validation_filter
.clone()
.contexts
.unwrap_or_default()
},
is_enabled: Some(true),
verified: if machine_validation_filter
.run_unverfied_tests
.unwrap_or(false)
{
None // This indicates run all tests including un verified
} else {
Some(true)
},
custom_tags: machine_validation_filter.clone().tags,
..rpc::forge::MachineValidationTestsGetRequest::default()
})
.await?;
let mut run_request = rpc::forge::MachineValidationRunRequest {
validation_id: Some(rpc::Uuid {
value: uuid.to_owned(),
}),
..rpc::forge::MachineValidationRunRequest::default()
};
let mut expected_time_duration = 0;
for test in tests.clone() {
if !machine_validation_filter.allowed_tests.is_empty()
&& !machine_validation_filter
.allowed_tests
.iter()
.any(|t| t.eq_ignore_ascii_case(&test.test_id))
{
continue;
}
run_request.total += 1;
expected_time_duration += test.timeout.unwrap_or(7200);
}
run_request.duration_to_complete = Some(rpc::Duration::from(
std::time::Duration::from_secs(expected_time_duration as u64),
));
//Update the duration
mc.clone()
.update_machine_validation_run(run_request)
.await?;
mc.run(
machine_id,
tests,
context,
uuid,
true,
machine_validation_filter,
)
.await?;
Ok(())
}
}