Skip to content

Commit a1dcdb4

Browse files
fix: Use python3 explicitly and clean up python commands in script-utils.sh. (#36)
* Use python3 explicitly and clean up python commands in script-utils.sh. This fixes the "python: command not found" error when python-is-python3 is not installed. * Ensure map and object keys are sorted.
1 parent 089914c commit a1dcdb4

File tree

2 files changed

+39
-39
lines changed

2 files changed

+39
-39
lines changed

modules/agent-policy/scripts/script-utils.sh

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ LAUNCH_STAGE="beta"
3131
# Return:
3232
# A well-formatted command line flag value for a list of strings
3333
function get_formatted_list_of_strings() {
34-
local formatted
35-
local python="python -c 'import json, sys;"
36-
python="$python list_of_strings = json.load(sys.stdin);"
37-
python="$python print (\",\".join(x for x in list_of_strings))'"
38-
formatted="$(echo "$1" | eval "$python")"
39-
echo "$formatted"
34+
local -a python_cmd=(
35+
'import json, sys;'
36+
'list_of_strings = json.load(sys.stdin);'
37+
'print (",".join(x for x in list_of_strings))'
38+
)
39+
echo "$1" | python3 -c "${python_cmd[*]}"
4040
}
4141

4242

@@ -45,14 +45,14 @@ function get_formatted_list_of_strings() {
4545
# Return:
4646
# A well-formatted command line flag value for a list of objects
4747
function get_formatted_list_of_objects() {
48-
local formatted
49-
local python="python -c 'import json, sys;"
50-
python="$python list_of_objs = json.load(sys.stdin);"
51-
python="$python print (\";\".join(\",\".join([\"{}={}\".format(k.replace(\"_\", \"-\"),"
52-
python="$python str(v).lower() if type(v) is bool else v) for k, v in obj.items()])"
53-
python="$python for obj in list_of_objs))'"
54-
formatted="$(echo "$1" | eval "$python")"
55-
echo "$formatted"
48+
local -a python_cmd=(
49+
'import json, sys;'
50+
'list_of_objs = json.load(sys.stdin);'
51+
'print (";".join(",".join(["{}={}".format(k.replace("_", "-"),'
52+
'str(v).lower() if type(v) is bool else v)'
53+
'for k, v in sorted(obj.items())]) for obj in list_of_objs))'
54+
)
55+
echo "$1" | python3 -c "${python_cmd[*]}"
5656
}
5757

5858

@@ -61,26 +61,26 @@ function get_formatted_list_of_objects() {
6161
# Return:
6262
# A well-formatted command line flag value for a list of list of objects
6363
function get_formatted_list_of_map() {
64-
local formatted
65-
local python="python -c 'import json, sys;"
66-
python="$python list_of_objs = json.load(sys.stdin);"
67-
python="$python print (\";\".join(\",\".join([\"{}={}\".format(k, v)"
68-
python="$python for k, v in obj.items()]) for obj in list_of_objs))'"
69-
formatted="$(echo "$1" | eval "$python")"
70-
echo "$formatted"
71-
64+
local -a python_cmd=(
65+
'import json, sys;'
66+
'list_of_objs = json.load(sys.stdin);'
67+
'print (";".join(",".join(["{}={}".format(k, v)'
68+
'for k, v in sorted(obj.items())]) for obj in list_of_objs))'
69+
)
70+
echo "$1" | python3 -c "${python_cmd[*]}"
7271
}
7372

7473
# Params:
7574
# $1 = output of successful describe command (json format)
7675
# Return:
7776
# the etag in the given string
7877
function get_etag() {
79-
local python="python -c 'import json, sys;"
80-
python="$python json_dump = json.load(sys.stdin);"
81-
python="$python print(json_dump[\"etag\"])'"
82-
formatted="$(echo "$1" | eval "$python")"
83-
echo "$formatted"
78+
local -a python_cmd=(
79+
'import json, sys;'
80+
'json_dump = json.load(sys.stdin);'
81+
'print(json_dump["etag"])'
82+
)
83+
echo "$1" | python3 -c "${python_cmd[*]}"
8484
}
8585

8686

test/agent-policy-tests/test-script-utils.bats

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ setup() {
5555
local expected_command="gcloud beta compute instances ops-agents"
5656
expected_command="$expected_command policies create ops-agents-test-policy"
5757
expected_command="$expected_command --agent-rules='type=metrics'"
58-
expected_command="$expected_command --os-types='version=8,short-name=centos'"
58+
expected_command="$expected_command --os-types='short-name=centos,version=8'"
5959
expected_command="$expected_command --project='test-project-id' --quiet"
6060

6161
run get_create_command "$PROJECT_ID" "$POLICY_ID" \
@@ -77,7 +77,7 @@ setup() {
7777
expected_command="$expected_command policies create ops-agents-test-policy"
7878
expected_command="$expected_command --description='an example test policy'"
7979
expected_command="$expected_command --agent-rules='type=metrics'"
80-
expected_command="$expected_command --os-types='version=8,short-name=centos'"
80+
expected_command="$expected_command --os-types='short-name=centos,version=8'"
8181
expected_command="$expected_command --project='test-project-id' --quiet"
8282

8383
run get_create_command "$PROJECT_ID" "$POLICY_ID" \
@@ -99,10 +99,10 @@ setup() {
9999

100100
local expected_command="gcloud beta compute instances ops-agents"
101101
expected_command="$expected_command policies create ops-agents-test-policy"
102-
expected_command="$expected_command --agent-rules='version=current-major,"
103-
expected_command="${expected_command}type=logging,enable-autoupgrade=true,"
104-
expected_command="${expected_command}package-state=installed;type=metrics'"
105-
expected_command="$expected_command --os-types='version=8,short-name=centos'"
102+
expected_command="$expected_command --agent-rules='enable-autoupgrade=true,"
103+
expected_command="${expected_command}package-state=installed,type=logging,"
104+
expected_command="${expected_command}version=current-major;type=metrics'"
105+
expected_command="$expected_command --os-types='short-name=centos,version=8'"
106106
expected_command="$expected_command --project='test-project-id' --quiet"
107107

108108
run get_create_command "$PROJECT_ID" "$POLICY_ID" \
@@ -124,9 +124,9 @@ setup() {
124124
local expected_command="gcloud beta compute instances ops-agents"
125125
expected_command="$expected_command policies create ops-agents-test-policy"
126126
expected_command="$expected_command --agent-rules='type=metrics'"
127-
expected_command="$expected_command --group-labels='product=myapp,env=prod;"
128-
expected_command="${expected_command}product=myapp,env=staging'"
129-
expected_command="$expected_command --os-types='version=8,short-name=centos'"
127+
expected_command="$expected_command --group-labels='env=prod,product=myapp;"
128+
expected_command="${expected_command}env=staging,product=myapp'"
129+
expected_command="$expected_command --os-types='short-name=centos,version=8'"
130130
expected_command="$expected_command --project='test-project-id' --quiet"
131131

132132
run get_create_command "$PROJECT_ID" "$POLICY_ID" \
@@ -147,7 +147,7 @@ setup() {
147147
local expected_command="gcloud beta compute instances ops-agents"
148148
expected_command="$expected_command policies create ops-agents-test-policy"
149149
expected_command="$expected_command --agent-rules='type=metrics'"
150-
expected_command="$expected_command --os-types='version=8,short-name=centos'"
150+
expected_command="$expected_command --os-types='short-name=centos,version=8'"
151151
expected_command="$expected_command --zones='us-central1-c,"
152152
expected_command="${expected_command}asia-northeast2-b,europe-north1-b'"
153153
expected_command="$expected_command --project='test-project-id' --quiet"
@@ -170,7 +170,7 @@ setup() {
170170
local expected_command="gcloud beta compute instances ops-agents"
171171
expected_command="$expected_command policies create ops-agents-test-policy"
172172
expected_command="$expected_command --agent-rules='type=metrics'"
173-
expected_command="$expected_command --os-types='version=8,short-name=centos'"
173+
expected_command="$expected_command --os-types='short-name=centos,version=8'"
174174
expected_command="$expected_command --instances='zones/us-central1-a/"
175175
expected_command="${expected_command}instances/test-instance'"
176176
expected_command="$expected_command --project='test-project-id' --quiet"
@@ -199,7 +199,7 @@ setup() {
199199
local expected_command="gcloud beta compute instances ops-agents"
200200
expected_command="$expected_command policies update ops-agents-test-policy"
201201
expected_command="$expected_command --agent-rules='type=metrics'"
202-
expected_command="$expected_command --os-types='version=8,short-name=centos'"
202+
expected_command="$expected_command --os-types='short-name=centos,version=8'"
203203
expected_command="$expected_command --clear-group-labels"
204204
expected_command="$expected_command --clear-zones"
205205
expected_command="$expected_command --clear-instances"

0 commit comments

Comments
 (0)