|
1 | 1 | --- |
2 | 2 | title: Extension scripts |
3 | | -description: Go out-of-scope, without destroying compatibility with other extensions |
| 3 | +description: Make out-of-scope modifications to the Pterodactyl panel |
4 | 4 | category: concepts |
5 | 5 | --- |
6 | 6 |
|
7 | | -... |
| 7 | +## Introduction |
| 8 | + |
| 9 | +Sometimes your extension may require to hook into or create a file Blueprint doesn't give you access to through extension APIs. In that case, you have to patch files yourself when your extension is installed, removed and updated. You can do so through scripts. |
| 10 | + |
| 11 | +## Writing extension scripts |
| 12 | + |
| 13 | +Extension scripts live in the root path of the `data.directory` [conf.yml bind](/docs/configs/confyml#datadirectory). |
| 14 | + |
| 15 | +The extension install script is called `install.sh`, update script `update.sh`, remove script `remove.sh` and export script `export.sh`. |
| 16 | + |
| 17 | +## Environment variables |
| 18 | + |
| 19 | +Blueprint exposes environment variables to extension scripts. Depending on the script type, the variables in this list can be used. |
| 20 | + |
| 21 | +| Variable | Description | |
| 22 | +| ------------------------ | -------------------------------------------------------------------------------------------------------------------------- | |
| 23 | +| `$ENGINE` | Codename of the engine handling the extension and matches the `{engine}` [placeholder](/docs/concepts/placeholders#engine) | |
| 24 | +| `$EXTENSION_IDENTIFIER` | Extension identifier (`info.identifier` in [conf.yml](/docs/configs/confyml#infoidentifier-required)) | |
| 25 | +| `$EXTENSION_VERSION` | Extension version (`info.version` in [conf.yml](/docs/configs/confyml#infoversion-required)) | |
| 26 | +| `$EXTENSION_TARGET` | Extension target version (`info.target` in [conf.yml](/docs/configs/confyml#infotarget-required)) | |
| 27 | +| `$PTERODACTYL_DIRECTORY` | Pterodactyl webserver directory | |
| 28 | +| `$BLUEPRINT_VERSION` | Blueprint version installed to the panel | |
| 29 | + |
| 30 | +### Limited availability |
| 31 | + |
| 32 | +Select environment variables are limited to select script types. |
| 33 | + |
| 34 | +| Variable | Description | Availability | |
| 35 | +| ---------------------- | ------------------------------------------------------------------------------------------ | ---------------------------- | |
| 36 | +| `$BLUEPRINT_DEVELOPER` | `true` if extension is being installed through developer commands, otherwise `false` | `install.sh` and `update.sh` | |
| 37 | +| `$BLUEPRINT_TMP` | Path to where the extracted extension files are currently located and handled by Blueprint | `update.sh` and `export.sh` | |
| 38 | + |
| 39 | +## Important considerations |
| 40 | + |
| 41 | +What differenciates Blueprint extensions from standalone modifications the most is the focus on compatibility. You as a developer are expected to build scripts with compatibility as a focus, extensively test your scripts and take conflicts seriously. These restrictions apply to scripts themselves and any (if applicable) other files being ran by the extension. |
| 42 | + |
| 43 | +### Script timing |
| 44 | + |
| 45 | +Especially during extension installation, it's important to know _when_ extension scripts get ran. Below is a quick rundown of script timing and potential conditions. |
| 46 | + |
| 47 | +| Script type | Runs when? | |
| 48 | +| ------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 49 | +| `install.sh` | During the final part of extension installation, before rebuilding dashboard frontend assets (if applicable) | |
| 50 | +| `update.sh` | During the initial part of extension installation, where Blueprint determines if the extension is already installed, and if so, runs the extension's update script (if available) | |
| 51 | +| `remove.sh` | During the initial part of extension removal, where Blueprint is yet to undo any changes made by the extension | |
| 52 | +| `export.sh` | During the final part of extension exporting, right before packaging the extension to a `{identifier}.blueprint` file | |
| 53 | + |
| 54 | +It is important to note that Blueprint does not run the `update.sh` script from an extension's updated `{identifier}.blueprint` file. Instead, Blueprint runs the `update.sh` file of the pre-updated extension. |
| 55 | + |
| 56 | +### Editing files |
| 57 | + |
| 58 | +**Scripts shouldn't overwrite/replace existing files**, instead, they should replace/append strings existent in out-of-scope files. |
| 59 | + |
| 60 | +#### What you should do |
| 61 | + |
| 62 | +You should use tools such as `sed` to search and replace strings to edit files. This tactic should be used for both adding modifications and removing them. |
| 63 | + |
| 64 | +Here is an example of how `sed` can be used for editing files. For more information, check out "[sed, a stream editor](https://www.gnu.org/software/sed/manual/sed.html)". |
| 65 | + |
| 66 | +```bash [install.sh] |
| 67 | +# Replaces foo with bar in the foo.txt file |
| 68 | +sed -i "s/foo/bar/g" $PTERODACTYL_DIRECTORY/foo.txt |
| 69 | +``` |
| 70 | + |
| 71 | +Then upon extension removal, we undo the changes. |
| 72 | + |
| 73 | +```bash [remove.sh] |
| 74 | +# Replaces foo back to bar in the foo.txt file |
| 75 | +sed -i "s/bar/foo/g" $PTERODACTYL_DIRECTORY/foo.txt |
| 76 | +``` |
| 77 | + |
| 78 | +#### What you shouldn't do |
| 79 | + |
| 80 | +Replacing files, overwriting files, moving files around, replacing complete file content and adding file content based on writing to a specific "file position" are all examples of scripts shouldn't do. |
| 81 | + |
| 82 | +::card |
| 83 | +If you are missing specific extension APIs that are preventing you from properly writing an extension, please propose them to us by making a [GitHub issue in BlueprintFramework/framework](https://github.com/BlueprintFramework/framework/issues/new/choose). |
| 84 | +:: |
| 85 | + |
| 86 | +### Safety and privacy concerns |
| 87 | + |
| 88 | +Extensions shouldn't make calls to internet services through scripts and be installable, removable and updatable without an internet connection. |
| 89 | + |
| 90 | +If your extension specifically requires calls to internet services through scripts to function, users should be prompted to "approve" the request beforehand and be shown the hostname. |
| 91 | + |
| 92 | +#### Obfuscation |
| 93 | + |
| 94 | +No. Extension scripts should not obfuscated by any means, for any purpose. |
| 95 | + |
| 96 | +#### Remote scripts |
| 97 | + |
| 98 | +Neither. Extensions scripts should by no means run anything sourced from a remote endpoint, even when given explicit user approval. |
| 99 | + |
| 100 | +### What happens in Pterodactyl, stays in Pterodactyl |
| 101 | + |
| 102 | +Do not create, modify or alter anything outside of the Pterodactyl directory, with the only exception being `/tmp`. |
| 103 | + |
| 104 | +For referencing Pterodactyl's location, you can use the `$PTERODACTYL_DIRECTORY` environment variable or the `{root}` [placeholder](/docs/concepts/placeholders#root). |
| 105 | + |
| 106 | +### Placeholder limitations |
| 107 | + |
| 108 | +Placeholder availability depends on the script. Below is a table showing a rundown of placeholder support per script. |
| 109 | + |
| 110 | +| Script type | Placeholder support | |
| 111 | +| ------------ | ----------------------------------------------------------------------------- | |
| 112 | +| `install.sh` | Full support | |
| 113 | +| `update.sh` | Partial support. Relevant to previous extension install; data may be outdated | |
| 114 | +| `remove.sh` | Full support | |
| 115 | +| `export.sh` | No support | |
| 116 | + |
| 117 | +Placeholders are not available in `export.sh`. They are available in `update.sh`. However, keep in mind that placeholders in that file were applied during the previous extension installation. |
| 118 | + |
| 119 | +### Export scripts |
| 120 | + |
| 121 | +Export scripts are for developers to simplify extension packaging. As long as you prevent yourself from destroying your own operating system, you are generally fine. |
| 122 | + |
| 123 | +That said, if you are about to export an extension that has a custom `export.sh` script, read through it before running it. |
0 commit comments