# Replica Developer Guide

The Replica platform includes developer features enabling you to automatically run code within your environment, create jobs that run automatically on a schedule, and control the flow of data in and out of Replica environments.

## Automatically Executing Code in an Environment

### Enclave Scripts

To automatically execute code in Replica, create an Enclave Script and mount it to an Environment.

{% stepper %}
{% step %}

### Create an Enclave

Create an Enclave to store the files that you would like to execute within your environment.
{% endstep %}

{% step %}

### Create an Enclave Script

Within your Enclave, simply upload a file named `enclave.sh` . Any bash script with this special name will be automatically executed on startup when mounted to an Environment. You can use this script to initiate other processes or install applications.  This file will run as `root` within the context of your environment.

<details open>

<summary>enclave.sh</summary>

```sh
#!/bin/bash
echo "Installing NodeJS"
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install -y nodejs
echo "Enclave Script Complete"
```

</details>
{% endstep %}

{% step %}

### Mount the Enclave to an Environment

You can now use this enclave to automatically run code in any environment you choose. You can create a new environment normally to run the script on demand. You can also choose the enclave when creating a job if you would like to run the script on a schedule.

{% hint style="info" %}
To debug your Enclave script, you can view the execution log within the environment at `/home/user/Desktop/.enclave.log`
{% endhint %}
{% endstep %}
{% endstepper %}

## Example: Scraping a Webpage

Web scraping is a common developer use case in Replica.  Here's a short example of how you can scrape a webpage, using an Enclave Script to initiate a Python script leveraging Selenium.

{% hint style="info" %}
These scripts presume Selenium is preinstalled in your Environment, and the name of your enclave is `my-enclave`
{% endhint %}

<details open>

<summary>enclave.sh</summary>

```sh
#!/bin/bash

# create output directory for scrape results
su user -c "mkdir /home/user/Desktop/scrape-results"
# run the web scraper
su user -c "python3 /home/user/Desktop/enclaves/my-enclave/scrape.py"
```

</details>

<details open>

<summary>scrape.py</summary>

```python
import os
import logging
from selenium import webdriver
from datetime import datetime

logging.basicConfig(encoding='utf-8', level=logging.INFO, format='%(levelname)s: %(message)s')

def scrape_page(url, dir_path='/home/user/Desktop/scrape-results'):
    file_name = datetime.now().strftime('%Y%m%d-%H-%M-%S')
    driver = webdriver.Chrome()
    driver.get(url)

    logging.info("Got page title %s from %s", driver.title, url)
    new_screenshot = os.path.join(dir_path, file_name + '.png') 
    driver.save_screenshot(new_screenshot)
    logging.info("Page screenshot saved to %s", new_screenshot)

    pageSource = driver.page_source
    new_source = os.path.join(dir_path, file_name  + '.html') 
    with open(new_source, 'w', encoding="utf-8") as f:
        f.write(pageSource)
    logging.info("Page source saved to %s", new_screenshot)
    

scrape_page('URL_TO_SCRAPE_HERE')

# You may want to upload your scrape results to another system,
# or back to a different Enclave. See Butler API examples for more info.
```

</details>

## Scheduling a Job

Replica Jobs enable you to schedule code to run on a recurring basis.&#x20;

All Replica Jobs are based on [Enclave Scripts](#enclave-scripts). Once you have defined an Enclave Script, you can schedule it to run as a Job [through the UI](/user-guide/butler-user-guide.md#create-job) or through the [Butler API](/developer-guide/butler-api-reference/jobs.md).

## File/Data Transfer

Data flow within Replica is controlled by the Butler subsystem.  See [Butler API Guide](/developer-guide/butler-api-guide.md).  Butler APIs can be used to transfer files and other data in and out of Replica.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.replicacyber.com/developer-guide/readme.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
