1313require ( 'loadenv' ) ( ) ;
1414
1515var fs = require ( 'fs' ) ;
16+ var Build = require ( 'models/mongo/build' ) ;
1617var Context = require ( 'models/mongo/context' ) ;
1718var ContextVersion = require ( 'models/mongo/context-version' ) ;
1819var InfraCodeVersion = require ( 'models/mongo/infra-code-version' ) ;
@@ -52,9 +53,8 @@ async.series([
5253 } ) ;
5354 } ) ;
5455 } ,
55- removeCurrentSourceTemplates ,
5656 createBlankSourceContext ,
57- createFirstSourceContext ,
57+ createFirstSourceContext
5858] , function ( err ) {
5959 console . log ( 'done' ) ;
6060 if ( err ) { console . error ( err ) ; }
@@ -65,14 +65,23 @@ var createdBy = {
6565 github : process . env . HELLO_RUNNABLE_GITHUB_ID
6666} ;
6767
68- function removeCurrentSourceTemplates ( cb ) {
69- Context . find ( { 'isSource' : true } , function ( err , docs ) {
68+ function removeCurrentSourceTemplates ( name , cb ) {
69+ Context . find ( { 'name' : name , ' isSource' : true } , function ( err , docs ) {
7070 if ( err ) { return cb ( err ) ; }
7171 async . each ( docs , function ( doc , cb ) {
7272 // we don't ever want to delete old contexts, once the are in use by users
7373 console . log ( 'UN-SOURCING OLD SOURCES' ) ;
74- doc . update ( { $set : { isSource :false , oldSource :Date . now ( ) } } , cb ) ;
75- } , cb ) ;
74+ doc . update ( {
75+ $set : {
76+ name : doc . name + Date . now ( ) ,
77+ lowerName : doc . lowerName + Date . now ( ) ,
78+ isSource : false ,
79+ oldSource : Date . now ( )
80+ }
81+ } , cb ) ;
82+ } , function ( ) {
83+ cb ( ) ;
84+ } ) ;
7685 } ) ;
7786}
7887
@@ -88,9 +97,12 @@ function createBlankSourceContext (cb) {
8897 async . each ( docs , function ( doc , cb ) {
8998 console . log ( 'REMOVING INSTANCES' , docs ) ;
9099 doc . remove ( cb ) ;
91- } , cb ) ;
100+ } , function ( ) {
101+ cb ( null , 'Blank' ) ;
102+ } ) ;
92103 } ) ;
93104 } ,
105+ removeCurrentSourceTemplates ,
94106 function newContext ( cb ) {
95107 console . log ( 'newContext (blank)' ) ;
96108 var context = new Context ( {
@@ -135,14 +147,21 @@ function createFirstSourceContext(finalCB) {
135147 'lowerName' : ( ( ( model . isTemplate ) ? 'TEMPLATE_' : '' ) + model . name ) . toLowerCase ( ) ,
136148 'owner' : createdBy
137149 } , function ( err , docs ) {
150+ if ( docs && docs . length ) {
151+ // If it already exists, just skip them
152+ return thisCb ( ) ;
153+ }
138154 console . log ( 'REMOVING existing instance for (' , model . name , ')' ) ;
139155 if ( err ) { return cb ( err ) ; }
140156 async . each ( docs , function ( doc , cb ) {
141157 console . log ( 'REMOVING INSTANCES' , docs ) ;
142158 doc . remove ( cb ) ;
143- } , cb ) ;
159+ } , function ( ) {
160+ cb ( null , model . name ) ;
161+ } ) ;
144162 } ) ;
145163 } ,
164+ removeCurrentSourceTemplates ,
146165 function ( cb ) {
147166 if ( model . isTemplate ) {
148167 return cb ( ) ;
@@ -202,9 +221,11 @@ function createFirstSourceContext(finalCB) {
202221
203222 function buildBuild ( build , cb ) {
204223 console . log ( 'buildBuild (' , model . name , ')' ) ;
205- build . build ( { message : 'seed instance script' } , function ( err ) {
206- cb ( err , build ) ;
207- } ) ;
224+ build . build ( { message : 'seed instance script' , noCache : true } , function ( err , buildDoc ) {
225+ setTimeout ( function ( ) {
226+ cb ( err , build ) ;
227+ } , 500 )
228+ } )
208229 }
209230
210231 function createInstance ( build , cb ) {
0 commit comments