Skip to content

Commit

Permalink
Merge pull request #2 from snobear/develop
Browse files Browse the repository at this point in the history
Destroy method, pypi build
  • Loading branch information
snobear committed Jun 30, 2014
2 parents 8c64d4a + 412b17f commit 5610b35
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 53 deletions.
9 changes: 9 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
LICENSE.txt
requirements.txt
setup.py
bin/ezmomi
ezmomi/__init__.py
ezmomi/cli.py
ezmomi/ezmomi.py
ezmomi/params.py
ezmomi/config/config.yml.example
46 changes: 27 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,58 @@ A simple command line interface for common VMware vSphere tasks.
EZmomi uses [pyvmomi](https://github.com/vmware/pyvmomi) (VMware vSphere API Python Bindings).


#### Example Usage
### Install

```
pip install ezmomi
```

### Example Usage

Clone a template with two static IPs:
##### Clone a template with two static IPs:

```
./ezmomi.py clone --hostname foo01 --cpus 2 --mem 4 --ips 172.10.16.203 172.10.16.204
ezmomi clone --template centos65 --hostname test01 --cpus 2 --mem 4 --ips 172.10.16.203 172.10.16.204
```

`ips` takes any number of ips.
`ips` takes any number of ips. See `ezmomi clone --help` for a list of params.

Get info about available resources, e.g.:
##### Destroy a VM

```
./ezmomi.py list --type Network
./ezmomi.py list --type Datastore
./ezmomi.py list --type VirtualMachine
ezmomi destroy --name test01
```

##### Listing your resources:

```
ezmomi list --type VirtualMachine
ezmomi list --type Network
ezmomi list --type Datastore
etc...
```

See [Managed Object Types](http://pubs.vmware.com/vsphere-50/index.jsp#com.vmware.wssdk.apiref.doc_50/mo-types-landing.html) in the vSphere API docs for a list of types to look up.

#### Help
### Help

Each command section has its own help:

```
./ezmomi.py --help
./ezmomi.py clone --help
./ezmomi.py list --help
ezmomi --help
ezmomi clone --help
ezmomi list --help
etc...
```

#### Install/Setup

I'm working on making this available via pip, but currently you can just clone via github:
### Install via github

```
git clone [email protected]:snobear/ezmomi.git
virtualenv --no-site-packages ezmomi
cd ezmomi && source bin/activate
pip install -r requirements.txt
mv config.yml.example config.yml
```

Then define your credentials, networks, and VMware objects in config.yml and you're all set.

#### Contributing
### Contributing
Pull requests, bug reports, and feature requests are extremely welcome.
6 changes: 6 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ezmomi
======

A simple command line interface for common VMware vSphere tasks. Please see https://github.com/snobear/ezmomi for usage examples.

ezmomi uses the lovely `pyvmomi <https://github.com/vmware/pyvmomi>`_ VMware vSphere API Python Bindings.
2 changes: 1 addition & 1 deletion bin/ezmomi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
from ezmomi import cli
if __name__ == '__main__':
cli()
cli.cli()
8 changes: 4 additions & 4 deletions ezmomi/cli.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'''
Command line definitions for ezmomi
'''
import argparse
from params import add_params
from ezmomi import EZMomi


'''
Start 'er up
'''
def cli():
# Set up command line arguments
parser = argparse.ArgumentParser(description='Perform common vSphere API tasks')
Expand Down
53 changes: 27 additions & 26 deletions ezmomi/ezmomi.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim, vmodl
import atexit
Expand All @@ -7,16 +8,9 @@
from pprint import pprint, pformat
import time
from netaddr import IPNetwork, IPAddress
from copy import deepcopy
import yaml
import logging
from netaddr import IPNetwork, IPAddress

'''
Logging
'''
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)

class EZMomi(object):
def __init__(self, **kwargs):
# load up our configs and connect to the vSphere server
Expand All @@ -25,37 +19,44 @@ def __init__(self, **kwargs):

def get_configs(self, kwargs):
default_cfg_dir = "%s/.config/ezmomi" % os.path.expanduser("~")
default_config_path = "%s/config.yml" % default_cfg_dir
default_config_file = "%s/config.yml" % default_cfg_dir

# use path from env var if it's set and valid
if 'EZMOMI_CONFIG' in os.environ:
if os.path.isfile(os.environ['EZMOMI_CONFIG']):
config_path = os.environ['EZMOMI_CONFIG']
config_file = os.environ['EZMOMI_CONFIG']
else:
print "%s does not exist. Set the EZMOMI_CONFIG environment variable to your config file's path."
sys.exit(1)

# or use the default config file path if it exists
elif os.path.isfile(default_config_path):
config_path = default_config_path
elif os.path.isfile(default_config_file):
config_file = default_config_file
# else create the default config path and copy the example config there
else:
from shutil import copy
if not os.path.exists(default_cfg_dir):
os.makedirs(default_cfg_dir)
config_path = default_config_path

#copy()

config_file = default_config_file

# copy example config
ezmomi_module_dir = os.path.dirname(os.path.abspath(__file__))
ezmomi_ex_config = "%s/config/config.yml.example" % ezmomi_module_dir
try:
copy(ezmomi_ex_config, default_cfg_dir)
except:
print "Error copying example config file from %s to %s" % (ezmomi_ex_config, default_cfg_dir)
sys.exit(1)

print "I could not find a config.yml file, so I copied an example to your home directory at %s/config.yml.example. Please rename this to config.yml and add your vSphere environment's settings." % default_cfg_dir
sys.exit(0)
try:
# get config file path from env var or use default
default_cfg_path = default_cfg_dir
config_path = os.environ.get('EZMOMI_CONFIG', default_cfg_path)
config = yaml.load(file(config_path))
config = yaml.load(file(config_file))
except IOError:
logging.exception('Unable to open config file. The default path for the ezmomi config file is ~/.config/ezmomi/config.yml. You can also specify the config file path by setting the EZMOMI_CONFIG environment variable.')
print 'Unable to open config file. The default path for the ezmomi config file is ~/.config/ezmomi/config.yml. You can also specify the config file path by setting the EZMOMI_CONFIG environment variable.'
sys.exit(1)
except Exception:
logging.exception('Unable to read config file. YAML syntax issue, perhaps?')
print 'Unable to read config file. YAML syntax issue, perhaps?'
sys.exit(1)

# Check all required values were supplied either via command line or config
Expand All @@ -70,7 +71,7 @@ def get_configs(self, kwargs):

if notset:
parser.print_help()
logging.error("Required parameters not set: %s\n" % notset)
print "Required parameters not set: %s\n" % notset
sys.exit(1)

return config
Expand All @@ -87,7 +88,7 @@ def connect(self):
port = int(self.config['port']),
)
except:
logging.exception('Unable to connect to vsphere server.')
print 'Unable to connect to vsphere server.'
sys.exit()

# add a clean up routine
Expand All @@ -106,7 +107,7 @@ def list_objects(self):
try:
container = self.content.viewManager.CreateContainerView(self.content.rootFolder, [eval(vim_obj)], True)
except AttributeError:
logging.error("%s is not a Managed Object Type. See the vSphere API docs for possible options." % vimtype)
print "%s is not a Managed Object Type. See the vSphere API docs for possible options." % vimtype
sys.exit()

# print header line
Expand All @@ -120,7 +121,7 @@ def clone(self):
self.config['hostname'] = self.config['hostname'].lower()
self.config['mem'] = self.config['mem'] * 1024 # convert GB to MB

logging.info("Cloning %s to new host %s..." % (self.config['template'], self.config['hostname']))
print "Cloning %s to new host %s..." % (self.config['template'], self.config['hostname'])

# initialize a list to hold our network settings
ip_settings = list()
Expand All @@ -142,7 +143,7 @@ def clone(self):

# throw an error if we couldn't find a network for this ip
if not any(d['ip'] == ip for d in ip_settings):
logging.error("I don't know what network %s is in. You can supply settings for this network in self.config.yml." % ip_string)
print "I don't know what network %s is in. You can supply settings for this network in self.config.yml." % ip_string
sys.exit(1)

# network to place new VM in
Expand Down
2 changes: 1 addition & 1 deletion ezmomi/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def add_params(subparsers):
list_parser = subparsers.add_parser('list',help='List VMware objects on your VMware server')

list_parser.add_argument('--type',
default='all',
required=True,
help='Object type, e.g. Network, VirtualMachine.')

# clone
Expand Down
59 changes: 59 additions & 0 deletions params.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'''
Command line option definitions
'''
def add_params(subparsers):
# list
list_parser = subparsers.add_parser('list',help='List VMware objects on your VMware server')

list_parser.add_argument('--type',
default='all',
help='Object type, e.g. Network, VirtualMachine.')

# clone
clone_parser = subparsers.add_parser('clone', help='Clone a VM template to a new VM')
clone_parser.add_argument('--server',
type=str,
help='vCenter server',
)
clone_parser.add_argument('--port',
type=str,
help='vCenter server port',
)
clone_parser.add_argument('--username',
type=str,
help='vCenter username',
)
clone_parser.add_argument('--password',
type=str,
help='vCenter password',
)
clone_parser.add_argument('--template',
type=str,
help='VM template name to clone from')
clone_parser.add_argument('--hostname',
type=str,
help='New host name',
)
clone_parser.add_argument('--ips',
type=str,
help='Static IPs of new host, separated by a space. List primary IP first.',
nargs='+',
)
clone_parser.add_argument('--cpus',
type=int,
help='Number of CPUs')
clone_parser.add_argument('--mem',
type=int,
help='Memory in GB')
clone_parser.add_argument('--domain',
type=str,
help='Domain, e.g. "example.com"',
)

# destroy
destroy_parser = subparsers.add_parser('destroy',
help='Destroy/delete a Virtual Machine')

destroy_parser.add_argument('--name',
required=True,
help='VM name (case-sensitive)')
7 changes: 5 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from distutils.core import setup
try:
from setuptools import setup
except ImportError:
from distutils.core import setup

setup(
name='ezmomi',
Expand All @@ -12,7 +15,7 @@
url='https://github.com/snobear/ezmomi',
license='LICENSE.txt',
description='VMware vSphere Command line tool',
long_description=open('README.txt').read(),
long_description=open('README.tx').read(),
install_requires=[
"PyYAML==3.11",
"argparse==1.2.1",
Expand Down

0 comments on commit 5610b35

Please sign in to comment.