-
Notifications
You must be signed in to change notification settings - Fork 33
/
win-gg-install.py
executable file
·124 lines (105 loc) · 4.85 KB
/
win-gg-install.py
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
# Copyright 2023 Amazon.com, Inc. and its affiliates. All Rights Reserved.
# Licensed under the Amazon Software License (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
# http://aws.amazon.com/asl/
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.
# This is to deploy Greengrass natively without automated provisioning
# it uses the environmental variable file .env in the current directory
import shutil
import core_resource_handler
import json
import sys
import os
from pathlib import Path
import dotenv
import pyuac
import subprocess
import yaml
def clean_config(dirs_to_clean: list):
"""remove greengrass root folder, to reset greengrass state"""
print("Cleaning greengrass ...")
for dir in dirs_to_clean:
path = Path(dir)
if path.exists():
print(f"Deleting files in {path}")
try:
shutil.rmtree(path)
except PermissionError as e:
print("Cannot clean greengrass folder, please stop Greengrass and try again (sc stop greengrass)", e)
exit(1)
#os.makedirs(path, 0o777)
print(f"Directory '{dir} cleaned")
def copy_creds_to_tmp(config_yaml, source_dir):
# copy credentials to tmp folder
with open(config_yaml, 'r') as f:
config = yaml.safe_load(f)
file_dst = config['system']['rootCaPath']
dir_dst = os.path.dirname(file_dst)
try:
shutil.copytree(source_dir, dir_dst, dirs_exist_ok=True)
except Exception as e:
print(e)
exit(1)
print("Credentials copied to tmp folder")
return
if __name__ == '__main__':
if not pyuac.isUserAdmin():
print("The script must be executed as admin")
exit(1)
# if file .env does not exist in current directory stop the program
if not os.path.isfile('.env'):
print('.env file does not exist in current directory. Execute python deploy.py')
exit(1)
else:
dotenv.load_dotenv()
GGC_ROOT_PATH = os.getenv('GGC_ROOT_PATH')
PROVISION = os.getenv('PROVISION')
AWS_DEFAULT_REGION = os.getenv("AWS_DEFAULT_REGION")
COMPONENT_DEFAULT_USER = os.getenv('COMPONENT_DEFAULT_USER')
DEPLOY_DEV_TOOLS = os.getenv('DEPLOY_DEV_TOOLS')
INIT_CONFIG = os.getenv('INIT_CONFIG')
THING_NAME = os.getenv("THING_NAME")
THING_GROUP_NAME = os.getenv("THING_GROUP_NAME")
THING_POLICY_NAME = os.getenv("THING_POLICY_NAME")
TES_ROLE_NAME = os.getenv("TES_ROLE_NAME")
TES_ROLE_ALIAS_NAME = os.getenv("TES_ROLE_ALIAS_NAME")
print(GGC_ROOT_PATH)
print(INIT_CONFIG)
print(AWS_DEFAULT_REGION)
print(COMPONENT_DEFAULT_USER)
print(DEPLOY_DEV_TOOLS)
print(THING_NAME)
print(PROVISION)
# check if Greengrass is already installed
if (GGC_ROOT_PATH is None) or (INIT_CONFIG is None) or (AWS_DEFAULT_REGION is None) or (COMPONENT_DEFAULT_USER is None) or (TES_ROLE_ALIAS_NAME is None) or (THING_NAME is None):
print("Some Environmental variables are empty, check or re-run the gg-docker-deploy.py script")
print(os.environ)
exit(1)
# verify the path using getcwd()
cwd = os.getcwd()
# print the current directory
print("Current working directory is:", cwd)
clean_config([GGC_ROOT_PATH])
print('Greengrass resources deleted')
print("Install Greengrass")
# certificates and private key are set to point to /tmp/certs but they are created in ./docker/volumes/certs
copy_creds_to_tmp(INIT_CONFIG, './docker/volumes/certs')
options = [
"--setup-system-service","true", "--provision","false", "--deploy-dev-tools",f"{DEPLOY_DEV_TOOLS}",
"--aws-region", f"{AWS_DEFAULT_REGION}", "--start", "false", "--tes-role-alias-name", f"{TES_ROLE_ALIAS_NAME}",
"--tes-role-name", f"{TES_ROLE_NAME}", "--thing-name", f"{THING_NAME}",
"--thing-group-name", f"{THING_GROUP_NAME}", "--thing-policy-name", f"{THING_POLICY_NAME}",
"--provision", "false", "--init-config", f"{INIT_CONFIG}", "--start", "true",
"--setup-system-service", "true",
"--component-default-user", "ggc_user"]
results = subprocess.call(['java', f"-Droot={GGC_ROOT_PATH}", "-Dlog.store=FILE",'-jar', 'greengrass-nucleus/lib/Greengrass.jar']+options, shell=False)
if results:
print("Greengrass installation failed")
exit(1)
loader_path = f"{GGC_ROOT_PATH}/alts/init/distro/bin/loader"
print(loader_path)
results = subprocess.call(['cmd.exe', loader_path], shell=False)