Last active
February 13, 2017 05:55
-
-
Save mohamed-el-habib/5076fe2f16c0bb268af9aab6a50205ff to your computer and use it in GitHub Desktop.
swarm in rancher using 3 docker-machine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -x | |
set -e errexit | |
set -o pipefail | |
username="admin-$(echo $RANDOM % 1000 + 1 | bc)" | |
password="$(uuidgen)" # change it | |
rancherServerId=$(docker-machine ip host1) | |
rancherServerPort="8080" | |
# TODO user="-u \"${CATTLE_ACCESS_KEY}:${CATTLE_SECRET_KEY}\"" | |
user="" | |
curl ${user} -X POST \ | |
-H 'Accept: application/json' \ | |
-H 'Content-Type: application/json' \ | |
-d '{"accessMode":"unrestricted", "enabled":true, "name":"'${username}'", "password":"'${password}'", "username":"'${username}'"}' \ | |
'http://'${rancherServerId}':'${rancherServerPort}'/v2-beta/localauthconfigs' | |
echo "${username}:${password}" > admin-user.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -x | |
set -e errexit | |
set -o pipefail | |
rancherServerId=$(docker-machine ip host1) | |
rancherServerPort="8080" | |
swarmEnvId=$(more environment.json | jq -r .id) | |
export $(cat apikey.txt | xargs) | |
user="-u ${ACCESS_KEY}:${SECRET_KEY}" | |
curl ${user} \ | |
-X POST \ | |
-H 'Accept: application/json' \ | |
-H 'Content-Type: application/json' \ | |
-d '{"description":"private docker registry", "name":"private-registry", "serverAddress":"docker.private.registry.domain"}' \ | |
'http://'${rancherServerId}':'${rancherServerPort}'/v2-beta/projects/'${swarmEnvId}'/registries' > registry.json | |
registryId=$(more registry.json | jq -r .id) | |
registryUser="registryUser" | |
registryPassword="registryPassword" | |
registryEmail="an.email@domain" | |
curl ${user} \ | |
-X POST \ | |
-H 'Accept: application/json' \ | |
-H 'Content-Type: application/json' \ | |
-d '{"description":"private registry credential", "name":"jenkins", "publicValue":"'${registryUser}'", "registryId":"'${registryId}'", "secretValue":"'${registryPassword}'", "email":"'${registryEmail}'"}' \ | |
'http://'${rancherServerId}':'${rancherServerPort}'/v2-beta/projects/'${swarmEnvId}'/registrycredentials' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -x | |
set -e errexit | |
set -o pipefail | |
rancherServerId=$(docker-machine ip host1) | |
rancherServerPort="8080" | |
# TODO user="-u \"${CATTLE_ACCESS_KEY}:${CATTLE_SECRET_KEY}\"" | |
user="" | |
swarmEnvId=$(more environment.json | jq -r .id) | |
curl ${user} -X POST \ | |
-H 'Accept: application/json' \ | |
-H 'Content-Type: application/json' \ | |
-d '{"description":"jenkins api key", "name":"jenkins-api-key"}' \ | |
'http://'${rancherServerId}':'${rancherServerPort}'/v2-beta/projects/'${swarmEnvId}'/apikeys' > apikey.json | |
ACCESS_KEY=$(more apikey.json | jq .publicValue) | |
SECRET_KEY=$(more apikey.json | jq .secretValue) | |
echo "ACCESS_KEY=${ACCESS_KEY}" > apikey.txt | |
echo "SECRET_KEY=${SECRET_KEY}" >> apikey.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#set -x | |
set -e errexit | |
set -o pipefail | |
## create three docker machine | |
declare -A hostsIp | |
for host in 1 2 3 ; do | |
docker-machine create --driver virtualbox host$host | |
hostsIp[$host]=$(docker-machine ip host$host) | |
done | |
echo "ips = ${hostsIp[@]}" | |
rancherServerMachineName="host1" | |
rancherServerId=${hostsIp[1]} | |
rancherServerPort="8080" | |
## launch rancher server into host1 | |
eval $(docker-machine env $rancherServerMachineName) && docker run -d --restart=unless-stopped --name rancher -p ${rancherServerPort}:8080 rancher/server | |
echo "Waiting until rancher server is ready ..." | |
until [ "$(curl --fail 'http://'${rancherServerId}':'${rancherServerPort}'/ping')" == "pong" ]; do | |
printf '.' | |
sleep 5 | |
done | |
# TODO user="-u \"${CATTLE_ACCESS_KEY}:${CATTLE_SECRET_KEY}\"" | |
user="" | |
echo "Waiting for environment templates list to be loaded ..." | |
until [ -n "$(curl ${user} "http://${rancherServerId}:${rancherServerPort}/v2-beta/projecttemplates" | jq -r '.data[] | select(.name | contains("Swarm")) | .id')" ]; do | |
printf '.' | |
sleep 5 | |
done | |
echo "Get the swarm environment template id ..." | |
swamTemplateId=$(curl ${user} "http://${rancherServerId}:${rancherServerPort}/v2-beta/projecttemplates" | jq -r '.data[] | select(.name | contains("Swarm")) | .id') | |
echo "Creating a new swarm environment ..." | |
curl ${user} -X POST \ | |
-H 'Accept: application/json' \ | |
-H 'Content-Type: application/json' \ | |
-d '{"description":"swarm environment", "name":"swarm-env", "projectTemplateId":"'$swamTemplateId'", "allowSystemRole":false, "members":[], "virtualMachine":false, "servicesPortRange":null, "projectLinks":[]}' \ | |
'http://'${rancherServerId}':'${rancherServerPort}'/v2-beta/project' > environment.json | |
echo "Setting the host API url to http://${rancherServerId}:${rancherServerPort}..." | |
curl ${user} -X PUT \ | |
-H 'Content-Type: application/json' \ | |
-d '{"id":"api.host","type":"activeSetting","baseType":"setting","name":"api.host","activeValue":null,"inDb":false,"source":null,"value":"http://'${rancherServerId}':'${rancherServerPort}'"}' \ | |
'http://'${rancherServerId}':'${rancherServerPort}'/v2-beta/settings/api.host' > host.api.json | |
echo "Requesting the creation of one registration token..." | |
swarmEnvId=$(more environment.json | jq -r .id) | |
curl ${user} -X POST \ | |
-H 'Accept: application/json' \ | |
-H 'Content-Type: application/json' \ | |
-d '{}' \ | |
'http://'${rancherServerId}':'${rancherServerPort}'/v2-beta/projects/'${swarmEnvId}'/registrationtokens' > registrationtokens.json | |
until [ "$(more registrationtokens.json | jq .status)" != "403" ]; do | |
curl ${user} -X POST \ | |
-H 'Accept: application/json' \ | |
-H 'Content-Type: application/json' \ | |
-d '{}' \ | |
'http://'${rancherServerId}':'${rancherServerPort}'/v2-beta/projects/'${swarmEnvId}'/registrationtokens' > registrationtokens.json | |
printf '.' | |
sleep 5 | |
done | |
echo "Waiting for registration tokens to be ready..." | |
until [ -n "$(curl ${user} -s $(more registrationtokens.json | jq -r .links.self) | jq -r .command)" ]; do | |
printf '.' | |
sleep 5 | |
done | |
## Add hosts to the new swarm environement | |
for host in 1 2 3 ; do | |
echo "adding host $host ..." | |
eval $(docker-machine env host$host) && eval $(curl ${user} -s $(more registrationtokens.json | jq -r .links.self) | jq -r .command | sed 's|sudo docker run |docker run -e CATTLE_AGENT_IP="'${hostsIp[$host]}'" |g') | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment