Skip to content

Commit

Permalink
#584: added commandlets for docker and kubernetes (#570)
Browse files Browse the repository at this point in the history
  • Loading branch information
isandesh1986 authored Jul 8, 2021
1 parent 47e0763 commit 0d89a96
Show file tree
Hide file tree
Showing 9 changed files with 360 additions and 0 deletions.
2 changes: 2 additions & 0 deletions documentation/cli.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ The following commandlets are currently available:

* link:build.asciidoc[build]
* link:cobigen.asciidoc[cobigen]
* link:docker.asciidoc[docker]
* link:eclipse.asciidoc[eclipse]
* link:gradle.asciidoc[gradle]
* link:help.asciidoc[help]
Expand All @@ -50,6 +51,7 @@ The following commandlets are currently available:
* link:jasypt.asciidoc[jasypt]
* link:java.asciidoc[java]
* link:jenkins.asciidoc[jenkins]
* link:kubernetes.asciidoc[kubernetes]
* link:mvn.asciidoc[mvn]
* link:ng.asciidoc[ng]
* link:node.asciidoc[node]
Expand Down
4 changes: 4 additions & 0 deletions documentation/devonfw-ide-usage.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ include::cli.asciidoc[leveloffset=2]

include::build.asciidoc[leveloffset=3]

include::docker.asciidoc[leveloffset=3]

include::eclipse.asciidoc[leveloffset=3]

include::gradle.asciidoc[leveloffset=3]
Expand All @@ -34,6 +36,8 @@ include::java.asciidoc[leveloffset=3]

include::jenkins.asciidoc[leveloffset=3]

include::kubernetes.asciidoc[leveloffset=3]

include::mvn.asciidoc[leveloffset=3]

include::ng.asciidoc[leveloffset=3]
Expand Down
28 changes: 28 additions & 0 deletions documentation/docker.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
:toc:
toc::[]

= docker

The `docker` commandlet allows to install https://www.docker.com/[docker]. This command is implemented to currently work on Windows. Other OS are not supported yet.

.Usage of `devon docker`

On Windows

* Checks whether https://docs.microsoft.com/en-us/windows/wsl/install-win10[Windows Subsystem for Linux(WSL)] has been enabled and any linux distribution has been installed.

* If yes, checks whether Docker has already been installed either on Windows or on WSL.

* If yes, program quits since Docker is already available.

* If not, this commandlet will install Docker on WSL

The arguments (`devon docker «args»`) are explained by the following table:

.Usage of `devon docker`
[options="header"]
|=======================
|*Argument(s)* |*Meaning*
|`setup` |setup Docker (install and verify) as per above flow.
|`«args»` |call docker with the specified arguments. Call `docker help` for details or use docker directly as preferred." (`«args»`)
|=======================
30 changes: 30 additions & 0 deletions documentation/kubernetes.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
:toc:
toc::[]

= kubernetes

The `kubernetes` commandlet allows to install https://kubernetes.io/[kubernetes]. This command is implemented to currently work on Windows. Other OS are not supported yet.

.Usage of `devon kubernetes`

On Windows

* Checks whether https://docs.microsoft.com/en-us/windows/wsl/install-win10[Windows Subsystem for Linux(WSL)] has been enabled and any linux distribution has been installed.

* If yes, checks whether Kubernetes has already been installed either on Windows or on WSL.

* If yes, program quits since Kubernetes is already available.

* If not, this will install Kubernetes on WSL along with https://k3d.io[K3D]

* As part of the setup, K3D will create a cluster with a single node with a default name as "devonfw-cluster"

The arguments (`devon kubernetes «args»`) are explained by the following table:

.Usage of `devon kubernetes`
[options="header"]
|=======================
|*Argument(s)* |*Meaning*
|`setup` |setup Kubernetes (install and verify) as per above flow.
|`«args»` |call kubernetes(kubectl) with the specified arguments. Call `kubectl help` for details or use kubectl directly as preferred. (`«args»`)
|=======================
107 changes: 107 additions & 0 deletions scripts/src/main/resources/scripts/command/docker
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/usr/bin/env bash

# autocompletion list
if [ "${1}" = "shortlist" ]
then
if [ -z "${2}" ]
then
echo "setup help"
fi
exit
fi

if [ -n "${DEVON_IDE_TRACE}" ]; then set -vx; fi
# shellcheck source=scripts/functions
source "$(dirname "${0}")"/../functions

# $1: optional setup
function doSetup() {
if doIsDockerInstalled
then
doEcho "docker is already installed at $(command -v docker)"
exit 0
else
if doIsBatch
then
doFail "Interactive installation is required, cannot proceed in batch mode. Please rerun without batch option."
fi
#Start setup
if doIsWindows
then
if doIsWSLEnabled
then
# Verify if docker is installed on Windows Subsystem for Linux(WSL)
docker_win_version=$(wsl docker version 2>/dev/null)
if [[ $docker_win_version == *"Client:"* ]];then
if [ "${1}" = "setup" ]
then
doEcho Docker already installed on WSL.
wsl docker version
fi
if [[ $docker_win_version != *"Server:"* ]];then
doEcho Please start Docker engine on WSL.
fi
else
doEcho Docker not installed on WSL.
# Install Docker on WSL
wsl bash ../scripts/setup-docker
fi
else
return 255
fi
else
doEcho "Sorry, docker-installation support is not yet implemented for your OS. Please install manually or help devonfw-ide to support it for your OS by contributing a pull-request."
exit 0
fi
fi
}

# Call Docker with specified arguments.
function doRun() {
if doSetup
then
# Run docker command if installed.
if doIsDockerInstalled
then
doEcho "Running: docker ${*}"
docker "${@}"
# Run docker command on Windows WSL if installed.
elif doIsWSLEnabled
then
if doIsDockerInstalledOnWSL
then
doEcho "Running: docker ${*} on WSL."
wsl docker "${@}"
fi
fi
fi
}

# OS independent check if docker is installed.
function doIsDockerInstalled() {
if command -v docker &> /dev/null
then
echo "docker is already installed at $(command -v docker)"
return
else
return 255
fi
}

# CLI
case ${1} in
"help" | "-h")
echo "Setup docker."
echo
echo "Arguments:"
echo " setup setup docker on Windows WSL."
echo " «args» call docker with the specified arguments. Call docker help for details or use docker directly as preferred."
echo
;;
"setup" | "s" | "")
doSetup setup
;;
*)
doRun "${@}"
;;
esac
116 changes: 116 additions & 0 deletions scripts/src/main/resources/scripts/command/kubernetes
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#!/usr/bin/env bash

# autocompletion list
if [ "${1}" = "shortlist" ]
then
if [ -z "${2}" ]
then
echo "setup help"
fi
exit
fi

if [ -n "${DEVON_IDE_TRACE}" ]; then set -vx; fi
# shellcheck source=scripts/functions
source "$(dirname "${0}")"/../functions

# $1: optional setup
function doSetup() {
if doIsKubernetesInstalled
then
doEcho "Kubernetes is already installed at $(command -v kubectl)"
exit 0
else
if doIsBatch
then
doFail "Interactive installation is required, cannot proceed in batch mode. Please rerun without batch option."
fi

if doIsWindows
then
if doIsWSLEnabled
then
# Check if Kubernetes is already installed on WSL
if doIsKubernetesInstalledOnWSL
then
if [ "${1}" = "setup" ]
then
echo Kubernetes is already installed on WSL and ready to use. devonfw-ide did not make any changes.
wsl kubectl version
fi
else
echo Kubernetes not installed on WSL.
# Install Kubernetes on WSL
wsl bash ../scripts/setup-kubernetes
fi
else
return 255
fi
else
echo "Sorry, kubernetes-installation support is not yet implemented for your OS. Please install manually or help devonfw-ide to support it for your OS by contributing a pull-request."
exit 0
fi
fi
}


# Call Kubernetes with specified arguments
function doRun() {
if doSetup
then
# Run Kubernetes command if installed on Windows.
if doIsKubernetesInstalled
then
doEcho "Running: kubectl ${*}"
kubectl "${@}"
# Run Kubernetes command on WSL if installed on WSL.
elif doIsWSLEnabled
then
if doIsKubernetesInstalledOnWSL
then
doEcho "Running: kubectl ${*} on WSL."
wsl kubectl "${@}"
else
echo "Kubernetes is not installed neither on Windows nor on WSL. Please call devon kubernetes setup to install on WSL."
fi
fi
fi
}

# OS independent check if Kubernetes is installed.
function doIsKubernetesInstalled() {
if command -v kubectl &> /dev/null
then
return
else
return 255
fi
}

# Checks if Kubernetes is installed on Windows Subsystem for Linux(WSL)
function doIsKubernetesInstalledOnWSL() {
if wsl command -v kubectl &> /dev/null
then
return
else
return 255
fi
}

# CLI
case ${1} in
"help" | "-h")
echo "Setup Kubernetes."
echo
echo "Arguments:"
echo " setup setup Kubernetes on Windows WSL."
echo " «args» call Kubernetes with the specified arguments. Call kubernetes help for details or use Kubernetes(kubectl) directly as preferred."
echo
;;
"setup" | "s" | "")
doSetup setup
;;
*)
doRun "${@}"
;;
esac
30 changes: 30 additions & 0 deletions scripts/src/main/resources/scripts/functions
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,34 @@ function doIsPackageJsonContainingScript() {
fi
}

# Checks if Windows Subsystem for Linux(WSL) is enabled or not.
function doIsWSLEnabled() {
if doIsWindows
then
wsl_distro_list=$(wsl -l)
if [[ $wsl_distro_list != *"no installed distributions"* ]];
then
return
else
doEcho "WSL is disabled or No Linux distributions found in WSL. Please enable WSL and install any linux distribution to proceed with docker setup."
doEcho "Refer here to setup WSL. https://docs.microsoft.com/en-us/windows/wsl/install-win10"
return 255
fi
else
return 255
fi
}

# Checks if docker is installed on Windows Subsystem for Linux(WSL)
function doIsDockerInstalledOnWSL() {
if wsl command -v docker &> /dev/null
then
return
else
return 255
fi
}

# $1: single CLI arg
# returns 0 if a standard option was detected and handled, 255 otherwise (regular argument to be handeled by CLI parser)
function doParseOption() {
Expand Down Expand Up @@ -1195,3 +1223,5 @@ while [ -n "${1}" ] && doParseOption "${1}"
do
shift
done


20 changes: 20 additions & 0 deletions scripts/src/main/resources/scripts/setup-docker
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash
docker_check=$(docker --version 2>/dev/null)
source "$(dirname "${0}")"/functions

if [[ $docker_check != *"Docker version"* ]];then
doEcho "Hint:You may be required to enter sudo user password of your Windows Subsystem for Linux(WSL) when prompted."
doEcho "Installing Docker..."
sudo apt-get update -y
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common gnupg-agent
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update -y
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
sudo service docker start
sudo usermod -aG docker $USER
doEcho Docker installed successfully on WSL. Please allow few moments for the docker engine to start using docker.
else
doEcho Docker already installed on WSL.
docker version
fi
Loading

0 comments on commit 0d89a96

Please sign in to comment.