@@ -21,10 +21,30 @@ Workspace.prototype.clone = async function() {
2121 if ( ! fs . existsSync ( parentDir ) ) {
2222 fs . mkdirSync ( parentDir , { recursive : true } ) ;
2323 }
24+ if ( ! this . workspace . workspace ) {
25+ return
26+ }
27+ if ( fs . existsSync ( parentDir + "/" + this . workspace . id ) ) {
28+ /* cleanup if workspace url change for same id : delete workspace */
29+ const currentRemote = execSync ( "git remote get-url origin" , { cwd : parentDir + "/" + this . workspace . id } ) . toString ( ) . trim ( ) ;
30+ if ( currentRemote != this . workspace . workspace ) {
31+ try {
32+ /* check access to the futur remote before deleting the workspace of the old one */
33+ execSync ( `git remote set-url origin ${ this . workspace . workspace } ` , { cwd : parentDir + "/" + this . workspace . id } ) ;
34+ execSync ( `git ls-remote` , { cwd : parentDir + "/" + this . workspace . id } ) ;
35+ /* delete workspace */
36+ fs . rmSync ( parentDir + "/" + this . workspace . id , { recursive : true } ) ;
37+ } catch ( e ) {
38+ /* rollback cleanup */
39+ execSync ( `git remote set-url origin ${ currentRemote } ` , { cwd : parentDir + "/" + this . workspace . id } ) ;
40+ }
41+ }
42+ }
2443 if ( ! fs . existsSync ( parentDir + "/" + this . workspace . id ) ) {
25- await exec ( `git clone ${ this . workspace . workspace } ${ this . workspace . id } ` , { cwd : parentDir } ) ;
44+ execSync ( `git clone ${ this . workspace . workspace } ${ this . workspace . id } ` , { cwd : parentDir } ) ;
2645 } else {
27- await exec ( "git pull" , { cwd : parentDir + "/" + this . workspace . id } ) ;
46+ /* update workspace */
47+ execSync ( "git pull" , { cwd : parentDir + "/" + this . workspace . id } ) ;
2848 /* prune log */
2949 exec ( `find ${ path . join ( parentDir , this . workspace . id , 'log' ) } -type f -exec sed -ne':a;$p;N;11,$D;ba' -i {} \\;` ) . catch ( e => { } ) ;
3050 }
@@ -61,7 +81,7 @@ Workspace.prototype.getEnvironementById = async function (id) {
6181Workspace . prototype . getUnixShebang = function ( relativeScriptPath ) {
6282 const filename = this . context . getFilenameInUserDir ( "genghistaskdata/" + this . workspace . id + "/shell/" + relativeScriptPath ) ;
6383 if ( ! fs . existsSync ( filename ) ) {
64- return '/bin/bash' ;
84+ return ' /bin/bash' ;
6585 }
6686 return (
6787 ( new String ( fs . readFileSync ( filename ) ) . match ( / ( ^ # ! .* ) / ) || [ ] ) [ 1 ] || ''
@@ -75,7 +95,7 @@ Workspace.prototype.getScriptStream = function(relativeScriptPath, env) {
7595 if ( fs . existsSync ( filename ) ) {
7696 script = fs . readFileSync ( filename )
7797 }
78- const declaredEnv = execSync ( 'export -p' , { env : env } ) ;
98+ const declaredEnv = execSync ( 'export -p' , { env : env , cwd : env . WORKSPACE } ) ;
7999 return Readable . from ( declaredEnv + script ) ;
80100}
81101
@@ -87,12 +107,15 @@ Workspace.prototype.getLogFile = function(logname, suffix) {
87107 }
88108 return fs . readFileSync ( logfile ) ;
89109}
110+ Workspace . prototype . getDirectory = function ( ) {
111+ return this . context . getFilenameInUserDir ( "genghistaskdata/" + this . workspace . id ) ;
112+ }
90113
91114Workspace . prototype . launch = function ( remote , script , payload , logname ) {
92115 //@TODO implement log.log to trace last execution date
93116 const logfile = this . context . getFilenameInUserDir ( "genghistaskdata/" + this . workspace . id + "/log/" ) ;
94117 fs . mkdirSync ( logfile + logname , { recursive : true } ) ;
95- const child = spawn ( "sh" , [ "-c" , remote + this . getUnixShebang ( script ) ] , payload ) ;
118+ const child = spawn ( "sh" , [ "-c" , remote + this . getUnixShebang ( script ) ] , { env : payload , cwd : payload . WORKSPACE } ) ;
96119 if ( ! child ) {
97120 return false ;
98121 }
0 commit comments