-
Notifications
You must be signed in to change notification settings - Fork 645
/
data_source_ibm_satellite_host_script.go
253 lines (227 loc) · 7.63 KB
/
data_source_ibm_satellite_host_script.go
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package satellite
import (
"fmt"
"io/ioutil"
"log"
"path/filepath"
"strings"
"time"
"github.com/IBM-Cloud/container-services-go-sdk/kubernetesserviceapiv1"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM/go-sdk-core/v5/core"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
homedir "github.com/mitchellh/go-homedir"
)
func DataSourceIBMSatelliteAttachHostScript() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMSatelliteAttachHostScriptRead,
Schema: map[string]*schema.Schema{
"location": {
Type: schema.TypeString,
Required: true,
Description: "A unique name for the new Satellite location",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "A unique name for the new Satellite location",
},
"coreos_host": {
Type: schema.TypeBool,
Optional: true,
Description: "If true, returns a CoreOS ignition file for the host. Otherwise, returns a RHEL attach script",
},
"labels": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Description: "List of labels for the attach host",
},
"host_provider": {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"host_provider", "custom_script"},
},
"script_dir": {
Description: "The directory where the satellite attach host script to be downloaded. Default is home directory",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"script_path": {
Description: "The absolute path to the generated host script file",
Type: schema.TypeString,
Computed: true,
},
"host_script": {
Type: schema.TypeString,
Computed: true,
Description: "Attach host script content",
},
"custom_script": {
Description: "The custom script that has to be appended to generated host script file",
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"host_provider", "custom_script"},
},
"host_link_agent_endpoint": {
Description: "The satellite link agent endpoint, required for reduced firewall attach script",
Type: schema.TypeString,
Optional: true,
},
},
}
}
func dataSourceIBMSatelliteAttachHostScriptRead(d *schema.ResourceData, meta interface{}) error {
var scriptDir string
location := d.Get("location").(string)
hostProvider := d.Get("host_provider").(string)
if _, ok := d.GetOk("script_dir"); ok {
scriptDir = d.Get("script_dir").(string)
}
satClient, err := meta.(conns.ClientSession).SatelliteClientSession()
if err != nil {
return err
}
var locData *kubernetesserviceapiv1.MultishiftGetController
var response *core.DetailedResponse
getSatLocOptions := &kubernetesserviceapiv1.GetSatelliteLocationOptions{
Controller: &location,
}
err = resource.Retry(1*time.Minute, func() *resource.RetryError {
locData, response, err = satClient.GetSatelliteLocation(getSatLocOptions)
if err != nil || locData == nil {
if response != nil && response.StatusCode == 404 {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
if conns.IsResourceTimeoutError(err) {
locData, response, err = satClient.GetSatelliteLocation(getSatLocOptions)
}
if err != nil || locData == nil {
return fmt.Errorf("[ERROR] Error getting Satellite location (%s): %s\n%s", location, err, response)
}
// script labels
labels := make(map[string]string)
if v, ok := d.GetOk("labels"); ok {
l := v.(*schema.Set)
labels = flex.FlattenKeyValues(l.List())
d.Set("labels", l)
}
if len(scriptDir) == 0 {
scriptDir, err = homedir.Dir()
if err != nil {
return fmt.Errorf("[ERROR] Error fetching homedir: %s", err)
}
}
scriptDir, _ = filepath.Abs(scriptDir)
var scriptPath string
//Generate script
createRegOptions := &kubernetesserviceapiv1.AttachSatelliteHostOptions{}
createRegOptions.Controller = locData.ID
createRegOptions.Labels = labels
//check to see if host attach is CoreOS or RHEL
var host_os string
var coreos_enabled bool
if _, ok := d.GetOk("coreos_host"); ok {
coreos_enabled = d.Get("coreos_host").(bool)
if coreos_enabled {
host_os = "RHCOS"
createRegOptions.OperatingSystem = &host_os
scriptPath = filepath.Join(scriptDir, "addHost.ign")
}
} else {
coreos_enabled = false
host_os = "RHEL"
createRegOptions.OperatingSystem = &host_os
scriptPath = filepath.Join(scriptDir, "addHost.sh")
}
// If the user supplied link agent endpoint, use reduced firewall attach script
if hlae, ok := d.GetOk("host_link_agent_endpoint"); ok {
host_link_agent_endpoint := hlae.(string)
createRegOptions.HostLinkAgentEndpoint = &host_link_agent_endpoint
}
resp, err := satClient.AttachSatelliteHost(createRegOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Generating Satellite Registration Script: %s\n%s", err, resp)
}
scriptContent := string(resp)
//if this is a RHEL host, find insert point for custom code
if !coreos_enabled {
lines := strings.Split(scriptContent, "\n")
var index int
for i, line := range lines {
if strings.Contains(line, `export OPERATING_SYSTEM`) {
index = i
break
}
}
var insertionText string
switch {
case strings.ToLower(hostProvider) == "aws":
insertionText = `
yum-config-manager --enable '*'
yum install container-selinux -y
`
case strings.ToLower(hostProvider) == "ibm":
insertionText = `
subscription-manager refresh
if [[ "${OPERATING_SYSTEM}" == "RHEL7" ]]; then
subscription-manager repos --enable rhel-server-rhscl-7-rpms
subscription-manager repos --enable rhel-7-server-optional-rpms
subscription-manager repos --enable rhel-7-server-rh-common-rpms
subscription-manager repos --enable rhel-7-server-supplementary-rpms
subscription-manager repos --enable rhel-7-server-extras-rpms
elif [[ "${OPERATING_SYSTEM}" == "RHEL8" ]]; then
subscription-manager release --set=8
subscription-manager repos --disable='*eus*'
subscription-manager repos --enable rhel-8-for-x86_64-baseos-rpms
subscription-manager repos --enable rhel-8-for-x86_64-appstream-rpms;
fi
yum install container-selinux -y
`
case strings.ToLower(hostProvider) == "azure":
insertionText = `
#if [[ "${OPERATING_SYSTEM}" == "RHEL8" ]]; then
# update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
# update-alternatives --set python3 /usr/bin/python3.8
#fi
yum install container-selinux -y
`
case strings.ToLower(hostProvider) == "google":
insertionText = `
#if [[ "${OPERATING_SYSTEM}" == "RHEL8" ]]; then
# update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
# update-alternatives --set python3 /usr/bin/python3.8
#fi
yum install container-selinux -y
`
default:
if script, ok := d.GetOk("custom_script"); ok {
insertionText = script.(string)
}
}
lines[index] = lines[index] + "\n" + insertionText
scriptContent = strings.Join(lines, "\n")
}
err = ioutil.WriteFile(scriptPath, []byte(scriptContent), 0644)
if err != nil {
return fmt.Errorf("[ERROR] Error Creating Satellite Attach Host Script: %s", err)
}
d.Set("location", location)
d.Set("host_script", scriptContent)
d.Set("host_provider", hostProvider)
d.Set("script_dir", scriptDir)
d.Set("script_path", scriptPath)
d.SetId(*locData.ID)
log.Printf("[INFO] Generated satellite location script : %s", *locData.Name)
return nil
}