├── .gitignore ├── CHANGELOG.md ├── LICENSE.md ├── README.md └── lib ├── serverpilot.sh └── serverpilot_config /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## v1.0.3 (4/17/2018) 2 | * Fixed SSL update bug [PR #5](https://github.com/kodie/serverpilot-shell/pull/5) 3 | 4 | ## v1.0.2 (2/16/2017) 5 | * Fixed [Issue #2](https://github.com/kodie/serverpilot-shell/issues/2) 6 | * Fixed [Issue #3](https://github.com/kodie/serverpilot-shell/issues/3) 7 | * Fixed a bug that wouldn't allow you to confirm delete functions 8 | * Added version output when running `serverpilot` with no additional commands 9 | * Added ChangeLog to repo 10 | * Script now also checks your home directory for `serverpilot_config` 11 | 12 | ## v1.0.1 (11/9/2016) 13 | * Fixed [Issue #1](https://github.com/kodie/serverpilot-shell/issues/1) 14 | 15 | ## v1.0 (9/7/2016) 16 | * Initial Release 17 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Kodie Grantham 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ServerPilot API Shell Wrapper 2 | ================ 3 | 4 | Manage your [ServerPilot](https://serverpilot.io) servers, system users, apps, and databases with command line! 5 | 6 | ## Requirements 7 | * [Curl](https://github.com/curl/curl) 8 | * [jq](https://github.com/stedolan/jq) (version 1.5 or later) 9 | 10 | ## Authentication 11 | Set the following variables to your credentials. If these variables are not set when the script is loaded, it will look for them in the "serverpilot_config" file which can either be located in the same directory as the script or in your home directory. 12 | ``` 13 | export serverpilot_client_id="CLIENT ID" 14 | export serverpilot_api_key="API KEY" 15 | ``` 16 | 17 | ## Installation 18 | Replace `CLIENT ID` and `API KEY`, then run the following two commands: 19 | 20 | ``` 21 | $ curl -sSL https://raw.githubusercontent.com/kodie/serverpilot-shell/master/lib/serverpilot.sh > /usr/local/bin/serverpilot && chmod a+x /usr/local/bin/serverpilot 22 | $ a="CLIENT ID"; b="API KEY"; printf '\nexport serverpilot_client_id="'$a'"\nexport serverpilot_api_key="'$b'"' >> ~/.bash_profile && source ~/.bash_profile 23 | ``` 24 | 25 | The first command will download `serverpilot.sh` to `/usr/local/bin/serverpilot` and make it executable. 26 | 27 | The second command will set the `serverpilot_client_id` and `serverpilot_api_key` variables in your `~/.bash_profile` file. 28 | 29 | To test for a successful installation, just run `serverpilot servers`. 30 | 31 | To update to the latest version at any time, just run the first command again. 32 | 33 | ## Options 34 | Options can be used with commands to do different things. 35 | 36 | | Option | Name | Description 37 | | :------: | :------: | :--------------------------------------- 38 | | `-f` | Force | Skips "Are you sure?" prompts. Used on all delete functions. | 39 | | `-r` | Raw | Returns raw JSON response instead of user friendly text. Used on all functions that return a response. | 40 | | `-s` | Silent | Returns nothing. Used on all functions that return a response. Takes priorty over "raw" option and enables "force" option. | 41 | | `-w` | Wait | Waits for action to complete before finishing. Used on all functions that return an action id. | 42 | 43 | This would delete the system user without prompt and would wait for the action to complete before finishing: 44 | ``` 45 | $ serverpilot -f -w sysusers delete PPkfc1NECzvwiEBI 46 | ``` 47 | This would return a JSON object containing all server info: 48 | ``` 49 | $ serverpilot -r servers 50 | ``` 51 | 52 | # Find 53 | An added feature of this API wrapper is the `find` function. 54 | 55 | | Name | Type | Description 56 | | ------ | :------: | :--------------------------------------- 57 | | `find` | `string` | A comma separated list of fields and values to search for. i.e: `name=www2` or `id=UXOSIYrdtL4cSGp3,firewall=true` 58 | | `fields` | `string` | A comma separated list of fields to return. i.e: `id,name` 59 | 60 | This would list all apps on the "www2" server: 61 | ``` 62 | $ serverpilot find apps serverid=$(serverpilot find servers name=www2 id) 63 | ``` 64 | 65 | # Resources 66 | **Servers** 67 | * [List All Servers](#list-all-servers) 68 | * [Connect a New Server](#connect-a-new-server) 69 | * [Retrieve an Existing Server](#retrieve-an-existing-server) 70 | * [Delete a Server](#delete-a-server) 71 | * [Update a Server](#update-a-server) 72 | 73 | **System Users** 74 | * [List All System Users](#list-all-system-users) 75 | * [Create a System User](#create-a-system-user) 76 | * [Retrieve an Existing System User](#retrieve-an-existing-system-user) 77 | * [Delete a System User](#delete-a-system-user) 78 | * [Update a System User](#update-a-system-user) 79 | 80 | **Apps** 81 | * [List All Apps](#list-all-apps) 82 | * [Create an App](#create-an-app) 83 | * [Get Details of an App](#get-details-of-an-app) 84 | * [Delete an App](#delete-an-app) 85 | * [Update an App](#update-an-app) 86 | * [Add a Custom SSL Cert](#add-a-custom-ssl-cert) 87 | * [Enable AutoSSL](#enable-autossl) 88 | * [Delete a Custom SSL Cert or Disable AutoSSL](#delete-a-custom-ssl-cert-or-disable-autossl) 89 | * [Enable or Disable ForceSSL](#enable-or-disable-forcessl) 90 | 91 | **Databases** 92 | * [List All Databases](#list-all-databases) 93 | * [Create a Database](#create-a-database) 94 | * [Retrieve an Existing Database](#retrieve-an-existing-database) 95 | * [Delete a Database](#delete-a-database) 96 | * [Update a Database User Password](#update-a-database-user-password) 97 | 98 | **Actions** 99 | * [Check the Status of an Action](#check-the-status-of-an-action) 100 | 101 | ## Servers 102 | ### List All Servers 103 | 104 | ``` 105 | $ serverpilot servers 106 | ``` 107 | 108 | ### Connect a New Server 109 | | Name | Type | Description 110 | | ------ | :------: | :--------------------------------------- 111 | | `name` | `string` | **Required**. The nickname of the Server. Length must be between 1 and 255 characters. Characters can be of lowercase ascii letters, digits, a period, or a dash ('abcdefghijklmnopqrstuvwxyz0123456789-'), but must start with a lowercase ascii letter and end with either a lowercase ascii letter or digit. `www.store2` is a valid name, while `.org.company` nor `www.blog-` are. 112 | ``` 113 | $ serverpilot servers create www2 114 | ``` 115 | 116 | ### Retrieve an Existing Server 117 | ``` 118 | $ serverpilot servers UXOSIYrdtL4cSGp3 119 | ``` 120 | 121 | ### Delete a Server 122 | ``` 123 | $ serverpilot servers delete 4zGDDO2xg30yEeum 124 | ``` 125 | 126 | ### Update a Server 127 | | Name | Type | Description 128 | | ------------- | :------: | :--------------------------------------- 129 | | `firewall` | `bool` | Describes the "enabled" state of the Server firewall. `false` means the firewall is not enabled. 130 | | `autoupdates` | `bool` | Describes the "enabled" state of automatic system updates. `false` means automatic system updates are not enabled. 131 | ``` 132 | $ serverpilot servers update UXOSIYrdtL4cSGp3 firewall false 133 | ``` 134 | 135 | ## System Users 136 | ### List All System Users 137 | ``` 138 | $ serverpilot sysusers 139 | ``` 140 | 141 | ### Create a System User 142 | | Name | Type | Description 143 | | ---------- | :------: | :--------------------------------------- 144 | | `serverid` | `string` | **Required**. The id of the Server. 145 | | `name` | `string` | **Required**. The name of the System User. Length must be between 3 and 32 characters. Characters can be of lowercase ascii letters, digits, or a dash ('abcdefghijklmnopqrstuvwxyz0123456789-'), but must start with a lowercase ascii letter. `user-32` is a valid name, while `3po` is not. 146 | | `password` | `string` | The password of the System User. If user has no password, they will not be able to log in with a password. No leading or trailing whitespace is allowed and the password must be at least 8 and no more than 200 characters long. 147 | ``` 148 | $ serverpilot sysusers create FqHWrrcUfRI18F0l derek hlZkUk 149 | ``` 150 | 151 | ### Retrieve an Existing System User 152 | ``` 153 | $ serverpilot sysusers PPkfc1NECzvwiEBI 154 | ``` 155 | 156 | ### Delete a System User 157 | **Warning**: Deleting a System User will delete all Apps (and Databases) 158 | associated. 159 | ``` 160 | $ serverpilot sysusers delete PPkfc1NECzvwiEBI 161 | ``` 162 | 163 | ### Update a System User 164 | | Name | Type | Description 165 | | ---------- | :------: | :---------- 166 | | `password` | `string` | **Required**. The new password of the System User. If user has no password, they will not be able to log in with a password. No leading or trailing whitespace is allowed and the password must be at least 8 and no more than 200 characters long. 167 | ``` 168 | $ serverpilot sysusers update RvnwAIfuENyjUVnl password mRak7S 169 | ``` 170 | 171 | ## Apps 172 | ### List All Apps 173 | ``` 174 | $ serverpilot apps 175 | ``` 176 | 177 | ### Create an App 178 | | Name | Type | Description 179 | | ----------- | :------------: | :--------------------------------------- 180 | | `name` | `string` | **Required**. The nickname of the App. Length must be between 3 and 30 characters. Characters can be of lowercase ascii letters and digits. 181 | | `sysuserid` | `string` | **Required**. The System User that will "own" this App. Since every System User is specific to a Server, this implicitly determines on which Server the App will be created. 182 | | `runtime` | `string` | **Required**. The PHP runtime for an App. Choose from `php5.4`, `php5.5`, `php5.6`, `php7.0`, or `php7.1`. 183 | | `domains` | `array` | An array of domains that will be used in the webserver's configuration. If you set your app's domain name to *example.com*, Nginx and Apache will be configured to listen for both *example.com* and *www.example.com*. **Note**: The complete list of domains must be included in every update to this field. 184 | | `wordpress` | `object` | If present, installs WordPress on the App. Value is a JSON object containing keys `site_title`, `admin_user`, `admin_password`, and `admin_email`, each with values that are strings. The `admin_password` value must be at least 8 and no more than 200 characters long. 185 | 186 | Creating an App without WordPress: 187 | ``` 188 | $ serverpilot apps create gallery RvnwAIfuENyjUVnl php7.0 '["example.com","www.example.com"]' 189 | ``` 190 | 191 | Creating an App with WordPress: 192 | ``` 193 | $ serverpilot apps create wordpress RvnwAIfuENyjUVnl php7.0 '["example.com","www.example.com"]' '{"site_title":"My WordPress Site","admin_user":"admin","admin_password":"mypassword","admin_email":"example@example.com"}' 194 | ``` 195 | 196 | ### Get Details of an App 197 | ``` 198 | $ serverpilot apps nlcN0TwdZAyNEgdp 199 | ``` 200 | 201 | ### Delete an App 202 | ``` 203 | $ serverpilot apps delete B1w7yc1tfUPQLIKS 204 | ``` 205 | 206 | ### Update an App 207 | | Name | Type | Description 208 | | --------- | :------------: | :--------------------------------------- 209 | | `runtime` | `string` | The PHP runtime for an App. Choose from `php5.4`, `php5.5`, `php5.6`, `php7.0`, or `php7.1`. 210 | | `domains` | `array` | An array of domains that will be used in the webserver's configuration. If you set your app's domain name to *example.com*, Nginx and Apache will be configured to listen for both *example.com* and *www.example.com*. **Note**: The complete list of domains must be included in every update to this field. 211 | ``` 212 | $ serverpilot apps update nlcN0TwdZAyNEgdp runtime php5.6 213 | ``` 214 | 215 | ### Add a Custom SSL Cert 216 | | Name | Type | Description 217 | | --------- | :------: | :--------------------------------------- 218 | | `key` | `string` | **Required**. The contents of the private key. 219 | | `cert` | `string` | **Required**. The contents of the certificate. 220 | | `cacerts` | `string` | **Required**. The contents of the CA certificate(s). If none, `null` is acceptable. 221 | ``` 222 | $ serverpilot apps ssl add nlcN0TwdZAyNEgdp \ 223 | '-----BEGIN PRIVATE KEY-----\ 224 | ...the rest of the key file contents here...\ 225 | -----END PRIVATE KEY-----' \ 226 | '-----BEGIN CERTIFICATE-----\ 227 | ...the rest of the key file contents here...\ 228 | -----END PRIVATE KEY-----' \ 229 | null 230 | ``` 231 | 232 | ### Enable AutoSSL 233 | | Name | Type | Description 234 | | --------- | :------------: | :--------------------------------------- 235 | | `auto` | `bool` | Value must be `true`. 236 | ``` 237 | $ serverpilot apps ssl update nlcN0TwdZAyNEgdp auto true 238 | ``` 239 | 240 | ### Delete a Custom SSL Cert or Disable AutoSSL 241 | ``` 242 | $ serverpilot apps ssl delete nlcN0TwdZAyNEgdp 243 | ``` 244 | 245 | ### Enable or Disable ForceSSL 246 | | Name | Type | Description 247 | | --------- | :------------: | :--------------------------------------- 248 | | `force` | `bool` | Whether forced redirection from HTTP to HTTPS is enabled. 249 | ``` 250 | $ serverpilot apps ssl update nlcN0TwdZAyNEgdp force true 251 | ``` 252 | 253 | ## Databases 254 | ### List All Databases 255 | ``` 256 | $ serverpilot dbs 257 | ``` 258 | 259 | ### Create a Database 260 | | Name | Type | Description 261 | | ---------------- | :------: | :--------------------------------------- 262 | | `appid` | `string` | **Required**. The id of the App. 263 | | `name` | `string` | **Required**. The name of the database. Length must be between 3 and 64 characters. Characters can be of lowercase ascii letters, digits, or a dash ('abcdefghijklmnopqrstuvwxyz0123456789-'). 264 | | `user` | `string` | **Required**. The name of the Database User. Length must be at most 16 characters. 265 | | `password` | `string` | **Required**. The password of the Database User. No leading or trailing whitespace is allowed and the password must be at least 8 and no more than 200 characters long. 266 | ``` 267 | $ serverpilot dbs create nlcN0TwdZAyNEgdp gallerydb arturo 8apNPT 268 | ``` 269 | 270 | ### Retrieve an Existing Database 271 | ``` 272 | $ serverpilot dbs 8PV1OIAlAW3jbGmM 273 | ``` 274 | 275 | ### Delete a Database 276 | ``` 277 | $ serverpilot dbs delete 8PV1OIAlAW3jbGmM 278 | ``` 279 | 280 | ### Update the Database User Password 281 | | Name | Type | Description 282 | | ---------------- | :------: | :---------- 283 | | `userid` | `string` | **Required**. The id of the Database User. 284 | | `userpassword` | `string` | **Required**. The *new* password of the Database User. No leading or trailing whitespace is allowed and the password must be at least 8 and no more than 200 characters long. 285 | ``` 286 | $ serverpilot dbs update 8PV1OIAlAW3jbGmM k2HWtU33mpUsfOdA 8aTWa7 287 | ``` 288 | 289 | ## Actions 290 | ### Check the Status of an Action 291 | ``` 292 | $ serverpilot actions g3kiiYzxPgAjbwcY 293 | ``` 294 | 295 | ## License 296 | MIT. See the License file for more info. 297 | -------------------------------------------------------------------------------- /lib/serverpilot.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # ServerPilot API Shell Wrapper 4 | # by Kodie Grantham (http://kodieg.com) 5 | # https://github.com/kodie/serverpilot-shell 6 | 7 | sp_version="1.0.3" 8 | 9 | # Check if api creds have been set. If not, check if they're in the config file. 10 | if [[ ! "$serverpilot_client_id" || ! "$serverpilot_api_key" ]]; then 11 | sp_config_filename="serverpilot_config" 12 | 13 | if [ -e "$sp_config_filename" ]; then 14 | . "$sp_config_filename" 15 | elif [ -e "$HOME/$sp_config_filename" ]; then 16 | . "$HOME/$sp_config_filename" 17 | fi 18 | fi 19 | 20 | # Main API call function (Internal) 21 | # Example: sp_run "sysusers" '{"serverid":'"$serverid"',"name":'"$name"',"password":'"$password"'}' "POST" 22 | function sp_run { 23 | if [ "$2" ]; then local d="-d $2"; fi 24 | if [ "$3" ]; then local X="-X $3"; fi 25 | local response=$(curl -s https://api.serverpilot.io/v1/$1 -u $serverpilot_client_id:$serverpilot_api_key -H "Content-Type: application/json" "$d" $X) 26 | if [ ! "$response" ]; then response='{"error":{"message":"No response from ServerPilot."}}'; fi 27 | echo $response | tr '\n' ' ' 28 | } 29 | 30 | # Checks for raw, silent, and wait options as well as errors (Internal) 31 | # Example: sp_data_check "$response" 32 | function sp_data_check { 33 | if [ "$sp_options_wait" == "true" ]; then 34 | local actionid="$(echo "$1" | jq -r -c ".actionid")" 35 | if [[ "$actionid" && ! "$actionid" == "null" ]]; then 36 | sp_actions_wait "$actionid" 37 | fi 38 | fi 39 | 40 | if [[ "$sp_options_raw" == "true" && ! "$sp_options_silent" == "true" ]]; then 41 | echo $1 42 | exit 0 43 | elif [ ! "${error=$(echo "$1" | jq -r -c ".error.message")}" == "null" ]; then 44 | if [ ! "$sp_options_silent" == "true" ]; then 45 | echo "Error: $error" >&2 46 | fi 47 | exit 1 48 | elif [ "$sp_options_silent" == "true" ]; then 49 | exit 0 50 | fi 51 | } 52 | 53 | # Splits a string by a delimiter (Internal) 54 | # Example: sp_data_split "," $string 55 | function sp_data_split { 56 | IFS="$1" 57 | local arr=($2) 58 | unset IFS 59 | echo ${arr[@]} 60 | } 61 | 62 | # Displays data in a nice table (Internal) 63 | # Example: sp_data_table "${data[@]}" 64 | function sp_data_table { 65 | local keys=($(echo "$1" | jq -r ". | keys_unsorted | .[]")) 66 | COLUMNS=100 67 | printf "%-20s" ${keys[@]} 68 | echo 69 | 70 | local i 71 | for i in "$@"; do 72 | local value=$(echo "$i" | jq -r ".[]") 73 | printf "%-20s" ${value[@]} 74 | echo 75 | done 76 | } 77 | 78 | # Default setup for sp_data_table function (Internal) 79 | # Example: sp_table "$response" "$selector" 80 | function sp_table { 81 | local o=($(echo "$1" | jq -r -c "$2")) 82 | sp_data_table "${o[@]}" 83 | } 84 | 85 | # Checks if required args are set 86 | # Example: sp_args_check 1 "$@" 87 | function sp_args_check { 88 | local a=("${@:2}") 89 | local c="$1" 90 | 91 | if [ "${#a[@]}" -lt "$c" ]; then 92 | echo "Error: Missing required arguments." 93 | exit 1 94 | fi 95 | } 96 | 97 | # Builds the jq string for find function (Internal) 98 | # Example: s=$(sp_find_setup "$@") 99 | function sp_find_setup { 100 | if [ "$1" ]; then 101 | local find=($(sp_data_split "," "$1")) 102 | local s; local i; 103 | for i in "${find[@]}"; do 104 | local f=($(sp_data_split "=" "$i")) 105 | s=$s" | select(.${f[0]}==\"${f[1]}\")" 106 | done 107 | fi 108 | 109 | if [ "$2" ]; then 110 | local out=($(sp_data_split "," "$2")) 111 | local k; local x; 112 | for x in "${out[@]}"; do 113 | if [ "$sp_options_raw" == "true" ]; then 114 | k=$k",$x:.$x" 115 | else 116 | k=$k",.$x" 117 | fi 118 | done 119 | if [ "$sp_options_raw" == "true" ]; then 120 | k=" | {"${k:1}"}" 121 | else 122 | k=" | "${k:1} 123 | fi 124 | fi 125 | 126 | echo $s$k 127 | } 128 | 129 | # Finds a server, system user, app, or database based on "search" field and returns specified fields. 130 | # Example: serverpilot find servers "name=production-1" "id" 131 | # Example: serverpilot find apps 'serverid=$serverid,runtime=php7.0' "id,name" 132 | # Note: Does not obey the "-s (Silent)" option. 133 | function sp_find { 134 | sp_args_check 1 "$@" 135 | local s=$(sp_find_setup "${@:2}") 136 | local response=$(sp_run "$1") 137 | local f=".data[] $s" 138 | 139 | if [ "$sp_options_raw" == "true" ]; then 140 | f="{data:[.data[] $s]}" 141 | fi 142 | 143 | local results=$(echo "$response" | jq -r -c "$f") 144 | 145 | if [[ ! "$3" && ! "$sp_options_raw" == "true" ]]; then 146 | case "$1" in 147 | "apps") sp_apps_table "$results" ".";; 148 | "dbs") sp_dbs_table "$results" ".";; 149 | *) sp_table "$results" ".";; 150 | esac 151 | else 152 | echo ${results[@]} 153 | fi 154 | } 155 | 156 | # Waits for action to be completed before letting the function continue (Internal) 157 | # Example: sp_actions_wait $actionid 158 | function sp_actions_wait { 159 | while true; do 160 | local response=$(sp_run "actions/$1") 161 | local status=$(echo "$response" | jq -r ".data.status") 162 | 163 | if [[ "$status" == "success" || "$status" == "error" ]]; then 164 | return 165 | fi 166 | 167 | sleep 2 168 | done 169 | } 170 | 171 | # Checks the status of an action id 172 | # Example: serverpilot actions $actionid 173 | function sp_actions { 174 | sp_args_check 1 "$@" 175 | local response=$(sp_run "actions/$1") 176 | sp_data_check "$response" 177 | 178 | local status=$(echo "$response" | jq -r -c ".data.status") 179 | case $status in 180 | "success") echo "The action '$1' has completed successfully.";; 181 | "open") echo "The action '$1' has not completed yet.";; 182 | "error") echo "The action '$1' has completed but there were errors.";; 183 | esac 184 | } 185 | 186 | # Gets a list of all servers or details of a specific server if passed a server id 187 | # Example: serverpilot servers 188 | # Example: serverpilot servers $serverid 189 | function sp_servers_get { 190 | if [ "$1" ]; then 191 | local response=$(sp_run "servers/$1") 192 | local selector=".data" 193 | else 194 | local response=$(sp_run "servers") 195 | local selector=".data[]" 196 | fi 197 | 198 | sp_data_check "$response" 199 | sp_table "$response" "$selector" 200 | } 201 | 202 | # Creates a server 203 | # Example: serverpilot server create $servername 204 | # Note: You will need to run the serverpilot-installer yourself. (https://github.com/ServerPilot/API#connect-a-new-server) 205 | function sp_servers_create { 206 | sp_args_check 1 "$@" 207 | local response=$(sp_run "servers" "{\"name\":\"$1\"}") 208 | sp_data_check "$response" 209 | } 210 | 211 | # Updates specified server's info 212 | # Example: serverpilot servers update $serverid firewall true 213 | # Example: serverpilot servers update $serverid autoupdates false 214 | function sp_servers_update { 215 | sp_args_check 3 "$@" 216 | local response=$(sp_run "servers/$1" "{\"$2\":\"$3\"}") 217 | sp_data_check "$response" 218 | } 219 | 220 | # Deletes specified server 221 | # Example: serverpilot servers delete $serverid 222 | function sp_servers_delete { 223 | sp_args_check 1 "$@" 224 | 225 | if [[ ! "$sp_options_force" == "true" && ! "$sp_options_silent" == "true" ]]; then 226 | read -p "You are aboute to delete server '$1'. This cannot be undone! Are you sure? " -n 1 -r && echo 227 | if [[ ! $REPLY =~ ^[Yy]$ ]]; then return; fi 228 | fi 229 | 230 | local response=$(sp_run "servers/$1" "" "DELETE") 231 | sp_data_check "$response" 232 | } 233 | 234 | # Namespace function for servers - Defaults to listing servers 235 | function sp_servers { 236 | case "$1" in 237 | "create") sp_servers_create "${@:2}";; 238 | "update") sp_servers_update "${@:2}";; 239 | "delete") sp_servers_delete "${@:2}";; 240 | *) sp_servers_get "${@:1}";; 241 | esac 242 | } 243 | 244 | # Gets a list of all system users or details of a specific system user if passed a system user id 245 | # Example: serverpilot sysusers 246 | # Example: serverpilot sysusers $sysuserid 247 | function sp_sysusers_get { 248 | if [ "$1" ]; then 249 | local response=$(sp_run "sysusers/$1") 250 | local selector=".data" 251 | else 252 | local response=$(sp_run "sysusers") 253 | local selector=".data[]" 254 | fi 255 | 256 | sp_data_check "$response" 257 | sp_table "$response" "$selector" 258 | } 259 | 260 | # Creates a system user 261 | # Example: serverpilot sysusers create $serverid $name $password 262 | # Note: "password" field is optional. If user has no password, they will not be able to log in with a password. 263 | function sp_sysusers_create { 264 | sp_args_check 2 "$@" 265 | if [ "$3" ]; then local p=",\"password\":$3"; fi 266 | local response=$(sp_run "sysusers" "{\"serverid\":\"$1\",\"name\":\"$2\"$p}") 267 | sp_data_check "$response" 268 | } 269 | 270 | # Updates specified system user's info 271 | # Example: serverpilot sysusers update $sysuserid password $password 272 | function sp_sysusers_update { 273 | sp_args_check 3 "$@" 274 | local response=$(sp_run "sysusers/$1" "{\"$2\":\"$3\"}") 275 | sp_data_check "$response" 276 | } 277 | 278 | # Deletes specified system user 279 | # Example: serverpilot sysusers delete $sysuserid 280 | function sp_sysusers_delete { 281 | sp_args_check 1 "$@" 282 | 283 | if [[ ! "$sp_options_force" == "true" && ! "$sp_options_silent" == "true" ]]; then 284 | read -p "You are aboute to delete system user '$1'. This cannot be undone! Are you sure? " -n 1 -r && echo 285 | if [[ ! $REPLY =~ ^[Yy]$ ]]; then return; fi 286 | fi 287 | 288 | local response=$(sp_run "sysusers/$1" "" "DELETE") 289 | sp_data_check "$response" 290 | } 291 | 292 | # Namespace function for system users - Defaults to listing system users 293 | function sp_sysusers { 294 | case "$1" in 295 | "create") sp_sysusers_create "${@:2}";; 296 | "update") sp_sysusers_update "${@:2}";; 297 | "delete") sp_sysusers_delete "${@:2}";; 298 | *) sp_sysusers_get "${@:1}";; 299 | esac 300 | } 301 | 302 | # Apps setup for sp_data_table function (Internal) 303 | # Example: sp_apps_table "$response" "$selector" 304 | function sp_apps_table { 305 | local o=($(echo "$1" | jq -r -c "$2 | del(.ssl) | del(.autossl) | del(.domains)")) 306 | sp_data_table "${o[@]}" 307 | } 308 | 309 | # Gets a list of all apps or details of a specific app if passed an app id 310 | # Example: serverpilot apps 311 | # Example: serverpilot apps $appid 312 | # Note: "ssl" and "domains" fields are omitted in table view for user readability. 313 | function sp_apps_get { 314 | if [ "$1" ]; then 315 | local response=$(sp_run "apps/$1") 316 | local selector=".data" 317 | else 318 | local response=$(sp_run "apps") 319 | local selector=".data[]" 320 | fi 321 | 322 | sp_data_check "$response" 323 | sp_apps_table "$response" "$selector" 324 | } 325 | 326 | # Creates an app 327 | # Example: serverpilot apps create $name $sysuserid $runtime '["domain.com", "www.domain.com"]' '$wordpressObj' 328 | # Note: "domains" and "wordpress" fields are optional. 329 | function sp_apps_create { 330 | sp_args_check 3 "$@" 331 | if [ "$4" ]; then local d=",\"domains\":$4"; fi 332 | if [ "$5" ]; then local w=",\"wordpress\":$5"; fi 333 | local response=$(sp_run "apps" "{\"name\":\"$1\",\"sysuserid\":\"$2\",\"runtime\":\"$3\"$d$w}") 334 | sp_data_check "$response" 335 | } 336 | 337 | # Updates specified apps's info 338 | # Example: serverpilot apps update $appid runtime $runtime 339 | # Example: serverpilot apps update $appid domains "[$domain1, $domain2]" 340 | function sp_apps_update { 341 | sp_args_check 3 "$@" 342 | local response=$(sp_run "apps/$1" "{\"$2\":\"$3\"}") 343 | sp_data_check "$response" 344 | } 345 | 346 | # Deletes specified app 347 | # Example: serverpilot apps delete $appid 348 | function sp_apps_delete { 349 | sp_args_check 1 "$@" 350 | 351 | if [[ ! "$sp_options_force" == "true" && ! "$sp_options_silent" == "true" ]]; then 352 | read -p "You are aboute to delete app '$1'. This cannot be undone! Are you sure? " -n 1 -r && echo 353 | if [[ ! $REPLY =~ ^[Yy]$ ]]; then return; fi 354 | fi 355 | 356 | local response=$(sp_run "apps/$1" "" "DELETE") 357 | sp_data_check "$response" 358 | } 359 | 360 | # Namespace function for apps - Defaults to listing apps 361 | function sp_apps { 362 | case "$1" in 363 | "create") sp_apps_create "${@:2}";; 364 | "update") sp_apps_update "${@:2}";; 365 | "delete") sp_apps_delete "${@:2}";; 366 | "ssl") sp_apps_ssl "${@:2}";; 367 | *) sp_apps_get "${@:1}";; 368 | esac 369 | } 370 | 371 | # Adds an SSL certificate to an app 372 | # Example: serverpilot apps ssl add $appid $key $cert $cacerts 373 | function sp_apps_ssl_add { 374 | sp_args_check 4 "$@" 375 | local response=$(sp_run "apps/$1/ssl" "{\"key\":\"$2\",\"cert\":\"$3\",\"cacerts\":\"$4\"}") 376 | sp_data_check "$response" 377 | } 378 | 379 | # Updates specified apps's SSL certificate info 380 | # Example: serverpilot apps ssl update $appid auto true 381 | # Example: serverpilot apps ssl update $appid force false 382 | function sp_apps_ssl_update { 383 | sp_args_check 3 "$@" 384 | local response=$(sp_run "apps/$1/ssl" "{\"$2\":$3}") 385 | sp_data_check "$response" 386 | } 387 | 388 | # Deletes custom SSL certificate or disables Auto SSL for specified app 389 | # Example: serverpilot apps ssl delete $appid 390 | function sp_apps_ssl_delete { 391 | sp_args_check 1 "$@" 392 | 393 | if [[ ! "$sp_options_force" == "true" && ! "$sp_options_silent" == "true" ]]; then 394 | read -p "You are aboute to delete the SSL certificate for app '$1'. This cannot be undone! Are you sure? " -n 1 -r && echo 395 | if [[ ! $REPLY =~ ^[Yy]$ ]]; then return; fi 396 | fi 397 | 398 | local response=$(sp_run "apps/$1/ssl" "" "DELETE") 399 | sp_data_check "$response" 400 | } 401 | 402 | # Namespace function for ssl - Defaults to nothing 403 | function sp_apps_ssl { 404 | case "$1" in 405 | "add") sp_apps_ssl_add "${@:2}";; 406 | "update") sp_apps_ssl_update "${@:2}";; 407 | "delete") sp_apps_ssl_delete "${@:2}";; 408 | *) echo "Error: Invalid command." >&2; exit 1;; 409 | esac 410 | } 411 | 412 | # Database setup for sp_data_table function (Internal) 413 | # Example: sp_dbs_table "$response" "$selector" 414 | function sp_dbs_table { 415 | local o=($(echo "$1" | jq -r -c "$2 |= .+ {"userid":.user.id,"username":.user.name} | del($2.user) | $2")) 416 | sp_data_table "${o[@]}" 417 | } 418 | 419 | # Gets a list of all databases or details of a database if passed a database id 420 | # Example: serverpilot dbs 421 | # Example: serverpilot dbs $dbid 422 | # Note: "user.id" and "user.name" fields are moved to "userid" and "username" in table view for user readability. 423 | function sp_dbs_get { 424 | if [ "$1" ]; then 425 | local response=$(sp_run "dbs/$1") 426 | local selector=".data" 427 | else 428 | local response=$(sp_run "dbs") 429 | local selector=".data[]" 430 | fi 431 | 432 | sp_data_check "$response" 433 | sp_dbs_table "$response" "$selector" 434 | } 435 | 436 | # Creates an database 437 | # Example: serverpilot dbs create $appid $dbname $dbuser $dbpass 438 | function sp_dbs_create { 439 | sp_args_check 4 "$@" 440 | local response=$(sp_run "dbs" "{\"appid\":\"$1\",\"name\":\"$2\",\"user\":{\"name\":\"$3\",\"password\":\"$4\"}}") 441 | sp_data_check "$response" 442 | } 443 | 444 | # Updates specified db's password 445 | # Example: serverpilot dbs update $dbid $dbuserid $newdbpass 446 | function sp_dbs_update { 447 | sp_args_check 3 "$@" 448 | local response=$(sp_run "dbs/$1" "{\"user\":{\"id\":\"$2\",\"password\":\"$3\"}}") 449 | sp_data_check "$response" 450 | } 451 | 452 | # Deletes specified database 453 | # Example: serverpilot dbs delete $dbid 454 | function sp_dbs_delete { 455 | sp_args_check 1 "$@" 456 | 457 | if [[ ! "$sp_options_force" == "true" && ! "$sp_options_silent" == "true" ]]; then 458 | read -p "You are aboute to delete database '$1'. This cannot be undone! Are you sure? " -n 1 -r && echo 459 | if [[ ! $REPLY =~ ^[Yy]$ ]]; then return; fi 460 | fi 461 | 462 | local response=$(sp_run "dbs/$1" "" "DELETE") 463 | sp_data_check "$response" 464 | } 465 | 466 | # Namespace function for databases - Defaults to listing databases 467 | function sp_dbs { 468 | case "$1" in 469 | "create") sp_dbs_create "${@:2}";; 470 | "update") sp_dbs_update "${@:2}";; 471 | "delete") sp_dbs_delete "${@:2}";; 472 | *) sp_dbs_get "${@:1}";; 473 | esac 474 | } 475 | 476 | # Namespace function for everything 477 | # Also sets up our options for us: 478 | # -f (Force): Skips "Are you sure?" prompts. Used on all delete functions. 479 | # -r (Raw): Returns raw JSON response instead of user friendly text. Used on all functions that return a response. 480 | # -s (Silent): Returns nothing. Used on all functions that return a response. Takes priorty over "raw" option and enables "force" option. 481 | # -w (Wait): Waits for action to complete before finishing. Used on all functions that return an action id. 482 | function serverpilot { 483 | sp_options_force=false 484 | sp_options_raw=false 485 | sp_options_silent=false 486 | sp_options_wait=false 487 | 488 | local options='frsw' 489 | while getopts $options option 490 | do 491 | case $option in 492 | "f") sp_options_force=true;; 493 | "r") sp_options_raw=true;; 494 | "s") sp_options_silent=true;; 495 | "w") sp_options_wait=true;; 496 | \?) exit 1;; 497 | :) exit 1;; 498 | *) exit 1;; 499 | esac 500 | done 501 | shift $(($OPTIND - 1)) 502 | 503 | case "$1" in 504 | "find") sp_find "${@:2}";; 505 | "actions") sp_actions "${@:2}";; 506 | "servers") sp_servers "${@:2}";; 507 | "sysusers") sp_sysusers "${@:2}";; 508 | "apps") sp_apps "${@:2}";; 509 | "dbs") sp_dbs "${@:2}";; 510 | *) 511 | echo 512 | echo "ServerPilot API Shell Wrapper v$sp_version" 513 | echo "by Kodie Grantham - http://kodieg.com" 514 | echo 515 | echo "GitHub: https://github.com/kodie/serverpilot-shell" 516 | echo 517 | exit 0;; 518 | esac 519 | } 520 | 521 | # Only run if we're not being sourced 522 | if [ "${BASH_SOURCE[0]}" == "${0}" ]; then 523 | serverpilot "$@" 524 | fi 525 | -------------------------------------------------------------------------------- /lib/serverpilot_config: -------------------------------------------------------------------------------- 1 | export serverpilot_client_id="CLIENT ID" 2 | export serverpilot_api_key="API KEY" 3 | --------------------------------------------------------------------------------