/usr/share/pyshared/juju/hooks/commands.py is in juju-0.7 0.7-0ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | import logging
import os
import pipes
import re
import sys
from twisted.internet.defer import inlineCallbacks, returnValue
from juju.hooks.cli import (
CommandLineClient, parse_log_level, parse_port_protocol)
from juju.hooks.protocol import MustSpecifyRelationName
from juju.state.errors import InvalidRelationIdentity
BAD_CHARS = re.compile("[\-\./:()<>|?*]|(\\\)")
class RelationGetCli(CommandLineClient):
keyvalue_pairs = False
def customize_parser(self):
remote_unit = os.environ.get("JUJU_REMOTE_UNIT")
self.parser.add_argument(
"-r", dest="relation_id", default="", metavar="RELATION ID")
self.parser.add_argument("settings_name", default="", nargs="?")
self.parser.add_argument("unit_name", default=remote_unit, nargs="?")
@inlineCallbacks
def run(self):
if self.options.settings_name == "-":
self.options.settings_name = ""
if self.options.unit_name is None:
print >>sys.stderr, "Unit name is not defined"
return
result = None
try:
result = yield self.client.relation_get(self.options.client_id,
self.options.relation_id,
self.options.unit_name,
self.options.settings_name)
except InvalidRelationIdentity, e:
# This prevents the exception from getting wrapped by AMP
print >>sys.stderr, e.relation_ident
except Exception, e:
print >>sys.stderr, str(e)
returnValue(result)
def format_shell(self, result, stream):
options = self.options
settings_name = options.settings_name
if settings_name and settings_name != "-":
# result should be a single value
result = {settings_name.upper(): result}
if result:
errs = []
for k, v in sorted(os.environ.items()):
if k.startswith("VAR_"):
print >>stream, "%s=" % (k.upper(), )
errs.append(k)
for k, v in sorted(result.items()):
k = BAD_CHARS.sub("_", k.upper())
v = pipes.quote(v)
print >>stream, "VAR_%s=%s" % (k.upper(), v)
# Order of output within streams is assured, but we output
# on (commonly) two streams here and the ordering of those
# messages is significant to the user. Make a best
# effort. However, this cannot be guaranteed when these
# streams are collected by `HookProtocol`.
stream.flush()
if errs:
print >>sys.stderr, "The following were omitted from " \
"the environment. VAR_ prefixed variables indicate a " \
"usage error."
print >>sys.stderr, "".join(errs)
def relation_get():
"""Entry point for relation-get"""
client = RelationGetCli()
sys.exit(client())
class RelationSetCli(CommandLineClient):
keyvalue_pairs = True
def customize_parser(self):
self.parser.add_argument(
"-r", dest="relation_id", default="", metavar="RELATION ID")
@inlineCallbacks
def run(self):
try:
yield self.client.relation_set(self.options.client_id,
self.options.relation_id,
self.options.keyvalue_pairs)
except InvalidRelationIdentity, e:
# This prevents the exception from getting wrapped by AMP
print >>sys.stderr, e.relation_ident
except Exception, e:
print >>sys.stderr, str(e)
def relation_set():
"""Entry point for relation-set."""
client = RelationSetCli()
sys.exit(client())
class RelationIdsCli(CommandLineClient):
keyvalue_pairs = False
def customize_parser(self):
relation_name = os.environ.get("JUJU_RELATION", "")
self.parser.add_argument(
"relation_name",
metavar="RELATION NAME",
nargs="?",
default=relation_name,
help=("Specify the relation name of the relation ids to list. "
"Defaults to $JUJU_RELATION, if available."))
@inlineCallbacks
def run(self):
if not self.options.relation_name:
raise MustSpecifyRelationName()
result = yield self.client.relation_ids(
self.options.client_id, self.options.relation_name)
returnValue(result)
def format_smart(self, result, stream):
for ident in result:
print >>stream, ident
def relation_ids():
"""Entry point for relation-set."""
client = RelationIdsCli()
sys.exit(client())
class ListCli(CommandLineClient):
keyvalue_pairs = False
def customize_parser(self):
self.parser.add_argument(
"-r", dest="relation_id", default="", metavar="RELATION ID")
@inlineCallbacks
def run(self):
result = None
try:
result = yield self.client.list_relations(self.options.client_id,
self.options.relation_id)
except InvalidRelationIdentity, e:
# This prevents the exception from getting wrapped by AMP
print >>sys.stderr, e.relation_ident
except Exception, e:
print >>sys.stderr, str(e)
returnValue(result)
def format_eval(self, result, stream):
""" eval `juju-list` """
print >>stream, "export JUJU_MEMBERS=\"%s\"" % (" ".join(result))
def format_smart(self, result, stream):
for member in result:
print >>stream, member
def relation_list():
"""Entry point for relation-list."""
client = ListCli()
sys.exit(client())
class LoggingCli(CommandLineClient):
keyvalue_pairs = False
require_cid = False
def customize_parser(self):
self.parser.add_argument("message", nargs="+")
self.parser.add_argument("-l",
metavar="CRITICAL|DEBUG|INFO|ERROR|WARNING",
help="Send log message at the given level",
type=parse_log_level, default=logging.INFO)
def run(self, result=None):
return self.client.log(self.options.l,
self.options.message)
def render(self, result):
return None
def log():
"""Entry point for juju-log."""
client = LoggingCli()
sys.exit(client())
class ConfigGetCli(CommandLineClient):
keyvalue_pairs = False
def customize_parser(self):
self.parser.add_argument("option_name", default="", nargs="?")
@inlineCallbacks
def run(self):
# handle settings_name being explictly skipped on the cli
result = yield self.client.config_get(self.options.client_id,
self.options.option_name)
returnValue(result)
def config_get():
"""Entry point for config-get"""
client = ConfigGetCli()
sys.exit(client())
class OpenPortCli(CommandLineClient):
keyvalue_pairs = False
def customize_parser(self):
self.parser.add_argument(
"port_protocol",
metavar="PORT[/PROTOCOL]",
help="The port to open. The protocol defaults to TCP.",
type=parse_port_protocol)
def run(self):
return self.client.open_port(
self.options.client_id, *self.options.port_protocol)
def open_port():
"""Entry point for open-port."""
client = OpenPortCli()
sys.exit(client())
class ClosePortCli(CommandLineClient):
keyvalue_pairs = False
def customize_parser(self):
self.parser.add_argument(
"port_protocol",
metavar="PORT[/PROTOCOL]",
help="The port to close. The protocol defaults to TCP.",
type=parse_port_protocol)
def run(self):
return self.client.close_port(
self.options.client_id, *self.options.port_protocol)
def close_port():
"""Entry point for close-port."""
client = ClosePortCli()
sys.exit(client())
class UnitGetCli(CommandLineClient):
keyvalue_pairs = False
def customize_parser(self):
self.parser.add_argument("setting_name")
@inlineCallbacks
def run(self):
result = yield self.client.get_unit_info(self.options.client_id,
self.options.setting_name)
returnValue(result["data"])
def unit_get():
"""Entry point for config-get"""
client = UnitGetCli()
sys.exit(client())
|