Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Modernization" of the plugin e.a. #10

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
},
extends: 'eslint:recommended',
env: {
browser: true
node: true
},
rules: {
}
Expand Down
29 changes: 22 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# ember-cli-deploy-gcloud
`ember-cli-deploy-gcloud` makes it easier to deploy Ember FastBoot applications to Google App Engine. It's like [ember-cli-deploy-fastboot-app-server-aws](https://github.com/ember-cli-deploy/ember-cli-deploy-fastboot-app-server-aws) but for the Google Cloud Platform. It:

`ember-cli-deploy-gcloud` makes it easier to deploy Ember FastBoot applications to Google App Engine.
- zips the built assets
- uploads the zip to Google Cloud Storage
- uploads a manifest file that indicates to [fastboot-gcloud-storage-downloader](https://github.com/EmberSherpa/fastboot-gcloud-storage-downloader) where to fetch the zip.

## Features
- `ember-cli-deploy-gcloud` blueprint
## FYI
This project only handles deploying the FastBoot server. It does not upload app code to Google Storage bucket.
For that, you need to use [`ember-cli-deploy-gcloud-storage`](https://github.com/knownasilya/ember-cli-deploy-gcloud-storage)

## `ember-cli-deploy-gcloud <project_id>` blueprint

Expand All @@ -15,10 +19,21 @@ To setup,
2. `cd server`
3. `npm run deploy`

## FYI

This project only handles deploying the FastBoot server. It does not upload app code to Google Storage bucket.
For that, you need to use [`ember-cli-deploy-gcloud-storage`](https://github.com/knownasilya/ember-cli-deploy-gcloud-storage)
## Usage as `ember-cli-deploy` plugin

```
// config/deploy.js

ENV['gcloud'] = {
bucket: <gcloud-storage-bucket>,
key: <path-for-manifest-file>, // e.g. 'fastboot-deploy-info.json
// optional
credentials: {
private_key: <google-cloud-private-key>
client_email: <google-cloud-client-email>
}
};
```

## Installation

Expand Down
39 changes: 16 additions & 23 deletions lib/gcloud/storage.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,26 @@
/* jshint node: true */
'use strict';

const Readable = require('stream').Readable;
const googleCloud = require('google-cloud');
const { Storage } = require('@google-cloud/storage');

class Storage {

upload(bucket, key, file) {

const storage = googleCloud.storage();

let upload = storage.bucket(bucket).file(key);

if (typeof file === 'string') {
let stream = new Readable();
stream.push(file);
stream.push(null);
file = stream;
class GoogleCloudStorage {
constructor(credentials) {
const options = {};
if (credentials) {
options.credentials = credentials;
}

return new Promise(function(resolve, reject){
file.pipe(upload.createWriteStream())
.on('error', function(error){
reject('Failed to upload file ' + key + ' - ' + error.message);
})
.on('finish', resolve);
});
this.storage = new Storage(options);
}

upload(bucket, key, filepath) {
return this.storage.bucket(bucket).upload(
filepath,
{
destination: key
}
);
}
}

module.exports = Storage;
module.exports = GoogleCloudStorage;
20 changes: 11 additions & 9 deletions lib/google-cloud-deploy-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,35 @@ const CONFIG_ENV_MAPPING = {
};

module.exports = ElasticBeanstalkDeployPlugin.extend({

configure() {
var config = this.pluginConfig;
let config = this.pluginConfig;

// Copy environment variables to the config if defined.
for (var key in CONFIG_ENV_MAPPING) {
for (let key in CONFIG_ENV_MAPPING) {
if (process.env[key]) {
config[CONFIG_ENV_MAPPING[key]] = process.env[key];
}
}

this._super.configure.apply(this, arguments);
},

upload: function(context) {
let bucket = this.readConfig('bucket');
let key = this.readConfig('key');
let credentials = this.readConfig('credentials');

let uploadTask = new UploadTask({
context: context,
context,
log: this.log.bind(this),
hashedZipPath: context.hashedZipPath,
bucket: bucket,
key: key
bucket,
key,
credentials
});

return uploadTask.run();
}
});

});
53 changes: 29 additions & 24 deletions lib/tasks/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const GoogleCloudStorage = require('../gcloud/storage');

const fs = require('fs');
const path = require('path');
const util = require('util');

class GoogleCloudStorageUploadTask {

Expand All @@ -15,38 +16,42 @@ class GoogleCloudStorageUploadTask {
this.key = options.key;
this.hashedZipPath = options.hashedZipPath;
this.hashedZipKey = path.basename(this.hashedZipPath);
this.storage = new GoogleCloudStorage();

this.storage = new GoogleCloudStorage(options.credentials);
}


run() {
return this.uploadHashedZip()
.then(() => this.uploadVersionFile());

async run() {
await this.uploadHashedZip();
await this.uploadVersionFile();
}

uploadHashedZip() {
let hashedZipPath = this.hashedZipPath;
let bucket = this.bucket;
this.log(`uploading ${this.hashedZipPath} to ${this.bucket} at ${this.hashedZipKey}`, { verbose: true });

this.log('uploading ' + hashedZipPath + ' to ' + bucket, { verbose: true });
return this.storage.upload(
this.bucket,
this.hashedZipKey,
path.resolve(this.hashedZipPath)
);
}

let file = fs.createReadStream(hashedZipPath);
async uploadVersionFile() {
this.log(`uploading version file to ${this.bucket} at ${this.key}`, { verbose: true });

return this.storage.upload(bucket, this.hashedZipKey, file);
}

uploadVersionFile() {
let versionFile = {
bucket: this.bucket,
key: this.hashedZipKey
};
const dir = path.dirname(this.hashedZipPath);
const filename = path.resolve(path.join(dir, this.key));

versionFile = JSON.stringify(versionFile);
const writeFile = util.promisify(fs.writeFile);
await writeFile(
filename,
JSON.stringify({
bucket: this.bucket,
key: this.hashedZipKey
})
);

return this.storage.upload(this.bucket, this.key, versionFile);
return this.storage.upload(this.bucket, this.key, filename);
}

}

module.exports = GoogleCloudStorageUploadTask;
module.exports = GoogleCloudStorageUploadTask;
32 changes: 16 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,31 @@
"start": "ember server",
"test": "ember try:each"
},
"dependencies": {
"dependencies": {
"@google-cloud/storage": "^4.1.0",
"ember-cli-deploy-elastic-beanstalk": "0.5.0",
"ember-cli-deploy-plugin": "0.2.9",
"google-cloud": "^0.55.0"
"ember-cli-deploy-plugin": "0.2.9"
},
"devDependencies": {
"broccoli-asset-rev": "^2.4.5",
"ember-ajax": "^3.0.0",
"ember-cli": "2.13.2",
"ember-cli-dependency-checker": "^1.3.0",
"ember-cli-eslint": "^3.0.0",
"ember-cli-htmlbars": "^1.1.1",
"ember-cli-htmlbars-inline-precompile": "^0.4.0",
"ember-cli-inject-live-reload": "^1.4.1",
"broccoli-asset-rev": "^3.0.0",
"ember-ajax": "^5.0.0",
"ember-cli": "3.13.1",
"ember-cli-dependency-checker": "^3.2.0",
"ember-cli-eslint": "^5.1.0",
"ember-cli-htmlbars": "^4.0.8",
"ember-cli-htmlbars-inline-precompile": "^3.0.1",
"ember-cli-inject-live-reload": "^2.0.2",
"ember-cli-qunit": "^4.0.0",
"ember-cli-release": "^1.0.0-beta.2",
"ember-cli-shims": "^1.1.0",
"ember-cli-sri": "^2.1.0",
"ember-cli-uglify": "^1.2.0",
"ember-cli-uglify": "^3.0.0",
"ember-disable-prototype-extensions": "^1.1.0",
"ember-export-application-global": "^2.0.0",
"ember-load-initializers": "^1.0.0",
"ember-resolver": "^4.0.0",
"ember-source": "~2.13.0",
"ember-welcome-page": "^3.0.0",
"ember-load-initializers": "^2.1.1",
"ember-resolver": "^5.3.0",
"ember-source": "~3.14.1",
"ember-welcome-page": "^4.0.0",
"loader.js": "^4.2.3"
},
"engines": {
Expand Down
Loading