5
from pymongo import MongoClient
7
#########################################################
9
#########################################################
13
#########################################################
14
# 3shard cluster configuration
15
#########################################################
16
d = amulet.Deployment(series='trusty')
18
d.add('configsvr', charm='mongodb', units=scale)
19
d.add('mongos', charm='mongodb', units=scale)
20
d.add('shard1', charm='mongodb', units=scale)
21
d.add('shard2', charm='mongodb', units=scale)
24
d.configure('configsvr', {'replicaset': 'configsvr'})
26
# define each shardset
27
d.configure('shard1', {'replicaset': 'shard1'})
28
d.configure('shard2', {'replicaset': 'shard2'})
30
d.configure('mongos', {})
32
#Connect the config servers to mongo shell
33
d.relate('configsvr:configsvr', 'mongos:mongos-cfg')
35
#connect each shard to the mongo shell
36
d.relate('mongos:mongos', 'shard1:database')
37
d.relate('mongos:mongos', 'shard2:database')
41
# Perform the setup for the deployment.
44
d.sentry.wait(seconds)
45
except amulet.helpers.TimeoutError:
46
message = 'The environment did not setup in %d seconds.', seconds
47
amulet.raise_status(amulet.SKIP, msg=message)
52
'config-sentry': d.sentry.unit['configsvr/0'],
53
'mongos-sentry': d.sentry.unit['mongos/0'],
54
'shard1-sentry': d.sentry.unit['shard1/0'],
55
'shard2-sentry': d.sentry.unit['shard2/0']
59
#############################################################
60
# Check presence of MongoDB GUI HEALTH Status
61
#############################################################
62
def validate_status_interface():
63
r = requests.get("http://{}:28017".format(
64
sentry_dict['config-sentry'].info['public-address']),
69
#############################################################
70
# Validate that each unit has an active mongo service
71
#############################################################
72
def validate_running_services():
73
for service in sentry_dict:
74
output = sentry_dict[service].run('service mongodb status')
75
service_active = str(output).find('mongodb start/running')
76
if service_active == -1:
77
message = "Failed to find running MongoDB on host {}".format(service)
78
amulet.raise_status(amulet.SKIP, msg=message)
81
#############################################################
82
# Validate connectivity from $WORLD
83
#############################################################
84
def validate_world_connectivity():
85
client = MongoClient(d.sentry.unit['mongos/0'].info['public-address'])
88
#Can we successfully insert?
89
insert_id = db.amulet.insert({'assert': True})
91
amulet.raise_status(amulet.FAIL, msg="Failed to insert test data")
92
#Can we delete from a shard using the Mongos hub?
93
result = db.amulet.remove(insert_id)
94
if result['err'] is not None:
95
amulet.raise_status(amulet.FAIL, msg="Failed to remove test data")
98
#############################################################
99
# Validate relationships
100
#############################################################
101
#broken pending 1273312
102
def validate_relationships():
103
d.sentry.unit['configsvr/0'].relation('configsvr', 'mongos:mongos-cfg')
104
d.sentry.unit['shard1/0'].relation('database', 'mongos:mongos')
105
d.sentry.unit['shard2/0'].relation('database', 'mongos:mongos')
106
print(d.sentry.unit['shard1/0'].relation('database', 'mongos:mongos'))
109
def validate_manual_connection():
110
output, code = d.sentry.unit['shard1/0'].run("mongo {}".format(
111
d.sentry.unit['mongos/0'].info['public-address']))
113
message = "Manual Connection failed for unit shard1"
114
amulet.raise_status(amulet.SKIP, msg=message)
116
output, code = d.sentry.unit['shard2/0'].run("mongo {}".format(
117
d.sentry.unit['mongos/0'].info['public-address']))
119
message = "Manual Connection failed for unit shard2"
120
amulet.raise_status(amulet.SKIP, msg=message)
123
validate_status_interface()
124
validate_running_services()
125
#validate_relationships()
126
validate_manual_connection()
127
validate_world_connectivity()