github.com/iron-io/functions@v0.0.0-20180820112432-d59d7d1c40b2/fn/README.md (about)

     1  # IronFunctions CLI
     2  
     3  ## Creating Functions
     4  
     5  ### init
     6  
     7  Init will help you create a [function file](../docs/function-file.md) (func.yaml) in the current directory.
     8  
     9  To make things simple, we try to use convention over configuration, so `init` will look for a file named `func.{language-extension}`. For example,
    10  if you are using Node, put the code that you want to execute in the file `func.js`. If you are using Python, use `func.py`. Ruby, use `func.rb`. Go, `func.go`. Etc.
    11  
    12  Run:
    13  
    14  ```sh
    15  fn init <DOCKER_HUB_USERNAME>/<FUNCTION_NAME>
    16  ```
    17  
    18  If you want to override the convention with configuration, you can do that as well using:
    19  
    20  ```sh
    21  fn init [--runtime node] [--entrypoint "node hello.js"] <DOCKER_HUB_USERNAME>/<FUNCTION_NAME>
    22  ```
    23  
    24  Or, if you want full control, just make a Dockerfile. If `init` finds a Dockerfile, it will use that instead of runtime and entrypoint.
    25  
    26  ### Bump, Build, Run, Push
    27  
    28  `fn` provides a few commands you'll use while creating and updating your functions: `bump`, `build`, `run` and `push`.
    29  
    30  Bump will bump the version number in your func.yaml file. Versions must be in [semver](http://semver.org/) format.
    31  
    32  ```sh
    33  fn bump
    34  ```
    35  
    36  Build will build the image for your function, creating a Docker image tagged with the version number from func.yaml.
    37  
    38  ```sh
    39  fn build
    40  ```
    41  
    42  Run will help you test your function. Functions read input from STDIN, so you can pipe the payload into the function like this:
    43  
    44  ```sh
    45  cat `payload.json` | fn run
    46  ```
    47  
    48  Push will push the function image to Docker Hub.
    49  
    50  ```sh
    51  fn push
    52  ```
    53  
    54  ## Using the API
    55  
    56  You can operate IronFunctions from the command line.
    57  
    58  ```sh
    59  $ fn apps list                                  # list apps
    60  myapp
    61  
    62  $ fn apps create otherapp                       # create new app
    63  otherapp created
    64  
    65  $ fn apps inspect otherapp config               # show app-specific configuration
    66  { ... }
    67  
    68  $ fn apps
    69  myapp
    70  otherapp
    71  
    72  $ fn routes list myapp                          # list routes of an app
    73  path	image
    74  /hello	iron/hello
    75  
    76  $ fn routes create otherapp /hello iron/hello   # create route
    77  /hello created with iron/hello
    78  
    79  $ fn routes delete otherapp hello              # delete route
    80  /hello deleted
    81  
    82  $ fn routes headers set otherapp hello header-name value         # add HTTP header to response
    83  otherapp /hello headers updated header-name with value
    84  
    85  $ fn version                                   # shows version both of client and server
    86  Client version: 0.1.0
    87  Server version: 0.1.21
    88  ```
    89  
    90  ## Application level configuration
    91  
    92  When creating an application, you can configure it to tweak its behavior and its
    93  routes' with an appropriate flag, `config`.
    94  
    95  Thus a more complete example of an application creation will look like:
    96  ```sh
    97  fn apps create --config DB_URL=http://example.org/ otherapp
    98  ```
    99  
   100  `--config` is a map of values passed to the route runtime in the form of
   101  environment variables.
   102  
   103  Repeated calls to `fn apps create` will trigger an update of the given
   104  route, thus you will be able to change any of these attributes later in time
   105  if necessary.
   106  
   107  ## Route level configuration
   108  
   109  When creating a route, you can configure it to tweak its behavior, the possible
   110  choices are: `memory`, `type` and `config`.
   111  
   112  Thus a more complete example of route creation will look like:
   113  ```sh
   114  fn routes create --memory 256 --type async --config DB_URL=http://example.org/ otherapp /hello iron/hello
   115  ```
   116  
   117  You can also update existent routes configurations using the command `fn routes update`
   118  
   119  For example:
   120  
   121  ```sh
   122  fn routes update --memory 64 --type sync --image iron/hello
   123  ```
   124  
   125  To know exactly what configurations you can update just use the command
   126  
   127  ```
   128  fn routes update --help
   129  ```
   130  
   131  To understand how each configuration affect your function checkout the [Definitions](/docs/definitions.md#Routes) document.
   132  
   133  ## Changing target host
   134  
   135  `fn` is configured by default to talk http://localhost:8080.
   136  You may reconfigure it to talk to a remote installation by updating a local
   137  environment variable (`$API_URL`):
   138  ```sh
   139  $ export API_URL="http://myfunctions.example.org/"
   140  $ fn ...
   141  ```
   142  
   143  ## Bulk deploy
   144  
   145  Also there is the `deploy` command that is going to scan all local directory for
   146  functions, rebuild them and push them to Docker Hub and update them in
   147  IronFunction. It will use the `route` entry in the existing function file to
   148  see the update in the daemon.
   149  
   150  
   151  ```sh
   152  $ fn deploy APP
   153  ```
   154  
   155  `fn deploy` expects that each directory to contain a file `func.yaml`
   156  which instructs `fn` on how to act with that particular update.
   157  
   158  ## Testing functions
   159  
   160  If you added `tests` to the `func.yaml` file, you can have them tested using
   161  `fn test`.
   162  
   163  ```sh
   164  $ fn test
   165  ```
   166  
   167  During local development cycles, you can easily force a build before test:
   168  ```sh
   169  $ fn test -b
   170  ```
   171  
   172  When preparing to deploy you application, remember adding `path` to `func.yaml`,
   173  it will simplify both the creation of the route, and the execution of remote
   174  tests:
   175  ```yaml
   176  name: me/myapp
   177  version: 1.0.0
   178  path: /myfunc
   179  ```
   180  
   181  Once you application is done and deployed, you can run tests remotely:
   182  ```
   183  # test the function locally first
   184  $ fn test -b
   185  
   186  # push it to Docker Hub and IronFunctions
   187  $ fn push
   188  $ fn routes create myapp
   189  
   190  # test it remotely
   191  $ fn test --remote myapp
   192  ```
   193  
   194  ## Other examples of usage
   195  
   196  ### Creating a new function from source
   197  ```
   198  fn init iron/hello --runtime ruby
   199  fn deploy myapp /hello
   200  ```
   201  
   202  ### Updating function
   203  ```
   204  fn deploy myapp (discover route path if available in func.yaml)
   205  ```
   206  
   207  ### Testing function locally
   208  ```
   209  fn run iron/hello
   210  ```
   211  
   212  ### Testing route
   213  ```
   214  fn call myapp /hello
   215  ```
   216  
   217  ### App management
   218  ```
   219  fn apps create myapp
   220  fn apps update myapp --headers "content-type=application/json"
   221  fn apps config set log_level info
   222  fn apps inspect myapp
   223  fn apps delete myapp
   224  ```
   225  
   226  ### Route management
   227  ```
   228  fn routes create myapp /hello iron/hello
   229  # routes update will also update any changes in the func.yaml file too. 
   230  fn routes update myapp /hello --timeout 30 --type async
   231  fn routes config set myapp /hello log_level info
   232  fn routes inspect myapp /hello
   233  fn routes delete myapp /hello
   234  ```
   235  
   236  ## Contributing
   237  
   238  Ensure you have Go configured and installed in your environment. Once it is
   239  done, run:
   240  
   241  ```sh
   242  $ make
   243  ```
   244  
   245  It will build fn compatible with your local environment. You can test this
   246  CLI, right away with:
   247  
   248  ```sh
   249  $ ./fn
   250  ```