forked from rancher/quickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rancher#3 from Oats87/do-userdata-scripts-backport
Backporting superseb's fixes for userdata
- Loading branch information
Showing
3 changed files
with
86 additions
and
32 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,63 @@ | ||
#!/bin/bash -x | ||
apt update | ||
apt -y install curl vim jq | ||
curl https://releases.rancher.com/install-docker/${docker_version_server}.sh | sh | ||
|
||
until docker inspect rancher/rancher:${rancher_version} > /dev/null 2>&1; do | ||
docker pull rancher/rancher:${rancher_version} | ||
sleep 2 | ||
export curlimage=appropriate/curl | ||
export jqimage=stedolan/jq | ||
|
||
if [ `command -v curl` ]; then | ||
curl -sL https://releases.rancher.com/install-docker/${docker_version_server}.sh | sh | ||
elif [ `command -v wget` ]; then | ||
wget -qO- https://releases.rancher.com/install-docker/${docker_version_server}.sh | sh | ||
fi | ||
|
||
for image in $curlimage $jqimage "rancher/rancher:${rancher_version}"; do | ||
until docker inspect $image > /dev/null 2>&1; do | ||
docker pull $image | ||
sleep 2 | ||
done | ||
done | ||
|
||
docker run -d -p 80:80 -p 443:443 -v /root/rancher:/var/lib/rancher rancher/rancher:${rancher_version} | ||
docker run -d --restart=unless-stopped -p 80:80 -p 443:443 -v /root/rancher:/var/lib/rancher rancher/rancher:${rancher_version} | ||
|
||
while ! curl -k https://localhost/ping; do sleep 3; done | ||
while true; do | ||
docker run --rm --net=host $curlimage -sLk https://127.0.0.1/ping && break | ||
sleep 5 | ||
done | ||
|
||
# Login | ||
LOGINRESPONSE=`curl -s 'https://127.0.0.1/v3-public/localProviders/local?action=login' -H 'content-type: application/json' --data-binary '{"username":"admin","password":"admin"}' --insecure` | ||
LOGINTOKEN=`echo $LOGINRESPONSE | jq -r .token` | ||
while true; do | ||
|
||
LOGINRESPONSE=$(docker run \ | ||
--rm \ | ||
--net=host \ | ||
$curlimage \ | ||
-s "https://127.0.0.1/v3-public/localProviders/local?action=login" -H 'content-type: application/json' --data-binary '{"username":"admin","password":"admin"}' --insecure) | ||
LOGINTOKEN=$(echo $LOGINRESPONSE | docker run --rm -i $jqimage -r .token) | ||
echo "Login Token is $LOGINTOKEN" | ||
if [ "$LOGINTOKEN" != "null" ]; then | ||
break | ||
else | ||
sleep 5 | ||
fi | ||
done | ||
|
||
|
||
# Change password | ||
curl -s 'https://127.0.0.1/v3/users?action=changepassword' -H 'content-type: application/json' -H "Authorization: Bearer $LOGINTOKEN" --data-binary '{"currentPassword":"admin","newPassword":"${admin_password}"}' --insecure | ||
docker run --rm --net=host $curlimage -s 'https://127.0.0.1/v3/users?action=changepassword' -H 'content-type: application/json' -H "Authorization: Bearer $LOGINTOKEN" --data-binary '{"currentPassword":"admin","newPassword":"${admin_password}"}' --insecure | ||
|
||
# Create API key | ||
APIRESPONSE=`curl -s 'https://127.0.0.1/v3/token' -H 'content-type: application/json' -H "Authorization: Bearer $LOGINTOKEN" --data-binary '{"type":"token","description":"automation"}' --insecure` | ||
APIRESPONSE=$(docker run --rm --net=host $curlimage -s 'https://127.0.0.1/v3/token' -H 'content-type: application/json' -H "Authorization: Bearer $LOGINTOKEN" --data-binary '{"type":"token","description":"automation"}' --insecure) | ||
|
||
# Extract and store token | ||
APITOKEN=`echo $APIRESPONSE | jq -r .token` | ||
APITOKEN=`echo $APIRESPONSE | docker run --rm -i $jqimage -r .token` | ||
|
||
# Configure server-url | ||
RANCHER_SERVER="https://$(curl -s http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address)" | ||
curl -s 'https://127.0.0.1/v3/settings/server-url' -H 'content-type: application/json' -H "Authorization: Bearer $APITOKEN" -X PUT --data-binary '{"name":"server-url","value":"'$RANCHER_SERVER'"}' --insecure | ||
RANCHER_SERVER="https://$(docker run --rm --net=host $curlimage -s http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address)" | ||
docker run --rm --net=host $curlimage -s 'https://127.0.0.1/v3/settings/server-url' -H 'content-type: application/json' -H "Authorization: Bearer $APITOKEN" -X PUT --data-binary '{"name":"server-url","value":"'$RANCHER_SERVER'"}' --insecure | ||
|
||
# Create cluster | ||
CLUSTERRESPONSE=`curl -s 'https://127.0.0.1/v3/cluster' -H 'content-type: application/json' -H "Authorization: Bearer $APITOKEN" --data-binary '{"type":"cluster","nodes":[],"rancherKubernetesEngineConfig":{"ignoreDockerVersion":true},"name":"${cluster_name}"}' --insecure` | ||
CLUSTERRESPONSE=$(docker run --rm --net=host $curlimage -s 'https://127.0.0.1/v3/cluster' -H 'content-type: application/json' -H "Authorization: Bearer $APITOKEN" --data-binary '{"type":"cluster","rancherKubernetesEngineConfig":{"addonJobTimeout":30,"ignoreDockerVersion":true,"sshAgentAuth":false,"type":"rancherKubernetesEngineConfig","authentication":{"type":"authnConfig","strategy":"x509"},"network":{"type":"networkConfig","plugin":"canal"},"ingress":{"type":"ingressConfig","provider":"nginx"},"services":{"type":"rkeConfigServices","kubeApi":{"podSecurityPolicy":false,"type":"kubeAPIService"},"etcd":{"snapshot":false,"type":"etcdService","extraArgs":{"heartbeat-interval":500,"election-timeout":5000}}}},"name":"${cluster_name}"}' --insecure) | ||
|
||
# Extract clusterid to use for generating the docker run command | ||
CLUSTERID=`echo $CLUSTERRESPONSE | jq -r .id` | ||
CLUSTERID=`echo $CLUSTERRESPONSE | docker run --rm -i $jqimage -r .id` | ||
|
||
# Generate registrationtoken | ||
curl -s 'https://127.0.0.1/v3/clusterregistrationtoken' -H 'content-type: application/json' -H "Authorization: Bearer $APITOKEN" --data-binary '{"type":"clusterRegistrationToken","clusterId":"'$CLUSTERID'"}' --insecure | ||
docker run --rm --net=host $curlimage -s 'https://127.0.0.1/v3/clusterregistrationtoken' -H 'content-type: application/json' -H "Authorization: Bearer $APITOKEN" --data-binary '{"type":"clusterRegistrationToken","clusterId":"'$CLUSTERID'"}' --insecure |
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