-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reorganize the chaincode package logic
Change-Id: I3009893c0f4c4e67fc3f9b83c08b5d6b2e7b1ea7 Signed-off-by: Greg Haskins <[email protected]>
- Loading branch information
Showing
7 changed files
with
232 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
Licensed under the Apache License, Version 2.0 (the 'License'); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an 'AS IS' BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var Golang = require('./packager/Golang.js'); | ||
var Car = require('./packager/Car.js'); | ||
var utils = require('./utils.js'); | ||
|
||
var logger = utils.getLogger('packager'); | ||
|
||
/** | ||
* Utility function to package a chaincode. The contents will be returned as a byte array. | ||
* | ||
* @param {Object} chaincodePath required - String of the path to location of | ||
* the source code of the chaincode | ||
* @param {Object} chaincodeType optional - String of the type of chaincode | ||
* ['golang', 'car', 'java'] (default 'golang') | ||
* @param {boolean} devmode optional - True if using dev mode | ||
* @returns {Promise} A promise for the data as a byte array | ||
*/ | ||
module.exports.package = function(chaincodePath, chaincodeType, devmode) { | ||
logger.debug('packager: chaincodePath: %s, chaincodeType: %s, devmode: %s',chaincodePath,chaincodeType,devmode); | ||
return new Promise(function(resolve, reject) { | ||
if (devmode) { | ||
logger.debug('packager: Skipping chaincode packaging due to devmode configuration'); | ||
return resolve(null); | ||
} | ||
|
||
if (!chaincodePath || chaincodePath && chaincodePath.length < 1) { | ||
// Verify that chaincodePath is being passed | ||
return reject(new Error('Missing chaincodePath parameter')); | ||
} | ||
|
||
let type = !!chaincodeType ? chaincodeType : 'golang'; | ||
logger.debug('packager: type %s ',type); | ||
|
||
let handler; | ||
|
||
switch (type) { | ||
case 'car': | ||
handler = Car.package; | ||
break; | ||
default: | ||
handler = Golang.package; | ||
} | ||
|
||
return resolve(handler(chaincodePath)); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
Licensed under the Apache License, Version 2.0 (the 'License'); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an 'AS IS' BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var utils = require('../utils.js'); | ||
|
||
var logger = utils.getLogger('packager/Car.js'); | ||
|
||
module.exports.package = function(path) { | ||
logger.info('Packaging CAR file from %s', path); | ||
return utils.readFile(path); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
Licensed under the Apache License, Version 2.0 (the 'License'); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an 'AS IS' BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var os = require('os'); | ||
var fs = require('fs-extra'); | ||
var tar = require('tar-fs'); | ||
var path = require('path'); | ||
var zlib = require('zlib'); | ||
var utils = require('../utils.js'); | ||
|
||
var logger = utils.getLogger('packager/Golang.js'); | ||
|
||
/** | ||
* All of the files in the directory of the environment variable | ||
* GOPATH joined to the request.chaincodePath will be included | ||
* in an archive file. | ||
*/ | ||
module.exports.package = function(chaincodePath) { | ||
return new Promise(function(resolve, reject) { | ||
logger.info('packaging GOLANG from %s', chaincodePath); | ||
|
||
// Determine the user's $GOPATH | ||
let goPath = process.env['GOPATH']; | ||
|
||
// Compose the path to the chaincode project directory | ||
let projDir = path.join(goPath, 'src', chaincodePath); | ||
|
||
// Create the .tar.gz file of the chaincode package | ||
fs.mkdtemp(path.join(os.tmpdir(), path.sep), (err, folder) => { | ||
if (err) return reject(new Error('Failed to create temp folder. ' + err)); | ||
|
||
// first copy all the target chaincode files from the source folder to | ||
// <this_temp_folder>/src/<chaincodePath> folder so that the tar.gz | ||
// archive can be created with the folder structure preserved | ||
var dest = path.join(folder, 'src', chaincodePath); | ||
fs.copy(projDir, dest, (err) => { | ||
if (err) return reject(new Error('Failed to copy chaincode source to temp folder. ' + err)); | ||
|
||
let targzFilePath = path.join(folder, 'deployment-package.tar.gz'); | ||
return generateTarGz(folder, targzFilePath) | ||
.then(function() { | ||
logger.debug('Successfully generated chaincode archive %s ', targzFilePath); | ||
return utils.readFile(targzFilePath) | ||
.then((data) => { | ||
logger.debug('Successful readFile to data in bytes'); | ||
return resolve(data); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}; | ||
|
||
// | ||
// generateTarGz creates a .tar.gz file from contents in the src directory and | ||
// saves them in a dest file. | ||
// | ||
function generateTarGz(src, dest) { | ||
// A list of file extensions that should be packaged into the .tar.gz. | ||
// Files with all other file extenstions will be excluded to minimize the size | ||
// of the install payload. | ||
var keep = [ | ||
'.go', | ||
'.yaml', | ||
'.json', | ||
'.c', | ||
'.h' | ||
]; | ||
|
||
return new Promise(function(resolve, reject) { | ||
// Create the pack stream specifying the ignore/filtering function | ||
var pack = tar.pack(src, { | ||
ignore: function(name) { | ||
// Check whether the entry is a file or a directory | ||
if (fs.statSync(name).isDirectory()) { | ||
// If the entry is a directory, keep it in order to examine it further | ||
return false; | ||
} else { | ||
// If the entry is a file, check to see if it's the Dockerfile | ||
if (name.indexOf('Dockerfile') > -1) { | ||
return false; | ||
} | ||
|
||
// If it is not the Dockerfile, check its extension | ||
var ext = path.extname(name); | ||
|
||
// Ignore any file who's extension is not in the keep list | ||
if (keep.indexOf(ext) === -1) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
} | ||
}) | ||
.pipe(zlib.Gzip()) | ||
.pipe(fs.createWriteStream(dest)); | ||
|
||
pack.on('close', function() { | ||
return resolve(dest); | ||
}); | ||
pack.on('error', function() { | ||
return reject(new Error('Error on fs.createWriteStream')); | ||
}); | ||
}); | ||
}; |
Oops, something went wrong.