This commit is contained in:
Jedrzej Nowak 2015-08-14 15:01:18 +02:00
parent 85f18de8b1
commit f18f8d2e02
11 changed files with 272 additions and 0 deletions

146
example-riaks.py Normal file
View File

@ -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()

View File

@ -0,0 +1,4 @@
- hosts: [{{ip}}]
sudo: yes
tasks:
- shell: riak-admin cluster commit

View File

@ -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: []

View File

@ -0,0 +1,4 @@
- hosts: [{{ip}}]
sudo: yes
tasks:
- shell: riak-admin cluster join {{join_to}}

View File

@ -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:

View File

@ -0,0 +1,4 @@
- hosts: [{{ip}}]
sudo: yes
tasks:
- shell: riak-admin cluster commit

View File

@ -0,0 +1,6 @@
- hosts: [{{ip}}]
sudo: yes
tasks:
- apt:
name: riak
state: absent

View File

@ -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

View File

@ -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:

View File

@ -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))

20
templates/riak_nodes.yml Normal file
View File

@ -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'