Skip to content
This repository has been archived by the owner on Dec 28, 2024. It is now read-only.

Commit

Permalink
Merge branch 'devel'
Browse files Browse the repository at this point in the history
  • Loading branch information
spencergibb committed Jan 31, 2014
2 parents a27c6c2 + cd640d6 commit d49ba5c
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 180 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.py[cod]
test/testconfig/cache
test/test_cache
MANIFEST

# C extensions
Expand Down
30 changes: 26 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ Built on and for macs, but should be usable on Linux

sudo pip install https://github.com/32degrees/battleschool/releases/download/v0.x.0/battleschool-0.x.0.tar.gz

### running battleschool for the first time

battle --config-file http://somesite/path/to/your/config.yml

As long as you config.yml doesn't have a source.local section (see configuration below), you don't need to download or
create a configuration for the first time.

### running battleschool

`battle`
Expand All @@ -33,6 +40,10 @@ put the following in ~/.battleschool/config.yml and uncomment the items you want
local:
#- playbook.yml

url:
#- name: playbook.yml
# url: https://db.tt/VcyI9dvr

git:
- name: 'osx'
repo: 'https://github.com/spencergibb/ansible-osx'
Expand All @@ -57,6 +68,7 @@ put the following in ~/.battleschool/config.yml and uncomment the items you want
#- xtra-finder.yml

[Here is my config.yml](https://db.tt/aG2uyydU)
[Here is my playbook.yml](https://db.tt/VcyI9dvr)

### explanation of ~/.battleschool/config.yml

Expand All @@ -70,6 +82,15 @@ Any [ansible playbooks](http://www.ansibleworks.com/docs/#playbooks) located in
can be listed under local. Each playbook will be executed in order. This can useful for custom
configuration per workstation. (You could install apps with homebrew or macports if those are installed, for example)

#### url sources

url:
- name: playbook.yml
url: https://db.tt/VcyI9dvr

Playbooks located at a url. Each playbook will be executed in order. Helpful for bootstrapping (ie, the first time
you run battleschool.

#### git sources

git:
Expand Down Expand Up @@ -159,7 +180,10 @@ Force update of the playbooks from a VCS such as git
of the changes that may occur
--config-dir=CONFIG_DIR
config directory for battleschool
(default=~/.battleschool)
(default=/Users/spencergibb/.battleschool)
--config-file=CONFIG_FILE
config file for battleschool
(default=/Users/spencergibb/.battleschool/config.yml)
-c CONNECTION, --connection=CONNECTION
connection type to use (default=smart)
-D, --diff when changing (small) files and templates, show the
Expand Down Expand Up @@ -192,7 +216,7 @@ Force update of the playbooks from a VCS such as git
-T TIMEOUT, --timeout=TIMEOUT
override the SSH timeout in seconds (default=10)
-t TREE, --tree=TREE log output to this directory
-u, --update-vcs update playbooks from a version control system (vcs)
-u, --update-source update playbooks from sources(git, url, etc...)
-v, --verbose verbose mode (-vvv for more, -vvvv to enable
connection debugging)
--version show program's version number and exit
Expand All @@ -203,6 +227,4 @@ TODO: cleanup cli output

TODO: more docs

TODO: submit mac port?

TODO: on a mac make cache dir go to ~/Library/Caches/Battleschool
103 changes: 81 additions & 22 deletions bin/battle
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#!/usr/bin/env python
#/opt/local/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python

#export PYTHONPATH=/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

import os
import sys
Expand All @@ -18,17 +15,22 @@ import ansible.utils.template
from ansible import callbacks
from ansible import errors
from ansible import utils
from ansible.callbacks import display

from battleschool.printing import *
from battleschool.source.git import Git
from battleschool.source.local import Local
from battleschool.source.url import Url

from copy import deepcopy
from tempfile import gettempdir
from urlparse import urlparse


#TODO: verify environment: macports, ansible
#TODO: verify environment: ansible

def getSourceHandlers():
handlers = [Git, Local]
handlers = [Git, Local, Url]
# TODO: auto load sources
# for name, obj in inspect.getmembers(sourcepkg):
# if inspect.ismodule(obj):
# package = obj.__dict__['__package__']
Expand All @@ -40,13 +42,41 @@ def getSourceHandlers():
return handlers


def load_config_path(options, inventory, sshpass, sudopass):
if options.config_file:
config_file = options.config_file
parse_result = urlparse(config_file)
if parse_result.scheme:
url_options = deepcopy(options)
url_options.update_sources = True
url_options.cache_dir = gettempdir()
name = 'downloaded_config.yml'
sources = {
'url': [
{'name': name, 'url': config_file}
]
}
display(banner("Downloading config from url"))
url = Url(url_options, sources)
files = url.run(inventory, sshpass, sudopass)
return files[0]
else:
return config_file

return "%s/config.yml" % options.config_dir


def main(args):

battleschool_dir = "%s/.battleschool" % os.environ['HOME']

# TODO: make battle OO or more modular
#-----------------------------------------------------------
# make ansible defaults, battleschool defaults
AC.DEFAULT_HOST_LIST = C.DEFAULT_HOST_LIST
AC.DEFAULT_SUDO_FLAGS = C.DEFAULT_SUDO_FLAGS

#-----------------------------------------------------------
# create parser for CLI options
usage = "%prog"
parser = utils.base_parser(
Expand All @@ -73,18 +103,24 @@ def main(args):
dest='sudo', help="run operations with sudo (nopasswd)")
parser.add_option('--config-dir', dest='config_dir', default=None,
help="config directory for battleschool (default=%s)" % battleschool_dir)
parser.add_option('-u', '--update-vcs', dest='update_vcs', default=False, action='store_true',
help="update playbooks from a version control system (vcs)")
parser.add_option('--config-file', dest='config_file', default=None,
help="config file for battleschool (default=%s/%s)" % (battleschool_dir, "config.yml"))
parser.add_option('-u', '--update-source', dest='update_sources', default=False, action='store_true',
help="update playbooks from sources(git, url, etc...)")

options, args = parser.parse_args(args)

playbooks_to_run = [C.DEFAULT_PLAYBOOK]

#-----------------------------------------------------------
# setup inventory
inventory = ansible.inventory.Inventory(options.inventory)
inventory.subset(options.subset)
if len(inventory.list_hosts()) == 0:
raise errors.AnsibleError("provided hosts list is empty")

#-----------------------------------------------------------
# setup default options
sshpass = None
sudopass = None
options.remote_user = AC.DEFAULT_REMOTE_USER
Expand All @@ -101,33 +137,54 @@ def main(args):
extra_vars = utils.parse_kv(options.extra_vars)
only_tags = options.tags.split(",")

#-----------------------------------------------------------
# setup config_dir and battleschool_dir
if options.config_dir:
battleschool_dir = options.config_dir
else:
options.config_dir = battleschool_dir

config_data = utils.parse_yaml_from_file("%s/config.yml" % battleschool_dir)
options.cache_dir = "%s/cache" % battleschool_dir
#-----------------------------------------------------------
# setup module_path
if options.module_path is None:
options.module_path = AC.DEFAULT_MODULE_PATH

if C.DEFAULT_MODULE_PATH not in options.module_path:
options.module_path = "%s:%s" % (C.DEFAULT_MODULE_PATH, options.module_path)

#-----------------------------------------------------------
# parse config data
config_path = load_config_path(options, inventory, sshpass, sudopass)
config_data = utils.parse_yaml_from_file(config_path)

output = subprocess.check_output(["/usr/bin/sw_vers", "-productVersion"])
mac_version = output.split()[-1].split(".")
#-----------------------------------------------------------
# set config_dir
if "cache_dir" in config_data:
options.cache_dir = os.path.expanduser(config_data["cache_dir"])
else:
options.cache_dir = "%s/cache" % battleschool_dir

#-----------------------------------------------------------
# setup extra_vars for later use
if extra_vars is None:
extra_vars = dict()

extra_vars['battleschool_config_dir'] = battleschool_dir
extra_vars['battleschool_cache_dir'] = options.cache_dir
extra_vars['mac_version'] = mac_version
extra_vars['mac_major_minor_version'] = "%s.%s" % (mac_version[0], mac_version[1])

#-----------------------------------------------------------
# set mac_version for extra_vars
sw_vers = "/usr/bin/sw_vers"
if os.path.exists(sw_vers) and os.path.isfile(sw_vers):
output = subprocess.check_output([sw_vers, "-productVersion"])
mac_version = output.split()[-1].split(".")
extra_vars['mac_version'] = mac_version
extra_vars['mac_major_minor_version'] = "%s.%s" % (mac_version[0], mac_version[1])

#-----------------------------------------------------------
# setup and run source handlers
handlers = getSourceHandlers()

if options.module_path is None:
options.module_path = AC.DEFAULT_MODULE_PATH

if C.DEFAULT_MODULE_PATH not in options.module_path:
options.module_path = "%s:%s" % (C.DEFAULT_MODULE_PATH, options.module_path)

if 'sources' in config_data:
display(banner("Updating sources"))
sources = config_data['sources']
Expand All @@ -138,14 +195,16 @@ def main(args):
for playbook in playbooks:
playbooks_to_run.append(playbook)

#-----------------------------------------------------------
# validate playbooks
for playbook in playbooks_to_run:
if not os.path.exists(playbook):
raise errors.AnsibleError("the playbook: %s could not be found" % playbook)
if not os.path.isfile(playbook):
raise errors.AnsibleError("the playbook: %s does not appear to be a file" % playbook)


# run all playbooks specified on the command line
#-----------------------------------------------------------
# run all playbooks specified from config
for playbook in playbooks_to_run:
stats = callbacks.AggregateStats()

Expand Down
95 changes: 0 additions & 95 deletions bin/battle-bootstrap

This file was deleted.

2 changes: 1 addition & 1 deletion lib/battleschool/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = '0.1.0'
__version__ = '0.2.1'
__author__ = 'Spencer Gibb'
Loading

0 comments on commit d49ba5c

Please sign in to comment.