Пример #1
0
def start(name, background=True):
    # TODO: Determine if machine is a VM by checking if a Vagrantfile exists in the machine_path

    log_path = os.path.join(util.get_gesso_root(), '.gesso', 'logs')
    vagrant_log_path = os.path.join(log_path, 'vagrant.log')

    # Start virtual machine
    machine_path = util.get_machine_path(name)
    with open(vagrant_log_path, 'w+') as log:
        if background == True:
            sys.stdout.write(
                'Starting VM in background. VM will be available in a few minutes. Check status with `gesso device list`.'
            )
            sys.stdout.flush()
        elif background == False:
            sys.stdout.write('Starting VM.')
            sys.stdout.flush()

        #process = subprocess.Popen(['vagrant', 'up'], stdout=subprocess.PIPE, cwd=machine_path)
        process = subprocess.Popen(['vagrant', 'up'],
                                   stdout=log,
                                   cwd=machine_path)

        if background == False:
            process.wait()
            sys.stdout.write(' OK.\n')
            sys.stdout.flush()
            # TODO: Announce with voice "device <device-name> is now available."
            None
Пример #2
0
def ssh(name=None):
    machine_path = util.get_machine_path(name)
    address_ip4 = util.get_machine_address(name)
    #device_ip = util.request_ip_address(name) # TODO: lookup the IP address of the device (and if it's a VM, make sure it's running!)
    if address_ip4 != None:
        username = '******'
        subprocess.call([
            'ssh', '-l', username, '-o', 'StrictHostKeyChecking=no',
            address_ip4
        ],
                        cwd=machine_path)
Пример #3
0
def remove(name):
    # TODO: Determine if machine is a VM by checking if a Vagrantfile exists in the machine_path
    # TODO: Check if machine is running. If so, shutdown before continuing. Verify that it's shut down.

    log_path = os.path.join(util.get_gesso_root(), '.gesso', 'logs')
    vagrant_log_path = os.path.join(log_path, 'vagrant.log')
    # Destroy virtual machine
    machine_path = util.get_machine_path(name)
    if os.path.exists(machine_path):
        with open(vagrant_log_path, 'w+') as log:
            sys.stdout.write('Removing VM.')
            sys.stdout.flush()
            process = subprocess.Popen(['vagrant', 'destroy', '-f'],
                                       stdout=log,
                                       cwd=machine_path)
            process.wait()
            sys.stdout.write(' OK.\n')
            sys.stdout.flush()

    # Recursively delete the machine path
    # TODO: Check if machine is shutdown. It shouldn't be running when deleting the folder hierarchy.
    if os.path.exists(machine_path):
        sys.stdout.write('Deleting %s.' % machine_path)
        sys.stdout.flush()
        shutil.rmtree(machine_path)
        sys.stdout.write(' OK.\n')
        sys.stdout.flush()

    # Remove device from registry (in SQLite database) if it exists
    db_path = util.get_database_path()
    db = TinyDB(db_path, default_table='gesso')
    Device = Query()
    device = db.table('device').get(Device.name == name)
    if device != None:
        sys.stdout.write('Removing %s from database.' % name)
        sys.stdout.flush()

        Device = Query()
        #device = db.table('device').get(Device.name == name)
        removed_device_ids = db.table('device').remove(Device.name == name)
        if len(removed_device_ids) > 0:
            sys.stdout.write(' OK.')
            sys.stdout.flush()
        else:
            sys.stdout.write(' Error.')
            sys.stdout.flush()

    else:
        # The device was not in the database
        None
Пример #4
0
def stop(name):
    # TODO: Determine if machine is a VM by checking if a Vagrantfile exists in the machine_path

    log_path = os.path.join(util.get_gesso_root(), '.gesso', 'logs')
    vagrant_log_path = os.path.join(log_path, 'vagrant.log')

    # Start virtual machine
    machine_path = util.get_machine_path(name)
    with open(vagrant_log_path, 'w+') as log:
        sys.stdout.write('Stopping VM.')
        sys.stdout.flush()
        process = subprocess.Popen(['vagrant', 'halt'],
                                   stdout=log,
                                   cwd=machine_path)
        process.wait()
        sys.stdout.write(' OK.\n')
        sys.stdout.flush()