Skip to content

Commit

Permalink
[IF-854] Add support to create Ansible playbook components (#48)
Browse files Browse the repository at this point in the history
Co-authored-by: sternik <[email protected]>
  • Loading branch information
sternik and sternik authored Apr 17, 2023
1 parent 3c3ec3a commit 5bc7cbc
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 40 deletions.
85 changes: 80 additions & 5 deletions docs/resources/custom_component.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,99 @@ description: |-
## Example Usage

```terraform
// Copyright 2021 Nordcloud Oy or its affiliates. All Rights Reserved.
// Copyright 2021-2023 Nordcloud Oy or its affiliates. All Rights Reserved.
resource "imagefactory_custom_component" "component" {
# An example of a SHELL component
resource "imagefactory_custom_component" "shell_component" {
name = "Install nginx"
description = "Install nginx on Ubuntu"
stage = "BUILD"
cloud_providers = ["AWS", "AZURE"]
os_types = ["LINUX"]
content {
script = <<-EOT
script = <<-EOT
apt-get update && apt-get install nginx -y
EOT
provisioner = "SHELL"
}
}
output "component" {
value = imagefactory_custom_component.component
output "shell_component" {
value = imagefactory_custom_component.shell_component
}
# An example of a Powershell component
resource "imagefactory_custom_component" "powershell_component" {
name = "Install Apache"
description = "Install Apache HTTP Server on Microsoft Windows"
stage = "BUILD"
cloud_providers = ["AWS", "AZURE"]
os_types = ["WINDOWS"]
content {
script = <<-EOT
---
- name: Installing Apache HTTP Server
hosts: all
tasks:
- name: Create directory structure
ansible.windows.win_file:
path: C:\ansible_examples
state: directory
- name: Download the Apache installer
win_get_url:
url: https://archive.apache.org/dist/httpd/binaries/win32/httpd-2.2.25-win32-x86-no_ssl.msi
dest: C:\ansible_examples\httpd-2.2.25-win32-x86-no_ssl.msi
- name: Install MSI
win_package:
path: C:\ansible_examples\httpd-2.2.25-win32-x86-no_ssl.msi
state: present
EOT
provisioner = "POWERSHELL"
}
}
output "powershell_component" {
value = imagefactory_custom_component.powershell_component
}
# An example of a Ansible playbook component
resource "imagefactory_custom_component" "ansible_component" {
name = "Install nginx"
description = "Install nginx using ansible playbook"
stage = "BUILD"
cloud_providers = ["AWS", "AZURE"]
os_types = ["LINUX"]
content {
script = <<-EOT
---
# playbook.yml
- name: set up webserver
hosts: all
tasks:
- name: ensure nginx is at the latest version
package:
name: nginx
state: present
- name: start nginx
service:
name: nginx
state: started
enabled: yes
EOT
provisioner = "ANSIBLE"
}
}
output "ansible_component" {
value = imagefactory_custom_component.ansible_component
}
```

Expand Down
85 changes: 80 additions & 5 deletions examples/resources/imagefactory_custom_component/resource.tf
Original file line number Diff line number Diff line change
@@ -1,19 +1,94 @@
// Copyright 2021 Nordcloud Oy or its affiliates. All Rights Reserved.
// Copyright 2021-2023 Nordcloud Oy or its affiliates. All Rights Reserved.

resource "imagefactory_custom_component" "component" {
# An example of a SHELL component

resource "imagefactory_custom_component" "shell_component" {
name = "Install nginx"
description = "Install nginx on Ubuntu"
stage = "BUILD"
cloud_providers = ["AWS", "AZURE"]
os_types = ["LINUX"]
content {
script = <<-EOT
script = <<-EOT
apt-get update && apt-get install nginx -y
EOT
provisioner = "SHELL"
}
}

output "component" {
value = imagefactory_custom_component.component
output "shell_component" {
value = imagefactory_custom_component.shell_component
}

# An example of a Powershell component

resource "imagefactory_custom_component" "powershell_component" {
name = "Install Apache"
description = "Install Apache HTTP Server on Microsoft Windows"
stage = "BUILD"
cloud_providers = ["AWS", "AZURE"]
os_types = ["WINDOWS"]
content {
script = <<-EOT
---
- name: Installing Apache HTTP Server
hosts: all
tasks:
- name: Create directory structure
ansible.windows.win_file:
path: C:\ansible_examples
state: directory
- name: Download the Apache installer
win_get_url:
url: https://archive.apache.org/dist/httpd/binaries/win32/httpd-2.2.25-win32-x86-no_ssl.msi
dest: C:\ansible_examples\httpd-2.2.25-win32-x86-no_ssl.msi
- name: Install MSI
win_package:
path: C:\ansible_examples\httpd-2.2.25-win32-x86-no_ssl.msi
state: present
EOT
provisioner = "POWERSHELL"
}
}

output "powershell_component" {
value = imagefactory_custom_component.powershell_component
}

# An example of a Ansible playbook component

resource "imagefactory_custom_component" "ansible_component" {
name = "Install nginx"
description = "Install nginx using ansible playbook"
stage = "BUILD"
cloud_providers = ["AWS", "AZURE"]
os_types = ["LINUX"]
content {
script = <<-EOT
---
# playbook.yml
- name: set up webserver
hosts: all
tasks:
- name: ensure nginx is at the latest version
package:
name: nginx
state: present
- name: start nginx
service:
name: nginx
state: started
enabled: yes
EOT
provisioner = "ANSIBLE"
}
}

output "ansible_component" {
value = imagefactory_custom_component.ansible_component
}
4 changes: 2 additions & 2 deletions imagefactory/component/resource.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2022 Nordcloud Oy or its affiliates. All Rights Reserved.
// Copyright 2021-2023 Nordcloud Oy or its affiliates. All Rights Reserved.

package component

Expand Down Expand Up @@ -97,7 +97,7 @@ func resourceComponentUpdate(ctx context.Context, d *schema.ResourceData, m inte
input := sdk.NewComponentContent{
ID: graphql.String(componentID),
Script: graphql.String(m["script"].(string)),
ScriptProvisioner: graphql.ShellScriptProvisioner(m["provisioner"].(string)),
ScriptProvisioner: graphql.ScriptProvisioner(m["provisioner"].(string)),
}

component, err = c.APIClient.CreateComponentVersion(input)
Expand Down
4 changes: 2 additions & 2 deletions imagefactory/component/structures.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Nordcloud Oy or its affiliates. All Rights Reserved.
// Copyright 2021-2023 Nordcloud Oy or its affiliates. All Rights Reserved.

package component

Expand Down Expand Up @@ -32,7 +32,7 @@ func expandContent(in []interface{}) graphql.NewVersionedContent {
m := in[0].(map[string]interface{})
return graphql.NewVersionedContent{
Script: graphql.String(m["script"].(string)),
ScriptProvisioner: graphql.ShellScriptProvisioner(m["provisioner"].(string)),
ScriptProvisioner: graphql.ScriptProvisioner(m["provisioner"].(string)),
}
}

Expand Down
3 changes: 2 additions & 1 deletion imagefactory/component/validations.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Nordcloud Oy or its affiliates. All Rights Reserved.
// Copyright 2021-2023 Nordcloud Oy or its affiliates. All Rights Reserved.

package component

Expand All @@ -23,4 +23,5 @@ var validOSTypes = []string{
var validProvisioners = []string{
"POWERSHELL",
"SHELL",
"ANSIBLE",
}
21 changes: 7 additions & 14 deletions pkg/graphql/graphql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 5 additions & 11 deletions pkg/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ type ChangeDetails {
}


# Copyright 2021-2022 Nordcloud Oy or its affiliates. All Rights Reserved.
# Copyright 2021-2023 Nordcloud Oy or its affiliates. All Rights Reserved.

enum ComponentType {
SYSTEM
Expand Down Expand Up @@ -448,19 +448,13 @@ type ComponentResults {
count: Int!
}


enum ShellScriptProvisioner {
POWERSHELL
SHELL
}

"""
NewVersionedContent contains the script that will be executed by the component.
Script provided as a plain text is saved in the ImageFactory S3. You can access it later using the presigned URL.
"""
input NewVersionedContent {
scriptProvisioner: ShellScriptProvisioner!
scriptProvisioner: ScriptProvisioner!
script: String!
}

Expand All @@ -484,7 +478,7 @@ input ComponentChanges {

input NewComponentContent {
id: String!
scriptProvisioner: ShellScriptProvisioner!
scriptProvisioner: ScriptProvisioner!
script: String!
}

Expand Down Expand Up @@ -994,8 +988,8 @@ type VMImageDefinition {
type TemplateAZUREConfig {
replicaRegions: [String]
excludeFromLatest: Boolean
eolDateOption: Boolean
vmImageDefinition: VMImageDefinition
eolDateOption: Boolean
}

type TemplateConfig {
Expand Down Expand Up @@ -1090,8 +1084,8 @@ input NewVMImageDefinition {
input NewTemplateAZUREConfig {
replicaRegions: [String]
excludeFromLatest: Boolean
eolDateOption: Boolean = true
vmImageDefinition: NewVMImageDefinition
eolDateOption: Boolean
}

"""
Expand Down

0 comments on commit 5bc7cbc

Please sign in to comment.