Created
August 21, 2014 18:58
-
-
Save marcoceppi/60f43803482ef33d306a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import argparse | |
import subprocess | |
from amulet.helpers import juju | |
def get_parser(params=None): | |
parser = argparse.ArgumentParser(description='Juju run with relation contexts') | |
parser.add_argument('-r', '--relation', | |
help='Relation or relation id which to run the command under') | |
parser.add_argument('--remote-unit', help='Remote unit for relation environment') | |
parser.add_argument('--unit', help='Remote unit for relation environment') | |
parser.add_argument('--description', help=argparse.SUPPRESS, action='store_true') | |
return parser | |
def relation_ids(rel, unit): | |
return juju(['run', '--unit', unit, 'relation-ids %s' % rel]).split() | |
def remote_units(rel_id, unit): | |
return juju(['run', '--unit', unit, 'relation-list -r %s' % rel_id]).split() | |
def inject_env(keys, command): | |
env = ' '.join(['='.join(k) for k in keys.iteritems()]) | |
return "%s %s" % (env, command) | |
def main(): | |
p = get_parser() | |
args, extra = p.parse_known_args() | |
if args.description: | |
print('Juju run with relation context') | |
return | |
if not args.description and not args.relation and not args.unit: | |
return p.print_help() | |
if not args.relation: | |
p.error('relation is required') | |
if not args.unit: | |
p.error('unit is required') | |
if ':' in args.relation: | |
relation_id = args.relation | |
relation = args.relation.split(':')[0] | |
else: | |
relation = args.relation | |
relids = relation_ids(relation, args.unit) | |
if not relids: | |
raise Exception('No relation found') | |
if len(relids) > 1: | |
raise Exception('Disambiguate: %s' % ', '.join(relids)) | |
relation_id = relids[0] | |
units = remote_units(relation_id, args.unit) | |
if not args.remote_unit: | |
if not units: | |
raise Exception('Some weird shit') | |
if len(units) > 1: | |
raise Exception('Disambiguate: %s' % ', '.join(units)) | |
remote_unit = units[0] | |
else: | |
if args.remote_unit not in units: | |
raise Exception('%s is not one of the following: %s' % ', '.join(units)) | |
remote_unit = args.remote_unit | |
extra[-1] = inject_env({'JUJU_RELATION_ID': relation_id, 'JUJU_REMOTE_UNIT': remote_unit}, extra[-1]) | |
# Thank god we have all this shit sorted now. | |
print(juju(['run', '--unit', args.unit] + extra)) | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
marco@home:~$ juju pprint | |
- juju-gui/0: 10.0.3.205 (started) 80/tcp, 443/tcp | |
- mongo-config/0: 10.0.3.155 (started) 27017/tcp, 27021/tcp, 28017/tcp | |
- shard2/0: 10.0.3.231 (started) 27017/tcp, 27019/tcp, 27021/tcp, 28017/tcp | |
- mongos/0: 10.0.3.43 (started) 27017/tcp, 27019/tcp, 28017/tcp | |
- shard1/4: 10.0.3.87 (started) 27017/tcp, 27019/tcp, 27021/tcp, 28017/tcp | |
- shard1/0: 10.0.3.118 (started) 27017/tcp, 27019/tcp, 27021/tcp, 28017/tcp | |
- shard1/1: 10.0.3.24 (started) 27017/tcp, 27019/tcp, 27021/tcp, 28017/tcp | |
- shard1/2: 10.0.3.126 (started) 27017/tcp, 27019/tcp, 27021/tcp, 28017/tcp | |
- shard1/3: 10.0.3.12 (started) 27017/tcp, 27019/tcp, 27021/tcp, 28017/tcp | |
marco@home:~$ juju rrun --relation mongos --unit mongos/0 "relation-get hostname" | |
Exception: Disambiguate: mongos:10, mongos:3 | |
ERROR subprocess encountered error code 1 | |
marco@home:~$ juju rrun --relation mongos:10 --unit mongos/0 "relation-get hostname" | |
..THIS WOULD WORK IF JUJU-CORE DIDN'T HATE ME | |
marco@home:~$ juju rrun --relation mongos:3 --unit mongos/0 "relation-get hostname" | |
Exception: Disambiguate: shard1/0, shard1/1, shard1/2, shard1/3, shard1/4 | |
ERROR subprocess encountered error code 1 | |
marco@home:~$ juju rrun --relation mongos:3 --remote-unit=shard1/0 --unit mongos/0 "relation-get hostname" | |
..THIS WOULD WORK, YOU KNOW, ONE DAY |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
environment: local | |
machines: | |
"0": | |
agent-state: started | |
agent-version: 1.20.1.1 | |
dns-name: localhost | |
instance-id: localhost | |
series: trusty | |
state-server-member-status: has-vote | |
"1": | |
agent-state: started | |
agent-version: 1.20.1.1 | |
dns-name: 10.0.3.155 | |
instance-id: marco-local-machine-1 | |
series: trusty | |
hardware: arch=amd64 | |
"2": | |
agent-state: started | |
agent-version: 1.20.1.1 | |
dns-name: 10.0.3.43 | |
instance-id: marco-local-machine-2 | |
series: trusty | |
hardware: arch=amd64 | |
"3": | |
agent-state: started | |
agent-version: 1.20.1.1 | |
dns-name: 10.0.3.205 | |
instance-id: marco-local-machine-3 | |
series: trusty | |
hardware: arch=amd64 | |
"4": | |
agent-state: started | |
agent-version: 1.20.1.1 | |
dns-name: 10.0.3.118 | |
instance-id: marco-local-machine-4 | |
series: trusty | |
hardware: arch=amd64 | |
"5": | |
agent-state: started | |
agent-version: 1.20.1.1 | |
dns-name: 10.0.3.24 | |
instance-id: marco-local-machine-5 | |
series: trusty | |
hardware: arch=amd64 | |
"6": | |
agent-state: started | |
agent-version: 1.20.1.1 | |
dns-name: 10.0.3.126 | |
instance-id: marco-local-machine-6 | |
series: trusty | |
hardware: arch=amd64 | |
"7": | |
agent-state: started | |
agent-version: 1.20.1.1 | |
dns-name: 10.0.3.12 | |
instance-id: marco-local-machine-7 | |
series: trusty | |
hardware: arch=amd64 | |
"8": | |
agent-state: started | |
agent-version: 1.20.1.1 | |
dns-name: 10.0.3.87 | |
instance-id: marco-local-machine-8 | |
series: trusty | |
hardware: arch=amd64 | |
"9": | |
agent-state: started | |
agent-version: 1.20.1.1 | |
dns-name: 10.0.3.231 | |
instance-id: marco-local-machine-9 | |
series: trusty | |
hardware: arch=amd64 | |
services: | |
juju-gui: | |
charm: cs:trusty/juju-gui-5 | |
exposed: false | |
units: | |
juju-gui/0: | |
agent-state: started | |
agent-version: 1.20.1.1 | |
machine: "3" | |
open-ports: | |
- 80/tcp | |
- 443/tcp | |
public-address: 10.0.3.205 | |
mongo-config: | |
charm: cs:trusty/mongodb-3 | |
exposed: false | |
relations: | |
configsvr: | |
- mongos | |
replica-set: | |
- mongo-config | |
units: | |
mongo-config/0: | |
agent-state: started | |
agent-version: 1.20.1.1 | |
machine: "1" | |
open-ports: | |
- 27017/tcp | |
- 27021/tcp | |
- 28017/tcp | |
public-address: 10.0.3.155 | |
mongos: | |
charm: cs:trusty/mongodb-3 | |
exposed: true | |
relations: | |
mongos: | |
- shard1 | |
- shard2 | |
mongos-cfg: | |
- mongo-config | |
replica-set: | |
- mongos | |
units: | |
mongos/0: | |
agent-state: started | |
agent-version: 1.20.1.1 | |
machine: "2" | |
open-ports: | |
- 27017/tcp | |
- 27019/tcp | |
- 28017/tcp | |
public-address: 10.0.3.43 | |
shard1: | |
charm: cs:trusty/mongodb-3 | |
exposed: false | |
relations: | |
database: | |
- mongos | |
replica-set: | |
- shard1 | |
units: | |
shard1/0: | |
agent-state: started | |
agent-version: 1.20.1.1 | |
machine: "4" | |
open-ports: | |
- 27017/tcp | |
- 27019/tcp | |
- 27021/tcp | |
- 28017/tcp | |
public-address: 10.0.3.118 | |
shard1/1: | |
agent-state: started | |
agent-version: 1.20.1.1 | |
machine: "5" | |
open-ports: | |
- 27017/tcp | |
- 27019/tcp | |
- 27021/tcp | |
- 28017/tcp | |
public-address: 10.0.3.24 | |
shard1/2: | |
agent-state: started | |
agent-version: 1.20.1.1 | |
machine: "6" | |
open-ports: | |
- 27017/tcp | |
- 27019/tcp | |
- 27021/tcp | |
- 28017/tcp | |
public-address: 10.0.3.126 | |
shard1/3: | |
agent-state: started | |
agent-version: 1.20.1.1 | |
machine: "7" | |
open-ports: | |
- 27017/tcp | |
- 27019/tcp | |
- 27021/tcp | |
- 28017/tcp | |
public-address: 10.0.3.12 | |
shard1/4: | |
agent-state: started | |
agent-version: 1.20.1.1 | |
machine: "8" | |
open-ports: | |
- 27017/tcp | |
- 27019/tcp | |
- 27021/tcp | |
- 28017/tcp | |
public-address: 10.0.3.87 | |
shard2: | |
charm: cs:trusty/mongodb-3 | |
exposed: false | |
relations: | |
database: | |
- mongos | |
replica-set: | |
- shard2 | |
units: | |
shard2/0: | |
agent-state: started | |
agent-version: 1.20.1.1 | |
machine: "9" | |
open-ports: | |
- 27017/tcp | |
- 27019/tcp | |
- 27021/tcp | |
- 28017/tcp | |
public-address: 10.0.3.231 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment