Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix attach script loop for satellite host attach #4117

Merged
merged 8 commits into from
Oct 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 28 additions & 18 deletions ibm/service/satellite/data_source_ibm_satellite_host_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,23 +160,29 @@ func dataSourceIBMSatelliteAttachHostScriptRead(d *schema.ResourceData, meta int
return fmt.Errorf("[ERROR] Error Generating Satellite Registration Script: %s\n%s", err, resp)
}

lines := strings.Split(string(resp), "\n")
scriptContent := string(resp)

//if this is a RHEL host, continue with custom script
//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`) {
i = i + 1
if script, ok := d.GetOk("custom_script"); ok {
lines[i] = script.(string)
} else {
if strings.ToLower(hostProvider) == "aws" {
lines[i] = `
index = i
break
}
}

var insertionText string

switch {
case strings.ToLower(hostProvider) == "aws":
insertionText = `
yum-config-manager --enable '*'
yum install container-selinux -y
`
} else if strings.ToLower(hostProvider) == "ibm" {
lines[i] = `
case strings.ToLower(hostProvider) == "ibm":
insertionText = `
subscription-manager refresh
if [[ "${OPERATING_SYSTEM}" == "RHEL7" ]]; then
subscription-manager repos --enable rhel-server-rhscl-7-rpms
Expand All @@ -188,30 +194,34 @@ elif [[ "${OPERATING_SYSTEM}" == "RHEL8" ]]; then
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`
} else if strings.ToLower(hostProvider) == "azure" {
lines[i] = `
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
`
} else if strings.ToLower(hostProvider) == "google" {
lines[i] = `
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
bhpratt marked this conversation as resolved.
Show resolved Hide resolved
scriptContent = strings.Join(lines, "\n")
}

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)
Expand Down