6
6
"context"
7
7
"crypto/md5"
8
8
"fmt"
9
- "io/fs"
10
9
"os"
11
10
"os/exec"
12
11
"os/user"
@@ -738,64 +737,82 @@ func (w *CurrentWorker) setupHooks(ctx context.Context, jobInfo sdk.WorkflowNode
738
737
// The error contains 'Executable file not found', the capa is not on the worker
739
738
continue
740
739
}
740
+
741
741
hookFilename := fmt .Sprintf ("%d-%s-%s" , hookConfig .Priority , integrationName , slug .Convert (hookConfig .Label ))
742
- hookFilePath := path .Join (workingDir , "setup" , hookFilename )
743
- log .Info (ctx , "setting up hook %q" , hookFilePath )
744
742
745
- hookFile , err := fs .Create (hookFilePath )
743
+ w .hooks = append (w .hooks , workerHook {
744
+ Config : hookConfig ,
745
+ SetupPath : path .Join (workingDir , "setup" , hookFilename ),
746
+ TeardownPath : path .Join (workingDir , "teardown" , hookFilename ),
747
+ })
748
+ }
749
+
750
+ for _ , h := range w .hooks {
751
+ infos := []sdk.SpawnInfo {{
752
+ RemoteTime : time .Now (),
753
+ Message : sdk.SpawnMsg {ID : sdk .MsgSpawnInfoWorkerHookSetup .ID , Args : []interface {}{h .Config .Label }},
754
+ }}
755
+ if err := w .Client ().QueueJobSendSpawnInfo (ctx , w .currentJob .wJob .ID , infos ); err != nil {
756
+ return sdk .WrapError (err , "cannot record QueueJobSendSpawnInfo for job (err spawn): %d" , w .currentJob .wJob .ID )
757
+ }
758
+
759
+ log .Info (ctx , "setting up hook at %q" , h .SetupPath )
760
+
761
+ hookFile , err := fs .Create (h .SetupPath )
746
762
if err != nil {
747
- return errors .Errorf ("unable to open hook file %q in %q: %v" , hookFilePath , w .basedir .Name (), err )
763
+ return errors .Errorf ("unable to open hook file %q in %q: %v" , h . SetupPath , w .basedir .Name (), err )
748
764
}
749
- if _ , err := hookFile .WriteString (hookConfig .Setup ); err != nil {
765
+ if _ , err := hookFile .WriteString (h . Config .Setup ); err != nil {
750
766
_ = hookFile .Close
751
- return errors .Errorf ("unable to setup hook %q: %v" , hookFilePath , err )
767
+ return errors .Errorf ("unable to setup hook %q: %v" , h . SetupPath , err )
752
768
}
753
769
if err := hookFile .Close (); err != nil {
754
- return errors .Errorf ("unable to setup hook %q: %v" , hookFilePath , err )
770
+ return errors .Errorf ("unable to setup hook %q: %v" , h . SetupPath , err )
755
771
}
756
772
757
- hookFilePath = path .Join (workingDir , "teardown" , hookFilename )
758
- hookFile , err = fs .Create (hookFilePath )
773
+ hookFile , err = fs .Create (h .TeardownPath )
759
774
if err != nil {
760
- return errors .Errorf ("unable to open hook file %q: %v" , hookFilePath , err )
775
+ return errors .Errorf ("unable to open hook file %q: %v" , h . TeardownPath , err )
761
776
}
762
- if _ , err := hookFile .WriteString (hookConfig .Teardown ); err != nil {
777
+ if _ , err := hookFile .WriteString (h . Config .Teardown ); err != nil {
763
778
_ = hookFile .Close
764
- return errors .Errorf ("unable to setup hook %q: %v" , hookFilePath , err )
779
+ return errors .Errorf ("unable to setup hook %q: %v" , h . TeardownPath , err )
765
780
}
766
781
if err := hookFile .Close (); err != nil {
767
- return errors .Errorf ("unable to setup hook %q: %v" , hookFilePath , err )
782
+ return errors .Errorf ("unable to setup hook %q: %v" , h . TeardownPath , err )
768
783
}
769
784
}
770
785
}
771
786
return nil
772
787
}
773
788
774
- func (w * CurrentWorker ) executeHooksSetup (ctx context.Context , basedir afero.Fs , workingDir string ) error {
789
+ func (w * CurrentWorker ) executeHooksSetup (ctx context.Context , fs afero.Fs , workingDir string ) error {
775
790
if strings .EqualFold (runtime .GOOS , "windows" ) {
776
791
log .Warn (ctx , "hooks are not supported on windows" )
777
792
return nil
778
793
}
779
794
780
795
var result = make (map [string ]string )
781
- var setupDir = path .Join (workingDir , "setup" )
782
796
783
- var absPath string
784
- if x , ok := basedir .(* afero.BasePathFs ); ok {
785
- absPath , _ = x .RealPath (setupDir )
786
- absPath , _ = filepath .Abs (path .Dir (absPath ))
797
+ basedir , ok := fs .(* afero.BasePathFs )
798
+ if ! ok {
799
+ return sdk .WithStack (fmt .Errorf ("invalid given basedir" ))
787
800
}
788
801
789
- setupDir = filepath .Join (absPath , filepath .Base (setupDir ))
790
-
791
802
workerEnv := w .Environ ()
792
803
793
- err := filepath .Walk (setupDir , func (filepath string , info os.FileInfo , err error ) error {
804
+ for _ , h := range w .hooks {
805
+ filepath , err := basedir .RealPath (h .SetupPath )
794
806
if err != nil {
795
- return err
807
+ return sdk . WrapError ( err , "cannot get real path for: %s" , h . SetupPath )
796
808
}
797
- if info .IsDir () {
798
- return nil
809
+
810
+ infos := []sdk.SpawnInfo {{
811
+ RemoteTime : time .Now (),
812
+ Message : sdk.SpawnMsg {ID : sdk .MsgSpawnInfoWorkerHookRun .ID , Args : []interface {}{h .Config .Label }},
813
+ }}
814
+ if err := w .Client ().QueueJobSendSpawnInfo (ctx , w .currentJob .wJob .ID , infos ); err != nil {
815
+ return sdk .WrapError (err , "cannot record QueueJobSendSpawnInfo for job (err spawn): %d" , w .currentJob .wJob .ID )
799
816
}
800
817
801
818
str := fmt .Sprintf ("source %s ; echo '<<<ENVIRONMENT>>>' ; env" , filepath )
@@ -821,22 +838,35 @@ func (w *CurrentWorker) executeHooksSetup(ctx context.Context, basedir afero.Fs,
821
838
}
822
839
}
823
840
}
824
- return nil
825
- })
841
+ }
826
842
w .currentJob .envFromHooks = result
827
- return errors . WithStack ( err )
843
+ return nil
828
844
}
829
845
830
- func (w * CurrentWorker ) executeHooksTeardown (_ context.Context , basedir afero.Fs , workingDir string ) error {
831
- err := afero .Walk (basedir , path .Join (workingDir , "setup" ), func (path string , info fs.FileInfo , err error ) error {
832
- if info .IsDir () {
833
- return nil
846
+ func (w * CurrentWorker ) executeHooksTeardown (ctx context.Context , fs afero.Fs , workingDir string ) error {
847
+ basedir , ok := fs .(* afero.BasePathFs )
848
+ if ! ok {
849
+ return sdk .WithStack (fmt .Errorf ("invalid given basedir" ))
850
+ }
851
+
852
+ for _ , h := range w .hooks {
853
+ filepath , err := basedir .RealPath (h .SetupPath )
854
+ if err != nil {
855
+ return sdk .WrapError (err , "cannot get real path for: %s" , h .SetupPath )
856
+ }
857
+
858
+ infos := []sdk.SpawnInfo {{
859
+ RemoteTime : time .Now (),
860
+ Message : sdk.SpawnMsg {ID : sdk .MsgSpawnInfoWorkerHookRunTeardown .ID , Args : []interface {}{h .Config .Label }},
861
+ }}
862
+ if err := w .Client ().QueueJobSendSpawnInfo (ctx , w .currentJob .wJob .ID , infos ); err != nil {
863
+ return sdk .WrapError (err , "cannot record QueueJobSendSpawnInfo for job (err spawn): %d" , w .currentJob .wJob .ID )
834
864
}
835
- cmd := exec .Command ("bash" , "-c" , path )
865
+
866
+ cmd := exec .Command ("bash" , "-c" , filepath )
836
867
if output , err := cmd .CombinedOutput (); err != nil {
837
868
return errors .WithMessage (err , w .blur .String (string (output )))
838
869
}
839
- return nil
840
- })
841
- return err
870
+ }
871
+ return nil
842
872
}
0 commit comments