# How to run scripts on edge nodes

import DocButton from '~/components/webkit/DocButton.vue';
import Tabs from '~/components/tabs/Tabs'
import Code from '~/components/Code/Code.astro'


To be able to orchestrate services on your device, you must configure all the resources needed to install, uninstall, and reload your services.

This guide presents how to install [Azion CLI](/en/documentation/products/azion-cli/overview/) on your edge nodes through a resource of the type `Shell Script` configured on a specific edge service. 

---

## Requirements

- An edge node with **Orchestrator Agent** installed and authorized.
- The Dpkg package manager installed on this node.

<DocButton href="/en/documentation/products/guides/deploy/install-orchestrator-agent/" label="Go to how to install Orchestrator agent" kind="secondary" target="_blank" size="medium" />
<DocButton href="/en/documentation/products/guides/deploy/authorize-an-edge-node/" label="Go to how to authorize an edge node" kind="secondary" target="_blank" size="medium" />

:::note
You can find the download links to the respective architectures on [Azion CLI's releases page](https://github.com/aziontech/azion/releases). You must choose the correct download link based on the architecture of your edge node and replace the `BINARY_URL` in the script with the correct URL.
:::

--- 

## Creating an edge service 

<Tabs client:visible>
    <Fragment slot="tab.console">Console</Fragment>
    <Fragment slot="tab.api">API</Fragment>

<Fragment slot="panel.console">
1. Access [Azion Console](/en/documentation/products/guides/how-to-access-azion-console/).
2. On the upper-left corner of the page, open the **Products menu**, represented by three horizontal lines, and then select **Edge Services**.
3. Click on the **+ Service** button.
4. Name this service `Azion CLI Installation`.
5. Enter the variables and values for the resource, if necessary.
6. Set the status as **Active**.
7. Click the **Save** button.
</Fragment>

<Fragment slot="panel.api">
1. Run the following `POST` request in your terminal, replacing `[TOKEN VALUE]` with your [personal token](/en/documentation/products/guides/personal-tokens/) and informing the name of the service being created:

```bash
curl --location 'https://api.azionapi.net/edge_services/' \
--header 'Accept: application/json; version=3' \
--header 'Authorization: Token [TOKEN VALUE]' \
--header 'Content-Type: application/json' \
--data '{
  "name": "Azion CLI Installation"
}'
``` 

2. Keep the ID of the edge service you've just created. You can access it in the response body, for example: 

```json 
{
    "id": 1196,
    "name": "Azion CLI Installation",
    "updated_at": "2024-01-18T17:24:33Z",
    "last_editor": "xxxxx",
    "active": false,
    "bound_nodes": 0,
    "permissions": [
        "read",
        "write"
    ]
}
```

:::note 
When running the request in the terminal, the response might not return formatted as presented on the example above.
:::

3. Run the following `PATCH` request in your terminal to set the service as active, replacing `[TOKEN VALUE]` with your [personal token](/en/documentation/products/guides/personal-tokens/):

```bash
curl --location --request PATCH 'https://api.azionapi.net/edge_services/:id' \
--header 'Accept: application/json; version=3' \
--header 'Authorization: Token [TOKEN VALUE]' \
--header 'Content-Type: application/json' \
--data '{
  "active": true,
  "name": "Azion CLI Installation",
  "variables": [
    {
      "name": "string",
      "value": "string"
    }
  ]
}'
``` 

:::note 
If an edge service isn't set as active, it isn't available for binding to an edge node.
:::
</Fragment>

</Tabs>

---

## Creating a resource

<Tabs client:visible>
    <Fragment slot="tab.console">Console</Fragment>
    <Fragment slot="tab.api">API</Fragment>

<Fragment slot="panel.console">
1. Inside the service you've just created, called `Azion CLI Installation`, go to the **Resources** tab.
2. Click on **+ Resource**.
3. Enter `/scripts/install-cli` in the **Path** field.
4. Choose the type **Shell Script**.
5. Choose the trigger **Install**.
6. Add the following content to the **Content** block: 

<Code lang="bash" code={`
#!/bin/bash

# Define the URL of the binary to download
BINARY_URL="https://github.com/aziontech/azion/releases/download/1.10.2/azion_1.10.2_linux_arm64.deb"

# Download the binary
wget $BINARY_URL -O /tmp/azion.deb

# Install the binary
# This assumes that you have dpkg installed on your system
sudo dpkg -i /tmp/azion.deb

# Clean up the downloaded binary
rm /tmp/azion.deb

echo "Installation completed successfully"
`} />

7. Click the **Save** button.

This script will install the Azion binary in the following path: `/usr/local/bin` 
</Fragment>

<Fragment slot="panel.api">
1. Run the following `POST` request in your terminal:

<Code lang="bash" code={`
curl --location 'https://api.azionapi.net/edge_services/:id/resources' \
--header 'Accept: application/json; version=3' \
--header 'Authorization: Token [TOKEN VALUE]' \
--header 'Content-Type: application/json' \
--data '{
  "content_type": "Shell Script",
  "name": "/scripts/install-cli/",
  "content": "#!/bin/bash\n\n# Define the URL of the binary to download\nBINARY_URL=\"https://github.com/aziontech/azion/releases/download/1.10.2/azion_1.10.2_linux_arm64.deb\"\n\n# Download the binary\nwget $BINARY_URL -O /tmp/azion.deb\n\n# Install the binary\n# This assumes that you have dpkg installed on your system\nsudo dpkg -i /tmp/azion.deb\n\n# Clean up the downloaded binary\nrm /tmp/azion.deb\n\necho \"Installation completed successfully\""
}'
`} />

Replace `[TOKEN VALUE]` with your [personal token](/en/documentation/products/guides/personal-tokens/), `:id` with the ID of the edge service and inform the following parameters in the request body:

| Property | Description | Required | 
| - | - | - | 
| `content_type` | Content type of the resource being created | Yes |
| `name` | Name of the resource being created | Yes |
| `content` | Content that defines actions performed when the resource state changes in the edge node | Yes |
</Fragment>

</Tabs>

---

## Binding an edge service to an edge node 

<Tabs client:visible>
    <Fragment slot="tab.console">Console</Fragment>
    <Fragment slot="tab.api">API</Fragment>

<Fragment slot="panel.console">
1. On the upper-left corner of the page, open the **Products menu**, represented by three horizontal lines, and then select **Edge Nodes**.
2. Select the edge node to which you want to bind a service.
3. Go to the **Services** tab and click the **+ Service** button.
4. Choose the service `Azion CLI Installation` you've created.
5. Click the **Save** button.
</Fragment>

<Fragment slot="panel.api">
1. Run the following `GET` request in your terminal, replacing `[TOKEN VALUE]` with your [personal token](/en/documentation/products/guides/personal-tokens/) and retrieve the edge node ID you wish to bind to an edge service:

<Code lang="bash" code={`
curl --location 'https://api.azionapi.net/edge_nodes/' \
--header 'Accept: application/json; version=3' \
--header 'Authorization: Token [TOKEN VALUE]'
`} />

2. Run the following `POST` request in your terminal:

```bash 
curl --location 'https://api.azionapi.net/edge_nodes/:id/services' \
--header 'Accept: application/json; version=3' \
--header 'Authorization: Token [TOKEN VALUE]' \
--header 'Content-Type: application/json' \
--data '{
  "service_id": :id,
  "variables": [
    {
      "name": "string",
      "value": "string"
    }
  ]
}'
```

Replace `[TOKEN VALUE]` with your [personal token](/en/documentation/products/guides/personal-tokens/), `:id` with the ID of the edge node and inform the ID of the service in the request body:

| Property | Description | Required | 
| - | - | - | 
| `service_id` | ID of the edge service being bound to the edge node | Yes |
| `variables` | Variables to be replaced during the processing on the edge node | No (requires checking) |
</Fragment>

</Tabs>

---

## Accessing Azion CLI

After this process, in your edge node, access the folder: 

```bash 
cd /usr/local/bin
``` 

Run: 

```bash 
./azion -h 
```

The output will be similar to: 

```bash 
Azion CLI 1.10.2

DESCRIPTION
  The Azion Command Line Interface is a unified tool to manage your Azion projects and resources

SYNOPSIS
  azion <command> <subcommand> [flags]

EXAMPLES
  $ azion
  $ azion -t azionb43a9554776zeg05b11cb1declkbabcc9la
  $ azion --debug
  $ azion -h
...
```

:::tip
Check the [Azion API documentation](https://api.azion.com/) and the [OpenAPI specification](https://github.com/aziontech/azionapi-openapi/) to know more about all features available via API.
:::