├── .gitignore ├── LICENSE ├── README.md └── bin ├── gce-10acre-ranch ├── mac-ranch └── user_data_clusters /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Wrapper to deploy Rancher on Google Compute Engine or Docker Machine on macOS 2 | 3 | These scripts bootstrap a Rancher server and register one or more host node VMs to it. 4 | 5 | - [Google Compute Engine](#google-compute-engine) 6 | - [Docker Machine on macOS](#docker-machine-on-macos) 7 | 8 | ## Google Compute Engine 9 | 10 | ### Prerequisites 11 | 12 | - Google Cloud SDK - [SDK](https://cloud.google.com/sdk/) 13 | 14 | ### Usage: 15 | ``` 16 | gce-10acre-ranch [opts] 17 | -a - Agent Container: 18 | needs full container repo/name[:tag] 19 | -b - Boot disk size in GB (40 default) 20 | -c - Cluster name[Required] 21 | -d - DELETE ALL NODES 22 | -e - External IP for master...(yes this is getting ridiculous) 23 | -h - Print this message 24 | -i - Show the IP address of the master 25 | -l - List nodes or clusters if no -c is passed 26 | -m - Master Machine type (g1-small default) 27 | -M - Node Machine type (g1-small default) 28 | -n - Number of nodes [defaults to 1] 29 | -N - Network [default is default] 30 | -o - OS image 31 | centos-6 (Servers only. Configuration is manual) 32 | centos-7 (Servers only. Configuration is manual) 33 | rhel-6 (Servers only. Config is manual) 34 | rhel-7 (Servers only. Config is manual) 35 | coreos- 36 | coreos (stable) 37 | debian-7-backports 38 | fedora-21 (Rancher Labs Only) 39 | ubuntu- 40 | -p - privileged (needed for fedora) 41 | - server 42 | - agent 43 | - all 44 | -q - Do not prompt user 45 | -r - Registration url 46 | -R - REPOS to use for rancher/server:master build 47 | -s - Server Container: 48 | needs full container repo/name[:tag] 49 | -u - Docker install URL 50 | -U - Docker version 1.12, 1.13, 17.06 ..etc. Overrides -u 51 | -z - Zone [us-central1-f default] 52 | ``` 53 | 54 | ### If you are using this outside of Rancher Labs 55 | 56 | Set the GCE Project via environment variable: ```GCE_PROJECT=""``` 57 | 58 | ### Images 59 | 60 | For CoreOS and Ubuntu first look at the output of: 61 | 62 | `gcloud compute images list --project ` 63 | 64 | These versions do not always have aliases and update frequently. So we are just passing that along. 65 | 66 | The default is Ubuntu-14-04 if no -o option is specified. 67 | 68 | ### To deploy a cluster: 69 | 70 | ``` 71 | ./gce-10acre-ranch -c -n 72 | ``` 73 | 74 | ### Deploy source code versions 75 | ``` 76 | gce-10acre-ranch -s rancher/server:master -p server -c rancher-dev -n 1 77 | ``` 78 | 79 | Currently all nodes will be deployed with Ubuntu 14.04. The naming convention is: 80 | -10acre-master-0 81 | -10acre-[1:N] 82 | 83 | For more customizations/testing capabilities you can sepecify the Docker images for server and agents: 84 | 85 | ``` 86 | Server 87 | ./gce-10acre-ranch -c -n -s rancher/server:vX.Y.Z 88 | 89 | Agent 90 | ./gce-10acre-ranch -c -n -a /agent:vX.Y.Z 91 | 92 | Or both 93 | ./gce-10acre-ranch -c -n -a /agent:vX.Y.Z -s /dev-server 94 | ``` 95 | The Docker images must be real and accessible to Docker. 96 | 97 | ### Using specific REPOS 98 | 99 | Ability to use [repos](https://github.com/rancher/rancher/blob/master/server/README.md) for `rancher/server:master` 100 | 101 | ``` 102 | ./gce-10acre-ranch -c -R rancher-compose-executor 103 | 104 | ./gce-10acre-rancher -c -R rancher-compose-executor,https://github.com/rancher/cattle,origin/otherbranch 105 | ``` 106 | 107 | ### Get the master IP: 108 | 109 | ``` 110 | ./gce-10acre-ranch -c -i 111 | ``` 112 | You can hit this IP over port 8080 to get to the UI 113 | 114 | ### List all Clusters: 115 | 116 | ``` 117 | ./gce-10acre-ranch -l 118 | ``` 119 | ### List all the nodes: 120 | 121 | ``` 122 | ./gce-10acre-ranch -c -l 123 | ``` 124 | 125 | ### Destroy the cluster(-q for quiet): 126 | 127 | ``` 128 | ./gce-10acre-ranch -c -d (-q) 129 | ``` 130 | 131 | ------ 132 | ## Docker Machine on macOS 133 | 134 | ### Prerequisites 135 | 136 | - Docker for Mac (https://www.docker.com/products/docker#/mac) (for docker-machine) 137 | - docker-machine-driver-xhyve (https://github.com/zchee/docker-machine-driver-xhyve) 138 | 139 | ### Usage 140 | ``` 141 | mac-ranch [opts] cluster_name 142 | 143 | Top-level actions: 144 | -c - Create cluster 145 | -d - Destroy cluster 146 | -h - Print this message 147 | -i - Print IP of server VM 148 | -l - List cluser VMs 149 | -p - Stop cluster 150 | -P - Start cluster 151 | -r registration_url - Create "-n" additional hosts using the given registration url 152 | 153 | Additional create options: 154 | -b - Boot2Docker URL or version tag (default: RancherOS Latest) 155 | -M - Host memory in MB (default: 1024) 156 | -m - Server memory in MB (default: 2048) 157 | -n - Number of hosts (default: 2) 158 | -R - REPOS to use for master build (implies -s rancher/server:master) 159 | -s - Server Container image (default: rancher/server:latest) 160 | -u - Registry mirror URL (default: none) 161 | ``` 162 | 163 | ### To deploy (single server named `rs` with two hosts `rs-host1` and `rs-host2`): 164 | 165 | ``` 166 | ./mac-ranch -c rs 167 | ``` 168 | 169 | #### A specific release 170 | ``` 171 | ./mac-ranch -c -s rancher/server:vX.Y.Z rs 172 | ``` 173 | 174 | #### Build from master 175 | ``` 176 | ./mac-ranch -c -s rancher/server:master rs 177 | ``` 178 | 179 | #### Build from master with specific REPO overrides 180 | ``` 181 | ./mac-ranch -c -R "https://github.com/you/cattle,origin/yourbranch" rs 182 | ``` 183 | 184 | ### Manipulate clusters 185 | ``` 186 | ./mac-ranch -l rs # List VMs 187 | ./mac-ranch -p rs # Stop VMs 188 | ./mac-ranch -P rs # Start VMs 189 | ./mac-ranch -d rs # Destroy VMS 190 | ``` 191 | 192 | ### Contact 193 | For bugs, questions, comments, corrections, suggestions, etc., open an issue in [rancher/rancher](//github.com/rancher/rancher/issues) with a title starting with `[10acre-ranch] `. 194 | 195 | Or just [click here](//github.com/rancher/rancher/issues/new?title=%5B10acre-ranch%5D%20) to create a new issue. 196 | 197 | # License 198 | Copyright (c) 2014-2017 [Rancher Labs, Inc.](http://rancher.com) 199 | 200 | Licensed under the Apache License, Version 2.0 (the "License"); 201 | you may not use this file except in compliance with the License. 202 | You may obtain a copy of the License at 203 | 204 | [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0) 205 | 206 | Unless required by applicable law or agreed to in writing, software 207 | distributed under the License is distributed on an "AS IS" BASIS, 208 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 209 | See the License for the specific language governing permissions and 210 | limitations under the License. 211 | -------------------------------------------------------------------------------- /bin/gce-10acre-ranch: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #set -o xtrace 3 | set -e 4 | 5 | if [ $(whoami) == "root" ]; then 6 | echo "Do not run as root!" 1>&2 7 | exit 1 8 | fi 9 | 10 | : ${GCE_PROJECT:="rancher-dev"} 11 | 12 | gcloud -v >/dev/null 2>&1 || { 13 | echo "gcloud command not found." 1>&2 14 | exit 1 15 | } 16 | 17 | print_help() 18 | { 19 | cat 1>&2 < 42 | coreos (stable) 43 | debian-7-backports 44 | fedora-21 (Rancher Labs Only) 45 | ubuntu- 46 | -p - privileged (needed for fedora) 47 | - server 48 | - agent 49 | - all 50 | -q - Do not prompt user 51 | -r - Registration url 52 | -R - REPOS to use for rancher/server:master build 53 | -s - Server Container: 54 | needs full container repo/name[:tag] 55 | -u - Docker install URL 56 | -U - Docker version 1.12, 1.13, 17.06 ..etc. Overrides -u 57 | -z - Zone [us-central1-f default] 58 | EOF 59 | } 60 | 61 | WAIT=true 62 | INSTANCE_COUNT=1 63 | OS_FAMILY=ubuntu-1604-lts 64 | OS_IMAGE_PROJECT=ubuntu-os-cloud 65 | #DOCKER_URL="https://releases.rancher.com/install-docker/1.10.3.sh" 66 | DOCKER_URL="https://get.docker.com" 67 | MASTER_MACHINE_TYPE="g1-small" 68 | NODE_MACHINE_TYPE="g1-small" 69 | ZONE="us-central1-f" 70 | NETWORK="default" 71 | DISK_SIZE=40 72 | DISK_TYPE="pd-standard" # SSD is "pd-ssd" 73 | 74 | while getopts ":a:b:c:de:f:hilm:M:n:o:p:qr:R:s:u:U:wz:N:" opt;do 75 | case $opt in 76 | a) 77 | AGENT_CONTAINER="${OPTARG}" 78 | ;; 79 | b) 80 | DISK_SIZE=${OPTARG} 81 | ;; 82 | c) 83 | # Add 10acre so that you can't accidentally list/delete lots of hosts easily. 84 | CLUSTER_NAME="${OPTARG}-10acre" 85 | MASTER_NAME="${CLUSTER_NAME}-master-0" 86 | ;; 87 | d) 88 | DELETE_NODES_FLAG="true" 89 | ;; 90 | e) 91 | EXTERNAL_IP=${OPTARG} 92 | ;; 93 | f) 94 | DISK_TYPE=${OPTARG} 95 | ;; 96 | h) 97 | print_help 98 | exit 1 99 | ;; 100 | i) 101 | MASTER_IP_FLAG="true" 102 | ;; 103 | l) 104 | LIST_NODES_FLAG="true" 105 | ;; 106 | m) 107 | MASTER_MACHINE_TYPE=${OPTARG} 108 | ;; 109 | M) 110 | NODE_MACHINE_TYPE=${OPTARG} 111 | ;; 112 | N) 113 | NETWORK=${OPTARG} 114 | ;; 115 | n) 116 | INSTANCE_COUNT=${OPTARG} 117 | ;; 118 | o) 119 | OS_FAMILY=${OPTARG} 120 | ;; 121 | p) 122 | case $OPTARG in 123 | "server") 124 | PRIVILEGED_MASTER="true" 125 | ;; 126 | "agent") 127 | PRIVILEGED_AGENT="true" 128 | ;; 129 | "all") 130 | PRIVILEGED_MASTER="true" 131 | PRIVILEGED_AGENT="true" 132 | ;; 133 | * ) 134 | print_help 135 | exit 1 136 | ;; 137 | esac 138 | ;; 139 | r) 140 | REGISTRATION_URL="${OPTARG}" 141 | ;; 142 | R) 143 | REPOS=${OPTARG} 144 | ;; 145 | q) 146 | QUIET="true" 147 | ;; 148 | s) 149 | SERVER_CONTAINER="${OPTARG}" 150 | ;; 151 | t) 152 | echo "-t option deprecated. Use -u https://test.docker.com" 153 | ;; 154 | u) 155 | DOCKER_URL="${OPTARG}" 156 | ;; 157 | U) 158 | DOCKER_VERSION="${OPTARG}" 159 | ;; 160 | w) 161 | echo "-w Deprecated... now default behavior" 162 | ;; 163 | \?) 164 | echo "Invalid arguemnts" >&2 165 | print_help 166 | exit 1 167 | ;; 168 | z) 169 | ZONE="${OPTARG}" 170 | ;; 171 | :) 172 | echo "Option -${OPTARG} requires arguement." >&2 173 | print_help 174 | exit 1 175 | ;; 176 | esac 177 | done 178 | shift $((OPTIND-1)) 179 | 180 | list_nodes() 181 | { 182 | local opts 183 | 184 | if [ -n "${CLUSTER_NAME}" ]; then 185 | opts="--regexp \"${CLUSTER_NAME}.*\"" 186 | fi 187 | 188 | gcloud compute instances list --project ${GCE_PROJECT} --zones ${ZONE} ${opts} 189 | } 190 | 191 | delete_nodes() 192 | { 193 | local opts 194 | local nodes 195 | 196 | nodes=$(list_nodes | grep -v ^NAME | cut -d' ' -f1) 197 | 198 | # This will prompt the user to confirm. 199 | if [ ${QUIET} ]; then 200 | opts="--quiet" 201 | else 202 | opts="" 203 | fi 204 | 205 | gcloud compute instances delete ${opts} --project ${GCE_PROJECT} --zone ${ZONE} ${nodes} 206 | } 207 | 208 | get_master_ip() 209 | { 210 | IP=$(gcloud compute instances describe "${MASTER_NAME}" --project ${GCE_PROJECT} --zone ${ZONE} | grep natIP | cut -d':' -f2) 211 | echo $IP 212 | } 213 | 214 | get_os_project() 215 | { 216 | local project 217 | 218 | # Has to be a better more dynamic way...that also isn't slow. 219 | case $OS_FAMILY in 220 | ubuntu*) 221 | project="ubuntu-os-cloud" 222 | ;; 223 | *-rancher-base) 224 | project="rancher-dev" 225 | ;; 226 | coreos*) 227 | project="coreos-cloud" 228 | ;; 229 | centos-*) 230 | project="centos-cloud" 231 | ;; 232 | rhel-*) 233 | project="rhel-cloud" 234 | ;; 235 | debian-*) 236 | project="debian-cloud" 237 | ;; 238 | fedora-21) 239 | project="fedora-cloud-base-21-20141217" 240 | ;; 241 | *) 242 | echo "Invalid OS Selection: $OS_FAMILY" 1>&2 243 | exit 1 244 | ;; 245 | esac 246 | 247 | echo "${project}" 248 | 249 | } 250 | 251 | get_user_data_flag() 252 | { 253 | case $OS_FAMILY in 254 | "fedora-21") 255 | echo "true" 256 | ;; 257 | *) 258 | echo "false" 259 | ;; 260 | esac 261 | } 262 | 263 | gcloud_create() 264 | { 265 | # 1 - instance name 266 | # 2 - startup script path/filename 267 | # 3 - GCE tags 268 | # 4 - Master IP Address 269 | # 5 - Machine Type 270 | # 6 - Boot disk size (gb) 271 | 272 | local metadata_key=startup-script 273 | local gcloud_args 274 | 275 | if [ "$(get_user_data_flag)" = "true" ]; then 276 | metadata_key=user-data 277 | fi 278 | 279 | gcloud_args="--zone ${ZONE} 280 | --machine-type ${5} 281 | --boot-disk-size=${DISK_SIZE}GB 282 | --boot-disk-type=${DISK_TYPE} 283 | --network ${NETWORK} 284 | --project ${GCE_PROJECT} 285 | --metadata-from-file ${metadata_key}=${2} 286 | --maintenance-policy MIGRATE 287 | --scopes https://www.googleapis.com/auth/devstorage.read_only 288 | --image-project $(get_os_project) 289 | --image-family ${OS_FAMILY} 290 | --tags ${3} " 291 | 292 | if [ ! -z ${4} ]; then 293 | gcloud_args="${gcloud_args} --address ${4}" 294 | fi 295 | 296 | gcloud compute instances create ${1} \ 297 | $gcloud_args 298 | } 299 | 300 | create_reg_tokens() # Signature: rancher_server_ip 301 | { 302 | project_id=$(curl -s -X GET http://${1}:8080/v2-beta/projects|python -c'import json,sys;print(json.load(sys.stdin)["data"][0]["id"])') 303 | echo $(curl -s -X POST http://${1}:8080/v2-beta/projects/${project_id}/registrationtokens|python -c'import json,sys; print(json.load(sys.stdin)["links"]["self"])') 304 | } 305 | 306 | get_reg_url() 307 | { 308 | ## This is a bit hacky... 309 | local reg_tokens_link 310 | reg_tokens_link=$(create_reg_tokens ${1}) 311 | sleep 1 312 | echo $(curl -s -X GET $reg_tokens_link|python -c'import json,sys; print(json.load(sys.stdin)["registrationUrl"])') 313 | } 314 | 315 | get_run_cmd() 316 | { 317 | ip=$(get_master_ip) 318 | reg_tokens_link=$(create_reg_tokens ${ip}) 319 | sleep 1 320 | echo $(curl -s -X GET $reg_tokens_link|python -c'import json,sys; print(json.load(sys.stdin)["command"])') 321 | } 322 | 323 | create_file() 324 | { 325 | local build_file=${1} 326 | local docker_run_cmd=${2} 327 | if [ ! -z ${DOCKER_VERSION} ]; then 328 | DOCKER_URL="https://releases.rancher.com/install-docker/${DOCKER_VERSION}.sh" 329 | fi 330 | cat > ${build_file} << EOF 331 | #!/bin/bash 332 | 333 | curl -sSL --retry 5 --retry-delay 10 ${DOCKER_URL}| sh 334 | if [ -r /etc/centos-release ] || [ -r /etc/redhat-release ]; then 335 | yum -y install docker-selinux 336 | fi 337 | 338 | if command -v systemctl > /dev/null; then 339 | echo "Starting docker..." 340 | systemctl stop docker 341 | sleep 10 342 | systemctl start docker 343 | fi 344 | 345 | sleep 5 346 | ${docker_run_cmd} 347 | EOF 348 | } 349 | 350 | 351 | generate_build_script() # signature: role container build_file 352 | { 353 | local container 354 | local default_container 355 | local build_file 356 | local docker_opts 357 | local docker_cmd 358 | local repos_value 359 | 360 | case $1 in 361 | master) 362 | default_container="rancher/server" 363 | 364 | docker_opts=('"run"', '"-d"', '"-p"', '"8080:8080"', '"-e"', '"DEFAULT_CATTLE_TELEMETRY_OPT=out"', '"-v"', '"/var/run/docker.sock:/var/run/docker.sock"'); 365 | docker_cmd= 366 | 367 | if [[ "${PRIVILEGED_MASTER}" = "true" ]]; then 368 | docker_opts+=(, '"--privileged"') 369 | fi 370 | 371 | if [[ "${REPOS}" != "" ]]; then 372 | repos_value="-e REPOS="\"${REPOS}\" 373 | fi 374 | 375 | ;; 376 | worker) 377 | default_container="rancher/agent:latest" 378 | 379 | IP=$(get_master_ip) 380 | docker_opts=('"run"', '"-d"', '"-v"', '"/var/run/docker.sock:/var/run/docker.sock"'); 381 | 382 | if [[ "${PRIVILEGED_AGENT}" = "true" ]]; then 383 | docker_opts+=(, '"--privileged"') 384 | fi 385 | 386 | docker_cmd=${REGISTRATION_URL:="$(get_reg_url ${IP})"} 387 | ;; 388 | *) 389 | echo "Can not generate build script" 390 | exit 1 391 | ;; 392 | esac 393 | 394 | if [ -z ${2} ]; then 395 | container=${default_container} 396 | else 397 | container="${2}" 398 | fi 399 | 400 | build_file=$3 401 | 402 | if [ "$(get_user_data_flag)" = "true" ]; then 403 | if [ ! -z ${DOCKER_VERSION} ]; then 404 | DOCKER_URL="https://releases.rancher.com/install-docker/${DOCKER_VERSION}.sh" 405 | fi 406 | user_data_clusters $(id -un) "$(cat ~/.ssh/id_rsa.pub)" ${build_file} "${container}" "${DOCKER_URL}" "${docker_cmd}" "${docker_opts[@]}" 407 | else 408 | docker_run_cmd="docker $(echo ${docker_opts[@]}| sed -e 's/,//g'|sed -e 's/\"//g') ${repos_value} ${container} ${docker_cmd}" 409 | echo ${docker_run_cmd} 410 | create_file ${build_file} "${docker_run_cmd}" 411 | fi 412 | 413 | } 414 | 415 | build_master() 416 | { 417 | build_file="/tmp/10acre-master-build.sh" 418 | generate_build_script "master" "${SERVER_CONTAINER}" ${build_file} 419 | gcloud_create "${MASTER_NAME}" "${build_file}" "alt-http-server" "${EXTERNAL_IP}" "${MASTER_MACHINE_TYPE}" 420 | } 421 | 422 | build_compute() 423 | { 424 | local instances 425 | 426 | build_file="/tmp/10acre-worker-build.sh" 427 | 428 | offset=$(list_nodes|grep -v '^NAME\|-10acre-master-0'|wc -l) 429 | 430 | for i in $(seq $((${offset} + 1)) $((${offset} + ${INSTANCE_COUNT}))); do 431 | instances="${CLUSTER_NAME}-${i} ${instances}" 432 | done 433 | 434 | if [ -n "${AGENT_CONTAINER}" ]; then 435 | generate_build_script "worker" "${AGENT_CONTAINER}" ${build_file} 436 | else 437 | docker_run_cmd="$(echo $(get_run_cmd)|sed 's/^sudo //')" 438 | create_file ${build_file} "${docker_run_cmd}" 439 | fi 440 | 441 | gcloud_create "${instances}" "${build_file}" "alt-http-server,open-compute-nodes" "" "${NODE_MACHINE_TYPE}" 442 | } 443 | 444 | build_cluster() 445 | { 446 | if [ -z "${REGISTRATION_URL}" ]; then 447 | build_master 448 | if [ "$WAIT" = "true" ]; then 449 | IP=$(get_master_ip) 450 | echo -n "Waiting for server to start " 451 | while sleep 5; do 452 | if [ "$(curl -s http://${IP}:8080/ping)" = "pong" ]; then 453 | echo Success 454 | break 455 | fi 456 | echo -n "." 457 | done 458 | fi 459 | fi 460 | if [ "${INSTANCE_COUNT}" -gt "0" ]; then 461 | build_compute 462 | fi 463 | } 464 | 465 | main() 466 | { 467 | if [[ "${LIST_NODES_FLAG}" = "true" && -z "${CLUSTER_NAME}" ]]; then 468 | list_nodes | grep -- '-10acre-master-' | sed -e 's/^\(.*\)\(-10acre-master.*\)$/\1/' | sort -u 469 | elif [[ "${LIST_NODES_FLAG}" = "true" && "${CLUSTER_NAME}" ]]; then 470 | list_nodes 471 | elif [ "${MASTER_IP_FLAG}" = "true" ]; then 472 | get_master_ip 473 | elif [[ "${DELETE_NODES_FLAG}" = "true" && -n "${CLUSTER_NAME}" ]]; then 474 | delete_nodes 475 | elif [[ -n "${INSTANCE_COUNT}" && -n "${CLUSTER_NAME}" ]]; then 476 | build_cluster 477 | else 478 | echo "Unable to take action" 1>&2 479 | print_help 480 | exit 1 481 | fi 482 | } 483 | 484 | main 485 | -------------------------------------------------------------------------------- /bin/mac-ranch: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #set -o xtrace 3 | set -e 4 | 5 | print_help() 6 | { 7 | cat 1>&2 <&2; 36 | } 37 | 38 | list_clusters() 39 | { 40 | (docker-machine ls --format "{{.Name}}" | grep -v "\-host\d*$") || echo -n "" 41 | } 42 | 43 | list_hosts() 44 | { 45 | docker-machine ls --filter name="^${CLUSTER_NAME}(\$|-)" 46 | } 47 | 48 | list_host_names() 49 | { 50 | (docker-machine ls --format "{{.Name}}" --filter name="^${HOST_PREFIX}") || echo -n "" 51 | } 52 | 53 | cluster_op() 54 | { 55 | local hosts 56 | local server 57 | 58 | hosts=$(list_host_names) 59 | server="" 60 | 61 | if docker-machine inspect ${CLUSTER_NAME} >/dev/null 2>&1; then 62 | server="${CLUSTER_NAME}" 63 | fi 64 | 65 | if [[ "${hosts}${server}" ]]; then 66 | docker-machine $1 ${hosts} ${server} 67 | else 68 | echoerr "Cluster ${CLUSTER_NAME} not found" 69 | fi 70 | } 71 | 72 | get_server_ip() 73 | { 74 | echo $(docker-machine ip ${CLUSTER_NAME}) 75 | } 76 | 77 | get_registration_cmd() 78 | { 79 | local cmd 80 | local server_ip 81 | local ip_cmd 82 | 83 | server_ip=$(get_server_ip) 84 | ip_cmd="\$(ifconfig eth0 | grep 'inet ' | awk -F'[: ]+' '{ print \$4 }')" 85 | cmd=${REGISTRATION_CMD} 86 | 87 | if [ -z "${cmd}" ]; then 88 | # v3 cluster 89 | cmd=$(curl -s http://${server_ip}:8080/v3/clusters | python -c 'import json,sys; print(json.load(sys.stdin)["data"][0]["registrationToken"]["hostCommand"])') 90 | fi 91 | 92 | if [ -z "${cmd}" ]; then 93 | # v2 registrationToken 94 | local project_id 95 | local link 96 | project_id=$(curl -s http://${server_ip}:8080/v2-beta/projects|python -c'import json,sys;print(json.load(sys.stdin)["data"][0]["id"])') 97 | reg_tokens_link=$(curl -s -X POST http://${server_ip}:8080/v2-beta/projects/${project_id}/registrationtokens|python -c'import json,sys; print(json.load(sys.stdin)["links"]["self"])') 98 | sleep 2 99 | cmd=$(curl -s $reg_tokens_link|python -c'import json,sys; print(json.load(sys.stdin)["command"])') 100 | fi 101 | 102 | cmd=$(echo $cmd | sed "s/docker run/docker run -e CATTLE_AGENT_IP=\"${ip_cmd}\"/") 103 | echo $cmd 104 | } 105 | 106 | build_master() 107 | { 108 | mirror="" 109 | if [[ "${MIRROR_URL}" ]]; then 110 | mirror="--engine-registry-mirror=${MIRROR_URL}" 111 | fi 112 | 113 | repos="" 114 | if [[ "${REPOS}" ]]; then 115 | repos="-e REPOS=\"${REPOS}\"" 116 | fi 117 | 118 | image="${SERVER_IMAGE}" 119 | if [[ "${REPOS}" != "" && "${image}" = "${DEFAULT_IMAGE}" ]]; then 120 | image="${MASTER_IMAGE}" 121 | fi 122 | 123 | docker-machine create \ 124 | --driver=xhyve \ 125 | --xhyve-boot2docker-url="${BOOT2DOCKER}" \ 126 | --xhyve-memory-size="${SERVER_MACHINE_SIZE}" \ 127 | --xhyve-cpu-count="${SERVER_MACHINE_CPUS}" \ 128 | --xhyve-boot-cmd="${BOOT_ARGS} host=${name}" \ 129 | ${mirror} ${CLUSTER_NAME} 130 | 131 | docker-machine ssh ${CLUSTER_NAME} docker run -d \ 132 | --restart=unless-stopped \ 133 | -p 8080:8080 \ 134 | -p 3306:3306 ${repos} \ 135 | -v /var/run/docker.sock:/var/run/docker.sock \ 136 | --name=rancher-server \ 137 | -e DEFAULT_CATTLE_TELEMETRY_OPT="out" \ 138 | -e DEFAULT_CATTLE_API_HOST="$(get_server_ip):8080" \ 139 | ${image} 140 | } 141 | 142 | build_hosts() 143 | { 144 | local name 145 | local offset 146 | local first_offset 147 | local sequence 148 | local i 149 | 150 | mirror="" 151 | if [[ "${MIRROR_URL}" ]]; then 152 | mirror="--engine-registry-mirror=${MIRROR_URL}" 153 | fi 154 | 155 | if [ "${HOST_COUNT}" -gt "0" ]; then 156 | offset=$(list_host_names|wc -l) 157 | first_offset=$offset 158 | sequence=$(seq $((${offset} + 1)) $((${offset} + ${HOST_COUNT}))) 159 | 160 | for i in $sequence 161 | do 162 | name="${HOST_PREFIX}${i}" 163 | echoerr "Creating ${name}" 164 | docker-machine create \ 165 | --driver=xhyve \ 166 | --xhyve-boot2docker-url="${BOOT2DOCKER}" \ 167 | --xhyve-memory-size="${HOST_MACHINE_SIZE}" \ 168 | --xhyve-boot-cmd="${BOOT_ARGS} host=${name}" ${mirror} ${name} 1>&2; 169 | done 170 | fi 171 | 172 | echo $first_offset 173 | } 174 | 175 | register_hosts() 176 | { 177 | local name 178 | local offset 179 | local registration_cmd 180 | local sequence 181 | local i 182 | local j 183 | 184 | offset=$1 185 | echoerr "Register hosts starting at ${offset}" 186 | registration_cmd=$(get_registration_cmd) 187 | sequence=$(seq $((${offset} + 1)) $((${offset} + ${HOST_COUNT}))) 188 | 189 | if [ "${HOST_COUNT}" -gt "0" ]; then 190 | for i in $sequence 191 | do 192 | name="${HOST_PREFIX}${i}" 193 | echoerr "Registering ${name}: ${registration_cmd}" 194 | docker-machine ssh ${name} ${registration_cmd} 1>&2; 195 | for j in "${PULL_IMAGES[@]}" 196 | do 197 | echoerr "Pulling images for ${name}" 198 | docker-machine ssh ${name} docker pull "${j}" 1>&2; 199 | done 200 | done 201 | fi 202 | } 203 | 204 | wait_for_server() 205 | { 206 | echo -n "Waiting for server to start " 207 | server_ip=$(get_server_ip) 208 | while sleep 5; do 209 | if [ "$(curl -s http://${server_ip}:8080/ping)" = "pong" ]; then 210 | echo Success 211 | break 212 | fi 213 | echo -n "." 214 | done 215 | } 216 | 217 | build_cluster() 218 | { 219 | local offset 220 | 221 | if [ -z "${REGISTRATION_CMD}" ]; then 222 | build_master 223 | fi 224 | 225 | offset=$(build_hosts) 226 | wait_for_server 227 | register_hosts $offset 228 | 229 | server_ip=$(get_server_ip) 230 | echo "Connect to rancher-server at http://${server_ip}:8080/" 231 | echo "" 232 | } 233 | 234 | main() 235 | { 236 | which docker-machine >/dev/null 2>&1 || { 237 | echoerr "docker-machine command not found." 238 | exit 1 239 | } 240 | 241 | which docker-machine-driver-xhyve >/dev/null 2>&1 || { 242 | echoerr "docker-machine-driver-xhyve command not found." 243 | exit 1 244 | } 245 | 246 | if [[ "${LIST_HOSTS_FLAG}" = "true" ]]; then 247 | if [ -z $CLUSTER_NAME ]; then 248 | list_clusters 249 | else 250 | list_hosts 251 | fi 252 | exit 0 253 | fi 254 | 255 | if [ -z $CLUSTER_NAME ]; then 256 | echoerr "A cluster_name is required" 257 | print_help 258 | exit 1 259 | fi 260 | 261 | if [[ "${SHOW_IP_FLAG}" = "true" ]]; then 262 | get_server_ip 263 | elif [[ "${START_FLAG}" = "true" ]]; then 264 | cluster_op "start" 265 | elif [[ "${STOP_FLAG}" = "true" ]]; then 266 | cluster_op "stop" 267 | elif [[ "${DELETE_FLAG}" = "true" ]]; then 268 | cluster_op "rm -y" 269 | elif [[ "${CREATE_FLAG}" = "true" || "${REGISTRATION_CMD}" ]]; then 270 | build_cluster 271 | else 272 | echoerr "No action specified" 273 | print_help 274 | exit 1 275 | fi 276 | } 277 | 278 | ############################## 279 | 280 | CLUSTER_NAME="" 281 | DEFAULT_IMAGE="rancher/server:latest" 282 | MASTER_IMAGE="rancher/server:master" 283 | SERVER_IMAGE="${DEFAULT_IMAGE}" 284 | SERVER_MACHINE_SIZE="2048" 285 | SERVER_MACHINE_CPUS="2" 286 | BOOT2DOCKER="https://releases.rancher.com/os/latest/rancheros.iso" 287 | BOOT_ARGS="loglevel=3 user=docker console=ttyS0 console=tty0 noembed nomodeset norestore waitusb=10 base" 288 | HOST_PREFIX="${CLUSTER_NAME}-host" 289 | HOST_COUNT=2 290 | HOST_MACHINE_SIZE="1024" 291 | MIRROR_URL="" 292 | PULL_IMAGES=("hello-world" "nginx" "busybox" "ubuntu" "rancher/curl" "cloudnautique/hello-world") 293 | 294 | while getopts ":b:cdhilM:m:n:pPr:R:s:u:" opt;do 295 | case $opt in 296 | b) 297 | if [[ "$OPTARG" == http* ]]; then 298 | BOOT2DOCKER="${OPTARG}" 299 | else 300 | BOOT2DOCKER="https://github.com/boot2docker/boot2docker/releases/download/${OPTARG}/boot2docker.iso" 301 | fi 302 | ;; 303 | c) 304 | CREATE_FLAG="true" 305 | CLUSTER_NAME=${OPTARG} 306 | HOST_PREFIX="${CLUSTER_NAME}-host" 307 | ;; 308 | d) 309 | DELETE_FLAG="true" 310 | ;; 311 | h) 312 | print_help 313 | exit 1 314 | ;; 315 | i) 316 | SHOW_IP_FLAG="true" 317 | ;; 318 | p) 319 | STOP_FLAG="true" 320 | ;; 321 | P) 322 | START_FLAG="true" 323 | ;; 324 | l) 325 | LIST_HOSTS_FLAG="true" 326 | ;; 327 | M) 328 | HOST_MACHINE_SIZE=${OPTARG} 329 | ;; 330 | M) 331 | SERVER_MACHINE_SIZE=${OPTARG} 332 | ;; 333 | n) 334 | HOST_COUNT=${OPTARG} 335 | ;; 336 | R) 337 | REPOS=${OPTARG} 338 | ;; 339 | r) 340 | REGISTRATION_CMD="${OPTARG}" 341 | ;; 342 | s) 343 | SERVER_IMAGE="${OPTARG}" 344 | ;; 345 | u) 346 | MIRROR_URL="${OPTARG}" 347 | ;; 348 | \?) 349 | echoerr "Invalid arguemnts" 350 | print_help 351 | exit 1 352 | ;; 353 | :) 354 | echoerr "Option -${OPTARG} requires an argument." 355 | print_help 356 | exit 1 357 | ;; 358 | esac 359 | done 360 | 361 | shift $((OPTIND-1)) 362 | if [[ "$1" ]]; then 363 | CLUSTER_NAME=$1 364 | fi 365 | 366 | if [ "$#" -gt 1 ]; then 367 | echoerr "Unexpected option after cluster_name: $2" 368 | exit 1 369 | fi 370 | 371 | HOST_PREFIX="${CLUSTER_NAME}-host" 372 | 373 | main 374 | -------------------------------------------------------------------------------- /bin/user_data_clusters: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | username=$1 6 | sshKey=$2 7 | 8 | build_file=$3 9 | container=$4 10 | docker_url=$5 11 | docker_command=$6 12 | 13 | if [ -z ${docker_command} ]; then 14 | slice_index=7 15 | else 16 | slice_index=8 17 | fi 18 | 19 | all_var=($@) 20 | declare -a docker_opts=("${all_var[@]:${slice_index}}") 21 | 22 | if [ -z "${docker_command}" ]; then 23 | declare -a docker_line=('"docker"', ${docker_opts[@]}, "\"${container}\"") 24 | else 25 | declare -a docker_line=('"docker"', ${docker_opts[@]}, "\"${container}\"", "\"${docker_command}\"") 26 | fi 27 | 28 | cat > ${build_file} << EOF 29 | #cloud-config 30 | users: 31 | - name: ${username} 32 | groups: 33 | - sudo 34 | shell: /bin/bash 35 | sudo: ['ALL=(ALL) NOPASSWD:ALL'] 36 | ssh-authorized-keys: 37 | - ${sshKey} 38 | runcmd: 39 | - [ "sleep", "10" ] 40 | - [ "modprobe", "arptable_filter" ] 41 | - [ "curl", "-o", "/tmp/docker_install.sh", "-sSL", "${docker_url}" ] 42 | - [ "/bin/sh", "/tmp/docker_install.sh" ] 43 | - [ "service", "docker", "start" ] 44 | - [ "sleep", "10" ] 45 | - [ "usermod", "-aG", "docker", "${username}" ] 46 | - [ ${docker_line[@]} ] 47 | EOF 48 | --------------------------------------------------------------------------------