MCPcopy Index your code
hub / github.com/linuxkit/linuxkit / CopyImageToInstance

Method CopyImageToInstance

src/cmd/linuxkit/scaleway.go:305–373  ·  view source on GitHub ↗

CopyImageToInstance copies the image to the instance via ssh

(instanceID, path, sshKeyPath string)

Source from the content-addressed store, hash-verified

303
304// CopyImageToInstance copies the image to the instance via ssh
305func (s *ScalewayClient) CopyImageToInstance(instanceID, path, sshKeyPath string) error {
306 _, base := filepath.Split(path)
307 s.fileName = base
308
309 serverResp, err := s.instanceAPI.GetServer(&instance.GetServerRequest{
310 ServerID: instanceID,
311 })
312 if err != nil {
313 return err
314 }
315 server := serverResp.Server
316
317 signer, err := getSSHAuth(sshKeyPath)
318 if err != nil {
319 return err
320 }
321
322 s.sshConfig = &ssh.ClientConfig{
323 User: "root",
324 Auth: []ssh.AuthMethod{
325 ssh.PublicKeys(signer),
326 },
327 HostKeyCallback: ssh.InsecureIgnoreHostKey(), // TODO validate server before?
328 }
329
330 client, err := ssh.Dial("tcp", server.PublicIP.Address.String()+":22", s.sshConfig) // TODO remove hardocoded port?
331 if err != nil {
332 return err
333 }
334
335 session, err := client.NewSession()
336 if err != nil {
337 return err
338 }
339 defer func() {
340 _ = session.Close()
341 }()
342
343 f, err := os.Open(path)
344 if err != nil {
345 return err
346 }
347 defer func() {
348 _ = f.Close()
349 }()
350
351 // code taken from bramvdbogaerde/go-scp
352 contentBytes, err := io.ReadAll(f)
353 if err != nil {
354 return err
355 }
356 bytesReader := bytes.NewReader(contentBytes)
357
358 log.Infof("Starting to upload %s on server", base)
359
360 go func() {
361 w, err := session.StdinPipe()
362 if err != nil {

Callers 1

pushScalewayCmdFunction · 0.95

Calls 4

getSSHAuthFunction · 0.85
StringMethod · 0.65
OpenMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected