From e8cba421e4d76ce3625efd551e6ffd5792b306fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Ole=C5=9B?= Date: Fri, 4 Sep 2015 10:42:29 +0000 Subject: [PATCH] Move riak examples to separate dir --- examples/riak/example-riaks-template.py | 119 ++++++++++++ examples/riak/example-riaks.py | 243 ++++++++++++++++++++++++ 2 files changed, 362 insertions(+) create mode 100644 examples/riak/example-riaks-template.py create mode 100644 examples/riak/example-riaks.py diff --git a/examples/riak/example-riaks-template.py b/examples/riak/example-riaks-template.py new file mode 100644 index 0000000..bf64392 --- /dev/null +++ b/examples/riak/example-riaks-template.py @@ -0,0 +1,119 @@ +#!/usr/bin/env python + +# WARNING: this might not be most up-to-date script and not all things might +# work here, for most up-to-date version see example-riaks.py +# This is just a demo of the template language of Solar + +from solar.interfaces.db import get_db +from solar.core import signals + +db = get_db() +db.clear() +signals.Connections.clear() + +from solar import template + + +nodes = template.nodes_from('templates/riak_nodes.yml') + +riak_services = nodes.on_each( + 'resources/riak_node', + { + 'riak_self_name': 'riak{num}', + 'riak_hostname': 'riak_server{num}.solar', + 'riak_name': 'riak{num}@riak_server{num}.solar', +}) + +slave_riak_services = riak_services.tail() + +riak_services.take(0).connect_list( + slave_riak_services, + { + 'riak_name': 'join_to', + } +) + +hosts_files = nodes.on_each('resources/hosts_file') + +riak_services.connect_list_to_each( + hosts_files, + { + 'ip': 'hosts_ips', + 'riak_hostname': 'hosts_names', + }, + events=False +) + + +hosts_files.add_deps('run/success', riak_services, 'run') +slave_riak_services.add_reacts('run/success', slave_riak_services, 'join') +slave_riak_services.add_reacts('leave/success', slave_riak_services, 'join') +slave_riak_services.add_react('run/success', riak_services.take(0), 'commit') + + +haproxy_services = nodes.on_each( + 'resources/haproxy_service' +) +haproxy_configs = nodes.on_each( + 'resources/haproxy_config' +) +haproxy_service_configs_http = nodes.on_each( + 'resources/haproxy_service_config', + { + 'listen_port': 8098, + 'protocol': 'http', + } +) +haproxy_service_configs_pb = nodes.on_each( + 'resources/haproxy_service_config', + { + 'listen_port': 8087, + 'protocol': 'tcp', + } +) + +riak_services.connect_list_to_each( + haproxy_service_configs_http, + { + 'riak_hostname': 'servers', + 'riak_port_http': 'ports', + } +) +riak_services.connect_list_to_each( + haproxy_service_configs_pb, + { + 'riak_hostname': 'servers', + 'riak_port_pb': 'ports', + } +) +haproxy_service_configs_http.connect_list( + haproxy_configs, + { + 'protocol': 'configs_protocols', + 'listen_port': 'listen_ports', + 'name': 'configs_names', + 'servers': 'configs', + 'ports': 'configs_ports', + } +) +haproxy_service_configs_pb.connect_list( + haproxy_configs, + { + 'protocol': 'configs_protocols', + 'listen_port': 'listen_ports', + 'name': 'configs_names', + 'servers': 'configs', + 'ports': 'configs_ports', + } +) +haproxy_configs.connect_list( + haproxy_services, + { + 'listen_ports': 'ports', + } +) + +nodes.add_reacts('run/success', haproxy_services, 'install') +haproxy_services.add_deps('install/success', haproxy_configs, 'run') +haproxy_configs.add_reacts('run/success', haproxy_services, 'run') +haproxy_configs.add_reacts('update/success', haproxy_services, 'update') diff --git a/examples/riak/example-riaks.py b/examples/riak/example-riaks.py new file mode 100644 index 0000000..28606f5 --- /dev/null +++ b/examples/riak/example-riaks.py @@ -0,0 +1,243 @@ +# To run: +# python example-riaks.py deploy +# python example-riaks.py add_haproxies +# solar changes stage +# solar changes process +# solar orch run-once last + + +import click +import sys + +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.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 + r = vr.create('riak_service%d' % num, + 'resources/riak_node', + {'riak_self_name': 'riak%d' % num, + 'riak_hostname': 'riak_server%d.solar' % num, + 'riak_name': 'riak%d@riak_server%d.solar' % (num, num)})[0] + riak_services.append(r) + + for i, riak in enumerate(riak_services): + signals.connect(nodes[i], riak) + + for i, riak in enumerate(riak_services[1:]): + signals.connect(riak_services[0], riak, {'riak_name': 'join_to'}, events=None) + + hosts_services = [] + for i, riak in enumerate(riak_services): + num = i + 1 + hosts_file = vr.create('hosts_file%d' % num, + 'resources/hosts_file', {})[0] + hosts_services.append(hosts_file) + signals.connect(nodes[i], hosts_file) + + for riak in riak_services: + for hosts_file in hosts_services: + signals.connect(riak, hosts_file, + {'riak_hostname': 'hosts_names', 'ip': 'hosts_ips'}, + events=False) + + has_errors = False + for r in locals().values(): + + # TODO: handle list + if not isinstance(r, resource.Resource): + continue + + # print 'Validating {}'.format(r.name) + local_errors = validation.validate_resource(r) + if local_errors: + has_errors = True + print 'ERROR: %s: %s' % (r.name, errors) + + if has_errors: + print "ERRORS" + sys.exit(1) + + events = [ + Dep('hosts_file1', 'run', 'success', 'riak_service1', 'run'), + Dep('hosts_file2', 'run', 'success', 'riak_service2', 'run'), + Dep('hosts_file3', 'run', 'success', 'riak_service3', 'run'), + + React('riak_service2', 'run', 'success', 'riak_service2', 'join'), + React('riak_service3', 'run', 'success', 'riak_service3', 'join'), + + # Dep('riak_service1', 'run', 'success', 'riak_service2', 'join'), + # Dep('riak_service1', 'run', 'success', 'riak_service3', 'join'), + + # React('riak_service2', 'join', 'error', 'riak_service2', 'leave'), + # React('riak_service3', 'join', 'error', 'riak_service3', 'leave'), + + React('riak_service2', 'leave', 'success', 'riak_service2', 'join'), + React('riak_service3', 'leave', 'success', 'riak_service3', 'join'), + + # React('riak_service2', 'leave', 'success', 'riak_service1', 'commit_leave'), + # React('riak_service3', 'leave', 'success', 'riak_service1', 'commit_leave'), + + # Dep('riak_service1', 'commit_leave', 'success', 'riak_service2', 'join'), + # Dep('riak_service1', 'commit_leave', 'success', 'riak_service3', 'join'), + + React('riak_service3', 'join', 'success', 'riak_service1', 'commit'), + React('riak_service2', 'join', 'success', 'riak_service1', 'commit') + ] + + for event in events: + add_event(event) + + print 'Use solar changes process & orch' + sys.exit(0) + + +def setup_haproxies(): + hps = [] + hpc = [] + hpsc_http = [] + hpsc_pb = [] + for i in xrange(3): + num = i + 1 + hps.append(vr.create('haproxy_service%d' % num, + 'resources/haproxy_service', + {})[0]) + hpc.append(vr.create('haproxy_config%d' % num, + 'resources/haproxy_config', + {})[0]) + hpsc_http.append(vr.create('haproxy_service_config_http%d' % num, + 'resources/haproxy_service_config', + {'listen_port': 8098, + 'protocol': 'http', + 'name': 'riak_haproxy_http%d' % num})[0]) + hpsc_pb.append(vr.create('haproxy_service_config_pb%d' % num, + 'resources/haproxy_service_config', + {'listen_port': 8087, + 'protocol': 'tcp', + 'name': 'riak_haproxy_pb%d' % num})[0]) + + riak1 = resource.load('riak_service1') + riak2 = resource.load('riak_service2') + riak3 = resource.load('riak_service3') + riaks = [riak1, riak2, riak3] + + for single_hpsc in hpsc_http: + for riak in riaks: + signals.connect(riak, single_hpsc, {'riak_hostname': 'servers', + 'riak_port_http': 'ports'}) + + for single_hpsc in hpsc_pb: + for riak in riaks: + signals.connect(riak, single_hpsc, {'riak_hostname': 'servers', + 'riak_port_pb': 'ports'}) + + # haproxy config to haproxy service + + for single_hpc, single_hpsc in zip(hpc, hpsc_http): + signals.connect(single_hpsc, single_hpc, {'protocol': 'configs_protocols', + 'listen_port': 'listen_ports', + 'name': 'configs_names', + 'servers': 'configs', + 'ports': 'configs_ports'}) + + for single_hpc, single_hpsc in zip(hpc, hpsc_pb): + signals.connect(single_hpsc, single_hpc, {'protocol': 'configs_protocols', + 'listen_port': 'listen_ports', + 'name': 'configs_names', + 'servers': 'configs', + 'ports': 'configs_ports'}) + + for single_hps, single_hpc in zip(hps, hpc): + signals.connect(single_hpc, single_hps, {'listen_ports': 'ports'}, + events=False) + + # assign haproxy services to each node + + node1 = resource.load('node1') + node2 = resource.load('node2') + node3 = resource.load('node3') + nodes = [node1, node2, node3] + + for single_node, single_hps in zip(nodes, hps): + signals.connect(single_node, single_hps) + + for single_node, single_hpc in zip(nodes, hpc): + signals.connect(single_node, single_hpc) + + has_errors = False + for r in locals().values(): + + # TODO: handle list + if not isinstance(r, resource.Resource): + continue + + # print 'Validating {}'.format(r.name) + local_errors = validation.validate_resource(r) + if local_errors: + has_errors = True + print 'ERROR: %s: %s' % (r.name, errors) + + if has_errors: + print "ERRORS" + sys.exit(1) + + events = [] + for node, single_hps, single_hpc in zip(nodes, hps, hpc): + # r = React(node.name, 'run', 'success', single_hps.name, 'install') + d = Dep(single_hps.name, 'run', 'success', single_hpc.name, 'run') + e1 = React(single_hpc.name, 'run', 'success', single_hps.name, 'apply_config') + e2 = React(single_hpc.name, 'update', 'success', single_hps.name, 'apply_config') + # events.extend([r, d, e1, e2]) + events.extend([d, e1, e2]) + + for event in events: + add_event(event) + + +@click.group() +def main(): + pass + + +@click.command() +def deploy(): + setup_riak() + +@click.command() +def add_haproxies(): + setup_haproxies() + + +@click.command() +def undeploy(): + raise NotImplemented("Not yet") + + +main.add_command(deploy) +main.add_command(undeploy) +main.add_command(add_haproxies) + + +if __name__ == '__main__': + main()