@@ -10,7 +10,7 @@ use std::{
1010use color_eyre:: { Result , eyre:: eyre} ;
1111use reqwest:: Url ;
1212
13- use super :: api_client:: client:: EdfSpec ;
13+ use super :: api_client:: client:: { EdfSpec , ScriptSpec } ;
1414use crate :: {
1515 config:: { ComputePlatform , Config } ,
1616 cscs:: {
@@ -236,8 +236,49 @@ async fn handle_edf(
236236 EdfSpec :: Remote ( path) => Ok ( path) ,
237237 }
238238}
239+ async fn handle_script (
240+ api_client : & CscsApi ,
241+ job_name : & str ,
242+ base_path : & Path ,
243+ current_system : & str ,
244+ environment_path : & Path ,
245+ workdir : & str ,
246+ options : & JobStartOptions ,
247+ ) -> Result < PathBuf > {
248+ let config = Config :: new ( ) . unwrap ( ) ;
249+ let script_path = base_path. join ( "script.sh" ) ;
250+ match options. script_spec . clone ( ) {
251+ ScriptSpec :: Generate => {
252+ let script_template = config. cscs . sbatch_script_template ;
253+ let mut tera = tera:: Tera :: default ( ) ;
254+ tera. add_raw_template ( "script.sh" , & script_template) ?;
255+ let mut context = tera:: Context :: new ( ) ;
256+ context. insert ( "name" , & job_name) ;
257+ context. insert (
258+ "command" ,
259+ & options. command . clone ( ) . unwrap_or ( config. cscs . command ) . join ( " " ) ,
260+ ) ;
261+ context. insert ( "environment_file" , & environment_path. to_path_buf ( ) ) ;
262+ context. insert ( "container_workdir" , & workdir) ;
263+ let script = tera. render ( "script.sh" , & context) ?;
264+ api_client
265+ . upload ( current_system, script_path. clone ( ) , script. into_bytes ( ) )
266+ . await ?;
267+
268+ Ok ( script_path)
269+ }
270+ ScriptSpec :: Local ( local_path) => {
271+ let script = std:: fs:: read_to_string ( local_path) ?;
272+ api_client
273+ . upload ( current_system, script_path. clone ( ) , script. into_bytes ( ) )
274+ . await ?;
275+
276+ Ok ( script_path)
277+ }
278+ ScriptSpec :: Remote ( script_path) => Ok ( script_path) ,
279+ }
280+ }
239281
240- #[ allow( clippy:: too_many_arguments) ]
241282pub async fn cscs_job_start (
242283 name : Option < String > ,
243284 options : JobStartOptions ,
@@ -276,6 +317,7 @@ pub async fn cscs_job_start(
276317
277318 let mut envvars = config. cscs . env . clone ( ) ;
278319 envvars. extend ( options. env . clone ( ) ) ;
320+
279321 let environment_path = handle_edf (
280322 & api_client,
281323 & base_path,
@@ -286,27 +328,16 @@ pub async fn cscs_job_start(
286328 )
287329 . await ?;
288330
289- // upload script
290- let script_path = base_path. join ( "script.sh" ) ;
291- let script_template = options
292- . script_file
293- . clone ( )
294- . map ( std:: fs:: read_to_string)
295- . unwrap_or ( Ok ( config. cscs . sbatch_script_template ) ) ?;
296- let mut tera = tera:: Tera :: default ( ) ;
297- tera. add_raw_template ( "script.sh" , & script_template) ?;
298- let mut context = tera:: Context :: new ( ) ;
299- context. insert ( "name" , & job_name) ;
300- context. insert (
301- "command" ,
302- & options. command . clone ( ) . unwrap_or ( config. cscs . command ) . join ( " " ) ,
303- ) ;
304- context. insert ( "environment_file" , & environment_path) ;
305- context. insert ( "container_workdir" , & container_workdir) ;
306- let script = tera. render ( "script.sh" , & context) ?;
307- api_client
308- . upload ( current_system, script_path. clone ( ) , script. into_bytes ( ) )
309- . await ?;
331+ let script_path = handle_script (
332+ & api_client,
333+ & job_name,
334+ & base_path,
335+ current_system,
336+ & environment_path,
337+ & container_workdir,
338+ & options,
339+ )
340+ . await ?;
310341
311342 // start job
312343 api_client
0 commit comments