Add more commands to work with resources

- Clear resources storage

solar clear

- Prepare ansible directory with resources

solar prepare -a run -r node_1 docker_1

- Executed different actions

solar exec -a run -r node_1 docker_1 docker_compose_1 some_service_1
solar exec -a remove -r node_1 docker_1 docker_compose_1 some_service_1
This commit is contained in:
Dmitry Shulyak 2015-04-02 15:15:09 -07:00
parent 0a317effe0
commit 857f87bdd3
3 changed files with 44 additions and 18 deletions

View File

@ -5,8 +5,7 @@ type: resource
handler: ansible handler: ansible
version: v1 version: v1
input: input:
ssh_host: localhost ssh_host: 127.0.0.1
ssh_port: 2222 ssh_port: 2222
connection_type: local
name: first name: first
user: vagrant
key: .vagrant/machines/default/virtualbox/private_key

View File

@ -16,6 +16,10 @@
On create "golden" resource should be moved to special place On create "golden" resource should be moved to special place
""" """
import subprocess
from copy import deepcopy
import sys import sys
import textwrap import textwrap
@ -39,6 +43,7 @@ class Cmd(object):
description='Supported actions', description='Supported actions',
help='Provide of one valid actions') help='Provide of one valid actions')
self.register_actions() self.register_actions()
self.dbm = DirDBM('tmp/created/')
def register_actions(self): def register_actions(self):
parser = self.subparser.add_parser('create') parser = self.subparser.add_parser('create')
@ -48,11 +53,24 @@ class Cmd(object):
'--resource', '--resource',
required=True) required=True)
parser.add_argument( parser.add_argument(
'-u', '--uid', help='Identifier or resource' '-t', '--tags', nargs='+',
help='Identifier or resource'
) )
parser = self.subparser.add_parser('actions') parser = self.subparser.add_parser('prepare')
parser.set_defaults(func=getattr(self, 'actions')) parser.set_defaults(func=getattr(self, 'prepare'))
parser.add_argument(
'-a',
'--action',
required=True)
parser.add_argument(
'-r',
'--resources',
nargs='+',
required=True)
parser = self.subparser.add_parser('exec')
parser.set_defaults(func=getattr(self, 'execute'))
parser.add_argument( parser.add_argument(
'-a', '-a',
'--action', '--action',
@ -70,6 +88,9 @@ class Cmd(object):
'--resource', '--resource',
required=True) required=True)
parser = self.subparser.add_parser('clear')
parser.set_defaults(func=getattr(self, 'clear'))
def parse(self, args): def parse(self, args):
parsed = self.parser.parse_args(args) parsed = self.parser.parse_args(args)
return parsed.func(parsed) return parsed.func(parsed)
@ -78,21 +99,23 @@ class Cmd(object):
resource = args.resource resource = args.resource
storage = Storage.from_files('./schema/resources') storage = Storage.from_files('./schema/resources')
dbm = DirDBM('tmp/created/')
resource_uid = '{0}_{1}'.format(resource, args.uid) resource_uid = '{0}_{1}'.format(resource, '_'.join(args.tags))
dbm[resource_uid] = yaml.dump(storage.get(resource), data = deepcopy(storage.get(resource))
default_flow_style=False) data['tags'] = args.tags
self.dbm[resource_uid] = yaml.dump(
data, default_flow_style=False)
def clear(self, args):
self.dbm.clear()
def show(self, args): def show(self, args):
dbm = DirDBM('tmp/created/') print self.dbm[args.resource]
print dbm[args.resource]
def prepare(self, args):
def actions(self, args):
storage = Storage.from_files('./schema/resources')
orch = ansible.AnsibleOrchestration( orch = ansible.AnsibleOrchestration(
[storage.get(r) for r in args.resources]) [yaml.load(self.dbm[r]) for r in args.resources])
utils.create_dir('tmp/group_vars') utils.create_dir('tmp/group_vars')
with open('tmp/hosts', 'w') as f: with open('tmp/hosts', 'w') as f:
@ -106,7 +129,12 @@ class Cmd(object):
yaml.dump(getattr(orch, args.action)(), yaml.dump(getattr(orch, args.action)(),
default_flow_style=False)) default_flow_style=False))
def execute(self, args):
self.prepare(args)
sub = subprocess.Popen(
['ansible-playbook', '-i', 'tmp/hosts', 'tmp/main.yml'])
out, err = sub.communicate()
print out
def main(): def main():
api = Cmd() api = Cmd()

View File

@ -8,8 +8,7 @@ from jinja2 import Template
ANSIBLE_INVENTORY = """ ANSIBLE_INVENTORY = """
{% for node in nodes %} {% for node in nodes %}
{{node.node.name}} ansible_ssh_host={{node.node.ssh_host}} ansible_ssh_port={{node.node.ssh_port}} {{node.node.name}} ansible_ssh_host={{node.node.ssh_host}} ansible_connection={{node.node.connection_type}}
{% endfor %} {% endfor %}
{% for res in resources %} {% for res in resources %}