Skip to content

Commit 07f8350

Browse files
cfergeaupraveenkumar
authored andcommitted
Unify ssh.CopyData and ssh.SetTextContentAsRoot
Both functions have a similar functionality, copy some content as root to the VM. They are doing it differently though, SetTextContentAsRoot sets the file permissions as well, while ssh.CopyData is able to deal with binary files. This commit adds a 'mode' argument to ssh.CopyData and removes ssh.SetTextContentAsRoot.
1 parent f2f94f8 commit 07f8350

File tree

4 files changed

+9
-16
lines changed

4 files changed

+9
-16
lines changed

pkg/crc/cluster/cluster.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,19 @@ Environment=HTTPS_PROXY=%s
145145
Environment=NO_PROXY=.cluster.local,.svc,10.128.0.0/14,172.30.0.0/16,%s`
146146
p := fmt.Sprintf(proxyTemplate, proxy.HTTPProxy, proxy.HTTPSProxy, proxy.GetNoProxyString())
147147
// This will create a systemd drop-in configuration for proxy (both for kubelet and crio services) on the VM.
148-
err := sshRunner.SetTextContentAsRoot("/etc/systemd/system/crio.service.d/10-default-env.conf", p, 0644)
148+
err := sshRunner.CopyData([]byte(p), "/etc/systemd/system/crio.service.d/10-default-env.conf", 0644)
149149
if err != nil {
150150
return err
151151
}
152-
err = sshRunner.SetTextContentAsRoot("/etc/systemd/system/kubelet.service.d/10-default-env.conf", p, 0644)
152+
err = sshRunner.CopyData([]byte(p), "/etc/systemd/system/kubelet.service.d/10-default-env.conf", 0644)
153153
if err != nil {
154154
return err
155155
}
156156
return nil
157157
}
158158

159159
func addPullSecretToInstanceDisk(sshRunner *ssh.Runner, pullSec string) error {
160-
err := sshRunner.SetTextContentAsRoot("/var/lib/kubelet/config.json", pullSec, 0600)
160+
err := sshRunner.CopyData([]byte(pullSec), "/var/lib/kubelet/config.json", 0600)
161161
if err != nil {
162162
return err
163163
}

pkg/crc/network/nameservers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func GetResolvValuesFromInstance(sshRunner *ssh.Runner) (*ResolvFileValues, erro
3434
func CreateResolvFileOnInstance(sshRunner *ssh.Runner, resolvFileValues ResolvFileValues) error {
3535
resolvFile, _ := CreateResolvFile(resolvFileValues)
3636

37-
err := sshRunner.CopyData([]byte(resolvFile), "/etc/resolv.conf")
37+
err := sshRunner.CopyData([]byte(resolvFile), "/etc/resolv.conf", 0644)
3838
if err != nil {
3939
return fmt.Errorf("Error creating /etc/resolv on instance: %s", err.Error())
4040
}

pkg/crc/services/dns/template.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func createDnsmasqDNSConfig(serviceConfig services.ServicePostStartConfig) error
5252
return err
5353
}
5454

55-
return serviceConfig.SSHRunner.CopyData([]byte(dnsConfig), "/var/srv/dnsmasq.conf")
55+
return serviceConfig.SSHRunner.CopyData([]byte(dnsConfig), "/var/srv/dnsmasq.conf", 0644)
5656
}
5757

5858
func createDNSConfigFile(values dnsmasqConfFileValues, tmpl string) (string, error) {

pkg/crc/ssh/ssh.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,6 @@ func (runner *Runner) Run(command string) (string, error) {
2828
return runner.runSSHCommandFromDriver(command, false)
2929
}
3030

31-
func (runner *Runner) SetTextContentAsRoot(destFilename string, content string, mode os.FileMode) error {
32-
logging.Debugf("Creating %s with permissions 0%o in the CRC VM", destFilename, mode)
33-
command := fmt.Sprintf("sudo install -m 0%o /dev/null %s && cat <<EOF | sudo tee %s\n%s\nEOF", mode, destFilename, destFilename, content)
34-
_, err := runner.RunPrivate(command)
35-
36-
return err
37-
}
38-
3931
func (runner *Runner) RunPrivate(command string) (string, error) {
4032
return runner.runSSHCommandFromDriver(command, true)
4133
}
@@ -44,10 +36,11 @@ func (runner *Runner) SetPrivateKeyPath(path string) {
4436
runner.privateSSHKey = path
4537
}
4638

47-
func (runner *Runner) CopyData(data []byte, destFilename string) error {
39+
func (runner *Runner) CopyData(data []byte, destFilename string, mode os.FileMode) error {
40+
logging.Debugf("Creating %s with permissions 0%o in the CRC VM", destFilename, mode)
4841
base64Data := base64.StdEncoding.EncodeToString(data)
49-
command := fmt.Sprintf("echo %s | base64 --decode | sudo tee %s > /dev/null", base64Data, destFilename)
50-
_, err := runner.Run(command)
42+
command := fmt.Sprintf("sudo install -m 0%o /dev/null %s && cat <<EOF | base64 --decode | sudo tee %s\n%s\nEOF", mode, destFilename, destFilename, base64Data)
43+
_, err := runner.RunPrivate(command)
5144

5245
return err
5346
}

0 commit comments

Comments
 (0)