From f18f8d2e02d5dcc307fe25dbbdf1fcafc5854b26 Mon Sep 17 00:00:00 2001 From: Jedrzej Nowak Date: Fri, 14 Aug 2015 15:01:18 +0200 Subject: [PATCH] Idea 1 --- example-riaks.py | 146 ++++++++++++++++++++ resources/riak_commit/actions/commit.yml | 4 + resources/riak_commit/meta.yaml | 18 +++ resources/riak_join_single/actions/join.yml | 4 + resources/riak_join_single/meta.yaml | 18 +++ resources/riak_node/actions/commit.yml | 4 + resources/riak_node/actions/remove.yml | 6 + resources/riak_node/actions/run.yml | 25 ++++ resources/riak_node/meta.yaml | 17 +++ solar/solar/core/handlers/python.py | 10 ++ templates/riak_nodes.yml | 20 +++ 11 files changed, 272 insertions(+) create mode 100644 example-riaks.py create mode 100644 resources/riak_commit/actions/commit.yml create mode 100644 resources/riak_commit/meta.yaml create mode 100644 resources/riak_join_single/actions/join.yml create mode 100644 resources/riak_join_single/meta.yaml create mode 100644 resources/riak_node/actions/commit.yml create mode 100644 resources/riak_node/actions/remove.yml create mode 100644 resources/riak_node/actions/run.yml create mode 100644 resources/riak_node/meta.yaml create mode 100644 solar/solar/core/handlers/python.py create mode 100644 templates/riak_nodes.yml diff --git a/example-riaks.py b/example-riaks.py new file mode 100644 index 00000000..d6aaf947 --- /dev/null +++ b/example-riaks.py @@ -0,0 +1,146 @@ +import click +import sys +import time + +from solar.core import actions +from solar.core import resource +from solar.core import signals +from solar.core import validation +from solar.core.resource import virtual_resource as vr +from solar import errors + +from solar.interfaces.db import get_db + +from solar import events as evapi +from solar.events.controls import React, Dep +from solar.events.api import add_event + + +db = get_db() + + +def setup_riak(): + db.clear() + signals.Connections.clear() + + nodes = vr.create('nodes', 'templates/riak_nodes.yml', {}) + node1, node2, node3 = nodes + + riak_services = [] + ips = '10.0.0.%d' + for i in xrange(3): + num = i + 1 + ip = ips % (num + 2) # XXX: da rade inaczej ? + r = vr.create('riak_service%d' % num, + 'resources/riak_node', + {'riak_name': 'riak%d@%s' % (num, ip)})[0] + riak_services.append(r) + + for i, riak in enumerate(riak_services): + signals.connect(nodes[i], riak) + + joiners = [] + for i in xrange(2): + num = i + 1 + join = vr.create('riak_join_single%d' % num, + 'resources/riak_join_single', {})[0] + joiners.append(join) + + for i, riak in enumerate(riak_services[:-1]): + signals.connect(nodes[i+1], joiners[i]) + signals.connect(riak, joiners[i], {'riak_name': 'join_to'}) + # signals.connect(riak, riak_joiner_service, {'riak_name': 'join_to'}) + # signals.connect(riak, riak_joiner_service, {'ip': 'join_from'}) + + commiter = vr.create('riak_commit1', 'resources/riak_commit', {})[0] + # for joiner in joiners: + # signals.connect(joiner, commiter, {'join_to': 'riak_names'}) + signals.connect(node1, commiter) + + has_errors = False + for r in locals().values(): + + # TODO: handle list + if not isinstance(r, resource.Resource): + continue + + # print 'Validating {}'.format(r.name) + errors = validation.validate_resource(r) + if errors: + has_errors = True + print 'ERROR: %s: %s' % (r.name, errors) + + if has_errors: + print "ERRORS" + sys.exit(1) + + events = [ + React('riak_service2', 'run', 'success', 'riak_join_single1', 'join'), + React('riak_service3', 'run', 'success', 'riak_join_single2', 'join'), + React('riak_join_single1', 'join', 'success', 'riak_commit1', 'commit'), + React('riak_join_single2', 'join', 'success', 'riak_commit1', 'commit') + ] + + for event in events: + add_event(event) + + print 'Use orch' + sys.exit(1) + + +resources_to_run = [ + 'riak_service1', + 'riak_service2', + 'riak_service3', + # 'riak_join_single1', + # 'riak_join_single2', + # 'riak_commit1' +] + + + +@click.group() +def main(): + pass + + +@click.command() +def deploy(): + setup_riak() + + resources = map(resource.wrap_resource, db.get_list(collection=db.COLLECTIONS.resource)) + resources = {r.name: r for r in resources} + + for name in resources_to_run: + try: + actions.resource_action(resources[name], 'run') + except errors.SolarError as e: + print 'WARNING: %s' % str(e) + raise + + time.sleep(10) + + +@click.command() +def undeploy(): + resources = map(resource.wrap_resource, db.get_list(collection=db.COLLECTIONS.resource)) + resources = {r.name: r for r in resources} + + for name in reversed(resources_to_run): + try: + actions.resource_action(resources[name], 'remove') + except errors.SolarError as e: + print 'WARNING: %s' % str(e) + + db.clear() + + signals.Connections.clear() + + + +main.add_command(deploy) +main.add_command(undeploy) + + +if __name__ == '__main__': + main() diff --git a/resources/riak_commit/actions/commit.yml b/resources/riak_commit/actions/commit.yml new file mode 100644 index 00000000..7a7a999c --- /dev/null +++ b/resources/riak_commit/actions/commit.yml @@ -0,0 +1,4 @@ +- hosts: [{{ip}}] + sudo: yes + tasks: + - shell: riak-admin cluster commit diff --git a/resources/riak_commit/meta.yaml b/resources/riak_commit/meta.yaml new file mode 100644 index 00000000..576f931a --- /dev/null +++ b/resources/riak_commit/meta.yaml @@ -0,0 +1,18 @@ +id: riak_commit +handler: ansible +version: 1.0.0 +actions: + commit: actions/commit.yml +input: + ip: + schema: str! + value: + ssh_key: + schema: str! + value: + ssh_user: + schema: str! + value: + riak_names: + schema: [{riak_name: str}] + value: [] diff --git a/resources/riak_join_single/actions/join.yml b/resources/riak_join_single/actions/join.yml new file mode 100644 index 00000000..501d16f4 --- /dev/null +++ b/resources/riak_join_single/actions/join.yml @@ -0,0 +1,4 @@ +- hosts: [{{ip}}] + sudo: yes + tasks: + - shell: riak-admin cluster join {{join_to}} diff --git a/resources/riak_join_single/meta.yaml b/resources/riak_join_single/meta.yaml new file mode 100644 index 00000000..f9d65034 --- /dev/null +++ b/resources/riak_join_single/meta.yaml @@ -0,0 +1,18 @@ +id: riak_join_single +handler: ansible +version: 1.0.0 +actions: + join: actions/join.yml +input: + join_to: + schema: str! + value: + ip: + schema: str! + value: + ssh_key: + schema: str! + value: + ssh_user: + schema: str! + value: diff --git a/resources/riak_node/actions/commit.yml b/resources/riak_node/actions/commit.yml new file mode 100644 index 00000000..7a7a999c --- /dev/null +++ b/resources/riak_node/actions/commit.yml @@ -0,0 +1,4 @@ +- hosts: [{{ip}}] + sudo: yes + tasks: + - shell: riak-admin cluster commit diff --git a/resources/riak_node/actions/remove.yml b/resources/riak_node/actions/remove.yml new file mode 100644 index 00000000..98fb0e40 --- /dev/null +++ b/resources/riak_node/actions/remove.yml @@ -0,0 +1,6 @@ +- hosts: [{{ip}}] + sudo: yes + tasks: + - apt: + name: riak + state: absent diff --git a/resources/riak_node/actions/run.yml b/resources/riak_node/actions/run.yml new file mode 100644 index 00000000..5f91f982 --- /dev/null +++ b/resources/riak_node/actions/run.yml @@ -0,0 +1,25 @@ +- hosts: [{{ip}}] + sudo: yes + tasks: + - shell: curl -s https://packagecloud.io/install/repositories/basho/riak/script.deb.sh | sudo bash + - apt: + name: riak + state: present + # - shell: sed -ie "s/127.0.0.1/{{ip}}/g" /etc/riak/riak.conf + - service: + name: riak + state: stopped + - replace: + dest: /etc/riak/riak.conf + regexp: '127.0.0.1' + replace: '0.0.0.0' + - replace: + dest: /etc/riak/riak.conf + regexp: '^nodename = .*\n' + replace: 'nodename = {{riak_name}}\n\n' + # TODO: should use name/hostname instead of ip + - shell: rm -fr /var/lib/riak/kv_vnode/* + - shell: rm -fr /var/lib/riak/ring/* + - service: + name: riak + state: reloaded diff --git a/resources/riak_node/meta.yaml b/resources/riak_node/meta.yaml new file mode 100644 index 00000000..94e386e2 --- /dev/null +++ b/resources/riak_node/meta.yaml @@ -0,0 +1,17 @@ +id: riak_node +handler: ansible +version: 1.0.0 +actions: +input: + ip: + schema: str! + value: + ssh_key: + schema: str! + value: + ssh_user: + schema: str! + value: + riak_name: + schema: str! + value: diff --git a/solar/solar/core/handlers/python.py b/solar/solar/core/handlers/python.py new file mode 100644 index 00000000..246c7f4b --- /dev/null +++ b/solar/solar/core/handlers/python.py @@ -0,0 +1,10 @@ +# -*- coding: utf-8 -*- +from fabric import api as fabric_api + +from solar.core.handlers.base import TempFileHandler + + +class Python(TempFileHandler): + def action(self, resource, action_name): + action_file = self._compile_action_file(resource, action_name) + fabric_api.local('python {}'.format(action_file)) diff --git a/templates/riak_nodes.yml b/templates/riak_nodes.yml new file mode 100644 index 00000000..97e0c093 --- /dev/null +++ b/templates/riak_nodes.yml @@ -0,0 +1,20 @@ +id: simple_riak_cluster +resources: + - id: node1 + from: resources/ro_node + values: + ip: '10.0.0.3' + ssh_key: '/vagrant/.vagrant/machines/solar-dev1/virtualbox/private_key' + ssh_user: 'vagrant' + - id: node2 + from: resources/ro_node + values: + ip: '10.0.0.4' + ssh_key: '/vagrant/.vagrant/machines/solar-dev2/virtualbox/private_key' + ssh_user: 'vagrant' + - id: node3 + from: resources/ro_node + values: + ip: '10.0.0.5' + ssh_key: '/vagrant/.vagrant/machines/solar-dev3/virtualbox/private_key' + ssh_user: 'vagrant'