Skip to content

Instantly share code, notes, and snippets.

@0xIslamTaha
Last active December 3, 2018 16:19
Show Gist options
  • Save 0xIslamTaha/35e6572349d18223291363ee80a62779 to your computer and use it in GitHub Desktop.
Save 0xIslamTaha/35e6572349d18223291363ee80a62779 to your computer and use it in GitHub Desktop.
from jumpscale import j
from termcolor import colored
from random import randint
import click, uuid
# Templates
zerotier_template = "github.com/threefoldtech/0-templates/zerotier_client/0.0.1"
dm_vm_template = "github.com/threefoldtech/0-templates/dm_vm/0.0.1"
#python3 dm.py -r local -t ******* -i 35c192ce9ba98299 -n ac1f6b456b98 -im ubuntu -dt hdd -ds 5 -m 2048 -c 1 -s "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCjPfKUsaFuaGJsnHvF3k0PbqQTr3GL2pNuddn/xQjsroF35ELJVEovAsd9IlsFWOmDWlL6B+JYFgj8g5IykklHCDfmTu6LcGXjdfAYVp+eXARmgoCJKxVyenSVHu6No9O1e+QKFvMJTiJXdl08fZD1Fd2kRetDRKAijCZ76pmB4/KwVFiJKCVVdsDW/0R+td0gNVJyCQyRTcWEPmBfGMW/JrvRCSHfxlLdqsD3txLOm9pHlQ/LmEwOP3bqEEpQU1jP32JbdAdreuD6BYB+YRp02yyU33gd1QbqIEgftcN+6TuZJOU3j2VRSiUQX8h5SjtWV1UXE15ELlIlhcFJYH6L root@islamtaha-TT"
@click.command()
@click.option("-r", "--robot", help="0-robot instance to use", required=True)
@click.option("-t", "--zt_token", help="zero tier token", required=True)
@click.option("-i", "--zt_nw_id", help="zero tier network id", required=True)
@click.option("-n", "--node_id", help="node id to host the vm", required=True)
@click.option("-im", "--image", help="image flist", default='ubuntu')
@click.option("-dt", "--disk_type", help="vm disk type hdd or ssd", required=True)
@click.option("-ds", "--disk_size", help="vm disk size in gigabyte", required=True)
@click.option("-m", "--memory", help="vm memory size in mbyte", required=True)
@click.option("-c", "--cpu", help="vm cpu", required=True)
@click.option("-s", "--ssh_key", help="ssh key", required=True)
def main(robot, zt_token, zt_nw_id, node_id, image, disk_type, disk_size, memory, cpu, ssh_key):
robot = j.clients.zrobot.robots[robot]
print(colored(' [*] create zerotier client', 'white'))
data = {
'token': zt_token
}
robot.services.find_or_create(zerotier_template, "xtremx_zt", data=data)
print(colored(' [*] Install vm', 'white'))
vm_service_name = 'xtremx_{}'.format(randint(100, 999))
print(colored(' [*] service name : {}'.format(vm_service_name), 'white'))
data = {
'nodeId': node_id,
'disks': [{
'diskType': disk_type,
'size': int(disk_size),
'mountPoint': '/mnt',
'filesystem': 'btrfs',
'label': str(uuid.uuid4()).replace('-', '')[:10],
}],
'image': image,
'memory': int(memory),
'cpu': int(cpu),
'mgmtNic': {'id': zt_nw_id, 'ztClient': 'xtremx_zt', 'type':'zerotier'},
'configs': [{'path': '/root/.ssh/authorized_keys',
'content': ssh_key,
'name': 'sshkey'}]
}
vmservice = robot.services.find_or_create(dm_vm_template, service_name=vm_service_name, data=data)
vmservice.schedule_action('install').wait(die=True)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment