my dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
7.9 KiB

  1. #!/bin/bash
  2. STACKS_CACHE=/tmp/stacks.$UID.cache
  3. LAYERS_CACHE=/tmp/layers.$UID.cache
  4. INSTANCE_CACHE=/tmp/instances.$UID.cache
  5. CACHE_AGE=60
  6. # Returns 1 if cache is too old
  7. cache_age_too_old () {
  8. cache_filename="$1"
  9. cache_seconds="$2"
  10. if [ -f "${cache_filename}" ] ; then
  11. if [ $(stat -f %m "${cache_filename}") -lt $(($(date +%s) - ${cache_seconds} )) ] ; then
  12. return 1
  13. fi
  14. fi
  15. return 0
  16. }
  17. stacks () {
  18. local cache_age=$CACHE_AGE
  19. local list_full=0 list_ids=0
  20. local opt OPTIND OPTARG
  21. while getopts "hrlc:" opt; do
  22. case "$opt" in
  23. h)
  24. echo "Usage:
  25. $FUNCNAME -h : get help
  26. $FUNCNAME [name] : print opsworks stackid's name (in plaintext)
  27. $FUNCNAME -i : list all stacks by id
  28. $FUNCNAME -l : list opsworks stacks with stack IDs
  29. $FUNCNAME -c age : max cache age in mins (default ${CACHE_AGE})
  30. "
  31. return
  32. ;;
  33. r) list_ids=1
  34. ;;
  35. l) list_full=1
  36. ;;
  37. c) cache_age="${OPTARG}"
  38. ;;
  39. esac
  40. shift $((OPTIND-1))
  41. done
  42. if cache_age_too_old "$STACKS_CACHE" $cache_age ; then
  43. aws opsworks describe-stacks --output json > "$STACKS_CACHE"
  44. fi
  45. if [ "$1" == "" ] ; then
  46. if [ ${list_full} -eq 1 ] ; then
  47. jq '[.Stacks | .[] | { name:.Name, stackid:.StackId } ]' < "${STACKS_CACHE}"
  48. elif [ ${list_ids} -eq 1 ] ; then
  49. jq -r '.Stacks | .[] | .StackId ' < "${STACKS_CACHE}"
  50. else
  51. jq '[.Stacks | .[] | .Name]' < "${STACKS_CACHE}"
  52. fi
  53. else
  54. jq -r '.Stacks | .[] | if .Name == "'"${1}"'" then .StackId else empty end' < "${STACKS_CACHE}"
  55. fi
  56. }
  57. layers_help () {
  58. echo "Usage:
  59. ${FUNCNAME[1]} -h : get help
  60. ${FUNCNAME[1]} -c [age : max cache age in mins (default ${CACHE_AGE})
  61. ${FUNCNAME[1]} -s [stackid] : list opsworks layers in a given stack id
  62. ${FUNCNAME[1]} -S [stackname] : list opsworks layers in a given stack name
  63. ${FUNCNAME[1]} -i [layerid] : list opsworks layer detail for a given layerid
  64. ${FUNCNAME[1]} -n [layername] : search for opsworks layer with a name (needs stack)
  65. ${FUNCNAME[1]} -D : list dnsnames of instances attached to a layer
  66. ${FUNCNAME[1]} -I : list instances attached to a layer
  67. ${FUNCNAME[1]} -L ; list detail for opsworks layer"
  68. }
  69. layers () {
  70. local cache_age=$CACHE_AGE layers_cache=""
  71. local list_full=0 stack_name="" stack_id="" layer_id="" list_machines=0 dns_name="" list_layerid=0
  72. local opt OPTIND OPTARG
  73. while getopts "hc:s:S:n:i:IDL" opt; do
  74. case "$opt" in
  75. h)
  76. layers_help
  77. return
  78. ;;
  79. c) cache_age="${OPTARG}"
  80. ;;
  81. S) stack_name="${OPTARG}"
  82. ;;
  83. s) stack_id="${OPTARG}"
  84. ;;
  85. i) layer_id="${OPTARG}"
  86. ;;
  87. n) layer_name="${OPTARG}"
  88. ;;
  89. I) list_instances=1
  90. ;;
  91. D) dns_name=1
  92. ;;
  93. L) list_full=1
  94. ;;
  95. esac
  96. done
  97. if [ -z "$stack_name" ] && [ -z "$stack_id" ] && [ -z "$layer_id" ] ; then
  98. echo "You must set a layer id (-i), stack name (-S) or a stack id (-s)"
  99. layers_help
  100. return
  101. fi
  102. if [ -n "$stack_name" ] ; then
  103. stack_id=$(stacks "$stack_name")
  104. fi
  105. if [ -n "$stack_id" ] ; then
  106. layers_cache="$LAYERS_CACHE.${stack_id}"
  107. if cache_age_too_old "$layers_cache" $cache_age ; then
  108. aws opsworks describe-layers --output json --stack-id $stack_id > "$layers_cache"
  109. fi
  110. else
  111. layers_cache="$LAYERS_CACHE.${layer_id}"
  112. if cache_age_too_old "$layers_cache" $cache_age ; then
  113. aws opsworks describe-layers --output json --layer-id $layer_id > "$layers_cache"
  114. fi
  115. fi
  116. if [ -n "$stack_id" ] ; then
  117. jq -r '.Layers | .[] | if .StackId == "'"${stack_id}"'" then .LayerId else empty end ' < "${layers_cache}"
  118. return
  119. fi
  120. if [ -n "$layer_name" ] ; then
  121. jq -r '.Layers | .[] | if .Name == "'"${layer_name}"'" then .LayerId else empty end ' < "${layers_cache}"
  122. return
  123. fi
  124. if [ -n "$layer_id" ] ; then
  125. jq -r '.Layers | .[] | if .LayerId == "'"${layer_id}"'" then .Name else empty end' < "${layers_cache}"
  126. return
  127. fi
  128. if [ "$list_full" -eq 0 ] && [ $list_instances -eq 0 ] && [ $dns_name -eq 0 ] ; then
  129. jq '[.Layers | .[] | .Name ]' < "${layers_cache}"
  130. return
  131. fi
  132. if [ $list_instances -eq 1 ] ; then
  133. jq '[.Layers | .[] | { name:.Name, layerid:.LayerId } ]' < "${layers_cache}"
  134. fi
  135. }
  136. instances_help () {
  137. echo "Usage:
  138. ${FUNCNAME[1]} -h : get help
  139. ${FUNCNAME[1]} -c [age : max cache age in mins (default ${CACHE_AGE})
  140. ${FUNCNAME[1]} -s [stackid] : list opsworks instances in a given stack id
  141. ${FUNCNAME[1]} -S [stackname] : list opsworks instances in a given stack name
  142. ${FUNCNAME[1]} -l [layerid] : list opsworks instances detail for a given layerid
  143. ${FUNCNAME[1]} -L [layername] ; list opsworks instances detail with a layer name (needs stack)
  144. ${FUNCNAME[1]} -i [instanceid] : list opsworks instance detail for a given id (needs stack or layer)
  145. ${FUNCNAME[1]} -I [instancename] ; list opsworks instance detail for a given name (needs stack or layer)
  146. ${FUNCNAME[1]} -D : list dnsnames of instances
  147. ${FUNCNAME[1]} -V : list full detail of instances
  148. To list instances, you must provide a layer ID, or stack name/id.
  149. "
  150. }
  151. instances () {
  152. local cache_age=$CACHE_AGE instances_cache=""
  153. local layer_id="" stack_id="" stack_name="" layer_name="" instance_id="" instance_name="" dns_name=0 list_full=0
  154. local opt OPTIND OPTARG instance_match="" select="" print=""
  155. while getopts "hc:s:S:L:l:i:I:DV" opt; do
  156. case "$opt" in
  157. h)
  158. instances_help
  159. return
  160. ;;
  161. c) cache_age="${OPTARG}"
  162. ;;
  163. S) stack_name="${OPTARG}"
  164. ;;
  165. s) stack_id="${OPTARG}"
  166. ;;
  167. l) layer_id="${OPTARG}"
  168. ;;
  169. L) layer_name="${OPTARG}"
  170. ;;
  171. i) instance_id="${OPTARG}"
  172. ;;
  173. I) instance_name="${OPTARG}"
  174. ;;
  175. D) dns_name=1
  176. ;;
  177. V) list_full=1
  178. ;;
  179. esac
  180. done
  181. if [ -n "$stack_name" ] ; then
  182. stack_id=$(stacks $stack_name)
  183. fi
  184. if [ -z "$layer_id" ] && [ -z "$stack_id" ] ; then
  185. instances_help
  186. fi
  187. if [ -n "$layer_name" ] ; then
  188. if [ -z "$stack_id" ] && [ -z [ "$stack_name"] ; then
  189. echo "To search on layer name you must provide a stack id or stack name"
  190. return 1
  191. fi
  192. layer_id=$(layers $layer_name -s $stackid)
  193. fi
  194. if [ -n "$instance_name" ] ; then
  195. if [ -z "$layer_id" ] && [ -z "$stack_id" ] ; then
  196. echo "To search for an instance name or instance id, you must provide a layer or stack"
  197. return 1
  198. fi
  199. instance_match="and (.Hostname == \"${instance_name}\")"
  200. fi
  201. if [ -n "$instance_id" ] ; then
  202. instance_match="and (.InstanceId == \"${instance_id}\")"
  203. fi
  204. if [ -n "$layer_id" ] ; then
  205. instance_cache=${INSTANCE_CACHE}.${layer_id}
  206. if cache_age_too_old "$instance_cache" $cache_age ; then
  207. aws opsworks describe-instances --layer-id ${layer_id} --output json > "$instance_cache"
  208. fi
  209. select="(select(.LayerIds[] | contains(\"${layer_id}\")))"
  210. elif [ -n "$stack_id" ] ; then
  211. instance_cache=${INSTANCE_CACHE}.${stack_id}
  212. if cache_age_too_old "$instance_cache" $cache_age ; then
  213. aws opsworks describe-instances --stack-id ${stack_id} --output json > "$instance_cache"
  214. fi
  215. select="(.StackId == \"$stack_id\")"
  216. fi
  217. if [ "$list_full" -eq 1 ] ; then
  218. print="{ name: .Hostname, az: .AvailabilityZone, ip: .PublicIp, status: .Status, dns: .PublicDns, type:
  219. .InstanceType, created: .CreatedAt, instanceid: .InstanceId, stackid: .StackId, layerids: .LayerIds }"
  220. elif [ "$dns_name" -eq 1 ] ; then
  221. print=".PublicDns"
  222. else
  223. print=".InstanceId"
  224. fi
  225. jq -r ".Instances | .[] | if ($select $instance_match) then $print else empty end" < "${instance_cache}"
  226. }