diff --git a/jenkins/add-docker-build-job.sh b/jenkins/add-docker-build-job.sh index 6b9eb4f..812c7b0 100644 --- a/jenkins/add-docker-build-job.sh +++ b/jenkins/add-docker-build-job.sh @@ -7,7 +7,7 @@ Command Arguments --jenkins_url|-j [Required]: Jenkins URL --jenkins_user_name|-ju [Required]: Jenkins user name - --jenkins_password|-jp [Required]: Jenkins password + --jenkins_password|-jp : Jenkins password. If not specified and the user name is "admin", the initialAdminPassword will be used --git_url|-g [Required]: Git URL with a Dockerfile in it's root --registry|-r [Required]: Registry url targeted by the pipeline --registry_user_name|-ru [Required]: Registry user name @@ -131,7 +131,9 @@ done throw_if_empty --jenkins_url $jenkins_url throw_if_empty --jenkins_user_name $jenkins_user_name -throw_if_empty --jenkins_password $jenkins_password +if [ "$jenkins_user_name" != "admin" ]; then + throw_if_empty --jenkins_password $jenkins_password +fi throw_if_empty --git_url $git_url throw_if_empty --registry $registry throw_if_empty --registry_user_name $registry_user_name @@ -194,6 +196,11 @@ function retry_until_successful { #download jenkins cli (wait for Jenkins to be online) retry_until_successful wget ${jenkins_url}/jnlpJars/jenkins-cli.jar -O jenkins-cli.jar +if [ -z "$jenkins_password" ]; then + # NOTE: Intentionally setting this after the first retry_until_successful to ensure the initialAdminPassword file exists + jenkins_password=`sudo cat /var/lib/jenkins/secrets/initialAdminPassword` +fi + #install the required plugins retry_until_successful java -jar jenkins-cli.jar -s ${jenkins_url} install-plugin "credentials" -deploy --username ${jenkins_user_name} --password ${jenkins_password} retry_until_successful java -jar jenkins-cli.jar -s ${jenkins_url} install-plugin "workflow-cps" -deploy --username ${jenkins_user_name} --password ${jenkins_password} diff --git a/quickstart_template/201-jenkins-to-azure-container-registry.sh b/quickstart_template/201-jenkins-to-azure-container-registry.sh index a2d84e0..5ca19b7 100644 --- a/quickstart_template/201-jenkins-to-azure-container-registry.sh +++ b/quickstart_template/201-jenkins-to-azure-container-registry.sh @@ -11,6 +11,7 @@ Arguments --registry|-r : Registry url targeted by the pipeline --registry_user_name|-ru : Registry user name --registry_password|-rp : Registry password + --repository|-rr : Repository targeted by the pipeline --artifacts_location|-al : Url used to reference other scripts/artifacts. --sas_token|-st : A sas token needed if the artifacts location is private. EOF @@ -29,6 +30,7 @@ function throw_if_empty() { #defaults include_docker_build_pipeline="0" artifacts_location="https://raw.githubusercontent.com/Azure/azure-devops-utils/master/" +repository="${vm_user_name}/myfirstapp" while [[ $# > 0 ]] do @@ -55,6 +57,10 @@ do registry_password="$1" shift ;; + --repository|-rr) + repository="$1" + shift + ;; --include_docker_build_pipeline|-i) include_docker_build_pipeline="$1" shift @@ -94,8 +100,10 @@ sudo apt-get install jenkins --yes sudo apt-get install jenkins --yes # sometime the first apt-get install jenkins command fails, so we try it twice sudo apt-get install git --yes -#install docker -sudo curl -sSL https://get.docker.com/ | sh +#install docker if not already installed +if !(command -v docker >/dev/null); then + sudo curl -sSL https://get.docker.com/ | sh +fi #make sure jenkins has access to docker cli sudo gpasswd -a jenkins docker @@ -106,8 +114,5 @@ if [[ "${include_docker_build_pipeline}" == "1" ]] then echo "Including the pipeline" - #get password and call build creation script - admin_password=`sudo cat /var/lib/jenkins/secrets/initialAdminPassword` - - curl --silent "${artifacts_location}/jenkins/add-docker-build-job.sh${artifacts_location_sas_token}" | sudo bash -s -- -j "http://localhost:8080/" -ju "admin" -jp "${admin_password}" -g "${git_url}" -r "${registry}" -ru "${registry_user_name}" -rp "${registry_password}" -rr "${vm_user_name}/myfirstapp" + curl --silent "${artifacts_location}/jenkins/add-docker-build-job.sh${artifacts_location_sas_token}" | sudo bash -s -- -j "http://localhost:8080/" -ju "admin" -g "${git_url}" -r "${registry}" -ru "${registry_user_name}" -rp "${registry_password}" -rr "$repository" fi diff --git a/quickstart_template/301-jenkins-acr-spinnaker-k8s.sh b/quickstart_template/301-jenkins-acr-spinnaker-k8s.sh new file mode 100644 index 0000000..6793567 --- /dev/null +++ b/quickstart_template/301-jenkins-acr-spinnaker-k8s.sh @@ -0,0 +1,146 @@ +#!/bin/bash + +function print_usage() { + cat <&2 + print_usage + exit -1 + fi +} + +#Set defaults +pipeline_port="8000" +artifacts_location="https://raw.githubusercontent.com/Azure/azure-devops-utils/master/" +docker_repository="${vm_user_name}/myfirstapp" + +while [[ $# > 0 ]] +do + key="$1" + shift + case $key in + --client_id|-ci) + client_id="$1" + shift + ;; + --client_key|-ck) + client_key="$1" + shift + ;; + --subscription_id|-si) + subscription_id="$1" + shift + ;; + --tenant_id|-ti) + tenant_id="$1" + shift + ;; + --user_name|-un) + user_name="$1" + shift + ;; + --git_repository|-gr) + git_repository="$1" + shift + ;; + --resource_group|-rg) + resource_group="$1" + shift + ;; + --master_fqdn|-mf) + master_fqdn="$1" + shift + ;; + --master_count|-mc) + master_count="$1" + shift + ;; + --storage_account_name|-san) + storage_account_name="$1" + shift + ;; + --storage_account_key|-sak) + storage_account_key="$1" + shift + ;; + --azure_container_registry|-acr) + azure_container_registry="$1" + shift + ;; + --docker_repository|-dr) + docker_repository="$1" + shift + ;; + --pipeline_port|-pp) + pipeline_port="$1" + shift + ;; + --artifacts_location|-al) + artifacts_location="$1" + shift + ;; + --sas_token|-st) + artifacts_location_sas_token="$1" + shift + ;; + --help|-help|-h) + print_usage + exit 13 + ;; + *) + echo "ERROR: Unknown argument '$key' to script '$0'" 1>&2 + exit -1 + esac +done + +throw_if_empty --client_id $client_id +throw_if_empty --client_key $client_key +throw_if_empty --subscription_id $subscription_id +throw_if_empty --tenant_id $tenant_id +throw_if_empty --user_name $user_name +throw_if_empty --git_repository $git_repository +throw_if_empty --resource_group $resource_group +throw_if_empty --master_fqdn $master_fqdn +throw_if_empty --master_count $master_count +throw_if_empty --storage_account_name $storage_account_name +throw_if_empty --storage_account_key $storage_account_key +throw_if_empty --azure_container_registry $azure_container_registry +throw_if_empty --docker_repository $docker_repository +throw_if_empty --pipeline_port $pipeline_port + +include_docker_build_pipeline="1" +include_kubernetes_pipeline="1" +pipeline_registry="$azure_container_registry" +front50_port="8081" + +# Configure Spinnaker (do this first because the default InstallSpinnaker.sh script sets up front50 on port 8080 and that might fail if we did Jenkins first) +curl --silent "${artifacts_location}quickstart_template/spinnaker_vm_to_kubernetes.sh${artifacts_location_sas_token}" | sudo bash -s -- -ci "$client_id" -ck "$client_key" -si "$subscription_id" -ti "$tenant_id" -un "$user_name" -rg "$resource_group" -mf "$master_fqdn" -mc "$master_count" -san "$storage_account_name" -sak "$storage_account_key" -acr "$azure_container_registry" -ikp "$include_kubernetes_pipeline" -prg "$pipeline_registry" -prp "$docker_repository" -pp "$pipeline_port" -fp "$front50_port" -al "$artifacts_location" -st "$artifacts_location_sas_token" + +# Configure Jenkins +curl --silent "${artifacts_location}quickstart_template/201-jenkins-to-azure-container-registry.sh${artifacts_location_sas_token}" | sudo bash -s -- -i "$include_docker_build_pipeline" -u "$user_name" -g "$git_repository" -r "https://$azure_container_registry" -ru "$client_id" -rp "$client_key" -rr "$docker_repository" -al "$artifacts_location" -st "$artifacts_location_sas_token" diff --git a/quickstart_template/spinnaker_vm_to_kubernetes.sh b/quickstart_template/spinnaker_vm_to_kubernetes.sh index f80fa99..cff7601 100644 --- a/quickstart_template/spinnaker_vm_to_kubernetes.sh +++ b/quickstart_template/spinnaker_vm_to_kubernetes.sh @@ -21,6 +21,7 @@ Arguments --pipeline_registry|-prg : Registry to target in the pipeline --pipeline_repository|-prp : Repository to target in the pipeline --pipeline_port|-pp : Port to target in your pipeline + --front50_port|-fp : Port used for Front50, defaulted to 8080 --artifacts_location|-al : Url used to reference other scripts/artifacts. --sas_token|-st : A sas token needed if the artifacts location is private. EOF @@ -42,6 +43,7 @@ pipeline_registry="index.docker.io" pipeline_repository="lwander/spin-kub-demo" pipeline_port="8000" artifacts_location="https://raw.githubusercontent.com/Azure/azure-devops-utils/master/" +front50_port=8080 while [[ $# > 0 ]] do @@ -108,6 +110,10 @@ do pipeline_port="$1" shift ;; + --front50_port|-fp) + front50_port="$1" + shift + ;; --artifacts_location|-al) artifacts_location="$1" shift @@ -137,6 +143,7 @@ throw_if_empty --master_count $master_count throw_if_empty --storage_account_name $storage_account_name throw_if_empty --storage_account_key $storage_account_key throw_if_empty --azure_container_registry $azure_container_registry +throw_if_empty --front50_port $front50_port spinnaker_kube_config_file="/home/spinnaker/.kube/config" kubectl_file="/usr/local/bin/kubectl" @@ -145,10 +152,18 @@ docker_hub_registry="index.docker.io" # Configure Spinnaker to use Azure Storage curl --silent "${artifacts_location}spinnaker/install_spinnaker/install_spinnaker.sh${artifacts_location_sas_token}" | sudo bash -s -- -san "$storage_account_name" -sak "$storage_account_key" -al "$artifacts_location" -st "$artifacts_location_sas_token" +# Front50 conflicts with the default Jenkins port, so allow for using a different port +if [ "$front50_port" != "8080" ]; then + sudo sed -i "s|front50:|front50:\n port: $front50_port|" /opt/spinnaker/config/spinnaker-local.yml + sudo service spinnaker restart # We have to restart all services so that they know how to communicate to front50 +fi + # Install Azure cli -curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash - -sudo apt-get -y install nodejs -sudo npm install -g azure-cli +if !(command -v azure >/dev/null); then + curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash - + sudo apt-get -y install nodejs + sudo npm install -g azure-cli +fi # Login to azure cli using service principal azure telemetry --disable @@ -170,10 +185,12 @@ fi curl --silent "${artifacts_location}spinnaker/configure_k8s/configure_k8s.sh${artifacts_location_sas_token}" | sudo bash -s -- -rg "$azure_container_registry" -ci "$client_id" -ck "$client_key" -rp "$docker_repository" -al "$artifacts_location" -st "$artifacts_location_sas_token" # Install and setup Kubernetes cli for admin user -sudo curl -L -s -o $kubectl_file https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl -sudo chmod +x $kubectl_file -mkdir -p /home/${user_name}/.kube -sudo cp $spinnaker_kube_config_file /home/${user_name}/.kube/config +if !(command -v kubectl >/dev/null); then + sudo curl -L -s -o $kubectl_file https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl + sudo chmod +x $kubectl_file + mkdir -p /home/${user_name}/.kube + sudo cp $spinnaker_kube_config_file /home/${user_name}/.kube/config +fi # Create pipeline if enabled if (( $include_kubernetes_pipeline )); then @@ -183,8 +200,10 @@ if (( $include_kubernetes_pipeline )); then docker_account_name="azure-container-registry" pipeline_registry="$azure_container_registry" - # Install docker CLI - curl -sSL https://get.docker.com/ | sh + #install docker if not already installed + if !(command -v docker >/dev/null); then + sudo curl -sSL https://get.docker.com/ | sh + fi sudo gpasswd -a $user_name docker # Add (virtually) empty container to ACR to properly initialize Spinnaker. This fixes two bugs: