def delete_host(name): todelete_hostdata = get_hostdata(name) image = todelete_hostdata['image'] wan_ip = todelete_hostdata['wan_ip'] if image in ['ubuntu', 'freebsd', 'freebsd-ufs', 'debian', 'centos']: os.system( '/usr/local/bin/ansible-playbook /home/bitclouds/bitclouds.sh/ansible/remove_vm.yml ' '--extra-vars="iname=' + name.replace('-', '_') + ' wan_ip=\'' + wan_ip + '\'"') deactivate_host(name) elif image in ['bitcoind', 'clightning', 'bsdjail', 'lnd']: os.system( '/usr/local/bin/ansible-playbook /home/bitclouds/bitclouds.sh/ansible/remove_jail.yml ' '--extra-vars="iname=' + name.replace('-', '_') + ' wan_ip=\'' + wan_ip + '\'"') deactivate_host(name) elif image in MARKET: if image == 'k8s': try: requests.get(os.environ['K8S_LINK'] + '/api/v1/destroy/' + todelete_hostdata['k8s']['id']) except Exception: notify('failed to delete k8s cluster') deactivate_host(name) else: return False
def status(host): hostdata = get_hostdata(host) if hostdata: if not hostdata['key_requested']: key_req = 'https://bitclouds.sh/key/' + host else: key_req = 'issued' if hostdata['status'] == 'subscribed': result = { "ip4": hostdata['wan_ip'], "user": hostdata['username'], "key": key_req, "tip": get_tip(), "status": hostdata['status'], "balance": hostdata['balance'] } elif hostdata['status'] == 'inactive': result = { "ip4": ".".join(map(str, (random.randint(0, 255) for _ in range(4)))), "user": hostdata['username'], "key": key_req, "tip": get_tip(), "status": 'inactive' } elif hostdata['status'] == 'init': result = {"status": 'unpaid'} else: result = { "ip4": ".".join(map(str, (random.randint(0, 255) for _ in range(4)))), "user": '******', "tip": get_tip() + str(random.randint(0, 255)), "key": "-----BEGIN OPENSSH PRIVATE KEY-----\n" + get_random_string(70) + "\n" + get_random_string(70) + "\n" + get_random_string(70) + "\n" + get_random_string(70) + "\n" + get_random_string(50) + "\n" + "\n-----END OPENSSH PRIVATE KEY-----", "status": 'subscribed' } return jsonify(result)
def getkey(host): hostdata = get_hostdata(host) if hostdata: if hostdata['key_requested'] is False and hostdata[ 'status'] == 'subscribed': hide_key(host) if hostdata['image'] not in MARKET: return hostdata['init_priv'] else: if hostdata['image'] == 'k8s': return hostdata['k8s']['kubeconfig'] else: return status(host) else: return jsonify(False)
def topup(host, sats): hostdata = get_hostdata(host) if hostdata: invoice = generate_invoice(sats, host)['bolt11'] register_payment(hostdata['name'], invoice, "topup", get_req_ip()) host_status = hostdata['status'] if host_status == 'subscribed': result = {"host": host, "invoice": invoice} else: result = {"error": "host is expired or not yet initialized"} else: result = { "error": "no such host", } return jsonify(result)
def create_vps(image): setup_fee = 0 if image in MARKET: setup_fee = 9900 if not check_k8s(): return jsonify({"error": 'out of stock'}) name = 'm-' + getStar() inc = 0 while get_hostdata(name): inc += 1 name = 'm-' + getStar() + '-' + str(inc) add_host(name, get_random_string(12), 'init', image, get_username(image)) invoice = generate_invoice(setup_fee + 99, name)['bolt11'] result = { "host": name, "price": "<1 sat/min", "setup_fee": setup_fee, "paytostart": invoice, "disclaimer": "If you pay the LN invoice, you agree with terms of service: any abuse usage is prohibited." " Your instance may be stopped and/or destroyed at any time without any reason. Do backups." " Your data is securely encrypted and instances hosted in enterprise datacenters." " Your digital identifiers are saved for authorization purposes." " Bitclouds never use your data for any purpose except mentioned above.", "support": "https://support.bitclouds.sh" } register_payment(name, invoice, "new", get_req_ip()) return jsonify(result) elif image in ALL_IMAGES: name = getStar() inc = 0 while get_hostdata(name): inc += 1 name = getStar() + '-' + str(inc) add_host(name, get_random_string(12), 'init', image, get_username(image)) if image == 'lnd': setup_fee = 900 elif image == 'bitcoind': setup_fee = 900 elif image in ['debian', 'ubuntu', 'centos']: setup_fee = 0 invoice = generate_invoice(setup_fee + 99, name)['bolt11'] result = { "host": name, "price": "<1 sat/min", "setup_fee": setup_fee, "performance": "1xXeon-2GB-40GB", "paytostart": invoice, "disclaimer": "If you pay the LN invoice, you agree with terms of service: any abuse usage is prohibited." " Your instance may be stopped and/or destroyed at any time without any reason. Do backups." " Your data is securely encrypted and instances hosted in enterprise datacenters." " Your digital identifiers are saved for authorization purposes." " Bitclouds never use your data for any purpose except mentioned above.", "support": "https://support.bitclouds.sh" } register_payment(name, invoice, "new", get_req_ip()) return jsonify(result) else: return {"error": 'no such image'}
def create_host(name): newhost_data = get_hostdata(name) print("create host output: \n" + str(newhost_data)) image = newhost_data['image'] pwd = newhost_data['pwd'] pub_key = newhost_data['init_pub'] print("check image for " + name) if image == 'ubuntu': print("get lan ip for " + name) lan_ip = os.popen('ssh nvme cbsd dhcpd').read().rstrip("\n") print(lan_ip) print("get wan ip for " + name) wan_ip = get_free_wan() print(wan_ip) print("bind ip for " + name) bind_ip(name, wan_ip) print("run ansible for " + name) print( '/usr/local/bin/ansible-playbook /home/bitclouds/bitclouds.sh/ansible/create_ubuntu.yml ' '--extra-vars="iname=' + name.replace('-', '_') + ' dname=' + name + ' pwd=' + pwd + ' pub_key=\'' + pub_key + '\' lan_ip=\'' + lan_ip + '\' wan_ip=\'' + wan_ip + '\'"') os.system( '/usr/local/bin/ansible-playbook /home/bitclouds/bitclouds.sh/ansible/create_ubuntu.yml ' '--extra-vars="iname=' + name.replace('-', '_') + ' dname=' + name + ' pwd=' + pwd + ' pub_key=\'' + pub_key + '\' lan_ip=\'' + lan_ip + '\' wan_ip=\'' + wan_ip + '\'"') print("init " + name) init_host(name, lan_ip, wan_ip) print("subscribe " + name) subscribe_host(name, 99 - 33) elif image == 'centos': lan_ip = os.popen('ssh nvme cbsd dhcpd').read().rstrip("\n") wan_ip = get_free_wan() bind_ip(name, wan_ip) os.system( '/usr/local/bin/ansible-playbook /home/bitclouds/bitclouds.sh/ansible/create_centos.yml ' '--extra-vars="iname=' + name.replace('-', '_') + ' dname=' + name + ' pwd=' + pwd + ' pub_key=\'' + pub_key + '\' lan_ip=\'' + lan_ip + '\' wan_ip=\'' + wan_ip + '\'"') init_host(name, lan_ip, wan_ip) subscribe_host(name, 99 - 33) elif image == 'debian': lan_ip = os.popen('ssh nvme cbsd dhcpd').read().rstrip("\n") wan_ip = get_free_wan() bind_ip(name, wan_ip) os.system( '/usr/local/bin/ansible-playbook /home/bitclouds/bitclouds.sh/ansible/create_debian.yml ' '--extra-vars="iname=' + name.replace('-', '_') + ' dname=' + name + ' pwd=' + pwd + ' pub_key=\'' + pub_key + '\' lan_ip=\'' + lan_ip + '\' wan_ip=\'' + wan_ip + '\'"') init_host(name, lan_ip, wan_ip) subscribe_host(name, 99 - 33) elif image == 'freebsd': lan_ip = os.popen('ssh nvme cbsd dhcpd').read().rstrip("\n") wan_ip = get_free_wan() bind_ip(name, wan_ip) os.system( '/usr/local/bin/ansible-playbook /home/bitclouds/bitclouds.sh/ansible/create_freebsd.yml ' '--extra-vars="iname=' + name.replace('-', '_') + ' dname=' + name + ' pwd=' + pwd + ' pub_key=\'' + pub_key + '\' lan_ip=\'' + lan_ip + '\' wan_ip=\'' + wan_ip + '\'"') init_host(name, lan_ip, wan_ip) subscribe_host(name, 99) elif image == 'freebsd-ufs': lan_ip = os.popen('ssh nvme cbsd dhcpd').read().rstrip("\n") wan_ip = get_free_wan() bind_ip(name, wan_ip) os.system( '/usr/local/bin/ansible-playbook /home/bitclouds/bitclouds.sh/ansible/create_freebsd-ufs.yml ' '--extra-vars="iname=' + name.replace('-', '_') + ' dname=' + name + ' pwd=' + pwd + ' pub_key=\'' + pub_key + '\' lan_ip=\'' + lan_ip + '\' wan_ip=\'' + wan_ip + '\'"') init_host(name, lan_ip, wan_ip) subscribe_host(name, 99) elif image == 'bitcoind': lan_ip = os.popen('ssh nvme cbsd dhcpd').read().rstrip("\n") wan_ip = get_free_wan() bind_ip(name, wan_ip) bitcoin_data = { 'rpcuser': get_random_string(10), 'rpc_pwd': get_random_string(10), } os.system( '/usr/local/bin/ansible-playbook /home/bitclouds/bitclouds.sh/ansible/create_bitcoind.yml ' '--extra-vars="iname=' + name.replace('-', '_') + ' dname=' + name + ' rpcuser='******'rpcuser'] + ' rpc_pwd=' + bitcoin_data['rpc_pwd'] + ' pwd=' + pwd + ' pub_key=\'' + pub_key + '\' lan_ip=\'' + lan_ip + '\' wan_ip=\'' + wan_ip + '\'"') init_host(name, lan_ip, wan_ip) init_bitcoind(name, bitcoin_data) subscribe_host(name, 99) elif image == 'lnd': lan_ip = os.popen('ssh nvme cbsd dhcpd').read().rstrip("\n") wan_ip = get_free_wan() bind_ip(name, wan_ip) bitcoin_data = { 'rpcuser': get_random_string(10), 'rpc_pwd': get_random_string(10), } os.system( '/usr/local/bin/ansible-playbook /home/bitclouds/bitclouds.sh/ansible/create_lnd.yml ' '--extra-vars="iname=' + name.replace('-', '_') + ' dname=' + name + ' rpcuser='******'rpcuser'] + ' rpc_pwd=' + bitcoin_data['rpc_pwd'] + ' pwd=' + pwd + ' pub_key=\'' + pub_key + '\' lan_ip=\'' + lan_ip + '\' wan_ip=\'' + wan_ip + '\'"') init_host(name, lan_ip, wan_ip) init_bitcoind(name, bitcoin_data) subscribe_host(name, 99 - 66) elif image == 'clightning': lan_ip = os.popen('ssh nvme cbsd dhcpd').read().rstrip("\n") wan_ip = get_free_wan() bind_ip(name, wan_ip) sparko_data = { 'login': get_random_string(10), 'pwd_web': get_random_string(10), 'pwd_rw': get_random_string(10), 'pwd_ro': get_random_string(10), } os.system( '/usr/local/bin/ansible-playbook /home/bitclouds/bitclouds.sh/ansible/create_clightning.yml ' '--extra-vars="iname=' + name.replace('-', '_') + ' dname=' + name + ' sparko_login='******'login'] + ' sparko_pwd_web=' + sparko_data['pwd_web'] + ' sparko_pwd_rw=' + sparko_data['pwd_rw'] + ' sparko_pwd_ro=' + sparko_data['pwd_ro'] + ' pwd=' + pwd + ' pub_key=\'' + pub_key + '\' lan_ip=\'' + lan_ip + '\' wan_ip=\'' + wan_ip + '\'"') init_host(name, lan_ip, wan_ip) init_sparko(name, sparko_data) subscribe_host(name, 99) elif image == 'bsdjail': lan_ip = os.popen('ssh nvme cbsd dhcpd').read().rstrip("\n") wan_ip = get_free_wan() bind_ip(name, wan_ip) os.system( '/usr/local/bin/ansible-playbook /home/bitclouds/bitclouds.sh/ansible/create_jail.yml ' '--extra-vars="iname=' + name.replace('-', '_') + ' dname=' + name + ' pwd=' + pwd + ' pub_key=\'' + pub_key + '\' lan_ip=\'' + lan_ip + '\' wan_ip=\'' + wan_ip + '\'"') init_host(name, lan_ip, wan_ip) subscribe_host(name, 99) elif image in MARKET: if image == 'k8s': k8s_data = get_k8s()['data'] init_k8s(name, k8s_data) subscribe_host(name, 99) else: return False else: return False
try: if int(float(data)): BTCPRICE = int(float(data)) print('BTC PRICE UPDATE: ' + str(BTCPRICE)) else: print("not digit") except Exception as e: print("price update exception: " + str(e)) print(dtime + ":\n" + str(data)) if data['status'] == 'paid': sats = round(data['msatoshi_received'] / 1000) instance_name = extract_name(data['label']) print('adding balance to ' + instance_name) if get_hostdata(instance_name): hostdata = get_hostdata(instance_name) print("host status: " + hostdata['status']) if hostdata['status'] == 'init': print("creating host " + instance_name) create_host(instance_name) elif hostdata['status'] == 'subscribed': print("subscribing host " + instance_name) subscribed_data = get_hostdata(instance_name) # remember paid sats and manipulate sats to be added paid_sats = sats if subscribed_data['image'] == 'lnd': sats = int(paid_sats * 0.7) elif subscribed_data['image'] == 'bitcoind': sats = int(paid_sats * 1.2) elif subscribed_data['image'] in ['debian', 'centos']: