-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit_copy.sh
executable file
·247 lines (212 loc) · 8.09 KB
/
init_copy.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/bin/bash
# Function to validate Ethereum account key
validate_ethereum_key() {
local key=$1
if [[ ${#key} -eq 66 && $key == 0x* ]]; then
echo "Valid Ethereum key."
return 0
else
echo "Invalid Ethereum key. Please ensure it starts with '0x' and is 66 characters long."
return 1
fi
}
# Function to create .env file and write to it
# Function to create .env file and write to it
write_env() {
local key=$1
local value=$2
local env_file=".env"
# Check if the .env file exists
if [[ -f $env_file ]]; then
# Check if the key exists in the .env file
if grep -q "^${key}=" "$env_file"; then
# Update the existing key with the new value
sed -i '' "s/^${key}=.*/${key}=${value}/" "$env_file"
else
# Add the new key-value pair to the .env file
echo "${key}=${value}" >> "$env_file"
fi
else
# Create the .env file and add the key-value pair
echo "${key}=${value}" > "$env_file"
fi
}
# Function to prompt for a non-blank project name
get_project_name() {
while true; do
echo "Choose a name for your project: "
read project_name
if [[ -z "$project_name" ]]; then
echo "Project name cannot be blank. Please enter a valid name."
else
echo "You have chosen the project name: $project_name"
break
fi
done
}
# Function to display the options
display_options() {
echo "Options:"
for i in "${!options[@]}"; do
echo "$((i + 1))) ${options[$i]}"
done
}
# ask for a project name
get_project_name
echo ""
PS3="Select the type of code to be ran during the compute layer (default is Nodenity): "
options=("Nodenithy" "Pynithy" "Custom")
default_option="Nodenithy"
while true; do
display_options
read -p "$PS3" REPLY
if [[ -z "$REPLY" ]]; then
service_type=$default_option
echo "No option selected. Defaulting to $default_option."
break
elif [[ "$REPLY" =~ ^[0-9]+$ ]] && [[ "$REPLY" -ge 1 && "$REPLY" -le ${#options[@]} ]]; then
service_type=${options[$REPLY-1]}
echo "You have selected the $service_type."
break
else
echo "Invalid option $REPLY. Please select a valid number."
fi
done
echo ""
if [ "$service_type" == "Custom" ]; then
# Ask about using custom images
echo "To use custom images, you need to specify the image tag, Docker login, password, and repository URL for the base image."
echo "Enter Docker repository URL: "
read docker_repo_url
echo "Enter Docker Login (username): "
read docker_login
echo "Enter Password: "
read docker_password
echo "Enter the image tag: "
read BASE_IMAGE_TAG
echo "Validating docker login credentials..."
echo "validating docker image tag..."
fi
echo ""
# Blockchain network setup
echo "On which Blockchain network do you want to have the app set up, as a starting point? (you can change it later in the .env file)"
echo "Note: The Ethernity Cloud supports Bloxberg and Polygon networks."
PS3="Select an option (default is Bloxberg Testnet): "
options=("Bloxberg Mainnet" "Bloxberg Testnet" "Polygon Mainnet" "Polygon Amoy Testnet")
default_option="Bloxberg Testnet"
while true; do
display_options
read -p "$PS3" REPLY
if [[ -z "$REPLY" ]]; then
blockchain_network=$default_option
echo "No option selected. Defaulting to $default_option."
break
elif [[ "$REPLY" =~ ^[0-9]+$ ]] && [[ "$REPLY" -ge 1 && "$REPLY" -le ${#options[@]} ]]; then
blockchain_network=${options[$REPLY-1]}
echo "You have selected the $blockchain_network."
break
else
echo "Invalid option $REPLY. Please select a valid number."
fi
done
echo ""
echo "Checking if the project name (image name) is available on the "${blockchain_network// /_}" network and ownership..."
python $(pwd)/node_modules/ethernity-cloud-sdk-js/nodenithy/run/image_registry.py "${blockchain_network// /_}" "${project_name// /-}" v3
echo ""
# IPFS details
echo "Select the IPFS pinning service you want to use: "
PS3="Select an option (default is Ehternity): "
options=("Ehternity (best effort)" "Custom IPFS") #"Pinata" "Infura" have free tiers
default_option="Ehternity (best effort)"
while true; do
display_options
read -p "$PS3" REPLY
if [[ -z "$REPLY" ]]; then
ipfs_service=$default_option
echo "No option selected. Defaulting to $default_option."
break
elif [[ "$REPLY" =~ ^[0-9]+$ ]] && [[ "$REPLY" -ge 1 && "$REPLY" -le ${#options[@]} ]]; then
ipfs_service=${options[$REPLY-1]}
echo "You have selected the $ipfs_service."
break
else
echo "Invalid option $REPLY. Please select a valid number."
fi
done
echo ""
if [ "$ipfs_service" == "Custom IPFS" ]; then
echo "Enter the endpoint URL for the IPFS pinning service you want to use: "
read custom_url
echo "Enter the acceess token to be used when calling the IPFS pinning service: "
read ipfs_token
fi
if [ "$ipfs_service" == "Ehternity (best effort)" ]; then
echo "Using the Ehternity IPFS service endpoint."
custom_url="https://ipfs.ethernity.cloud:5001"
fi
echo ""
# # Add project prerequisites based on the selected service
# if [ "$service_type" == "1" ]; then
# echo "Adding prerequisites for Pynity..."
# elif [ "$service_type" == "2" ]; then
# echo "Adding prerequisites for Nodenity..."
# fi
# Create src and src/serverless folders
mkdir -p src/serverless
# Ask if the user wants the app template as a starting point
echo "Do you want an 'Hello World' app template as a starting point? (yes/no)"
PS3="Select an option (default is yes): "
options=("yes" "no")
default_option="yes"
while true; do
display_options
read -p "$PS3" REPLY
if [[ -z "$REPLY" ]]; then
use_app_template=$default_option
echo "No option selected. Defaulting to $default_option."
break
elif [[ "$REPLY" =~ ^[0-9]+$ ]] && [[ "$REPLY" -ge 1 && "$REPLY" -le ${#options[@]} ]]; then
use_app_template=${options[$REPLY-1]}
echo "You have selected the $use_app_template."
break
else
echo "Invalid option $REPLY. Please select a valid number."
fi
done
echo ""
if [ "$use_app_template" == "yes" ]; then
echo "Bringing Frontend/Backend templates..."
echo " src/serverless/backend.js (Hello World function)"
echo " src/ec_helloworld_example.js (Hello World function call - Frontend)"
# copy all files from node_modules/ethernity-cloud-sdk-js/nodenithy/src/ to src/
cp -r node_modules/ethernity-cloud-sdk-js/nodenithy/src/* src/
echo "Installing required packages..."
npm install @ethernity-cloud/[email protected] @testing-library/[email protected] @testing-library/[email protected] @testing-library/[email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected]
else
echo "Define backend functions in src/ectasks to be available for Frontend interaction."
fi
write_env "PROJECT_NAME" "${project_name// /_}"
write_env "SERVICE_TYPE" "$service_type"
if [ "$service_type" == "Custom" ]; then
write_env "BASE_IMAGE_TAG" "$base_image_tag"
write_env "DOCKER_REPO_URL" "$docker_repo_url"
write_env "DOCKER_LOGIN" "$docker_login"
write_env "DOCKER_PASSWORD" "$docker_password"
elif [ "$service_type" == "Nodenithy" ]; then
write_env "BASE_IMAGE_TAG" "$BASE_IMAGE_TAG"
write_env "DOCKER_REPO_URL" "registry.scontain.com:5050"
write_env "DOCKER_LOGIN" ""
write_env "DOCKER_PASSWORD" ""
elif [ "$service_type" == "Pynithy" ]; then
write_env "BASE_IMAGE_TAG" "$BASE_IMAGE_TAG"
write_env "DOCKER_REPO_URL" "$docker_repo_url"
write_env "DOCKER_LOGIN" "$docker_login"
write_env "DOCKER_PASSWORD" "$docker_password"
fi
# we need to "$blockchain_network" replace any space with _
write_env "BLOCKCHAIN_NETWORK" "${blockchain_network// /_}"
write_env "IPFS_ENDPOINT" "$custom_url"
write_env "IPFS_TOKEN" "$ipfs_token"
write_env "VERSION" "v3"
write_env "CI_COMMIT_BRANCH" "${project_name// /_}"
echo "To start the application, run the appropriate start command based on your setup."