1
1
package tasks
2
2
3
3
import (
4
- "bytes"
5
4
"crypto/md5"
6
5
"encoding/json"
7
6
"fmt"
8
7
"github.com/ansible-semaphore/semaphore/lib"
9
8
"io"
10
9
"io/ioutil"
11
10
"os"
12
- "os/exec"
13
- "regexp"
14
11
"strconv"
15
12
"strings"
16
13
"time"
@@ -257,10 +254,8 @@ func (t *TaskRunner) prepareRun() {
257
254
return
258
255
}
259
256
260
- // todo: write environment
261
-
262
- if stderr , err := t .listPlaybookHosts (); err != nil {
263
- t .Log ("Listing playbook hosts failed: " + err .Error () + "\n " + stderr )
257
+ if err := t .listPlaybookHosts (); err != nil {
258
+ t .Log ("Listing playbook hosts failed: " + err .Error ())
264
259
t .fail ()
265
260
return
266
261
}
@@ -546,64 +541,45 @@ func (t *TaskRunner) installRequirements() error {
546
541
}
547
542
548
543
func (t * TaskRunner ) runGalaxy (args []string ) error {
549
- cmd := exec .Command ("ansible-galaxy" , args ... ) //nolint: gas
550
- cmd .Dir = t .getRepoPath ()
551
-
552
- t .setCmdEnvironment (cmd , t .repository .SSHKey .GetSshCommand ())
553
-
554
- t .LogCmd (cmd )
555
- return cmd .Run ()
544
+ return lib.AnsiblePlaybook {
545
+ Logger : t ,
546
+ TemplateID : t .template .ID ,
547
+ Repository : t .repository ,
548
+ }.RunGalaxy (args )
556
549
}
557
550
558
- func (t * TaskRunner ) listPlaybookHosts () (string , error ) {
559
-
551
+ func (t * TaskRunner ) listPlaybookHosts () (err error ) {
560
552
if util .Config .ConcurrencyMode == "project" {
561
- return "" , nil
553
+ return
562
554
}
563
555
564
556
args , err := t .getPlaybookArgs ()
565
557
if err != nil {
566
- return "" , err
558
+ return
567
559
}
568
- args = append (args , "--list-hosts" )
569
-
570
- cmd := exec .Command ("ansible-playbook" , args ... ) //nolint: gas
571
- cmd .Dir = t .getRepoPath ()
572
- t .setCmdEnvironment (cmd , "" )
573
560
574
- var errb bytes.Buffer
575
- cmd .Stderr = & errb
576
-
577
- out , err := cmd .Output ()
561
+ t .hosts , err = lib.AnsiblePlaybook {
562
+ Logger : t ,
563
+ TemplateID : t .template .ID ,
564
+ Repository : t .repository ,
565
+ }.GetHosts (args )
578
566
579
- re := regexp .MustCompile (`(?m)^\\s{6}(.*)$` )
580
- matches := re .FindAllSubmatch (out , 20 )
581
- hosts := make ([]string , len (matches ))
582
- for i := range matches {
583
- hosts [i ] = string (matches [i ][1 ])
584
- }
585
- t .hosts = hosts
586
- return errb .String (), err
567
+ return
587
568
}
588
569
589
570
func (t * TaskRunner ) runPlaybook () (err error ) {
590
571
args , err := t .getPlaybookArgs ()
591
572
if err != nil {
592
573
return
593
574
}
594
- cmd := exec .Command ("ansible-playbook" , args ... ) //nolint: gas
595
- cmd .Dir = t .getRepoPath ()
596
- t .setCmdEnvironment (cmd , "" )
597
575
598
- t .LogCmd (cmd )
599
- cmd .Stdin = strings .NewReader ("" )
600
- err = cmd .Start ()
601
- if err != nil {
602
- return
603
- }
604
- t .process = cmd .Process
605
- err = cmd .Wait ()
606
- return
576
+ process := make (chan * os.Process )
577
+ t .process = <- process
578
+ return lib.AnsiblePlaybook {
579
+ Logger : t ,
580
+ TemplateID : t .template .ID ,
581
+ Repository : t .repository ,
582
+ }.Run (args , process )
607
583
}
608
584
609
585
func (t * TaskRunner ) getExtraVars () (str string , err error ) {
@@ -746,20 +722,6 @@ func (t *TaskRunner) getPlaybookArgs() (args []string, err error) {
746
722
return
747
723
}
748
724
749
- func (t * TaskRunner ) setCmdEnvironment (cmd * exec.Cmd , gitSSHCommand string ) {
750
- env := os .Environ ()
751
- env = append (env , fmt .Sprintf ("HOME=%s" , util .Config .TmpPath ))
752
- env = append (env , fmt .Sprintf ("PWD=%s" , cmd .Dir ))
753
- env = append (env , fmt .Sprintln ("PYTHONUNBUFFERED=1" ))
754
- env = append (env , fmt .Sprintln ("GIT_TERMINAL_PROMPT=0" ))
755
- env = append (env , extractCommandEnvironment (t .environment .JSON )... )
756
-
757
- if gitSSHCommand != "" {
758
- env = append (env , fmt .Sprintf ("GIT_SSH_COMMAND=%s" , gitSSHCommand ))
759
- }
760
- cmd .Env = env
761
- }
762
-
763
725
func hasRequirementsChanges (requirementsFilePath string , requirementsHashFilePath string ) bool {
764
726
oldFileMD5HashBytes , err := ioutil .ReadFile (requirementsHashFilePath )
765
727
if err != nil {
@@ -783,25 +745,6 @@ func writeMD5Hash(requirementsFile string, requirementsHashFile string) error {
783
745
return ioutil .WriteFile (requirementsHashFile , []byte (newFileMD5Hash ), 0644 )
784
746
}
785
747
786
- // extractCommandEnvironment unmarshalls a json string, extracts the ENV key from it and returns it as
787
- // []string where strings are in key=value format
788
- func extractCommandEnvironment (envJSON string ) []string {
789
- env := make ([]string , 0 )
790
- var js map [string ]interface {}
791
- err := json .Unmarshal ([]byte (envJSON ), & js )
792
- if err == nil {
793
- if cfg , ok := js ["ENV" ]; ok {
794
- switch v := cfg .(type ) {
795
- case map [string ]interface {}:
796
- for key , val := range v {
797
- env = append (env , fmt .Sprintf ("%s=%s" , key , val ))
798
- }
799
- }
800
- }
801
- }
802
- return env
803
- }
804
-
805
748
// checkTmpDir checks to see if the temporary directory exists
806
749
// and if it does not attempts to create it
807
750
func checkTmpDir (path string ) error {
0 commit comments