Created
September 17, 2018 15:59
-
-
Save 0xIslamTaha/667f1c6a88d3276d17d11042f4a4976c to your computer and use it in GitHub Desktop.
This file contains 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
import requests, time, random | |
mykey = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCjPfKUsaFuaGJsnHvF3k0PbqQTr3GL2pNuddn/xQjsroF35ELJVEovAsd9IlsFWOmDWlL6B+JYFgj8g5IykklHCDfmTu6LcGXjdfAYVp+eXARmgoCJKxVyenSVHu6No9O1e+QKFvMJTiJXdl08fZD1Fd2kRetDRKAijCZ76pmB4/KwVFiJKCVVdsDW/0R+td0gNVJyCQyRTcWEPmBfGMW/JrvRCSHfxlLdqsD3txLOm9pHlQ/LmEwOP3bqEEpQU1jP32JbdAdreuD6BYB+YRp02yyU33gd1QbqIEgftcN+6TuZJOU3j2VRSiUQX8h5SjtWV1UXE15ELlIlhcFJYH6L root@islamtaha-TT' | |
client_id = '' | |
client_secret = '' | |
packet_auth_token = '' | |
zt_token = '' | |
#ipxe = 'http://unsecure.bootstrap.gig.tech/ipxe/v1.2.2/9f77fc393e097c6e/console=ttyS1,115200%20development' | |
# ipxe_url = 'ipxe: http://unsecure.bootstrap.gig.tech/ipxe/v1.2.2/{}/console=ttyS1,115200%20development' | |
# ipxe = 'http://unsecure.bootstrap.gig.tech/ipxe/v1.2.2/{}/console=ttyS1,115200%20development' | |
ipxe_url = 'ipxe: http://unsecure.bootstrap.gig.tech/ipxe/v.1.4.1/{}/farmer_id=' | |
ipxe = 'http://unsecure.bootstrap.gig.tech/ipxe/v.1.4.1/{}/console=ttyS1,115200%20development' | |
jwt = '' | |
ovc_data={"account": "xtremx", | |
"address": "be-g8-3.demo.greenitglobe.com", | |
"jwt_": jwt, | |
"location": "be-g8-3", | |
"port": 443, | |
"space":""} | |
def get_available_facility(client, plan): | |
facilities = client.client.list_facilities() | |
for facility in facilities: | |
try: | |
if client.client.validate_capacity([(facility.code, plan, 1)]): | |
return facility.code | |
except: | |
pass | |
else: | |
return None | |
print(' [*] Install zerotier') | |
try: | |
j.tools.prefab.local.network.zerotier.install() | |
j.tools.prefab.local.network.zerotier.start() | |
except: | |
# That means its already installed | |
pass | |
zt_config_instance_name = 'my_zt_account' | |
zt_data = {'token_':zt_token} | |
zt_client = j.clients.zerotier.get(instance=zt_config_instance_name, data=zt_data) | |
zt_admin_network_name = 'admin_network' | |
zt_admin_network = zt_client.network_create(public=False, name=zt_admin_network_name, auto_assign=True, subnet='10.147.19.0/24') | |
zt_admin_network_id = zt_admin_network.id | |
print(' [*] Host join zt network') | |
j.tools.prefab.local.network.zerotier.network_join(network_id=zt_admin_network_id) | |
zt_machine_addr = j.tools.prefab.local.network.zerotier.get_zerotier_machine_address() | |
time.sleep(60) | |
host_member = zt_admin_network.member_get(address=zt_machine_addr) | |
host_member.authorize() | |
print(' [*] Get jwt') | |
get_jwt = requests.post('https://itsyou.online/v1/oauth/access_token?grant_type=client_credentials&client_id=%s&client_secret=%s&response_type=id_token' % (client_id, client_secret)) | |
jwt = get_jwt.text | |
print(' [*] GET OVC client') | |
ovccl = j.clients.openvcloud.get(data=ovc_data) | |
print(' [*] Create a CS under xtremx account') | |
account = ovccl.account_get('xtremx') | |
cs = account.space_get('xtremxCS%d' % random.randint(1, 1000)) | |
print(' [*] Create zOS machine') | |
zeroos_name = 'zeroos%d' % random.randint(1, 10000) | |
zt_nw_id = zt_admin_network_id | |
ipxe_url = ipxe_url.format(zt_nw_id) | |
vm_zos = cs.machine_create(name=zeroos_name, memsize=8, disksize=10, datadisks=[50], image='ipxe boot', | |
authorize_ssh=False, userdata=ipxe_url) | |
# print(' [*] Forward port 6379') | |
# vm_zos.portforward_create(6379, 6379) | |
# time.sleep(120) | |
print(' [*] Authorize ZOS vm') | |
znw_member = zt_admin_network.member_get(address=zt_admin_network.members_list()[0].address) | |
znw_member.authorize() | |
print(' [*] Create a GW inside ZOS VM') | |
zos_instance_name = vm_zos.name | |
node_address = znw_member.private_ip # ZOS with zt only lesson on ZT interface | |
zos_cfg = {"host": node_address} | |
zos_client = j.clients.zos.get(instance=zos_instance_name, data=zos_cfg) | |
zos_node = j.clients.zos.sal.get_node(instance=zos_instance_name) | |
print(' [*] Create a GW with public and ZT networks') | |
gw_name = 'my-gw' | |
gw = zos_node.primitives.create_gateway(name=gw_name) | |
print(' [*] Create public network') | |
vm_zos.externalnetwork_attach() | |
time.sleep(60) | |
external_network_ip_address = vm_zos.model['interfaces'][1]['ipAddress'] | |
external_network_mac_address = vm_zos.model['interfaces'][1]['macAddress'] | |
external_gw_ip_address = vm_zos.model['interfaces'][1]['params'].split()[0].rsplit(':')[1] | |
public_network_name = 'public' | |
public_net = gw.networks.add(name=public_network_name, type_='passthrough', networkid='eth1') | |
public_net.ip.cidr = external_network_ip_address | |
public_net.ip.gateway = external_gw_ip_address | |
print('[*] Create zerotier network interface') | |
private_network_name = 'zt' | |
private_net = gw.networks.add_zerotier(zt_admin_network) | |
private_net.hosts.nameservers = ['8.8.8.8'] | |
gw.deploy() | |
print(' [*] PACKET SETUP') | |
print(' [*] Deploy a ZOS packet machine') | |
ipxe = ipxe.format(zt_admin_network_id) | |
packet_machine_name = '0-core-TEST' | |
plan = "baremetal_0" | |
packet_client_data = {"auth_token_":packet_auth_token, | |
"project_name":"GIG Engineering"} | |
packet_client = j.clients.packetnet.get(data=packet_client_data) | |
project_id=packet_client.client.list_projects()[0].id | |
facility = get_available_facility(client=packet_client, plan=plan) | |
device_data = packet_client.client.create_device(project_id=project_id, hostname=packet_machine_name, plan=plan, | |
facility=facility, operating_system="custom_ipxe", | |
ipxe_script_url=ipxe, termination_time=10800) | |
time.sleep(240) | |
device = packet_client.client.get_device(device_data.id) | |
print(' [*] Authorize it') | |
zos_zt_member = zt_admin_network.member_get(public_ip=device.ip_addresses[0]['address']) | |
zos_zt_member.authorize() | |
print( ' [*] Deploy ZDB disk') | |
data = {"host": "%s" % zos_zt_member.private_ip} | |
j.clients.zos.get('mypacketdotnetzos', data=data) | |
node = j.clients.zero_os.sal.get_node('mypacketdotnetzos') | |
node.client.bash('mkdir -p /var/cache/zdb') | |
zdb = node.primitives.create_zerodb('myzdb', '/var/cache/zdb') | |
zdb = node.primitives.create_disk('mydisk', zdb, filesystem='btrfs') | |
zdb.deploy() | |
print(' [*] Creat an ubuntu vm and attach it to the ZT') | |
vm = node.primitives.create_virtual_machine('myvm', 'ubuntu:latest') | |
vm.disks.add(zdb) | |
vm.configs.add('mysshkey', '/root/.ssh/authorized_keys', mykey) | |
vm.nics.add_zerotier(network=zt_admin_network) # This will add two machine to the zt | |
vm.deploy() | |
print(' [*] Portforward from public to the vm machine') | |
vm_zt_ip = zt_admin_network.members_list()[1].private_ip | |
gw.portforwards.add('httpforward', (public_network_name, 8080), (vm_zt_ip, 8080)) | |
gw.portforwards.add('sshforward', (public_network_name, 34022), (vm_zt_ip, 22)) | |
gw.deploy() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment