forked from Leverege/deprecated-microchip-avr-iot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
91 lines (72 loc) · 2.87 KB
/
setup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/bash
CLOUD_REGION=us-central1
# color variables
RED='\033[0;31m'
BLU='\033[0;34m'
GRN='\033[0;32m'
PUR='\033[0;35m'
NC='\033[0m'
printf "\n${BLU}***********************************************\n\n"
printf "Welcome to the AVR-IoT interactive quick setup\n\n"
printf "***********************************************${NC}\n\n"
# get device name, project name and device public key from user
echo
read -p 'Please enter device UID: ' DEVICE_ID
echo
# let user choose to set IoT core registry name
REG_NAME=""
ATTEMPT=0
# check that REG_NAME matches pattern
while ! [[ $REG_NAME =~ ^[a-zA-Z]{1}[a-zA-Z0-9+%~._-]{2,254} ]]
do
# display hint if user has made 1 or more attempt
if (( ATTEMPT > 0 )); then
printf "\n${RED}IoT Core Registry names must be between 3-255 characters,\nstart with a letter, and contain only letters, numbers and\n the following characters:\n"
echo '- . % ~ +'
echo
printf "${NC}"
fi
# get user REG_NAME input
read -p 'Choose an IoT Core registry name (return for AVR-IOT): ' REG_NAME
ATTEMPT=$((ATTEMPT + 1))
# set to default if no text entered
if [ "$REG_NAME" = "" ]; then
REG_NAME="AVR-IOT"
fi
# strip white space
REG_NAME="$(echo "${REG_NAME}" | tr -d '[:space:]')"
done
# set the project and tell firebase to use it firebase
gcloud config set project $GOOGLE_CLOUD_PROJECT
firebase use $GOOGLE_CLOUD_PROJECT
# enable cloud functions, IoT core, and pub sub
gcloud services enable cloudfunctions.googleapis.com cloudiot.googleapis.com pubsub.googleapis.com
# create pubsub topic
gcloud pubsub topics create avr-iot
# create IoT core device registry
gcloud iot registries create $REG_NAME --region=$CLOUD_REGION --event-notification-config=topic=avr-iot
# add device to registry
printf "\n${BLU}Creating IoT core registry ${REG_NAME}${NC}"
gcloud iot devices create "d$DEVICE_ID" --region=$CLOUD_REGION --registry=$REG_NAME
#install npm dependencies
printf "${BLU}Installing Cloud Function dependencies (this may take a few minutes)...\n${NC}"
npm install --prefix ./functions/
printf "\n${BLU}Installing UI dependencies (this may take a few minutes)...\n${NC}"
npm install --prefix ./ui/
# retrieve UI config vars
firebase setup:web > config.txt
node getFirebaseConfig.js config.txt
# build UI
printf "${BLU}Creating a production build of the UI (this may take a few minutes)...\n${NC}"
npm run build --prefix ./ui
chmod +x ./ui/src/Config.js
firebase deploy --only functions:recordMessage
firebase deploy --only database
firebase deploy --only hosting
printf "\n${GRN}**************************************\n\n"
printf "Setup complete!\n\n"
printf "${PUR}Remember to add your device\'s public key in the registry:\n\n"
printf "https://console.cloud.google.com/iot/registries\n\n"
printf "${GRN}Once you\'ve added the public key, checkout your app:\n\n"
printf "${GOOGLE_CLOUD_PROJECT}.firebaseapp.com/device/${DEVICE_ID}\n\n"
printf "**************************************\n\n${NC}"