Giter Site home page Giter Site logo

serverless-scripting-cli's Introduction

Serverless Scripting CLI

StackPath's serverless scripting platform allows you to add custom logic to your applications or even build entire applications on the edge. A single deployment distributes your scripts across StackPath's vast network containing over 45 POPs.

This CLI makes deploying as easy as running a single command, no matter the number of scripts you have. It's easy to use, but allows for a variety of use cases. Run the CLI locally to make for fast deployments, run it in a CI/CD pipeline to automate deployments, or implement it in any other way you can think of.

Version Downloads/week License Build Status

Installation

Run any of the following commands to install the CLI depending on your platform and preferences:

Using NPM

Run npm install -g @stackpath/serverless-scripting-cli to install sp-serverless as a global package.

Using Yarn

Run yarn global add @stackpath/serverless-scripting-cli to install sp-serverless as a global package.

Using Homebrew or Linuxbrew

StackPath maintains a Homebrew tap for users to access utilities through the popular Homebrew and Linuxbrew package managers for macOS and Linux.

Run brew tap stackpath/stackpath to install the tap, then run brew install sp-serverless to install the sp-serverless utility.

Configuration

Perform the following to configure the CLI utility after it's installed:

Setting authentication details

The CLI saves your authentication data in a file in your home directory (~/.stackpath/credentials). Once this is set up, the CLI will continue to read the credentials from the file, meaning you don't have to provide your credentials over and over again.

You need a StackPath API client ID and client secret in order to authenticate. You can find them in the StackPath customer portal.

Authenticating in an interactive environment (e.g. on your local machine)

Run sp-serverless auth to authenticate in an interactive environment. The CLI will prompt you for required details.

Authenticating in a non-interactive environment (e.g. in a CI/CD pipeline)

Are you integrating the CLI with a non-interactive environment? Then provide the client ID and the client secret as flags of the command. For example:

sp-serverless auth --client_id example-client-id --client_secret example-client-secret --force

Use the --force (or -f) flag so that the credentials file is always overwritten, even if it already exists.

Configuring a project

The serverless scripting CLI works with a per-project (or per-directory) based configuration. Each project should have its own configuration file defining the scripts that apply to that project. You might use it to order scripts by site, category (such as firewalls), or otherwise.

Start by creating a sp-serverless.json file in your project's directory.

The configuration file

When deploying via sp-serverless deploy, the CLI looks for the sp-serverless.json configuration file in the directory you're running the command from. Through this file you can configure which scripts you'd like to deploy to which site. For example:

{
  "stack_id": "2dad0e92-61f1-4fb2-bbc0-0c26466e91bf",
  "site_id": "1cb3a9ba-06a4-4528-96a4-c4e04598c856",
  "scripts": [
    {
      "name": "Admin IP firewall",
      "paths": [
        "admin/*"
      ],
      "file": "serverless_scripts/ip-firewall.js",
      // The ID is generated on first deploy, or optionally you can configure it yourself.
      "id": "dcdf7824-b6bd-42b8-9b16-9235eefd583d"
    },
    {
      "name": "Script to show a deploy to a different site",
      "paths": [
        "demo/*"
      ],
      "file": "serverless_scripts/demo.js",
      "site_id": "15ece821-9eed-4590-9577-b83beda947f7"
    },
    {
      "name": "Script to show a deploy to a different site and stack",
      "paths": [
        "demo/*"
      ],
      "file": "serverless_scripts/demo.js",
      "site_id": "15ece821-9eed-4590-9577-b83beda947f7",
      "stack_id": "7be2de57-d6d9-4c27-8361-aef01e1870f0"
    }
  ]
}

Note that you can define the site_id either in a global scope or in a per-script scope. This allows you to deploy to different sites from a single configuration file.

The configuration file contains the following parameters:

  • stack_id: The ID or slug of the stack where your site is in
  • site_id: The ID of the site to apply the scripts to
  • scripts[name]: The name of the script. This should be descriptive and unique to each site
  • scripts[paths][]: The HTTP request paths the script should apply to, relative to the site root and without an initial /. For example, use the path admin/* to apply the script to requests to https://example.org/admin/*
  • scripts[file]: The local path to the JavaScript file to use as the serverless script, relative to sp-serverless.json's path and without an initial /.
  • scripts[id]: The ID of the script in the serverless scripting platform. This is created after your first deployment. It should be checked into version control after being created

Finding the stack slug and site ID

Find the stack slug and site ID in the URL when you're logged into the StackPath customer portal and have selected the CDN site you'd like to deploy scripts to.

Deploying serverless scripts

Run sp-serverless deploy from your project directory. To push your configured scripts to the StackPath edge.

Deploying from a non-interactive environment

The CLI may prompt you in certain situations when deploying scripts. For example, if your sp-serverless.json file contains an ID that cannot be found in the platform, the CLI will then prompt you if you'd like to re-create the script. Use the --force or -f flag to always try to re-create the script.

Usage

sp-serverless auth

Configures StackPath's authentication details.

USAGE
  $ sp-serverless auth

OPTIONS
  -c, --client_id=client_id          StackPath Client ID used to authenticate with
  -f, --force                        Set this to always overwrite current credential file, defaults to false.
  -h, --help                         show CLI help
  -s, --client_secret=client_secret  StackPath Client Secret used to authenticate with
  -v, --verbose                      Turns on verbose logging. Defaults to false

EXAMPLE
  $ sp-serverless auth

sp-serverless deploy

Deploys the scripts in the working directory according to its sp-serverless.json configuration file.

USAGE
  $ sp-serverless deploy

OPTIONS
  -f, --force    Force recreation of scripts if they do not exist. Defaults to false
  -h, --help     show CLI help
  -v, --verbose  Turns on verbose logging. Defaults to false

EXAMPLE
  $ sp-serverless deploy

sp-serverless help

Deploy to StackPath's serverless scripting platform from your local machine.

VERSION
  @stackpath/serverless-scripting-cli/2.0.1 darwin-x64 node-v13.12.0

USAGE
  $ sp-serverless [COMMAND]

COMMANDS
  auth    Configures StackPath's authentication details.
  deploy  Deploys the scripts in the working directory according to its sp-serverless.json configuration file.
  help    display help for sp-serverless

Development

Development requires nodejs versions 10 and up. The CLI doesn't have any build commands. Make code changes and run yarn test to run the test suite or bin/run to test changes directly.

We welcome contributions and pull requests. See our contributing guide for more information.

serverless-scripting-cli's People

Contributors

bgabler avatar dependabot[bot] avatar edskeizer avatar freave-public avatar klaude avatar stackpathtladd2 avatar tladd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

serverless-scripting-cli's Issues

Sharing edgeengine.json across stacks

Looks like we can set the site_id on the global scope OR override it on the per script scope. It would be nice to have the option to do that for the stack_id as well.

Missing site_id in global scope errors

If I have site_id in my script block, I shouldn't really need to set a global site_id as well. But the CLI will error if I don't define one globally:

Missing required property 'site_id' in your edgeengine.json file.

Easy to work around obviously, but seems like it shouldn't be necessary.

invalid ScriptId

The documentation indicates that we can specify our own script id. Maybe this has to be in a GUID format... that's not clear. I thought it would be nice to be able to make it something human readable. So I tried to set "id": "jwt-auth" and got invalid ScriptId

Script doesn't deploy (FetchError: invalid json response body)

Describe the bug

Script doesn't deploy

To reproduce

Install dependencies, add SP token, send a 400kb script on a site.

Expected behavior

Deploying scripts...... Deployment completed.

Actual behavior

yarn run v1.22.10
$ sp-serverless deploy
Refreshing access token...... Refreshing access token done.
Deploying scripts...... ?
An error has occurred (FetchError: invalid json response body at https://gateway.stackpath.com/cdn/v1/stacks/xxx/sites/xxx/scripts/xxx reason: Unexpected end of JSON input). Continue? y/n:

System Details (please complete the following information):

  • CLI Tool Version: 2.1.1 (latest at the date)
  • Operating System: macOS
  • Operating System Version: 11.5.1
  • Shell Information: zsh 5.8

Only deploys first script

When I have more than one script in my edgeengine.json it just deploys whichever one is first. If I flip their order in the edgeengine.json it will deploy the other!

Error when deploying

When I try to deploy a script again it comes up with this...

An error has occurred (a script with this name already exists on this site....

Asks: Continue? y/n:

When I say "y" it gives this error:

Deploying scripts...... An error has occurred. Error: An error occurred when connecting to StackPath host https://gateway.stackpath.com.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.