1 | fs = require 'fs-extra'
|
2 | path = require 'path'
|
3 | temp = require 'temp'
|
4 | request = require 'request'
|
5 | TarGz = require 'tar.gz'
|
6 | Zip = require 'zip'
|
7 |
|
8 | class CommandBuild
|
9 | parseOptions: =>
|
10 | options = {
|
11 | connector: process.env.PACKAGER_CONNECTOR
|
12 | tag: process.env.PACKAGER_TAG
|
13 | os: process.env.PACKAGER_OS
|
14 | arch: process.env.PACKAGER_ARCH
|
15 | build_dir: process.cwd()
|
16 | }
|
17 |
|
18 | return @panic new Error('Missing PACKAGER_CONNECTOR') unless options.connector?
|
19 | return @panic new Error('Missing PACKAGER_TAG') unless options.tag?
|
20 | return @panic new Error('Missing PACKAGER_OS') unless options.os?
|
21 | return @panic new Error('Missing PACKAGER_ARCH') unless options.arch?
|
22 |
|
23 | unless options.os in ['darwin', 'windows', 'linux']
|
24 | return @panic new Error('Invalid OS, must be one of ["darwin", "windows", "linux"]')
|
25 |
|
26 | unless options.arch in ['386', 'amd64']
|
27 | return @panic new Error('Invalid ARCH, must be one of ["386", "amd64"]')
|
28 |
|
29 | @options = options
|
30 |
|
31 | getFileName: =>
|
32 | {os, arch} = @options
|
33 | return "#{os}-#{arch}.bundle"
|
34 |
|
35 | getFileNameWithExt: =>
|
36 | {os} = @options
|
37 | return "#{@getFileName()}.tar.gz"
|
38 |
|
39 | getStartScript: (tmpDir, callback) =>
|
40 | console.log "getting start script"
|
41 | {os, arch} = @options
|
42 | ext = ""
|
43 | ext = ".exe" if os == "windows"
|
44 | destination = path.join(tmpDir, @getFileName(), "start#{ext}")
|
45 | request({
|
46 | baseUrl: "https://meshblu-connector.octoblu.com/tools"
|
47 | uri: "/go-meshblu-connector-ignition/latest/meshblu-connector-ignition-#{os}-#{arch}"
|
48 | })
|
49 | .on 'error', (error) =>
|
50 | console.log '### start script error'
|
51 | callback error
|
52 | .on 'end', =>
|
53 | console.log '### start script done'
|
54 | fs.chmodSync(destination, 0x0755)
|
55 | callback null
|
56 | .pipe(fs.createWriteStream(destination))
|
57 |
|
58 | bundle: (tmpDir, tag) =>
|
59 | console.log "bundling #{tag}"
|
60 | {build_dir, connector} = @options
|
61 | destination = path.join(build_dir, "deploy/#{connector}/#{tag}", @getFileNameWithExt())
|
62 | bundle_dir = path.join tmpDir, @getFileName()
|
63 | new TarGz({}, {fromBase: true}).compress bundle_dir, destination, (error) =>
|
64 | return @panic error if error?
|
65 | console.log "bundled #{tag}"
|
66 |
|
67 | copyToTemp: (tmpDir) =>
|
68 | console.log '### copying to temp'
|
69 | {build_dir} = @options
|
70 | filter = (filePath) =>
|
71 | relativePath = filePath.replace("#{build_dir}/", "")
|
72 | if relativePath.indexOf('.') == 0
|
73 | return false
|
74 | if relativePath.indexOf('deploy') == 0
|
75 | return false
|
76 | return true
|
77 |
|
78 | toDir = path.join tmpDir, @getFileName()
|
79 | fs.copySync build_dir, toDir, {filter}
|
80 |
|
81 | setupDirectories: (callback) =>
|
82 | console.log '### setting up...'
|
83 | {connector, os, arch, tag, build_dir} = @options
|
84 | fs.removeSync path.join(build_dir, "deploy")
|
85 | dirName = "#{connector}-#{tag}-#{os}-#{arch}"
|
86 | temp.mkdir dirName, (error, tmpDir) =>
|
87 | return callback error if error?
|
88 | fs.mkdirpSync path.join(tmpDir, @getFileName())
|
89 | fs.mkdirpSync path.join build_dir, "deploy/#{connector}/latest"
|
90 | fs.mkdirpSync path.join build_dir, "deploy/#{connector}/#{tag}"
|
91 | callback null, tmpDir
|
92 |
|
93 | run: =>
|
94 | @parseOptions()
|
95 | temp.track()
|
96 | @setupDirectories (error, tmpDir) =>
|
97 | return @panic error if error?
|
98 | @getStartScript tmpDir, (error) =>
|
99 | return @panic error if error?
|
100 | @copyToTemp tmpDir
|
101 | @bundle tmpDir, 'latest'
|
102 | @bundle tmpDir, @options.tag
|
103 |
|
104 | panic: (error) =>
|
105 | console.error error if error?
|
106 | process.exit 1
|
107 |
|
108 | new CommandBuild().run()
|