Here's a Python script that reads all of the domains from your WebFaction account and adds them to your Opalstack account if they don't exist already:
#!/bin/env python3
import requests
from xmlrpc.client import ServerProxy
WF_USER = 'your webfaction username'
WF_PASS = 'your webfaction password'
OPAL_TOKEN = 'your opalstack API token'
OPAL_HEADERS = {
'Authorization': f'Token {OPAL_TOKEN}',
'Content-Type':'application/json'
}
wf = ServerProxy('https://api.webfaction.com/')
wf_session, wf_account = wf.login(WF_USER,WF_PASS)
wf_domains = wf.list_domains(wf_session)
opal_domains = requests.get(
'https://my.opalstack.com/api/v0/domain/list/',
headers=OPAL_HEADERS
).json()['domains']
opal_domain_names = [d['name'] for d in opal_domains]
for d in wf_domains:
if d['domain'] not in opal_domain_names:
print(f'adding {d["domain"]}')
payload = [{'name': d['domain']}]
r = requests.post('https://my.opalstack.com/api/v0/domain/add/',
headers=OPAL_HEADERS,
json=payload
)
print(r.json())
for s in d['subdomains']:
subdomain = f'{s}.{d["domain"]}'
if subdomain not in opal_domain_names:
print(f'adding {subdomain}')
payload = [{'name': subdomain}]
r = requests.post('https://my.opalstack.com/api/v0/domain/add/',
headers=OPAL_HEADERS,
json=payload
)
print(r.json())
To use it:
- Get an Opalstack API token from https://my.opalstack.com/tokens/ if you don't have one already.
- Copy the script and save it into a python file like
import_domains.py
on your Opalstack shell user account (or on your own computer if you have Python 3 with the requests
library installed).
- Edit the script to put in your own API token and WebFaction credentials.
- Run it like:
python3 import_domains.py
Easy peasy, give it a try and let me know how it goes 🙂