| 
									
										
										
										
											2017-05-19 10:17:38 -07:00
										 |  |  | #!/bin/bash | 
					
						
							| 
									
										
										
										
											2017-06-02 13:25:04 -07:00
										 |  |  | set -e -o pipefail | 
					
						
							| 
									
										
										
										
											2017-05-19 10:17:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-19 13:06:38 -07:00
										 |  |  | triton_docker_version=1.12.6 | 
					
						
							|  |  |  | triton_compose_version=1.9.0 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-19 10:17:38 -07:00
										 |  |  | # Check if triton is installed and executable | 
					
						
							| 
									
										
										
										
											2017-05-19 11:45:36 -07:00
										 |  |  | function checkTritonInstalled () { | 
					
						
							|  |  |  |     command -v triton >/dev/null 2>&1 || { echo >&2 "Triton CLI tools do not appear to be installed. Please install before continuing."; exit 1; } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-05-19 10:17:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-19 11:45:36 -07:00
										 |  |  | # Check if curl is installed and executable | 
					
						
							|  |  |  | function checkCurlInstalled () { | 
					
						
							|  |  |  |     command -v curl >/dev/null 2>&1 || { echo >&2 "curl is required to complete this installation. Please install before continuing."; exit 1; } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-19 12:58:30 -07:00
										 |  |  | # Check if the Docker binary (named triton-docker-helper) | 
					
						
							|  |  |  | # and Docker Compose (named triton-compose-helper) are installed and executable | 
					
						
							| 
									
										
										
										
											2017-05-19 11:45:36 -07:00
										 |  |  | function checkDockerInstalled () { | 
					
						
							| 
									
										
										
										
											2017-05-19 12:58:30 -07:00
										 |  |  |     if  | 
					
						
							|  |  |  |         [ ! -f /usr/local/bin/triton-docker-helper ] ||  | 
					
						
							|  |  |  |         [ ! -x /usr/local/bin/triton-docker-helper ] || | 
					
						
							| 
									
										
										
										
											2017-06-02 09:12:45 -07:00
										 |  |  |         [ "${triton_docker_version}," != "$(/usr/local/bin/triton-docker-helper -v | awk '{print $3}')" ] || | 
					
						
							| 
									
										
										
										
											2017-05-19 12:58:30 -07:00
										 |  |  |         [ ! -f /usr/local/bin/triton-compose-helper ] || | 
					
						
							|  |  |  |         [ ! -x /usr/local/bin/triton-compose-helper ] || | 
					
						
							| 
									
										
										
										
											2017-06-02 09:12:45 -07:00
										 |  |  |         [ "${triton_compose_version}," != "$(/usr/local/bin/triton-compose-helper -v | awk '{print $3}')" ] | 
					
						
							| 
									
										
										
										
											2017-05-19 11:45:36 -07:00
										 |  |  |     then | 
					
						
							| 
									
										
										
										
											2017-05-19 12:58:30 -07:00
										 |  |  |         echo 'Additional or updated components are required.' | 
					
						
							|  |  |  |         echo 'Please run `sudo triton-docker-install` to continue.' | 
					
						
							| 
									
										
										
										
											2017-05-19 11:45:36 -07:00
										 |  |  |         echo | 
					
						
							| 
									
										
										
										
											2017-05-19 12:58:30 -07:00
										 |  |  |         exit 1 | 
					
						
							| 
									
										
										
										
											2017-05-19 11:45:36 -07:00
										 |  |  |     fi | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-19 12:58:30 -07:00
										 |  |  | # Install architecture-specific Docker and Docker Compose binaries | 
					
						
							|  |  |  | function install () { | 
					
						
							|  |  |  |     echo "Triton needs to install additional components for Docker and Docker Compose interactions." | 
					
						
							|  |  |  |     echo "This installation will only happen once." | 
					
						
							| 
									
										
										
										
											2017-05-19 10:39:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-19 12:58:30 -07:00
										 |  |  |     # Check if curl is installed and executable | 
					
						
							|  |  |  |     checkCurlInstalled | 
					
						
							| 
									
										
										
										
											2017-05-19 10:17:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-19 12:58:30 -07:00
										 |  |  |     # Install the specific version of Docker for Triton | 
					
						
							|  |  |  |     rm -Rf /tmp/triton-docker /usr/local/bin/triton-docker-helper | 
					
						
							|  |  |  |     mkdir -p /tmp/triton-docker | 
					
						
							| 
									
										
										
										
											2017-06-02 09:12:45 -07:00
										 |  |  |     curl https://get.docker.com/builds/"$(uname -a | awk '{ print $1 }')"/x86_64/docker-${triton_docker_version}.tgz | tar zxvf - -C /tmp/triton-docker | 
					
						
							| 
									
										
										
										
											2017-05-19 12:58:30 -07:00
										 |  |  |     mv /tmp/triton-docker/docker/docker /usr/local/bin/triton-docker-helper | 
					
						
							|  |  |  |     rm -Rf /tmp/triton-docker | 
					
						
							|  |  |  |     chmod +x /usr/local/bin/triton-docker-helper | 
					
						
							| 
									
										
										
										
											2017-05-19 10:17:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-19 12:58:30 -07:00
										 |  |  |     echo "The triton-docker-helper is now installed." | 
					
						
							|  |  |  |     echo | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Install the specific version of Docker Compose for Triton | 
					
						
							| 
									
										
										
										
											2017-06-02 09:12:45 -07:00
										 |  |  |     curl -Lo /usr/local/bin/triton-compose-helper https://github.com/docker/compose/releases/download/${triton_compose_version}/docker-compose-"$(uname -a | awk '{ print $1 }')"-x86_64 | 
					
						
							| 
									
										
										
										
											2017-05-19 12:58:30 -07:00
										 |  |  |     chmod +x /usr/local/bin/triton-compose-helper | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     echo "The triton-compose-helper is now installed." | 
					
						
							|  |  |  |     echo | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-02 09:46:34 -07:00
										 |  |  | # Emit the current Triton profile and time | 
					
						
							|  |  |  | function currentProfile () { | 
					
						
							|  |  |  |     if [ -n "$TRITON_PROFILE" ] | 
					
						
							|  |  |  |     then | 
					
						
							|  |  |  |         local profile_source='via env var; use `eval "$(triton env <profile name>)"` to change' | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         local profile_source='default; use `triton profile set <profile name>` to change' | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     echo "Executing in '$(triton profile get | awk 'NR == 1 {print $2}')' (${profile_source})" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-02 13:25:04 -07:00
										 |  |  | # Set env vars to make using CNS easier | 
					
						
							|  |  |  | function CnsEnvVars () { | 
					
						
							| 
									
										
										
										
											2017-06-02 09:46:34 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-02 10:17:19 -07:00
										 |  |  |     # If the CNS env vars are already set, don't bother continuing | 
					
						
							| 
									
										
										
										
											2017-06-02 13:25:04 -07:00
										 |  |  |     if [ -n "$TRITON_CNS_SEARCH_DOMAIN_PUBLIC" ] && [ -n "$TRITON_CNS_SEARCH_DOMAIN_PRIVATE" ] | 
					
						
							| 
									
										
										
										
											2017-06-02 09:46:34 -07:00
										 |  |  |     then | 
					
						
							| 
									
										
										
										
											2017-06-02 10:17:19 -07:00
										 |  |  |         return | 
					
						
							| 
									
										
										
										
											2017-06-02 09:46:34 -07:00
										 |  |  |     fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-02 13:25:04 -07:00
										 |  |  |     # Get and the user's account information and CloudAPI URL for parsing later | 
					
						
							| 
									
										
										
										
											2017-06-02 10:17:19 -07:00
										 |  |  |     local triton_account="$(triton account get)" | 
					
						
							| 
									
										
										
										
											2017-06-02 13:25:04 -07:00
										 |  |  |     local triton_url="$(triton profile get | awk -F"/" '/url:/{print $3}')" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Do not continue if the target is not a Triton Public Cloud data center | 
					
						
							|  |  |  |     if [ ! "api.joyent.com" == "$(echo "${triton_url}" | awk -F'.' '{print $2 "." $3 "." $4}')" ] | 
					
						
							|  |  |  |     then | 
					
						
							|  |  |  |         return | 
					
						
							|  |  |  |     fi | 
					
						
							| 
									
										
										
										
											2017-06-02 10:17:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-02 09:46:34 -07:00
										 |  |  |     # Check if CNS is enabled, require it | 
					
						
							| 
									
										
										
										
											2017-06-02 13:25:04 -07:00
										 |  |  |     local triton_cns_enabled="$(echo "${triton_account}" | awk -F": " '/cns/{print $2}')" | 
					
						
							|  |  |  |     if [ ! "true" == "$triton_cns_enabled" ] | 
					
						
							|  |  |  |     then | 
					
						
							| 
									
										
										
										
											2017-06-02 09:46:34 -07:00
										 |  |  |         echo | 
					
						
							|  |  |  |         tput rev  # reverse | 
					
						
							|  |  |  |         tput bold # bold | 
					
						
							|  |  |  |         echo 'Error! Triton CNS is required and not enabled.' | 
					
						
							|  |  |  |         tput sgr0 # clear | 
					
						
							|  |  |  |         echo | 
					
						
							|  |  |  |         echo 'Please run:' | 
					
						
							|  |  |  |         echo '  triton account update triton_cns_enabled=true' | 
					
						
							|  |  |  |         echo | 
					
						
							|  |  |  |         exit 1 | 
					
						
							|  |  |  |     fi | 
					
						
							| 
									
										
										
										
											2017-06-02 10:17:19 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Get the user's UUID | 
					
						
							| 
									
										
										
										
											2017-06-02 13:25:04 -07:00
										 |  |  |     local triton_account_uuid="$(echo "${triton_account}" | awk -F": " '/id:/{print $2}')" | 
					
						
							| 
									
										
										
										
											2017-06-02 10:17:19 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Get current data center name | 
					
						
							|  |  |  |     # Note: this makes assumptions that work in our public cloud, but might not work elsewhere | 
					
						
							|  |  |  |     # Further note: set TRITON_CNS_SUFFIX_PUBLIC and TRITON_CNS_SUFFIX_PRIVATE to work around this | 
					
						
							| 
									
										
										
										
											2017-06-02 13:25:04 -07:00
										 |  |  |     local triton_dc="$(echo "${triton_url}" | awk -F'.' '{print $1}')" | 
					
						
							| 
									
										
										
										
											2017-06-02 10:17:19 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Set the CNS base for public and private names | 
					
						
							| 
									
										
										
										
											2017-06-02 13:25:04 -07:00
										 |  |  |     export TRITON_CNS_SEARCH_DOMAIN_PUBLIC="${triton_account_uuid}.${triton_dc}.triton.zone" | 
					
						
							|  |  |  |     export TRITON_CNS_SEARCH_DOMAIN_PRIVATE="${triton_account_uuid}.${triton_dc}.cns.joyent.com" | 
					
						
							| 
									
										
										
										
											2017-06-02 09:46:34 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-19 13:06:38 -07:00
										 |  |  | # Run all checks | 
					
						
							| 
									
										
										
										
											2017-05-19 12:58:30 -07:00
										 |  |  | function checkAll () { | 
					
						
							|  |  |  |     checkTritonInstalled && | 
					
						
							| 
									
										
										
										
											2017-06-02 08:47:24 -07:00
										 |  |  |     checkDockerInstalled | 
					
						
							| 
									
										
										
										
											2017-05-19 11:45:36 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-05-19 10:17:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-02 08:47:24 -07:00
										 |  |  | # Special case handling if the sub-command is `profile` | 
					
						
							|  |  |  | if [ "profile" == "$1" ] | 
					
						
							|  |  |  | then | 
					
						
							|  |  |  |     currentProfile | 
					
						
							|  |  |  |     exit 1 | 
					
						
							|  |  |  | fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Change behavior based on the calling name of this executable | 
					
						
							| 
									
										
										
										
											2017-06-02 09:12:45 -07:00
										 |  |  | case "$(basename $0)" in | 
					
						
							| 
									
										
										
										
											2017-05-19 11:45:36 -07:00
										 |  |  |     triton-docker) | 
					
						
							| 
									
										
										
										
											2017-05-19 12:58:30 -07:00
										 |  |  |         checkAll && | 
					
						
							| 
									
										
										
										
											2017-05-19 11:45:36 -07:00
										 |  |  |         eval "$(triton env)" && | 
					
						
							| 
									
										
										
										
											2017-06-02 13:25:04 -07:00
										 |  |  |         CnsEnvVars && | 
					
						
							| 
									
										
										
										
											2017-05-19 11:45:36 -07:00
										 |  |  |         exec /usr/local/bin/triton-docker-helper $@ | 
					
						
							|  |  |  |         ;; | 
					
						
							|  |  |  |     triton-compose) | 
					
						
							| 
									
										
										
										
											2017-05-19 12:58:30 -07:00
										 |  |  |         checkAll && | 
					
						
							| 
									
										
										
										
											2017-05-19 11:45:36 -07:00
										 |  |  |         eval "$(triton env)" && | 
					
						
							|  |  |  |         export COMPOSE_HTTP_TIMEOUT=750 && | 
					
						
							| 
									
										
										
										
											2017-06-02 13:25:04 -07:00
										 |  |  |         CnsEnvVars && | 
					
						
							| 
									
										
										
										
											2017-05-19 11:45:36 -07:00
										 |  |  |         exec /usr/local/bin/triton-compose-helper $@ | 
					
						
							|  |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2017-05-19 12:58:30 -07:00
										 |  |  |     triton-docker-install) | 
					
						
							|  |  |  |         install | 
					
						
							|  |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2017-05-19 11:45:36 -07:00
										 |  |  | esac |