donal
2018-06-14 aa93ed4dfa25ec1eb834c2aed59672773bc67128
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Generated on 2016-08-09 using generator-angular-fullstack 2.1.1
'use strict';
 
module.exports = function (grunt) {
  let localConfig;
  try {
    localConfig = require('./server/config/local.env');
  } catch(e) {
    localConfig = {};
  }
 
  // Load grunt tasks automatically, when needed
  require('jit-grunt')(grunt, {
    express: 'grunt-express-server'
  });
 
  // Time how long tasks take. Can help when optimizing build times
  require('time-grunt')(grunt);
 
  grunt.task.loadTasks('./tasks');
 
  // Define the configuration for all the tasks
  grunt.initConfig({
 
    // Project settings
    pkg: grunt.file.readJSON('package.json'),
    express: {
      options: {
        port: process.env.PORT || 9000
      },
      dev: {
        options: {
          script: 'server/app.js',
          debug: true
        }
      },
      prod: {
        options: {
          script: 'dist/server/app.js'
        }
      }
    },
 
    // Make sure code styles are up to par and there are no obvious mistakes
    jshint: {
      options: {
        reporter: require('jshint-stylish')
      },
      server: {
        options: {
          jshintrc: 'server/.jshintrc'
        },
        src: [
          'server/**/*.js',
          '!server/**/*.spec.js'
        ]
      },
      serverTest: {
        options: {
          jshintrc: 'server/.jshintrc'
        },
        src: ['server/**/*.spec.js']
      },
      ci_server: {
        options: {
          jshintrc: 'server/.jshintrc',
          reporter: require('jshint-jenkins-checkstyle-reporter'),
          reporterOutput: 'reports/server/linting/jshint-server.xml'
        },
        src: [
          'server/**/*.js',
          'server/**/*.spec.js'
        ]
      },
      // bm_server: {
      //   options: {
      //     jshintrc: 'server/.jshintrc',
      //     reporter: require('jshint-junit-reporter'),
      //     reporterOutput: 'reports/server/linting/jshint-junit-server.xml'
      //   },
      //   src: [
      //     'server/**/*.js',
      //     'server/**/*.spec.js'
      //   ]
      // }
    },
 
    // Empties folders to start fresh
    clean: {
      server: '.tmp',
      mochareports: 'reports/server/mocha/**',
      lint: 'reports/{server}/linting/**',
      coverage: 'reports/coverage/**'
    },
 
    // Use nodemon to run server in debug mode with an initial breakpoint
    nodemon: {
      debug: {
        script: 'server/app.js',
        options: {
          nodeArgs: ['--debug-brk'],
          env: {
            PORT: process.env.PORT || 9000
          },
          callback: function (nodemon) {
            nodemon.on('log', function (event) {
              console.log(event.colour);
            });
 
            // opens browser on initial server start
            nodemon.on('config:update', function () {
              setTimeout(function () {
                require('open')('http://localhost:8080/debug?port=5858');
              }, 500);
            });
          }
        }
      }
    },
 
    // Run some tasks in parallel to speed up the build process
    concurrent: {
      server: [
      ],
      test: [
      ],
      debug: {
        tasks: [
          'nodemon',
        ],
        options: {
          logConcurrentOutput: true
        }
      },
      dist: [
      ]
    },
 
    mochaTest: {
      terminal: {
        options: {
          reporter: 'spec',
          require: 'tasks/blanket'
        },
        src: ['server/**/*.spec.js']
      },
      junit: {
        options: {
          reporter: 'mocha-junit-reporter',
          // require: require("blanket"),
          require: 'tasks/blanket'
        },
        src: ['server/**/*.spec.js']
      },
      html: {
        options: {
          // reporters are  ['html-cov', 'json-cov', 'travis-cov', 'mocha-lcov-reporter', 'mocha-cobertura-reporter'],
          reporter: 'html-cov',
          quiet: true,
          captureFile: 'reports/server/coverage/cobertura-report.html'
        },
        src: ['server/**/*.spec.js']
      },
      cobertura: {
        options: {
          reporter: 'mocha-cobertura-reporter',
          // use the quiet flag to suppress the mocha console output
          quiet: true,
          // specify a destination file to capture the mocha
          // output (the quiet option does not suppress this)
          captureFile: 'reports/server/coverage/cobertura-report.xml'
        },
        src: ['server/**/*.spec.js']
      },
      travis: {
        options: {
          reporter: 'travis-cov',
          quiet: false
        },
        src: ['server/**/*.spec.js']
      }
    },
 
    env: {
      test: {
        NODE_ENV: 'test'
      },
      dev: {
        NODE_ENV: 'development'
      },
      ci: {
        NODE_ENV: 'ci'
      },
      si: {
        NODE_ENV: 'si'
      },
      prod: {
        NODE_ENV: 'production'
      },
      jenkins: {
        MOCHA_FILE: 'reports/server/mocha/test-results.xml'
      },
      all: localConfig
    },
  });
 
  // Used for delaying livereload until after server has restarted
  grunt.registerTask('wait', function () {
    grunt.log.ok('Waiting for server reload...');
 
    const done = this.async();
 
    setTimeout(function () {
      grunt.log.writeln('Done waiting!');
      done();
    }, 1500);
  });
 
  grunt.registerTask('express-keepalive', 'Keep grunt running', function() {
    this.async();
  });
 
  grunt.registerTask('build-image', 'Build the image', function(imageId) {
    const shell = require("shelljs");
    grunt.log.ok('BUILDING IMAGE');
    if (!imageId) {
      grunt.fail.warn('must supply an imageId to build');
    }
    const rc = shell.exec('docker build -t todolist:' + imageId + ' -f ./dist/Dockerfile ./dist').code;
    if (rc > 0){
      grunt.fail.warn("DOCKER FAILURE")
    }
  });
 
  grunt.registerTask('deploy', 'deploy the node js app to a docker container and start it in the correct mode', function(target_env, build_tag) {
    grunt.log.ok('this task must run on a host that has the Docker Daemon running on it');
    const ports = {
      ci: '9001',
      si: '9002',
      production: '80'
    };
 
    if (target_env === undefined || build_tag === undefined){
      grunt.fail.warn('Required param not set - use grunt deploy\:\<target\>\:\<tag\>');
    } else {
      const shell = require("shelljs");
      grunt.log.ok('STOPPING AND REMOVING EXISTING CONTAINERS');
      shell.exec('docker stop todolist-'+ target_env + ' && docker rm todolist-'+ target_env);
 
      grunt.log.ok('DEPLOYING ' + target_env + ' CONTAINER');
      if (target_env === 'ci'){
        const rc = shell.exec('docker run -t -d --name todolist-' + target_env + ' -p ' + ports[target_env]+ ':'+ports[target_env]+' --env NODE_ENV=' + target_env + ' todolist:' + build_tag);
        if (rc > 0){
          grunt.fail.warn("DOCKER FAILURE")
        }
      } else {
        // ensure mongo is up
        const isMongo = shell.exec('docker ps | grep devops-mongo').code;
        if (isMongo > 0){
          grunt.log.ok('DEPLOYING Mongodb CONTAINER FIRST');
          shell.exec('docker run --name devops-mongo -p 27017:27017 -d mongo');
        }
        const rc = shell.exec('docker run -t -d --name todolist-' + target_env + ' --link devops-mongo:mongo.server -p '
            + ports[target_env]+ ':' + ports[target_env] + ' --env NODE_ENV=' + target_env + ' todolist:' + build_tag).code;
        if (rc > 0){
          grunt.fail.warn("DOCKER FAILURE");
        }
      }
    }
 
  });
 
  grunt.registerTask('serve', function (target) {
    if (target === 'dist') {
      return grunt.task.run(['build', 'env:all', 'env:prod', 'express:prod', 'wait', 'express-keepalive']);
    }
 
    if (target === 'debug') {
      return grunt.task.run([
        'clean:server',
        'env:all',
        'concurrent:server',
        'concurrent:debug'
      ]);
    }
 
    grunt.task.run([
      'clean:server',
      'env:all',
      'env:' + (target || 'dev'),
      'express:dev',
      'wait',
      'express-keepalive'
    ]);
  });
 
  grunt.registerTask('test', function(target, environ) {
    environ = environ !== undefined ? environ : 'test';
    const reporter = 'terminal';
    const coverage = 'travis';
    if (target === 'server-jenkins') {
      target = 'server';
      reporter = 'junit';
      coverage = 'cobertura';
      grunt.task.run(['env:jenkins']);
    }
    if (target === 'server') {
      return grunt.task.run([
        'clean:mochareports',
        'clean:coverage',
        'env:all',
        'env:'+environ,
        'mochaTest:' + reporter,
        'mochaTest:' + 'html',
        'mochaTest:' + coverage
      ]);
    }
 
    else grunt.task.run([
      'test:server'
    ]);
  });
 
  grunt.registerTask('build', [
    'clean',
    'concurrent:dist',
    'newer:jshint',
    'test',
  ]);
 
  grunt.registerTask('default', [
    'newer:jshint',
    'test',
    'build'
  ]);
};