def check_server_for_expiration(id): account = Account(droplet_to_uid[id]) now = int(time.time()) creation_ts = account.creation_ts.get() paid_minutes = account.total_minutes.get() if (now) < (creation_ts + 6 * 60 * 60): # created in within the last 6 hours should be ignored to allow for unconf servers not being destroyed return if now > (creation_ts + paid_minutes * 60): # then destroy logging.warning('Destroying node %s' % id) res = requests.post("https://api.vultr.com/v1/server/destroy?api_key=%s" % vultr_api_key.get(), data={"SUBID": int(id)}) if res.status_code == 200: account.destroy() account.add_msg('Node lifetime consumed in total, node destroyed.') else: logging.error('Could not destroy server! %s' % id) account.add_msg('Attempted to destroy node (unpaid into future) but I failed :(') if account.destroyed.get(): active_servers.remove(id)
def process_next_creation(): if len(nodes_recently_updated) == 0: return next_uid = nodes_recently_updated.popleft() logging.info("Processing uid: %s" % next_uid) account = Account(next_uid) if not account.node_created.get(): if account.unconf_minutes.get() < MIN_TIME: account.add_msg('Node creation failed! A minimum of %d minutes need to be purchased at a time. You need %d more minutes.' % (MIN_TIME, MIN_TIME - account.unconf_minutes.get())) return if account.destroyed.get(): account.add_msg('Node creation failed as it has already been destroyed, please use a new account and contact [email protected] to get your coins back.') return account.add_msg('Creating server now. ETA 10-20 minutes.') dcid = random.choice([1, 5, 7]) # NJ, LA, Amsterdam try: res = requests.post("https://api.vultr.com/v1/server/create?api_key=%s" % vultr_api_key.get(), data={"DCID": dcid, "VPSPLANID": 87, "OSID": 192, "SSHKEYID": ssh_management_key.get(), "label": str(account.uid), "enable_private_network": 'yes', 'enable_ipv6 string': 'yes'}) except Exception as e: nodes_recently_updated.prepend(next_uid) logging.error('Attempted to create server / Exception: %s' % repr(e)) if res.status_code == 200: # accepted response = res.json() subid = response['SUBID'] logging.info(response) account.creation_ts.set(int(time.time())) account.droplet_id.set(subid) account.node_created.set(True) account.dcid.set(dcid) droplets_to_configure.add(subid, account.creation_ts.get()) droplet_to_uid[subid] = account.uid active_servers.add(subid) account.add_msg('Server created successfully! Server ID %s' % (account.droplet_id.get(),)) account.tweet_creation() node_creation_issues.set(False) else: logging.error('Server creation failed! Status %d' % res.status_code) if res.status_code == 412 and res.content == NODE_CREATION_LIMIT_MSG: node_creation_issues.set(True) logging.error(res.content) nodes_recently_updated.append(next_uid) account.add_msg('Server creation failed... will keep retrying') return 'CREATION_FAILED' # import pdb; pdb.set_trace() else: logging.warning('Account already has a node created.') print(account.node_created.get())
def restart_server(id): return requests.post('https://api.vultr.com/v1/server/reboot?api_key=%s' % vultr_api_key.get(), data={'SUBID': id})
def get_servers(): return requests.get('https://api.vultr.com/v1/server/list?api_key=%s' % vultr_api_key.get()).json()
import datetime import logging import subprocess import asyncio import socket from paramiko.client import SSHClient, AutoAddPolicy, HostKeys import requests from models import currently_compiling, Account, nodes_recently_updated, ssh_management_key, vultr_api_key, droplet_to_uid, droplets_to_configure, droplet_ips, nodes_currently_syncing, active_servers, tweet_queue, total_nodeminutes, node_creation_issues, servers_to_restart from constants import REQUIRED_CONFIRMATIONS, COIN, MIN_TIME, MINUTES_IN_MONTH from digitalocean_custom import calc_node_minutes, regions, droplet_creation_json, create_headers, actually_charge logging.basicConfig(level=logging.INFO) headers = create_headers(vultr_api_key.get()) NODE_CREATION_LIMIT_MSG = b'Server add failed: You have reached the maximum number of active virtual machines for this account. For security reasons, please contact our support team to have the limit raised' def get_servers(): return requests.get('https://api.vultr.com/v1/server/list?api_key=%s' % vultr_api_key.get()).json() def restart_server(id): return requests.post('https://api.vultr.com/v1/server/reboot?api_key=%s' % vultr_api_key.get(), data={'SUBID': id}) def ssh(hostname, username, password, cmd): client = SSHClient() client.set_missing_host_key_policy(AutoAddPolicy())