Skip to content

Commit d3b777e

Browse files
authored
Removed policies (#11)
* Application information + output for LB IP + deploy script * Version * gitattributes * Dynamic names for subnet and subnet dns * Choice of compartement for project and DNS * Removed comments and unused fields * Removed policies and moved token creation * Inverted token condition * Update README.md * Readme + bug fix * added branche name to objects + added commit or version to image * Readme
1 parent 3b54694 commit d3b777e

14 files changed

+180
-229
lines changed

README.md

+17-3
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,25 @@ For deploying your Java App with the App Stack, here is the list of OCI prerequi
3636

3737
- **DevOps project (optional):** A Java application in an OCI DevOps project (can be a mirror of an existing GitHub repo). This isn't required if the application is provided as a container image.
3838
- **Database:** an existing Autonomous Database - Shared Infrastructure (ADB-S) can be used with the stack. The stack may create a new one, if specified during the Stack configuration.
39-
- **Vault (optional):** A new user in IAM (<application_name>-user) is created and its token used for connectng to the DevOps repo, is stored in the vault. When the stack is destroyed this user is removed. A Vault is necessary to avoid the limit on the number of tokens the current user has however, the Vault isn't required if the application is provided as a container image.
39+
- **Vault (optional):** An authentication token can either be provided or created by the stack. It is used for connecting to the DevOps repos. A Vault is necessary to store this token, the Vault isn't required if the application is provided as a container image.
4040
- **DNS (optional):** A DNS zone for creating the application URL (for example https://myapp.domain.com). If not provided during the stack configuration, the application will be available through the load balancer's public IP. You can then configure your third-party DNS provider to point to this IP address.
4141
- **HTTPS certificate (optional):** is needed for the load balancer. If no certificate is provided, HTTP will be used against the IP address.
42-
43-
42+
- **IAM:** The following rights are needed:
43+
- Two *dynamic groups* need to be created, one for *container instances* and the other for *DevOps*.
44+
- *Container instances* should be allowed to **read** **repos**. This will allow container instances to fetch the container image generated by the stack from the container registry.
45+
- *DevOps* should be allowed to: **read** **secret-family** and **all-artifacts**; **use** **ons-topics**, **load-balancers** and **virtual-network-family**; and **manage** **devops-family**, **compute-container-instances**, **compute-containers** and **devops-family**. This will allow *DevOps* to build and deploy the application.
46+
```
47+
Allow dynamic-group 'appstack-container-instances' to read repos in tenancy
48+
Allow dynamic-group 'appstack-devops' to read secret-family in tenancy
49+
Allow dynamic-group 'appstack-devops' to read all-artifacts in tenancy
50+
Allow dynamic-group 'appstack-devops' to use ons-topics in tenancy
51+
Allow dynamic-group 'appstack-devops' to use load-balancers in tenancy
52+
Allow dynamic-group 'appstack-devops' to use virtual-network-family in tenancy
53+
Allow dynamic-group 'appstack-devops' to manage compute-container-instances in tenancy
54+
Allow dynamic-group 'appstack-devops' to manage compute-containers in tenancy
55+
Allow dynamic-group 'appstack-devops' to manage devops-family in tenancy
56+
```
57+
4458
## Which Cloud Resources will be used?
4559

4660
The [Oracle Cloud Free Tier service](https://www.oracle.com/cloud/free/) allows you to build, test, and deploy your applications on Oracle Cloud for free. Upon signing up, the service comes with a $300 credit with 30 days expiration; following the expiration or the exhaustion of the credit, most of the provisioned services remain available as [Always Free](https://www.oracle.com/cloud/free/#always-free). You may add additional credit for services that do not fall under Always-Free.

apm.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
resource "oci_apm_apm_domain" "app_apm_domain" {
77
compartment_id = var.compartment_id
8-
display_name = "${var.application_name}-apm-domain"
8+
display_name = "${local.application_name}-apm-domain"
99
is_free_tier = var.is_free_tier
1010
}
1111

build-artifact.yaml.template

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ env:
1313
inputArtifacts:
1414
- name: javaapp
1515
type: GENERIC_ARTIFACT
16-
artifactId: ${artifactId}
16+
artifactId: $${artifactId}
1717
registryId: ${registryId}
18-
path: "javaapp"
19-
version: "0.0.1"
18+
path: ${artifact_path}
19+
version: $${artifact_version}
2020
location: $${OCI_WORKSPACE_DIR}/${config_repo_name}/${fileName}
2121
steps:
2222
- type: Command
@@ -40,7 +40,7 @@ steps:
4040
timeoutInSeconds: 600
4141
failImmediatelyOnError: true
4242
command: |
43-
docker build . --file Dockerfile --tag ${image_remote_tag}
43+
docker build . --file Dockerfile --tag ${image_remote_tag}:${image_tag}-$${artifact_version} --tag ${image_latest_tag}
4444
- type: Command
4545
name: Login to repo
4646
timeoutInSeconds: 900
@@ -52,4 +52,4 @@ steps:
5252
timeoutInSeconds: 600
5353
failImmediatelyOnError: true
5454
command: |
55-
docker push ${image_remote_tag}
55+
docker push ${image_remote_tag} --all-tags

build-repo.yaml.template

+5-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ steps:
6666
timeoutInSeconds: 600
6767
failImmediatelyOnError: true
6868
command: |
69-
docker build . --file Dockerfile --tag ${image_remote_tag}
69+
cd $${OCI_WORKSPACE_DIR}/${repo_name}
70+
export commit=$(git rev-list --all --max-count=1 --abbrev-commit)
71+
cd $${OCI_WORKSPACE_DIR}/${config_repo_name}
72+
docker build . --file Dockerfile --tag ${image_remote_tag}:${image_tag}-$commit --tag ${image_latest_tag}
7073
- type: Command
7174
name: Login to repo
7275
timeoutInSeconds: 900
@@ -78,4 +81,4 @@ steps:
7881
timeoutInSeconds: 600
7982
failImmediatelyOnError: true
8083
command: |
81-
docker push ${image_remote_tag}
84+
docker push ${image_remote_tag} --all-tags

config-repo.tf

+3-5
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ resource "null_resource" "create_config_repo_war" {
6868
local_file.wallet,
6969
local_file.self_signed_certificate,
7070
local_file.oci_build_config,
71-
random_password.wallet_password,
72-
oci_identity_policy.user_manage_all_policy
71+
random_password.wallet_password
7372
]
7473

7574
# clone new repository
@@ -217,8 +216,7 @@ resource "null_resource" "create_config_repo_jar" {
217216
local_file.wallet,
218217
local_file.self_signed_certificate,
219218
local_file.oci_build_config,
220-
random_password.wallet_password,
221-
oci_identity_policy.user_manage_all_policy
219+
random_password.wallet_password
222220
]
223221

224222
# clone new repository
@@ -334,7 +332,7 @@ resource "oci_artifacts_repository" "application_repository" {
334332
compartment_id = var.compartment_id
335333
is_immutable = true
336334
repository_type = "GENERIC"
337-
display_name = "${var.application_name}-repository"
335+
display_name = "${local.application_name}-repository"
338336
}
339337

340338
resource "oci_generic_artifacts_content_artifact_by_path" "update_container_instance_script" {

container_instance.tf

+2-10
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,18 @@
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33
# this file creates the container instances that tun the application
44

5-
# wait for policy to be active
6-
resource "time_sleep" "wait_60_seconds" {
7-
depends_on = [ oci_identity_policy.container_instances_read_repo ]
8-
create_duration = "60s"
9-
}
105

116
# create container instances
127
resource "oci_container_instances_container_instance" "app_container_instance" {
138
depends_on = [
149
oci_devops_build_run.create_docker_image,
1510
oci_core_subnet.app_oci_core_subnet,
16-
oci_core_network_security_group.app_nsg,
17-
oci_identity_user_group_membership.user_group_membership,
18-
oci_identity_policy.image_access_to_user,
19-
time_sleep.wait_60_seconds
11+
oci_core_network_security_group.app_nsg
2012
]
2113
availability_domain = var.availability_domain
2214
compartment_id = var.compartment_id
2315
containers {
24-
image_url = local.image-remote-tag
16+
image_url = local.image-latest-tag
2517
display_name = "${local.instance-name}-${count.index}container"
2618
environment_variables = merge(local.env_variables, local.other_env_variables)
2719
}

datasources.tf

+13-4
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ data "template_file" "oci_config" {
9090
}
9191
}
9292

93+
data "oci_artifacts_generic_artifact" "app_artifact" {
94+
artifact_id = var.artifact_id
95+
count = local.use-artifact ? 1 : 0
96+
}
97+
9398
# build spec file
9499
data "template_file" "oci_build_config" {
95100
depends_on = [
@@ -98,11 +103,15 @@ data "template_file" "oci_build_config" {
98103
template = "${(local.use-repository ? file("${path.module}/build-repo.yaml.template") : file("${path.module}/build-artifact.yaml.template"))}"
99104
vars = {
100105
image_remote_tag = "${local.image-remote-tag}"
106+
image_latest_tag = "${local.image-latest-tag}"
107+
image_tag = "${local.image-name}"
101108
container_registry_repo = "${local.container-registry-repo}"
102109
login = local.login_container
103110
build_command = var.build_command
104111
artifact_location = var.artifact_location
105-
oci_token = oci_vault_secret.auth_token_secret.id
112+
artifact_path = (local.use-artifact ? data.oci_artifacts_generic_artifact.app_artifact[0].artifact_path : "")
113+
artifact_version = (local.use-artifact ? data.oci_artifacts_generic_artifact.app_artifact[0].version : "")
114+
oci_token = local.auth_token_secret
106115
repo_name = (local.use-repository ? data.oci_devops_repository.devops_repository[0].name : "")
107116
config_repo_name = local.config_repo_name
108117
artifactId = (local.use-artifact ? var.artifact_id : "")
@@ -122,7 +131,7 @@ data "template_file" "oci_deploy_config" {
122131
]
123132
template = "${file("${path.module}/deploy.yaml.template")}"
124133
vars = {
125-
oci_token = oci_vault_secret.auth_token_secret.id
134+
oci_token = local.auth_token_secret
126135
config_repo_url = local.config_repo_url
127136
config_repo_name = local.config_repo_name
128137
artifact_ocid = oci_generic_artifacts_content_artifact_by_path.update_container_instance_script.id
@@ -140,7 +149,7 @@ data "template_file" "deploy_script" {
140149
template = "${file("${path.module}/deploy.sh.template")}"
141150
vars = {
142151
"backend_name" = "${oci_container_instances_container_instance.app_container_instance[count.index].vnics[0].private_ip}:${var.exposed_port}"
143-
"backend_set_name" = "${local.load-balancer-name}_bset"
152+
"backend_set_name" = "${var.application_name}_bset"
144153
"load_balancer_id" = oci_load_balancer.flexible_loadbalancer.id
145154
"container_instance_id" = oci_container_instances_container_instance.app_container_instance[count.index].id
146155
}
@@ -157,7 +166,7 @@ data "oci_devops_repository" "devops_repository" {
157166
}
158167

159168
data "oci_dns_zones" "zones" {
160-
compartment_id = var.compartment_id
169+
compartment_id = (var.dns_compartment == "" ? var.compartment_id : var.dns_compartment)
161170
name = var.zone
162171
zone_type = "PRIMARY"
163172
}

devops.tf

+36-18
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# create container registry in case the application is not an image (so
77
# either source code or artifact)
88
resource "oci_artifacts_container_repository" "application-container-repository" {
9-
compartment_id = var.compartment_id
9+
compartment_id = var.devops_compartment
1010
display_name = local.repository-name
1111

1212
is_immutable = false
@@ -30,14 +30,14 @@ resource "oci_identity_api_key" "dbconnection_api_key" {
3030
# if the app is an artifact (jar/war), we need to create a topic in order
3131
# to create a project in devops to host the config repo
3232
resource "oci_ons_notification_topic" "topic" {
33-
compartment_id = var.compartment_id
33+
compartment_id = var.devops_compartment
3434
name = var.application_name
3535
count = local.use-artifact ? 1 : 0 # app is an artifact
3636
}
3737

3838
# now we can create the project (jar/war case)
3939
resource "oci_devops_project" "project" {
40-
compartment_id = var.compartment_id
40+
compartment_id = var.devops_compartment
4141
name = var.application_name
4242
notification_config {
4343
topic_id = oci_ons_notification_topic.topic[0].id
@@ -46,13 +46,13 @@ resource "oci_devops_project" "project" {
4646
}
4747

4848
resource "oci_logging_log_group" "devops_log_group" {
49-
compartment_id = var.compartment_id
50-
display_name = "logGroup"
49+
compartment_id = var.devops_compartment
50+
display_name = "logGroup-${formatdate("MMDDhhmm", timestamp())}"
5151
count = local.use-artifact ? 1 : 0
5252
}
5353

5454
resource "oci_logging_log" "devops_log" {
55-
display_name = "log"
55+
display_name = "log-${formatdate("MMDDhhmm", timestamp())}"
5656
log_group_id = oci_logging_log_group.devops_log_group[0].id
5757
log_type = "SERVICE"
5858
configuration {
@@ -72,14 +72,14 @@ resource "oci_logging_log" "devops_log" {
7272
resource "oci_devops_build_pipeline" "build_pipeline" {
7373
project_id = local.project_id
7474
description = "Build container image"
75-
display_name = "${var.application_name}-build"
75+
display_name = "${local.application_name}-build"
7676
count = local.use-repository ? 1 : 0
7777
}
7878

7979
resource "oci_devops_build_pipeline" "build_pipeline_artifact" {
8080
project_id = local.project_id
8181
description = "Build container image"
82-
display_name = "${var.application_name}-build"
82+
display_name = "${local.application_name}-build"
8383
build_pipeline_parameters {
8484
items {
8585
default_value = local.use-artifact ? var.artifact_id : "none"
@@ -88,6 +88,13 @@ resource "oci_devops_build_pipeline" "build_pipeline_artifact" {
8888
#Optional
8989
description = "Artifact to deploy"
9090
}
91+
items {
92+
default_value = local.use-artifact ? data.oci_artifacts_generic_artifact.app_artifact[0].version : "none"
93+
name = "artifact_version"
94+
95+
#Optional
96+
description = "Artifact version"
97+
}
9198
}
9299
count = local.use-artifact ? 1 : 0
93100
}
@@ -164,7 +171,7 @@ resource "oci_devops_build_pipeline_stage" "art_build_pipeline_stage" {
164171
}
165172
build_spec_file = "build_spec.yaml"
166173
description = "Build container image"
167-
display_name = "${var.application_name}-build-stage"
174+
display_name = "${local.application_name}-build-stage"
168175
image = var.devops_pipeline_image
169176
is_pass_all_parameters_enabled = false
170177
primary_build_source = oci_devops_repository.config_repo[0].name
@@ -208,7 +215,7 @@ resource "oci_devops_trigger" "generated_oci_devops_trigger" {
208215
}
209216
}
210217
}
211-
display_name = "${var.application_name}-trigger"
218+
display_name = "${local.application_name}-trigger"
212219
project_id = local.project_id
213220
repository_id = data.oci_devops_repository.devops_repository[0].id
214221
trigger_source = "DEVOPS_CODE_REPOSITORY"
@@ -218,16 +225,27 @@ resource "oci_devops_trigger" "generated_oci_devops_trigger" {
218225
# run the pipeline
219226
resource "oci_devops_build_run" "create_docker_image" {
220227
depends_on = [
221-
oci_identity_policy.devops_secrets_policy,
222228
oci_artifacts_container_repository.application-container-repository,
223229
oci_devops_build_pipeline.build_pipeline,
224230
oci_devops_build_pipeline.build_pipeline_artifact,
225231
oci_devops_build_pipeline_stage.repo_build_pipeline_stage,
226232
oci_devops_build_pipeline_stage.art_build_pipeline_stage,
227233
null_resource.create_config_repo_jar,
228-
null_resource.create_config_repo_war,
229-
oci_identity_policy.devops_secrets_policy
234+
null_resource.create_config_repo_war
230235
]
236+
dynamic "build_run_arguments" {
237+
for_each = local.use-artifact ? [1] : []
238+
content {
239+
items {
240+
value = local.use-artifact ? var.artifact_id : "none"
241+
name = "artifactId"
242+
}
243+
items {
244+
value = local.use-artifact ? data.oci_artifacts_generic_artifact.app_artifact[0].version : "none"
245+
name = "artifact_version"
246+
}
247+
}
248+
}
231249
build_pipeline_id = (local.use-artifact ? oci_devops_build_pipeline.build_pipeline_artifact[0].id : oci_devops_build_pipeline.build_pipeline[0].id)
232250
display_name = "triggered-by-terraform"
233251
count = (local.use-image ? 0 : 1)
@@ -251,7 +269,7 @@ resource "oci_devops_deploy_pipeline" "deploy_pipeline" {
251269
]
252270
project_id = local.project_id
253271
description = "Deploy pipeline"
254-
display_name = "${var.application_name}-deploy"
272+
display_name = "${local.application_name}-deploy"
255273
}
256274

257275
resource "oci_devops_deploy_stage" "deploy_stage" {
@@ -289,14 +307,14 @@ resource "oci_devops_deploy_pipeline" "deploy_pipeline" {
289307

290308
# Create a projet to contain deploy pipeline when deploying for container image
291309
resource "oci_ons_notification_topic" "deploy_image_topic" {
292-
compartment_id = var.compartment_id
293-
name = "topic-${var.application_name}"
310+
compartment_id = var.devops_compartment
311+
name = "topic-${local.application_name}"
294312
count = (local.use-image ? 1 : 0)
295313
}
296314

297315
resource "oci_devops_project" "deploy_image_project" {
298-
compartment_id = var.compartment_id
299-
name = "deploy-${var.application_name}"
316+
compartment_id = var.devops_compartment
317+
name = "deploy-${local.application_name}"
300318
notification_config {
301319
topic_id = oci_ons_notification_topic.deploy_image_topic[0].id
302320
}

0 commit comments

Comments
 (0)