Skip to content

Instantly share code, notes, and snippets.

@leandroscardua
Forked from doodlesbykumbi/script.sh
Created June 9, 2022 19:12
Show Gist options
  • Select an option

  • Save leandroscardua/8a9c9488afe4a87c4208eb4476d1928f to your computer and use it in GitHub Desktop.

Select an option

Save leandroscardua/8a9c9488afe4a87c4208eb4476d1928f to your computer and use it in GitHub Desktop.
Automation for single node Rancher in Docker
# TODO: inside any container consuming Rancher API
# echo "172.17.0.1 local.com" >> /etc/hosts
# Run single node
docker run -d --name rancher --restart=unless-stopped \
-p 80:80 -p 443:443 \
--privileged \
rancher/rancher:latest
# Wait for node to be ready
while ! curl -k https://localhost/ping; do sleep 3; done
# Login
ADMINPASS=$(docker logs rancher 2>/dev/null | grep Password: | awk '{ print $6 }')
LOGINRESPONSE=$(curl -s 'https://127.0.0.1/v3-public/localProviders/local?action=login' -H 'content-type: application/json' --data-binary '{"username":"admin","password":"'$ADMINPASS'"}' --insecure)
LOGINTOKEN=$(echo $LOGINRESPONSE | jq -r .token)
# 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":"'$ADMINPASS'","newPassword":"thisisyournewpassword"}' --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)
# Extract and store token
APITOKEN=$(echo $APIRESPONSE | jq -r .token)
# Update server-url
RANCHER_SERVER=https://172.17.0.1
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 > /dev/null
# Get kubeconfig for local cluster
CLUSTERCONFIG=$(curl -s https://127.0.0.1/v3/clusters/local?action=generateKubeconfig -X POST -H 'content-type: application/json' -H "Authorization: Bearer $APITOKEN" --insecure | \
jq -r .config | ruby -r yaml -r json -e 'puts JSON.pretty_generate(YAML.load(ARGF.read))')
CLUSTERTOKEN=$(echo $CLUSTERCONFIG | jq -r .users[0].user.token)
CLUSTERAPIURL=$(echo $CLUSTERCONFIG | jq -r .clusters[0].cluster.server)
CLUSTERCERT=$(echo $CLUSTERCONFIG | jq -r '.clusters[0].cluster["certificate-authority-data"]' | base64 -d)
echo "
CLUSTERTOKEN=${CLUSTERTOKEN}
CLUSTERAPIURL=${CLUSTERAPIURL}
CLUSTERCERT=${CLUSTERCERT}
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment