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.
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.
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.
To debug your Enclave script, you can view the execution log within the environment at /home/user/Desktop/.enclave.log
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.
These scripts presume Selenium is preinstalled in your Environment, and the name of your enclave is my-enclave
enclave.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"scrape.py
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.Scheduling a Job
Replica Jobs enable you to schedule code to run on a recurring basis.
All Replica Jobs are based on Enclave Scripts. Once you have defined an Enclave Script, you can schedule it to run as a Job through the UI or through the Butler API.
File/Data Transfer
Data flow within Replica is controlled by the Butler subsystem. See Butler API Guide. Butler APIs can be used to transfer files and other data in and out of Replica.
Last updated
Was this helpful?