rwsp

A Python package to interact with ServerPilot API.


Keywords
python, python-2, python-3, python2, python3, serverpilot, serverpilot-api
License
MIT
Install
pip install rwsp==1.0.1

Documentation

Python API Wrapper for ServerPilot API

ServerPilot is an awesome tool to manage Ubuntu servers in order to run PHP (and other) web apps flawlessly. This is a Python package to interact with ServerPilot API.

Installation

Install from PyPi:

pip install rwsp

or clone this repo and run:

python setup.py install

Usage

from serverpilot.serverpilot import ServerPilot
# Get API Creds at https://manage.serverpilot.io/account/api
clientId = '###############'
apiKey = '##############'
sp = ServerPilot(clientId, apiKey)

Servers

List Servers

sp.list_servers()

Get a Server

serverId = 'ID of the server'
sp.get_server(serverId)

Create a Server

You will get server ID in response that you can use manually to configure your server

data = {
    'name': 'mynewserver',
    'plan': 'business',
    'enable_ssh_password_auth': True
}
sp.create_server(data)

Update a Server

data = {
    'plan': 'business',
    'firewall': True,
    'autoupdates': True,
    'deny_unknown_domains': True
}
serverId = 'ID of the server'
sp.update_server(serverId, data)

Delete a Server

This action will detach your server from your ServerPilot account.

serverId = 'ID of the server'
sp.delete_server(serverId)

SSH Keys

List SSH Keys

sp.list_sshkeys()

Add an SSH Key

name = 'my_ssh_key'
public_key = 'public_key_data'
sp.add_sshkey(name, public_key)

Get an SSH Key

ssh_key_id = 'your-ssh-key-id'
sp.get_sshkey(ssh_key_id)

Rename an SSH Key

ssh_key_id = 'your-ssh-key-id'
new_name = 'my_updated_ssh_key'
sp.rename_sshkey(ssh_key_id, new_name)

Delete an SSH Key

ssh_key_id = 'your-ssh-key-id'
sp.delete_sshkey(ssh_key_id)

Assign SSH Key

This action assigns an SSH key to a system user.

sys_user_id = 'system-user-id'
ssh_key_id = 'your-ssh-key-id'
sp.assign_sshkey(ssh_key_id, sys_user_id)

Detach SSH Key

This action will detach an SSH key from a system user.

sys_user_id = 'system-user-id'
ssh_key_id = 'your-ssh-key-id'
sp.detach_sshkey(ssh_key_id, sys_user_id)

List User SSH Keys

sys_user_id = 'system-user-id'
sp.list_userkeys(sys_user_id)

Users

List Users

sp.list_users()

Create a User

data = {
    'name': 'mynewsshuser',
    'password': 'mysecurepassword'
}
serverId = 'ID of the server'
sp.create_user(serverId, data)

Get a User

userId = 'ID of the SSH user'
sp.get_user(userId)

Update a User

You can update a user password.

data = {
    'password': 'mynewsecurepassword'
}
userId = 'ID of the SSH user'
sp.update_user(userId, data)

Delete a User

userId = 'ID of the SSH user'
sp.delete_user(userId)

Apps

List Apps

sp.list_apps()

Create a New App

data = {
    'sysuserid': '####', # ID of the owner (SSH User)
    'name': 'myawesomeapp',
    'runtime': 'php7.2',
    'domains': ['mysite.com', 'www.mysite.com'],
    # Provide wordpress dictionary only if you need WP to be installed in your new app
    'wordpress': {
        'site_title': 'My WP Site',
        'admin_user': 'admin',
        'admin_password': 'mysecurepassword', # Min: 8, Max: 200
        'admin_email': 'admin@example.com'
    }
}
sp.create_app(data)

Get App Details

appId = 'ID of the app'
sp.get_app(appId)

Update App Details

data = {
    'runtime': 'php7.3',
    'domains': ['mysite.com', 'www.mysite.com', 'newsite.com', 'www.newsite.com']
}
appId = 'ID of the app'
sp.update_app(appId, data)

Enable / Install SSL

To enable Auto-SSL, don't pass the data object, just pass appId

appId = 'ID of the app'
sp.enable_ssl(appId) # Throws an exception if SSL is already enabled

If you want to install a custom SSL, pass data with SSL assets

appId = 'ID of the app'
data = {
    'key': 'privateKey',
    'cert': 'SSLCertContent',
    'cacerts': 'CACertsIfApplicable' # Optional
}
sp.enable_ssl(appId, data) # Throws an exception if already installed

Force SSL

appId = 'ID of the app'
sp.force_ssl(appId) # Throws an exception if already forced

Disable SSL

This will uninstall / disable SSL (regardless of it is auto-ssl or custom)

appId = 'ID of the app'
sp.disable_ssl(appId) # Throws an exception if already disabled

Delete an App

appId = 'ID of the app'
sp.delete_app(appId)

Databases

List Databases

sp.list_databases()

Create a Database

data = {
    'appid': 'appId', # ID of the app for which you are creating the database
    'name': 'databasename',
    'user': {
        'name': 'dbusername',
        'password': 'dbpassword'
    }
}
sp.create_database(data)

Get a Database

databaseId = 'ID of the database'
sp.get_database(databaseId)

Update a Database

You can only update the user password for a database.

data = {
    'user': {
        'password': 'newpassword'
    }
}
databaseId = 'ID of the database'
sp.update_database(databaseId, data)

Delete a Database

databaseId = 'ID of the database'
sp.delete_database(databaseId)

Actions

Each API call returns an action ID and you can use that ID to check the status of the concerned API call.

actionId = 'ID of the action'
sp.check_action(actionId)

Troubleshooting

Some of the most common errors you may encounter during utilizing this library are explained below:

  • Exception: 401 Error: Unauthorized is thrown when API credentials are incorrect
  • Exception: 404 Error: Not Found is thrown when you attempt to access a non-existent resource
  • Exception: 400 Error: Bad Request is thrown generally when form parameters are invalid

This package covers all API operations supported by ServerPilot. If you have any confusions on required parameters or something else, you should check the API documentation of ServerPilot.