# Manage vagrant vms

## Problem
Many vagrant VMs are being created on the Strongbox vCenter, but they are not having a correct life cycle management (VMs are not being destroyed by the owners). This is provoking a slow vCenter with resource and load issues.

## Solution
Create a script using python and pyvmomi to:
  1. Connect to the vCenter 
  2. Return a list of vagrant vms
  3. Obtain the logins made during the last 30 days for every vm
  4. Destroy those VMs that have not been used

## Script usage

### Requirements
  1. Install Python 3.6.5
  2. Clone repository
      ```bash
      git clone https://git.strongboxdata.com:8443/scm/do/manage-vagrant-vms.git
      ```
  3. Navidate to manage-vagrant-vms directory
     ```bash
     cd manage-vagrant-vms
     ```
  3. Install virtualenv
     ```bash
     pip3 install virtualenv
     ```
  4. Create and activate virtual environment
     ```bash
     virtualenv venv
     source venv/bin/activate
     ```
  5. Install requirements
      ```bash
      pip install -r requirements.txt
      ```

### Execute

#### List all vagrant vms

Command
```bash
python manage_vagrant_vms.py --vcenter vcenter.strongboxdata.com --user vagrant@sbds.local --passwd 'vagrant4SBDS!' --all
```

Result

```bash
-------------------------------------------------------------------------
Name   :  vm1_default_1548869754700_43443
Guest  :  CentOS 4/5 or later (64-bit)
Folder :  VAGRANT
State  :  poweredOn
IP     :  10.10.4.96
Logins during the last 30 days :
Username         Port     From             Latest

vagrant          pts/0    10.10.1.55       Wed Jan 30 17:41:49 +0000 2019

radium           pts/1    10.11.200.114    Wed Jan 30 17:48:25 +0000 2019
-------------------------------------------------------------------------
Name   :  centos7-vm-1.15.0-test-17_default_1547183055151_73565
Guest  :  CentOS 4/5 or later (64-bit)
Folder :  VAGRANT
State  :  poweredOn
IP     :  10.10.4.85
Logins during the last 30 days :
* There aren't logins
* This vm can be destroyed. Pass --destroy flag
```

#### List only vagrant vms that have not been used in 30 days

Command
```bash
python manage_vagrant_vms.py --vcenter vcenter.strongboxdata.com --user vagrant@sbds.local --passwd 'vagrant4SBDS!'
```

Result
```bash
-------------------------------------------------------------------------
Name   :  centos7-vm-1.15.0-test-17_default_1547183055151_73565
Guest  :  CentOS 4/5 or later (64-bit)
Folder :  VAGRANT
State  :  poweredOn
IP     :  10.10.4.85
Logins during the last 30 days :
* There aren't logins
* This vm can be destroyed. Pass --destroy flag
```

#### List only vagrant vms that have not been used in 30 days and destroy them

Command
```bash
python manage_vagrant_vms.py --vcenter vcenter.strongboxdata.com --user vagrant@sbds.local --passwd 'vagrant4SBDS!' --destroy
```

Result
```bash
-------------------------------------------------------------------------
Name   :  centos7-vm-1.15.0-test-17_default_1547183055151_73565
Guest  :  CentOS 4/5 or later (64-bit)
Folder :  VAGRANT
State  :  poweredOn
IP     :  10.10.4.85
Logins during the last 30 days :
* There aren't logins
* This vm will be destroyed
Powering off vm
success
Destroying vm
success
```
