diff --git a/examples/kubectl.gpt b/examples/kubectl.gpt new file mode 100644 index 00000000..6630f19f --- /dev/null +++ b/examples/kubectl.gpt @@ -0,0 +1,41 @@ +tools: sys.write, kubectl, helm, browser + +Do not make parallel function calls. Only call one function at a time. +Perform the following tasks in order: + +1. Create a Namespace named vote but do not fail if it already exists +2. Deploy in the vote namespace the application which yaml specification is available at https://luc.run/vote.yaml +3. Use a single command to wait for all the Pods in the vote namespace to be ready +4. Install Traefik ingress controller in kube-system namespace with helm only if it is not already installed in this namespace +5. Make sure the Traefik Pod is in running status +6. Wait for the IP address of the traefik Service to be available and save it in the file ./lb-ip.txt +7. Create the file ./ingress.yaml and make sure it contains the yaml specification of an Ingress resource which exposes the vote-ui Service on vote.LBIP.nip.io and the result-ui Service on result.LBIP.nip.io, first making sure to replace the LBIP placeholders with the content of the file ./lb-ip.txt +8. Create the Ingress resource specified in ./ingress.yaml +9. Open a browser on vote.LBIP.nip.io but make sure to replace the LBIP placeholder with the content of lb-ip.txt in this URL first + +--- +name: kubectl +tools: sys.exec +description: use kubectl command to manage k8s resources +args: command: the command kubectl needs to run + +You are a kubernetes operator which can run kubectl commands to manage clusters and applications +The only reason you use sys.exec tool must be to use kubectl to run the command provided, this command must start with kubectl + +--- +name: helm +tools: sys.exec +description: use helm command to manage k8s charts +args: command: the command helm needs to run + +You are a kubernetes operator which can run helm commands to manage charts +The only reason you use sys.exec tool must be to use helm to run the command provided, this command must start with helm + +--- +name: browser +tools: sys.exec +args: url: the url to open +description: open a browser window + +You are only in charge of opening a browser window on the requested url +You can only use the sys.exec tool to open a browser window \ No newline at end of file diff --git a/examples/regular-tasks/README.md b/examples/regular-tasks/README.md new file mode 100644 index 00000000..469b07b3 --- /dev/null +++ b/examples/regular-tasks/README.md @@ -0,0 +1,31 @@ +This example shows how to use GPTScript to run regular tasks. It involves the creation of a VM and defines a crontab entry onto that one. + +This example checks if a specific URL is reachable and sends the status code to an external webhook. If you want to test this example, you need to follow the steps below: + +- Create a DigitalOcean PAT token and export it in the DIGITALOCEAN_ACCESS_TOKEN environment variable + +- Create a new SSH key on your local machine + +``` +ssh-keygen -f /tmp/do_gptscript +``` + +- Define a new SSH key in DigitalOcean using the public part of the SSH key created above and call it `gptscript` + +- Get a token from [https://webhooks.app](https://webhooks.app) + +![webhooks](./picts/webhooks-1.png) + +Note: your token will be different + +- Run gptscript example using this token + +``` +gptscript --cache=false ./regular-task.gpt --url https://fakely.app --token 1e105ea8bef80ca6aba7c8953c34d3 +``` + +- Check the message coming every minute on the [Webhooks dashboard](https://webhooks.app/dashboard) + +![webhooks](./picts/webhooks-2.png) + +- Once you're done, do not forget to remove the DigitalOcean VM created in the process \ No newline at end of file diff --git a/examples/regular-tasks/picts/webhooks-1.png b/examples/regular-tasks/picts/webhooks-1.png new file mode 100644 index 00000000..a7484fb9 Binary files /dev/null and b/examples/regular-tasks/picts/webhooks-1.png differ diff --git a/examples/regular-tasks/picts/webhooks-2.png b/examples/regular-tasks/picts/webhooks-2.png new file mode 100644 index 00000000..f45dbad1 Binary files /dev/null and b/examples/regular-tasks/picts/webhooks-2.png differ diff --git a/examples/regular-tasks/regular-task.gpt b/examples/regular-tasks/regular-task.gpt new file mode 100644 index 00000000..65a1825a --- /dev/null +++ b/examples/regular-tasks/regular-task.gpt @@ -0,0 +1,58 @@ +tools: get-webhook-token, create-regular-task +args: url: URL of the website to check + +First get a new token to authenticate against a webhook + +Next create a task which verifies every minute if the website ${url} is reachable and sends the status code to the following HTTP POST request: +- URL is https://webhooks.app/data +- Authorization bearer is the content of ./token.txt file +- A json payload must be returned, containing a "message" property with the value of the ${url} and the status code returned + +--- +tools: sys.exec +name: get-webhook-token +description: get a token to authenticate against https://webhooks.app + +#!/bin/bash + +curl -s -XGET https://webhooks.app/wh | jq -r .token > ./token.txt + +--- +tools: create-vm, create-crontab-entry +name: create-regular-task +decription: Manage the creation of a crontab on a remove VM +args: command: command to be run in a crontab without the schedule part +args: schedule: schedule to be used in a crontab + +Perform the actions in the following order: + +1. Create a virtual machine on DigitalOcean +2. Create a crontab entry for command ${command} and schedule ${schedule} in that VM + +--- +tools: sys.exec, sys.write +name: create-crontab-entry +description: Create a crontab entry in a remote VM +args: command: command to run in a crontab on the remote VM +args: schedule: schedule for the crontab + +Perform the step in this exact order taking into account that if you need to call a ssh command you must use user root and the IP address which value is in ./vm.ip + +1. Create a bash file containing the ${command} to run without the schedule part, make it executable, and make sure the components are correctly escaped first. +2. Make sure this file contains the value of the token that is inside the ./token.txt file and that it does not only reference this file +3. Send this file to the remote VM via ssh saving it to /tmp/cron.sh on the remove VM +4. Create a crontab entry calling /tmp/cron.sh file for the schedule specified in the ${command} + +--- +tools: sys.exec +name: create-vm +description: create a virtual machine on DigitalOcean + +You are an operator which can use the doctl command line tool to interact with DigitalOcean infrastructure + +Perform the actions in this exact order: + +1. Get the ID of the ssh-key named gptscript and save it in ./key.id +2. Create a Virtual Machine in the new-york datacenter named cron making sure to provide the id from ./key.id as the ssh-key of the new droplet +3. Wait for the VM to be up and running and save its IP address only in ./vm.ip +4. Loop waiting for the SSH connection with user root to be possible to that VM. Do not raise an error in case the SSH connection is not yet possible. \ No newline at end of file