1
+ #! /usr/bin/env bash
2
+ set -euo pipefail
3
+
4
+ # Fancy colors for output
5
+ RED=' \033[0;31m'
6
+ GREEN=' \033[0;32m'
7
+ YELLOW=' \033[0;33m'
8
+ NC=' \033[0m' # No Color aka reset
9
+
10
+ # Version to install. Defaults to latest or set by --version or -v
11
+ VERSION=" "
12
+
13
+ # Print in colors - 0=green, 1=red, 2=neutral
14
+ # e.g. fancy_print 0 "All is great"
15
+ fancy_print () {
16
+ if [[ $1 == 0 ]]; then
17
+ echo -e " ${GREEN}${2}${NC} "
18
+ elif [[ $1 == 1 ]]; then
19
+ echo -e " ${RED}${2}${NC} "
20
+ elif [[ $1 == 3 ]]; then
21
+ echo -e " ${YELLOW}${2}${NC} "
22
+ else
23
+ echo -e " ${2} "
24
+ fi
25
+ }
26
+
27
+ # Function to print the help message
28
+ print_help () {
29
+ fancy_print 2 " "
30
+ fancy_print 2 " ---- Spin Installer Script ----"
31
+ fancy_print 2 " This script installs Spin in the current directory."
32
+ fancy_print 2 " "
33
+ fancy_print 2 " Command line arguments"
34
+ fancy_print 2 " --version or -v : Provide what version to install e.g. \" v0.8.0\" or \" canary\" ."
35
+ fancy_print 2 " --help or -h : Shows this help message"
36
+ }
37
+
38
+ # Function used to check if utilities are available
39
+ require () {
40
+ if ! hash " $1 " & > /dev/null; then
41
+ fancy_print 1 " '$1 ' not found in PATH. This is required for this script to work."
42
+ exit 1
43
+ fi
44
+ }
45
+
46
+ # Parse input arguments
47
+ while [[ $# -gt 0 ]]; do
48
+ case $1 in
49
+ ' --version' | -v)
50
+ shift
51
+ if [[ $# -ne 0 ]]; then
52
+ VERSION=" ${1} "
53
+ else
54
+ fancy_print 1 " Please provide the desired version. e.g. --version v0.8.0 or -v canary"
55
+ exit 0
56
+ fi
57
+ ;;
58
+ ' --help' | -h)
59
+ shift
60
+ print_help
61
+ ;;
62
+ * )
63
+ fancy_print 1 " Unknown argument ${1} ."
64
+ print_help
65
+ exit 1
66
+ ;;
67
+ esac
68
+ shift
69
+ done
70
+
71
+ # Check all required utilities are available
72
+ require curl
73
+ require tar
74
+ require uname
75
+
76
+ # Check if we're on a supported system and get OS and processor architecture to download the right version
77
+ UNAME_ARC=$( uname -m)
78
+
79
+ case $UNAME_ARC in
80
+ " x86_64" )
81
+ ARC=" amd64"
82
+ ;;
83
+ " arm64" | " aarch64" )
84
+ ARC=" aarch64"
85
+ ;;
86
+ * )
87
+ fancy_print 1 " The Processor type: ${UNAME_ARC} is not yet supported by Spin."
88
+ exit 1
89
+ ;;
90
+ esac
91
+
92
+ case $OSTYPE in
93
+ " linux-gnu" * )
94
+ OS=" linux"
95
+ ;;
96
+ " darwin" * )
97
+ OS=" macos"
98
+ ;;
99
+ * )
100
+ fancy_print 1 " The OSTYPE: ${OSTYPE} is not supported by this script."
101
+ fancy_print 2 " Please refer to this article to install Spin: https://wasm-functions.fermyon.app/fermyon-wasm-functions/quickstart"
102
+ exit 1
103
+ ;;
104
+ esac
105
+
106
+ # Check desired version. Default to latest if no desired version was requested
107
+ if [[ $VERSION = " " ]]; then
108
+ VERSION=$( curl -so- https://github.com/spinframework/spin/releases | grep ' href="/spinframework/spin/releases/tag/v[0-9]*.[0-9]*.[0-9]*\"' | sed -E ' s/.*\/spinframework\/spin\/releases\/tag\/(v[0-9\.]+)".*/\1/g' | head -1)
109
+ fi
110
+
111
+ # Constructing download FILE and URL
112
+ FILE=" spin-${VERSION} -${OS} -${ARC} .tar.gz"
113
+ URL=" https://github.com/spinframework/spin/releases/download/${VERSION} /${FILE} "
114
+
115
+ # Establish the location of current working environment
116
+ current_dir=$( pwd)
117
+
118
+ # Define Spin directory name
119
+ spin_directory_name=(" /spin" )
120
+
121
+ if [ -d " ${current_dir}${spin_directory_name} " ]; then
122
+ fancy_print 1 " Error: .${spin_directory_name} already exists, please delete ${current_dir}${spin_directory_name} and run the installer again."
123
+ exit 1
124
+ fi
125
+
126
+ # Download file, exit if not found - e.g. version does not exist
127
+ fancy_print 0 " Step 1: Downloading: ${URL} "
128
+ curl -fsOL $URL || (fancy_print 1 " Error downloading the file: ${FILE} " ; exit 1)
129
+ fancy_print 0 " Done...\n"
130
+
131
+ # Decompress the file
132
+ fancy_print 0 " Step 2: Decompressing: ${FILE} "
133
+ tar xfv $FILE
134
+ ./spin --version
135
+ fancy_print 0 " Done...\n"
136
+
137
+ # Remove the compressed file
138
+ fancy_print 0 " Step 3: Removing the downloaded tarball"
139
+ rm $FILE
140
+ fancy_print 0 " Done...\n"
141
+
142
+ # Install default templates
143
+ fancy_print 0 " Step 4: Installing default templates"
144
+ ./spin templates install --git " https://github.com/spinframework/spin" --upgrade
145
+ ./spin templates install --git " https://github.com/spinframework/spin-python-sdk" --upgrade
146
+ ./spin templates install --git " https://github.com/spinframework/spin-js-sdk" --upgrade
147
+
148
+ fancy_print 0 " Step 5: Installing default plugins"
149
+ ./spin plugins update
150
+ ./spin plugins install aka --yes
151
+
152
+ if [[ $VERSION = " canary" ]]; then
153
+ fancy_print 3 " Warning: You are using canary Spin, but templates use latest stable SDKs."
154
+ fancy_print 3 " Be sure to update templates to point to 'canary' SDKs."
155
+ fi
156
+ # Direct to quicks-start doc
157
+ fancy_print 0 " You're good to go. Check here for the next steps: https://wasm-functions.fermyon.app/fermyon-wasm-functions/quickstart"
158
+ fancy_print 0 " Run './spin' to get started"
0 commit comments