Skip to content

Commit

Permalink
Merge pull request #13 from serverless/dev
Browse files Browse the repository at this point in the history
Publish first release
  • Loading branch information
pragnagopa committed Feb 20, 2017
2 parents 38fbc52 + 27e39f0 commit 629ade7
Show file tree
Hide file tree
Showing 26 changed files with 3,289 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
*.dll
.eslintrc.js
.eslintrc.json
36 changes: 36 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2016 Serverless

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
77 changes: 75 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,75 @@
# serverless-azure-functions
WIP – Coming soon...
# Azure Functions Plugin

This plugin enables Azure Functions support within the Serverless Framework.

## Getting started


### Get a Serverless Service with Azure as the Provider

1. Clone gitrepo: `git clone -b dev https://github.com/pragnagopa/boilerplate-azurefunctions.git`.
2. npm install

### Get an Azure Subscription
- <a href="https://azure.microsoft.com/en-us/free/?b=17.01" target="_blank">Create your free Azure account today</a>

### Create Service Principal User for your Azure subscription
1. Create a Service Principal User with <a href="https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal" target="_blank">portal</a> or <a href="https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-authenticate-service-principal-cli" target="_blank">Azure CLI</a>
2. <a href="https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal#get-tenant-id" target="_blank">Get tenant ID</a>
3. <a href="https://blogs.msdn.microsoft.com/mschray/2015/05/13/getting-your-azure-guid-subscription-id/" target="_blank">Get Azure subscription ID</a>
4. <a href="https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal#get-application-id-and-authentication-key" target="_blank">Get application ID</a>. Note this is also referred to as the client id.


### Set the following environment variables:

- azureSubId: YourAzureSubscriptionID
- azureServicePrincipalTenantId: servicePrincipalTenantId
- azureservicePrincipalClientId: servicePrincipalClientId
- azureServicePrincipalPassword: servicePrincipalPassword

**Note:** If you created Service Principal User from Portal, servicePrincipalPassword is the authentication key

### Update the config in `serverless.yml`

Open up your `serverless.yml` file and update the following information:

#### `service` property

```yml
service: my-azure-functions-app # Name of the Azure function App you want to create
```
### Quick Start
1. **Deploy a Service:**
Use this when you have made changes to your Functions or you simply want to deploy all changes within your Service at the same time.
```bash
serverless deploy
```

2. **Deploy the Function:**

Use this to quickly upload and overwrite your Azure function, allowing you to develop faster.
```bash
serverless deploy function -f httpjs
```

3. **Invoke the Function:**

Invokes an Azure Function on Azure
```bash
serverless invoke --path httpQueryString.json -f httpjs
```

4. **Stream the Function Logs:**

Open up a separate tab in your console and stream all logs for a specific Function using this command.
```bash
serverless logs -f httpjs -t
```

5. **Remove the Service:**

Removes all Functions and Resources from your Azure subscription.
```bash
serverless remove
36 changes: 36 additions & 0 deletions deploy/azureDeploy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

const BbPromise = require('bluebird');
const CreateResourceGroupAndFunctionApp = require('./lib/CreateResourceGroupAndFunctionApp');
const createFunctions = require('./lib/createFunctions');
const cleanUpFunctions = require('./lib/cleanUpFunctions');
const loginToAzure = require('../shared/loginToAzure');

class AzureDeploy {
constructor (serverless, options) {
this.serverless = serverless;
this.options = options;
this.provider = this.serverless.getProvider('azure');

Object.assign(
this,
loginToAzure,
cleanUpFunctions,
CreateResourceGroupAndFunctionApp,
createFunctions
);

this.hooks = {
'before:deploy:deploy': () => BbPromise.bind(this)
.then(this.loginToAzure)
.then(this.cleanUpFunctions),

'deploy:deploy': () => BbPromise.bind(this)
.then(this.CreateResourceGroupAndFunctionApp)
.then(this.createFunctions)
.then(() => this.serverless.cli.log('Successfully created Function App'))
};
}
}

module.exports = AzureDeploy;
28 changes: 28 additions & 0 deletions deploy/azureDeployFunction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const BbPromise = require('bluebird');
const createFunction = require('./lib/createFunction');
const loginToAzure = require('../shared/loginToAzure');

class AzureDeployFunction {
constructor (serverless, options) {
this.serverless = serverless;
this.options = options;
this.provider = this.serverless.getProvider('azure');

Object.assign(
this,
loginToAzure,
createFunction
);

this.hooks = {
'deploy:function:deploy': () => BbPromise.bind(this)
.then(this.loginToAzure)
.then(this.createFunction)
.then(() => this.serverless.cli.log('Successfully uploaded Function'))
};
}
}

module.exports = AzureDeployFunction;
10 changes: 10 additions & 0 deletions deploy/lib/CreateResourceGroupAndFunctionApp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

module.exports = {
CreateResourceGroupAndFunctionApp () {

return this.provider.CreateResourceGroup()
.then(() => this.provider.CreateFunctionApp());
}
};

9 changes: 9 additions & 0 deletions deploy/lib/cleanUpFunctions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = {
cleanUpFunctions () {
return this.provider.isExistingFunctionApp()
.then(() => this.provider.getDeployedFunctionsNames())
.then(() => this.provider.cleanUpFunctionsBeforeDeploy(this.serverless.service.getAllFunctions()));
}
};
14 changes: 14 additions & 0 deletions deploy/lib/createFunction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

const utils = require('../../shared/utils');

module.exports = {
createFunction () {
const functionName = this.options.function;
const metaData = utils.getFunctionMetaData(functionName, this.provider.getParsedBindings(), this.serverless);

return this.provider.createZipObject(functionName, metaData.entryPoint, metaData.handlerPath, metaData.params)
.then(() => this.provider.createAndUploadZipFunctions())
.then(() => this.provider.syncTriggers());
}
};
20 changes: 20 additions & 0 deletions deploy/lib/createFunctions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const BbPromise = require('bluebird');
const utils = require('../../shared/utils');

module.exports = {
createFunctions () {
const createFunctionPromises = [];

this.serverless.service.getAllFunctions().forEach((functionName) => {
const metaData = utils.getFunctionMetaData(functionName, this.provider.getParsedBindings(), this.serverless);

createFunctionPromises.push(this.provider.createZipObject(functionName, metaData.entryPoint, metaData.handlerPath, metaData.params));
});

return BbPromise.all(createFunctionPromises)
.then(() => this.provider.createAndUploadZipFunctions())
.then(() => this.provider.syncTriggers());
}
};
31 changes: 31 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

/*
NOTE: this plugin is used to add all the differnet provider related plugins at once.
This way only one plugin needs to be added to the service in order to get access to the
whole provider implementation.
*/

const AzureDeploy = require('./deploy/azureDeploy');
const AzureDeployFunction = require('./deploy/azureDeployFunction');
const AzureProvider = require('./provider/azureProvider');
const AzureInvoke = require('./invoke/azureInvoke');
const AzureLogs = require('./logs/azureLogs');
const AzureRemove = require('./remove/azureRemove');


class AzureIndex {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;

this.serverless.pluginManager.addPlugin(AzureProvider);
this.serverless.pluginManager.addPlugin(AzureDeploy);
this.serverless.pluginManager.addPlugin(AzureDeployFunction);
this.serverless.pluginManager.addPlugin(AzureInvoke);
this.serverless.pluginManager.addPlugin(AzureLogs);
this.serverless.pluginManager.addPlugin(AzureRemove);
}
}

module.exports = AzureIndex;
45 changes: 45 additions & 0 deletions invoke/azureInvoke.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

const BbPromise = require('bluebird');
const invokeFunction = require('./lib/invokeFunction');
const getAdminKey = require('../shared/getAdminKey');
const loginToAzure = require('../shared/loginToAzure');
const path = require('path');

class AzureInvoke {
constructor (serverless, options) {
this.serverless = serverless;
this.options = options;
this.provider = this.serverless.getProvider('azure');

Object.assign(
this,
loginToAzure,
getAdminKey,
invokeFunction
);

if (this.options.path) {
const absolutePath = path.isAbsolute(this.options.path)
? this.options.path
: path.join(this.serverless.config.servicePath, this.options.path);

if (!this.serverless.utils.fileExistsSync(absolutePath)) {
throw new this.serverless.classes.Error('The file you provided does not exist.');
}
this.options.data = this.serverless.utils.readFileSync(absolutePath);
}

this.hooks = {

'before:invoke:invoke': () => BbPromise.bind(this)
.then(this.loginToAzure)
.then(this.getAdminKey),

'invoke:invoke': () => BbPromise.bind(this)
.then(this.invokeFunction)
};
}
}

module.exports = AzureInvoke;
14 changes: 14 additions & 0 deletions invoke/lib/invokeFunction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

module.exports = {
invokeFunction () {
const func = this.options.function;
const functionObject = this.serverless.service.getFunction(func);
const eventType = Object.keys(functionObject.events[0])[0];

return this.provider.invoke(func, eventType, this.options.data);
// TODO: Github issue: https://github.com/Azure/azure-webjobs-sdk-script/issues/1122
// .then(() => this.provider.getInvocationId(func))
// .then(() => this.provider.getLogsForInvocationId());
}
};
29 changes: 29 additions & 0 deletions logs/azureLogs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const BbPromise = require('bluebird');
const retrieveLogs = require('./lib/retrieveLogs');
const loginToAzure = require('../shared/loginToAzure');

class AzureLogs {
constructor (serverless, options) {
this.serverless = serverless;
this.options = options;
this.provider = this.serverless.getProvider('azure');

Object.assign(
this,
loginToAzure,
retrieveLogs
);

this.hooks = {
'before:logs:logs': () => BbPromise.bind(this)
.then(this.loginToAzure),

'logs:logs': () => BbPromise.bind(this)
.then(this.retrieveLogs)
};
}
}

module.exports = AzureLogs;
11 changes: 11 additions & 0 deletions logs/lib/retrieveLogs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

module.exports = {
retrieveLogs () {
const functionName = this.options.function;

return this.provider.getAdminKey()
.then(() => this.provider.pingHostStatus(functionName))
.then(() => this.provider.getLogsStream(functionName));
}
};
Loading

0 comments on commit 629ade7

Please sign in to comment.