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.
Install from PyPi:
pip install rwsp
or clone this repo and run:
python setup.py install
from serverpilot.serverpilot import ServerPilot
# Get API Creds at https://manage.serverpilot.io/account/api
clientId = '###############'
apiKey = '##############'
sp = ServerPilot(clientId, apiKey)
sp.list_servers()
serverId = 'ID of the server'
sp.get_server(serverId)
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)
data = {
'plan': 'business',
'firewall': True,
'autoupdates': True,
'deny_unknown_domains': True
}
serverId = 'ID of the server'
sp.update_server(serverId, data)
This action will detach your server from your ServerPilot account.
serverId = 'ID of the server'
sp.delete_server(serverId)
sp.list_sshkeys()
name = 'my_ssh_key'
public_key = 'public_key_data'
sp.add_sshkey(name, public_key)
ssh_key_id = 'your-ssh-key-id'
sp.get_sshkey(ssh_key_id)
ssh_key_id = 'your-ssh-key-id'
new_name = 'my_updated_ssh_key'
sp.rename_sshkey(ssh_key_id, new_name)
ssh_key_id = 'your-ssh-key-id'
sp.delete_sshkey(ssh_key_id)
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)
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)
sys_user_id = 'system-user-id'
sp.list_userkeys(sys_user_id)
sp.list_users()
data = {
'name': 'mynewsshuser',
'password': 'mysecurepassword'
}
serverId = 'ID of the server'
sp.create_user(serverId, data)
userId = 'ID of the SSH user'
sp.get_user(userId)
You can update a user password.
data = {
'password': 'mynewsecurepassword'
}
userId = 'ID of the SSH user'
sp.update_user(userId, data)
userId = 'ID of the SSH user'
sp.delete_user(userId)
sp.list_apps()
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)
appId = 'ID of the app'
sp.get_app(appId)
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)
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
appId = 'ID of the app'
sp.force_ssl(appId) # Throws an exception if already forced
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
appId = 'ID of the app'
sp.delete_app(appId)
sp.list_databases()
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)
databaseId = 'ID of the database'
sp.get_database(databaseId)
You can only update the user password for a database.
data = {
'user': {
'password': 'newpassword'
}
}
databaseId = 'ID of the database'
sp.update_database(databaseId, data)
databaseId = 'ID of the database'
sp.delete_database(databaseId)
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)
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 incorrectException: 404 Error: Not Found
is thrown when you attempt to access a non-existent resourceException: 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.