Browse Source

new: [graph] new command.

raw-remaining-args
Valentin Lab 9 years ago
parent
commit
700445f0f6
  1. 157
      bin/compose

157
bin/compose

@ -2091,17 +2091,153 @@ get_docker_compose_single_opts_list() {
tr ',' "\n" | xargs echo
}
clean_cache() {
local i=0
for f in $(ls -t "$CACHEDIR/"*.cache.* 2>/dev/null | tail -n +500); do
((i++))
rm -f "$f"
done
if (( i > 0 )); then
debug "${WHITE}Cleaned cache:${NORMAL} Removed $((i)) elements (current cache size is $(du -sh "$CACHEDIR" | cut -f 1))"
_graph_service() {
local service="$1" base="$1"
charm=$(get_service_charm "$service") || return 1
metadata=$(charm.metadata "$charm") || return 1
subordinate=$(echo "$metadata" | shyaml get-value "subordinate" 2>/dev/null)
if [ "$subordinate" == "True" ]; then
requires="$(echo "$metadata" | shyaml get-value "requires" 2>/dev/null)"
master_charm=
while read-0 relation_name relation; do
[ "$(echo "$relation" | shyaml get-value "scope" 2>/dev/null)" == "container" ] || continue
interface="$(echo "$relation" | shyaml get-value "interface" 2>/dev/null)"
if [ -z "$interface" ]; then
err "No ${WHITE}$interface${NORMAL} set for relation $relation_name."
return 1
fi
## Action provided by relation ?
target_service=
while read-0 relation_name candidate_target_service _relation_config _tech_dep; do
[ "$interface" == "$relation_name" ] && {
target_service="$candidate_target_service"
break
}
done < <(get_compose_relations "$service")
if [ -z "$target_service" ]; then
err "Couldn't find ${WHITE}relations.$interface${NORMAL} in" \
"${DARKYELLOW}$service$NORMAL compose definition."
return 1
fi
master_service="$target_service"
master_charm=$(get_service_charm "$target_service") || return 1
break
done < <(echo "$requires" | shyaml key-values-0 2>/dev/null)
fi
_graph_node_service "$service" "$base" "$charm"
_graph_edge_service "$service" "$subordinate" "$master_service"
}
_graph_node_service() {
local service="$1" base="$2" charm="$3"
cat <<EOF
"$(_graph_node_service_label ${service})" [
style = "filled, $([ "$subordinate" == "True" ] && echo "dashed" || echo "bold")"
penwidth = $([ "$subordinate" == "True" ] && echo "3" || echo "5")
color = $([ "$base" ] && echo "blue" || echo "black")
fillcolor = "white"
fontname = "Courier New"
shape = "Mrecord"
label =<$(_graph_node_service_content "$service")>
];
EOF
}
_graph_edge_service() {
local service="$1" subordinate="$2" master_service="$3"
while read-0 relation_name target_service relation_config tech_dep; do
cat <<EOF
"$(_graph_node_service_label ${service})" -> "$(_graph_node_service_label ${target_service})" [
penwidth = $([ "$master_service" == "$target_service" ] && echo 3 || echo 2)
fontsize = 16
fontcolor = "black"
style = $([ "$master_service" == "$target_service" ] && echo dashed || echo "\"\"")
weight = $([ "$master_service" == "$target_service" ] && echo 2.0 || echo 1.0)
dir = $([ "$master_service" == "$target_service" ] && echo none || echo both)
arrowtail = odot
# arrowhead = dotlicurve
taillabel = "$relation_name" ];
EOF
done < <(get_compose_relations "$service") || return 1
}
_graph_node_service_label() {
local service="$1"
echo "service_$service"
}
_graph_node_service_content() {
local service="$1"
charm=$(get_service_charm "$service") || return 1
cat <<EOF
<table border="0" cellborder="0" cellpadding="3" bgcolor="white">
<tr>
<td bgcolor="black" align="center" colspan="2">
<font color="white">$service</font>
</td>
</tr>
$(if [ "$charm" != "$service" ]; then
cat <<EOF2
<tr>
<td align="left" port="r0">charm: $charm</td>
</tr>
EOF2
fi)
</table>
EOF
}
cla_contains () {
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
return 1
}
graph() {
local services=("$@")
declare -A entries
cat <<EOF
digraph g {
graph [
fontsize=30
labelloc="t"
label=""
splines=true
overlap=false
#rankdir = "LR"
];
ratio = auto;
EOF
for target_service in "$@"; do
services=$(get_ordered_service_dependencies "$target_service")
for service in $services; do
[ "${entries[$service]}" ] && continue || entries[$service]=1
if cla_contains "$service" "${services[@]}"; then
base=true
else
base=
fi
_graph_service "$service" "$base"
done
done
echo "}"
}
[ "$SOURCED" ] && return 0
if [ -z "$DISABLE_SYSTEM_CONFIG_FILE" ]; then
@ -2267,7 +2403,7 @@ charm.sanity_checks || die "Sanity checks about charm-store failed. Please corre
is_service_action=
case "$action" in
up|build|start|stop|config)
up|build|start|stop|config|graph)
services="$(get_default_target_services "${action_posargs[@]}")" || exit 1
orig_services="${action_posargs[@]:1}"
;;
@ -2376,6 +2512,9 @@ case "$action" in
# [ "${remainder_args[*]}" ] || remainder_args=("/bin/bash" "-c" "export TERM=xterm; exec bash")
# docker exec -ti "${action_opts[@]}" "$master_service" "${remainder_args[@]}"
# ;;
graph)
graph $SERVICE_PACK
;;
config)
## removing the services
launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" "${remainder_args[@]}"

Loading…
Cancel
Save