Skip to content

Commit e217a01

Browse files
authored
Add assemble_gcp rule that encapsulates generating config (#128)
## What is the goal of this PR? First part of making AWS/GCP rules more user-friendly (issue #124) ## What are the changes implemented in this PR? - Adds `generate_json_config` rule which allows to substitute values in template - Adds `assemble_gcp` which generates config and calls `assemble_packer`. Sample usage: ``` assemble_gcp( name = "assemble-gcp-snapshot", files = GCP_FILES, install = "//deployment/deploy-gcp-image/files:install.sh", project_id = "grakn-dev", zone = "europe-west1-b", image_name = "grakn-kgms-snapshot-{{user `version`}}", image_licenses = 'projects/grakn-public/global/licenses/grakn-kgms-premium' ) ```
1 parent 1afb74c commit e217a01

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed

common/generate_json_config.bzl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
20+
def _generate_json_config_impl(ctx):
21+
ctx.actions.expand_template(
22+
template = ctx.file.template,
23+
output = ctx.outputs.config,
24+
substitutions = ctx.attr.substitutions
25+
)
26+
27+
generate_json_config = rule(
28+
attrs = {
29+
"template": attr.label(
30+
allow_single_file = [".json"]
31+
),
32+
"substitutions": attr.string_dict()
33+
},
34+
implementation = _generate_json_config_impl,
35+
outputs = {
36+
"config": "%{name}.json"
37+
}
38+
)

gcp/BUILD

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
20+
exports_files(["packer.template.json"])

gcp/packer.template.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"variables": {
3+
"gcp_account_file": "{{env `DEPLOY_PACKER_GCP_ACCOUNT_FILE`}}",
4+
"version": "{{env `DEPLOY_PACKER_VERSION`}}"
5+
},
6+
"builders": [
7+
{
8+
"type": "googlecompute",
9+
"account_file": "{{user `gcp_account_file`}}",
10+
"project_id": "{project_id}",
11+
"source_image_family": "ubuntu-1604-lts",
12+
"zone": "{zone}",
13+
"ssh_username": "ubuntu",
14+
"image_name": "{image_name}",
15+
"image_licenses": ["{image_licenses}"]
16+
}
17+
],
18+
19+
"provisioners": [
20+
{
21+
"type": "shell",
22+
"inline": [ "mkdir /tmp/deployment" ]
23+
},
24+
{
25+
"type": "file",
26+
"source": "files/",
27+
"destination": "/tmp/deployment/"
28+
},
29+
{
30+
"type": "shell",
31+
"inline": [ "sudo /tmp/deployment/{install}" ]
32+
}
33+
]
34+
}

gcp/rules.bzl

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
20+
load("//packer:rules.bzl", "assemble_packer")
21+
load("//common:generate_json_config.bzl", "generate_json_config")
22+
23+
24+
def assemble_gcp(name,
25+
project_id,
26+
install,
27+
zone,
28+
image_name,
29+
image_licenses,
30+
files):
31+
install_fn = Label(install).name
32+
generated_config_target_name = name + "__do_not_reference_config"
33+
generate_json_config(
34+
name = generated_config_target_name,
35+
template = "@graknlabs_bazel_distribution//gcp:packer.template.json",
36+
substitutions = {
37+
"{project_id}": project_id,
38+
"{zone}": zone,
39+
"{image_name}": image_name,
40+
"{image_licenses}": image_licenses,
41+
"{install}": install_fn
42+
}
43+
)
44+
files[install] = install_fn
45+
assemble_packer(
46+
name = name,
47+
config = generated_config_target_name,
48+
files = files
49+
)

0 commit comments

Comments
 (0)