Skip to content

Instantly share code, notes, and snippets.

@toolboc
Last active May 2, 2019 01:39
Show Gist options
  • Save toolboc/a0f701c3adc5553414e83811c737bade to your computer and use it in GitHub Desktop.
Save toolboc/a0f701c3adc5553414e83811c737bade to your computer and use it in GitHub Desktop.
Provision edge runtime for GPU / CPU platorms on Ubuntu 16.04 (Requires az-cli & jq)
#!/bin/bash
usage(){
echo "***Azure IoT Edge GPU Provisioning Script***"
echo "Usage: ./edgeprovision.sh <iothub.name> <environment> <platform>”
}
provision(){
install_docker
if [ $platform == "gpu" ]
then
install_nvidia_docker
fi
install_iot_edge_runtime
configure_iot_edge_runtime
}
install_docker(){
echo "***Installing Docker***"
docker -v
# Check if docker installed
if [ $? -ne 0 ]
then
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
sudo apt-get install -y docker-ce=18.03.1~ce-0~ubuntu
else
echo "***Docker already installed***"
fi
sudo usermod -aG docker $USER
}
install_nvidia_docker(){
echo "***Installing Nvidia Docker***"
nvidia-docker -v
# Check if nvidia-docker installed
if [ $? -ne 0 ]
then
# Add the kernel headers to support the nvida module
sudo apt-get install -y gcc jq linux-headers-$(uname -r)
# Add the package repositories
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_9.2.88-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu1604_9.2.88-1_amd64.deb
sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub
sudo apt-get update
sudo apt-get install -y cuda
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | \
sudo apt-key add -
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | \
sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt-get update
# Install nvidia-docker2 and reload the Docker daemon configuration
sudo apt-get install -y nvidia-docker2
sudo pkill -SIGHUP dockerd
else
echo "***Nvidia Docker already installed***"
fi
#Replace daemon.json contents to point to nvidia container runtime
cat <<'EOF' | sudo tee /etc/docker/daemon.json
{
"default-runtime": "nvidia",
"runtimes": {
"nvidia": {
"path": "nvidia-container-runtime",
"runtimeArgs": []
}
}
}
EOF
sudo service docker restart
sudo service docker restart
}
install_iot_edge_runtime(){
echo "***Installing IoT Edge Runtime***"
# Install repository configuration
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > ./microsoft-prod.list
sudo cp ./microsoft-prod.list /etc/apt/sources.list.d/
# Install Microsoft GPG public key
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
sudo cp ./microsoft.gpg /etc/apt/trusted.gpg.d/
sudo apt-get update
sudo apt-get install iotedge
}
configure_iot_edge_runtime(){
echo "***Configuring IoT Edge Runtime***"
az iot hub device-identity create --device-id $(hostname) --hub-name $iothub_name --edge-enabled
connectionString=$(az iot hub device-identity show-connection-string --device-id $(hostname) --hub-name $iothub_name | jq -r '.cs')
az iot hub device-twin update --device-id $(hostname) --hub-name $iothub_name --set tags='{"environment":"'$environment'","platform":"'$platform'"}'
IP=$(ifconfig eth0 | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p')
cat <<EOF > /etc/iotedge/config.yaml
provisioning:
source: "manual"
device_connection_string: "$connectionString"
agent:
name: "edgeAgent"
type: "docker"
env: {}
config:
image: "mcr.microsoft.com/azureiotedge-agent:1.0"
auth: {}
hostname: $(cat /proc/sys/kernel/hostname)
connect:
management_uri: "http://$IP:15580"
workload_uri: "http://$IP:15581"
listen:
management_uri: "http://$IP:15580"
workload_uri: "http://$IP:15581"
homedir: "/var/lib/iotedge"
moby_runtime:
docker_uri: "/var/run/docker.sock"
network: "azure-iot-edge"
EOF
cat /etc/iotedge/config.yaml
iotedged -c /etc/iotedge/config.yaml
}
# Arguments
iothub_name=$1
environment=$2
platform=$3
# Check Arguments
[ "$#" -ne 3 ] && { usage && exit 1; } || provision
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment