Created
June 29, 2020 14:43
-
-
Save mckelvin/5610392923f3962edeb5c5b0c2db2d45 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# BWG_VE_ID=<TODO> | |
# BWG_API_KEY=<TODO> | |
# */10 * * * * /usr/bin/python3 /usr/local/bin/bwg-migrate.py >> /var/log/bwg-migrate.log 2>&1 | |
import os | |
import random | |
import logging | |
import requests | |
logger = logging.getLogger(__name__) | |
class BandwagonClient: | |
def __init__(self, ve_id, api_key): | |
self._ve_id = ve_id | |
self._api_key = api_key | |
def request(self, sub_uri, params={}): | |
_params = { | |
"veid": self._ve_id, | |
"api_key": self._api_key, | |
} | |
_params.update(params) | |
resp = requests.get( | |
"https://api.64clouds.com/v1/{}".format(sub_uri), | |
_params | |
) | |
return resp.json() | |
def get_migrate_locations(self): | |
return self.request("migrate/getLocations") | |
def migrate_start(self, location="USCA_3"): | |
return self.request("migrate/start", {"location": location}) | |
def main(): | |
logging.basicConfig(level=logging.INFO) | |
bwg_client = BandwagonClient(os.environ["BWG_VE_ID"], os.environ["BWG_API_KEY"]) | |
locations_info = bwg_client.get_migrate_locations() | |
assert locations_info["error"] == 0, locations_info | |
cn2_locations = { | |
loc | |
for loc in locations_info["locations"] | |
if "CN2" in locations_info["descriptions"][loc] | |
} | |
if locations_info["currentLocation"] in cn2_locations: | |
return | |
target_location = random.choice(list(cn2_locations)) | |
logger.info("migrating from %s to %s", locations_info["currentLocation"], target_location) | |
migrate_result = bwg_client.migrate_start(target_location) | |
logger.info(migrate_result) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment