def start_cloudspaces(job): for cloudspace in job.get(): cloudspace_id = cloudspace['id'] job = execute_async_ovc(ovc, ovc.api.cloudapi.machines.list, cloudspaceId=cloudspace_id) job.link(start_vms)
def _run(job): job = execute_async_ovc(ovc, ovc.api.cloudapi.machines.stop, machineId=machine_id, force=True) job.link_value( lambda _: print("Succesfully killed vm %s" % machine_id)) job.link_exception( lambda _: print("Failed to stop vm %s" % machine_id))
def main(options): from JumpScale import j ovc = j.clients.openvcloud.get(options.environment, options.username, options.password) def hardshutdown_vm(machine_id): def _run(job): job = execute_async_ovc(ovc, ovc.api.cloudapi.machines.stop, machineId=machine_id, force=True) job.link_value( lambda _: print("Succesfully killed vm %s" % machine_id)) job.link_exception( lambda _: print("Failed to stop vm %s" % machine_id)) return _run def print_message(message): def _run(_): print(message) return _run def shutdown_vms(job): machines = job.get() for machine in machines: machine_id = machine['id'] if machine['status'] != 'RUNNING': print("Skipping machine {} with status {}".format( machine_id, machine['status'])) continue job = execute_async_ovc(ovc, ovc.api.cloudapi.machines.stop, machineId=machine_id) job.link_value( print_message("Succesfully shutdown vm %s" % machine_id)) job.link_exception(hardshutdown_vm(machine_id)) def shutdown_cloudspaces(job): for cloudspace in job.get(): cloudspace_id = cloudspace['id'] print( "Listing machines in cloudspace {} (id={}, status={})".format( cloudspace['name'], cloudspace_id, cloudspace['status'])) job = execute_async_ovc(ovc, ovc.api.cloudapi.machines.list, cloudspaceId=cloudspace_id) job.link(shutdown_vms) job.link_exception(print_message) job = execute_async_ovc(ovc, ovc.api.cloudapi.cloudspaces.list) job.link(shutdown_cloudspaces) gevent.wait()
def shutdown_cloudspaces(job): for cloudspace in job.get(): cloudspace_id = cloudspace['id'] print( "Listing machines in cloudspace {} (id={}, status={})".format( cloudspace['name'], cloudspace_id, cloudspace['status'])) job = execute_async_ovc(ovc, ovc.api.cloudapi.machines.list, cloudspaceId=cloudspace_id) job.link(shutdown_vms) job.link_exception(print_message)
def shutdown_vms(job): machines = job.get() for machine in machines: machine_id = machine['id'] if machine['status'] != 'RUNNING': print("Skipping machine {} with status {}".format( machine_id, machine['status'])) continue job = execute_async_ovc(ovc, ovc.api.cloudapi.machines.stop, machineId=machine_id) job.link_value( print_message("Succesfully shutdown vm %s" % machine_id)) job.link_exception(hardshutdown_vm(machine_id))
def main(options): from js9 import j j.clients.itsyouonline.get(data={ 'application_id_': options.application_id, 'secret_': options.secret }) ovc = j.clients.openvcloud.get(data={ 'address': options.environment, 'account': options.username }) def print_message(message): def _run(_): print(message) return _run def start_vms(job): for machine in job.get(): if machine['status'] != 'HALTED': continue machine_id = machine['id'] # Need to start machines one by one, to work arround OVC problem. # Otherwise they get started all on the same node with concurrency: try: ovc.api.cloudapi.machines.start(machineId=machine_id) print("Succesfully started vm %s" % machine_id) except: print("Could not start vm %s" % machine_id) #job = execute_async_ovc(ovc, ovc.api.cloudapi.machines.start, machineId=machine_id) #job.link_value(print_message("Succesfully started vm %s" % machine_id)) #job.link_exception(print_message("Could not start vm %s" % machine_id)) def start_cloudspaces(job): for cloudspace in job.get(): cloudspace_id = cloudspace['id'] job = execute_async_ovc(ovc, ovc.api.cloudapi.machines.list, cloudspaceId=cloudspace_id) job.link(start_vms) job = execute_async_ovc(ovc, ovc.api.cloudapi.cloudspaces.list) job.link(start_cloudspaces) gevent.wait()