@@ -21,71 +21,109 @@ pub fn apply_vm_steps_to_cloud_init(
2121
2222 for step in steps {
2323 let step_slug = slugify ( & step. name ) ;
24- let script_path = format ! ( "/usr/local/bin/intar-step-{vm_slug}-{step_slug}.sh" ) ;
25- let script = render_step_script ( & vm_slug, & step_slug, step) ?;
24+ let hidden = is_hidden_step ( step) ;
25+ let script_path = if hidden {
26+ format ! ( "/run/intar-step-{vm_slug}-{step_slug}.sh" )
27+ } else {
28+ format ! ( "/usr/local/bin/intar-step-{vm_slug}-{step_slug}.sh" )
29+ } ;
30+ let script = render_step_script ( & vm_slug, & step_slug, step, hidden) ?;
2631
2732 config. write_files . push ( WriteFile {
2833 path : script_path. clone ( ) ,
2934 content : script,
3035 permissions : Some ( "0755" . into ( ) ) ,
3136 } ) ;
3237
33- append_runcmd_line (
34- & mut runcmd,
35- & format ! ( "cloud-init-per once intar-step-{vm_slug}-{step_slug} {script_path}" ) ,
36- ) ;
38+ if hidden {
39+ append_runcmd_line ( & mut runcmd, & format ! ( "bash {script_path}" ) ) ;
40+ } else {
41+ append_runcmd_line (
42+ & mut runcmd,
43+ & format ! ( "cloud-init-per once intar-step-{vm_slug}-{step_slug} {script_path}" ) ,
44+ ) ;
45+ }
3746 }
3847
3948 config. runcmd = Some ( runcmd) ;
4049 Ok ( ( ) )
4150}
4251
43- fn render_step_script ( vm_slug : & str , step_slug : & str , step : & VmStep ) -> Result < String , VmError > {
52+ fn render_step_script (
53+ vm_slug : & str ,
54+ step_slug : & str ,
55+ step : & VmStep ,
56+ hidden : bool ,
57+ ) -> Result < String , VmError > {
4458 let mut script = String :: new ( ) ;
4559
46- render_step_header ( & mut script, vm_slug, step_slug) ?;
60+ render_step_header ( & mut script, vm_slug, step_slug, hidden ) ?;
4761
4862 for ( idx, action) in step. actions . iter ( ) . enumerate ( ) {
4963 render_action ( & mut script, step_slug, idx, action) ?;
5064 }
5165
52- render_step_footer ( & mut script, vm_slug, step_slug) ?;
66+ render_step_footer ( & mut script, vm_slug, step_slug, hidden ) ?;
5367
5468 Ok ( script)
5569}
5670
57- fn render_step_header ( script : & mut String , vm_slug : & str , step_slug : & str ) -> Result < ( ) , VmError > {
71+ fn render_step_header (
72+ script : & mut String ,
73+ vm_slug : & str ,
74+ step_slug : & str ,
75+ hidden : bool ,
76+ ) -> Result < ( ) , VmError > {
5877 writeln ! ( script, "#!/usr/bin/env bash" )
5978 . map_err ( |_| VmError :: CloudInit ( "format error" . into ( ) ) ) ?;
6079 writeln ! ( script, "set -euo pipefail" ) . map_err ( |_| VmError :: CloudInit ( "format error" . into ( ) ) ) ?;
6180
62- writeln ! ( script, "LOG_DIR=/var/log/intar" )
81+ if hidden {
82+ writeln ! ( script, "trap 'rm -f -- \" $0\" ' EXIT" )
83+ . map_err ( |_| VmError :: CloudInit ( "format error" . into ( ) ) ) ?;
84+ writeln ! ( script, "exec >/dev/null 2>&1" )
85+ . map_err ( |_| VmError :: CloudInit ( "format error" . into ( ) ) ) ?;
86+ } else {
87+ writeln ! ( script, "LOG_DIR=/var/log/intar" )
88+ . map_err ( |_| VmError :: CloudInit ( "format error" . into ( ) ) ) ?;
89+ writeln ! ( script, "mkdir -p \" $LOG_DIR\" " )
90+ . map_err ( |_| VmError :: CloudInit ( "format error" . into ( ) ) ) ?;
91+ writeln ! (
92+ script,
93+ "exec >\" $LOG_DIR/step-{vm_slug}-{step_slug}.log\" 2>&1"
94+ )
6395 . map_err ( |_| VmError :: CloudInit ( "format error" . into ( ) ) ) ?;
64- writeln ! ( script, "mkdir -p \" $LOG_DIR\" " )
96+ writeln ! (
97+ script,
98+ "echo \" [intar] step {vm_slug}/{step_slug} starting\" "
99+ )
65100 . map_err ( |_| VmError :: CloudInit ( "format error" . into ( ) ) ) ?;
66- writeln ! (
67- script,
68- "exec >\" $LOG_DIR/step-{vm_slug}-{step_slug}.log\" 2>&1"
69- )
70- . map_err ( |_| VmError :: CloudInit ( "format error" . into ( ) ) ) ?;
71- writeln ! (
72- script,
73- "echo \" [intar] step {vm_slug}/{step_slug} starting\" "
74- )
75- . map_err ( |_| VmError :: CloudInit ( "format error" . into ( ) ) ) ?;
101+ }
76102
77103 Ok ( ( ) )
78104}
79105
80- fn render_step_footer ( script : & mut String , vm_slug : & str , step_slug : & str ) -> Result < ( ) , VmError > {
81- writeln ! (
82- script,
83- "echo \" [intar] step {vm_slug}/{step_slug} complete\" "
84- )
85- . map_err ( |_| VmError :: CloudInit ( "format error" . into ( ) ) ) ?;
106+ fn render_step_footer (
107+ script : & mut String ,
108+ vm_slug : & str ,
109+ step_slug : & str ,
110+ hidden : bool ,
111+ ) -> Result < ( ) , VmError > {
112+ if !hidden {
113+ writeln ! (
114+ script,
115+ "echo \" [intar] step {vm_slug}/{step_slug} complete\" "
116+ )
117+ . map_err ( |_| VmError :: CloudInit ( "format error" . into ( ) ) ) ?;
118+ }
86119 Ok ( ( ) )
87120}
88121
122+ fn is_hidden_step ( step : & VmStep ) -> bool {
123+ let name = step. name . to_lowercase ( ) ;
124+ name. starts_with ( "break" ) || name. contains ( "break-" ) || name. contains ( "break_" )
125+ }
126+
89127fn render_action (
90128 script : & mut String ,
91129 step_slug : & str ,
@@ -497,16 +535,16 @@ mod tests {
497535
498536 let runcmd = config. runcmd . as_deref ( ) . unwrap ( ) ;
499537 assert ! ( runcmd. contains( "echo pre" ) ) ;
500- assert ! ( runcmd. contains(
501- "cloud-init-per once intar-step-web-break-nginx /usr/local/bin/intar-step-web-break-nginx.sh"
502- ) ) ;
538+ assert ! ( runcmd. contains( "bash /run/intar-step-web-break-nginx.sh" ) ) ;
503539
504540 let script = config
505541 . write_files
506542 . iter ( )
507- . find ( |f| f. path == "/usr/local/bin /intar-step-web-break-nginx.sh" )
543+ . find ( |f| f. path == "/run /intar-step-web-break-nginx.sh" )
508544 . map ( |f| f. content . as_str ( ) )
509545 . unwrap ( ) ;
546+ assert ! ( script. contains( "trap 'rm -f -- \" $0\" ' EXIT" ) ) ;
547+ assert ! ( script. contains( "exec >/dev/null 2>&1" ) ) ;
510548 assert ! ( script. contains( "systemctl stop 'nginx'" ) ) ;
511549 assert ! ( script. contains( "rm -f -- '/etc/nginx/sites-enabled/default'" ) ) ;
512550 assert ! ( script. contains( "export KUBECONFIG='/etc/rancher/k3s/k3s.yaml'" ) ) ;
0 commit comments