def list(*, pattern=None, project=None, username=None, ssh_key_path=None): _generate_gcloud_ssh_key() cmd = [ 'gcloud', 'compute', 'instances', 'list', '--format', 'value(zone, name, networkInterfaces[0].accessConfigs[0].natIP)' ] if project: cmd += ['--project', project] else: project = get_project() p = run(cmd) result = [] lines = p.stdout.strip('\n').split('\n') for line in lines: zone, name, ip = re.split(r'\s+', line) if pattern: if pattern not in name: continue if username is None: username = _get_username(project) if ssh_key_path is None: ssh_key_path = SSH_KEY_PATH result.append( Machine(provider=gcloud_provider, name=name, zone=zone, ip=ip, username=username, ssh_key_path=ssh_key_path, project=project)) return result
def create(*, name, machine_size, disk_size_gb=None, image, location): args = ['--name', name] args += ['--resource-group', name] args += ['--image', image] args += ['--size', machine_size] if disk_size_gb is not None: args += ['--os-disk-size-gb', str(disk_size_gb)] if _group_exist(name): raise MachineCreationException('resource group already exist') p = _create_group(name, location) if p.returncode != 0: raise MachineCreationException(p.stderr) p = run(['az', 'vm', 'create', *args]) if p.returncode != 0: _delete_group(name) raise MachineCreationException(p.stderr) ip = json.loads(p.stdout)['publicIpAddress'] machine = Machine(provider=azure_provider, name=name, zone=None, ip=ip, username=os.getlogin(), ssh_key_path=SSH_KEY_PATH) return machine
def get(name, *, username=None, ssh_key_path=None, project=None, **kwargs): _generate_gcloud_ssh_key() cmd = [ 'gcloud', 'compute', 'instances', 'list', '--format', 'value(zone, name, networkInterfaces[0].accessConfigs[0].natIP)', '--filter', 'name=' + name ] if project: cmd += ['--project', project] else: project = get_project() p = run(cmd) if p.stdout.strip('\n') == '': return None zone, name, ip = re.split(r'\s+', p.stdout.strip('\n')) if username is None: username = _get_username(project) if ssh_key_path is None: ssh_key_path = SSH_KEY_PATH return Machine(provider=gcloud_provider, name=name, zone=zone, ip=ip, username=username, ssh_key_path=ssh_key_path, project=project)
def get(name): p = run(['gcloud', 'compute', 'instances', 'list', '--format', 'value(zone, name, networkInterfaces[0].accessConfigs[0].natIP)', '--filter', 'name=' + name]) if p.stdout.strip('\n') == '': return None zone, name, ip = re.split(r'\s+', p.stdout.strip('\n')) return Machine(provider=gcloud_provider, name=name, zone=zone, ip=ip, username=_get_username(), ssh_key_path=SSH_KEY_PATH)
def list(): p = run([ 'doctl', 'compute', 'droplet', 'list', '--no-header', '--format', 'Region,Name,PublicIPv4,ID' ]) result = [] lines = p.stdout.strip('\n').split('\n') for line in lines: zone, name, ip, id_ = re.split(r'\s+', line) m = Machine(provider=digitalocean_provider, name=name, zone=zone, ip=ip, username='******', ssh_key_path=SSH_KEY_PATH) m.id = id_ result.append(m) return result
def list(): p = run(['gcloud', 'compute', 'instances', 'list', '--format', 'value(zone, name, networkInterfaces[0].accessConfigs[0].natIP)']) result = [] lines = p.stdout.strip('\n').split('\n') for line in lines: zone, name, ip = re.split(r'\s+', line) result.append(Machine(provider=gcloud_provider, name=name, zone=zone, ip=ip, username=_get_username(), ssh_key_path=SSH_KEY_PATH)) return result
def create(*, name, machine_size, disk_size_gb=None, image, location, username=None, ssh_key_path=None, reserve_ip=True): """ Available machine_size: find online for detail and price, or: az vm list-sizes --location location -o table Available image: az vm image list -o table Available location: az account list-locations -o table """ args = ['--name', name] args += ['--resource-group', name] args += ['--image', image] args += ['--size', machine_size] if reserve_ip: args += ['--public-ip-address-allocation', 'static'] if disk_size_gb is not None: args += ['--os-disk-size-gb', str(disk_size_gb)] # if _group_exist(name): # raise MachineCreationException('resource group already exist') if username is None: username = os.getlogin() else: args += ['--admin-username', username] if ssh_key_path is None: ssh_key_path = SSH_KEY_PATH else: args += ['--ssh-key-value', f'@{os.path.expanduser(ssh_key_path)}.pub'] p = _create_group(name, location) if p.returncode != 0: raise MachineCreationException(p.stderr) p = run(['az', 'vm', 'create', *args]) if p.returncode != 0: _delete_group(name) raise MachineCreationException(p.stderr) ip = json.loads(p.stdout)['publicIpAddress'] machine = Machine(provider=azure_provider, name=name, location=location, ip=ip, username=username, ssh_key_path=ssh_key_path) return machine
def get(name): p = run(['az', 'vm', 'list-ip-addresses', '-n', name, '-g', name]) if p.returncode == 0: res = json.loads(p.stdout) if not res: return None ip_address = res[ 0]['virtualMachine']['network']['publicIpAddresses'][0]['ipAddress'] return Machine(provider=azure_provider, name=name, zone=None, ip=ip_address, username=os.getlogin(), ssh_key_path=SSH_KEY_PATH) else: return None
def get(name, username=None, ssh_key_path=None, **kwargs): if username is None: username = '******' if ssh_key_path is None: ssh_key_path = SSH_KEY_PATH p = run( f'doctl compute droplet list --no-header --format Region,Name,PublicIPv4,ID' ) lines = p.stdout.strip('\n').split('\n') for line in lines: zone, name_, ip, id_ = re.split(r'\s+', line) if name_ == name: m = Machine(provider=digitalocean_provider, name=name, zone=zone, ip=ip, username=username, ssh_key_path=ssh_key_path) m.id = id_ return m return None
def create(*, name, machine_type, disk_size, image_project, image_family=None, image=None, zone, min_cpu_platform=None, preemptible=False, firewall_allows=None, reserve_ip=True, firewalls=None): args = [name] args += ['--machine-type', machine_type] args += ['--boot-disk-size', disk_size] if image_family: args += ['--image-family', image_family] args += ['--image-project', image_project] if image: args += ['--image', image] args += ['--zone', zone] if min_cpu_platform: args += ['--min-cpu-platform', min_cpu_platform] if preemptible: args += ['--preemptible'] if firewall_allows: if firewall_exist(name): delete_firewall(name) p = create_firewall(name=name, allows=firewall_allows) if p.returncode != 0: raise MachineCreationException(p.stderr) args += ['--tags', name] elif firewalls: args += ['--tags', ','.join(firewalls)] p = run(['gcloud', 'compute', 'instances', 'create', *args]) if p.returncode != 0: if firewall_allows: delete_firewall(name) raise MachineCreationException(p.stderr) _wait_bootup(name) ip = _get_ip(name) if not preemptible and reserve_ip: if _address_exist(name, _zone_region(zone)): _release_ip_address(name, _zone_region(zone)) p = _reserve_ip_address(ip, name, _zone_region(zone)) if p.returncode != 0: if firewall_allows: delete_firewall(name) _delete_machine(name, zone) raise MachineCreationException(p.stderr) machine = Machine(provider=gcloud_provider, name=name, zone=zone, ip=ip, username=_get_username(), ssh_key_path=SSH_KEY_PATH) _wait_ssh(machine) return machine
def _instance_to_machine(i, username, ssh_key_path): if i['State']['Name'] != 'terminated': name = _find_instance_name(i) zone = i['Placement']['AvailabilityZone'] region = _zone_region(zone) ip = i.get('PublicIpAddress') id_ = i['InstanceId'] return Machine(provider=aws_provider, name=name, zone=zone, region=region, ip=ip, username=username, ssh_key_path=ssh_key_path, id=id_) return None
def get(name, *, username=None, ssh_key_path=None, **kwargs): p = run(['az', 'vm', 'list-ip-addresses', '-n', name, '-g', name]) if p.returncode == 0: res = json.loads(p.stdout) if not res: return None ip_address = res[0]['virtualMachine']['network']['publicIpAddresses'][ 0]['ipAddress'] if not username: username = os.getlogin() if not ssh_key_path: ssh_key_path = SSH_KEY_PATH p = run(f'az vm get-instance-view -n {name} -g {name}') res = json.loads(p.stdout) location = res["location"] return Machine(provider=azure_provider, name=name, location=location, ip=ip_address, username=username, ssh_key_path=ssh_key_path) else: return None