Skip to content

Commit

Permalink
Abstract away file upload (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
QubitPi authored Jul 3, 2024
1 parent 50df1be commit ef59654
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 61 deletions.
3 changes: 2 additions & 1 deletion provisioner/docker-mailserver/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package dockerMailserver
import (
"context"
"fmt"
fileProvisioner "github.com/QubitPi/packer-plugin-hashicorp-aws/provisioner/file-provisioner"
sslProvisioner "github.com/QubitPi/packer-plugin-hashicorp-aws/provisioner/ssl-provisioner"
"github.com/hashicorp/hcl/v2/hcldec"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
Expand Down Expand Up @@ -51,7 +52,7 @@ func (p *Provisioner) Provision(ctx context.Context, ui packersdk.Ui, communicat
composeFile := strings.Replace(getDockerComposeFileTemplate(), "mail.domain.com", mailServerDomain, -1)
composeFileDst := fmt.Sprintf(filepath.Join(p.config.HomeDir, "compose.yaml"))
composeFileSource, err := sslProvisioner.WriteToFile(composeFile)
err = sslProvisioner.UploadFile(p.config.ctx, ui, communicator, composeFileSource, composeFileDst)
err = fileProvisioner.Provision(p.config.ctx, ui, communicator, composeFileSource, composeFileDst)
if err != nil {
return fmt.Errorf("error uploading '%s' to '%s': %s", composeFileSource, composeFileDst, err)
}
Expand Down
69 changes: 69 additions & 0 deletions provisioner/file-provisioner/file-provisioner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) Jiaqi Liu
// SPDX-License-Identifier: MPL-2.0

// This package implements a provisioner for Packer that uploads a local file onto the remote machine
package fileProvisioner

import (
"fmt"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
"os"
"path/filepath"
"strings"
)

func Provision(ctx interpolate.Context, ui packersdk.Ui, communicator packersdk.Communicator, source string, destination string) error {
src, err := interpolate.Render(source, &ctx)
if err != nil {
return fmt.Errorf("error interpolating source: %s", err)
}

dst, err := interpolate.Render(destination, &ctx)
if err != nil {
return fmt.Errorf("error interpolating destination: %s", err)
}

ui.Say(fmt.Sprintf("Uploading %s => %s", src, dst))

info, err := os.Stat(src)
if err != nil {
return err
}

if info.IsDir() {
return fmt.Errorf("source should be a file; '%s', however, is a directory", src)
}

f, err := os.Open(src)
if err != nil {
return err
}
defer f.Close()

fi, err := f.Stat()
if err != nil {
return err
}

filedst := dst
if strings.HasSuffix(dst, "/") {
filedst = dst + filepath.Base(src)
}

pf := ui.TrackProgress(filepath.Base(src), 0, info.Size(), f)
defer pf.Close()

// Upload the file
if err = communicator.Upload(filedst, pf, &fi); err != nil {
if strings.Contains(err.Error(), "Error restoring file") {
ui.Error(fmt.Sprintf("Upload failed: %s; this can occur when "+
"your file destination is a folder without a trailing "+
"slash.", err))
}
ui.Error(fmt.Sprintf("Upload failed: %s", err))
return err
}

return nil
}
64 changes: 4 additions & 60 deletions provisioner/ssl-provisioner/ssl-provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import (
"encoding/base64"
"fmt"
basicProvisioner "github.com/QubitPi/packer-plugin-hashicorp-aws/provisioner/basic-provisioner"
fileProvisioner "github.com/QubitPi/packer-plugin-hashicorp-aws/provisioner/file-provisioner"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
"github.com/hashicorp/packer-plugin-sdk/tmp"
"os"
"path/filepath"
"strings"
)

const SSL_CERT_PATH string = "/etc/ssl/certs/server.crt"
Expand Down Expand Up @@ -55,82 +54,27 @@ func WriteToFile(content string) (string, error) {
return file.Name(), nil
}

func UploadFile(ctx interpolate.Context, ui packersdk.Ui, communicator packersdk.Communicator, source string, destination string) error {
src, err := interpolate.Render(source, &ctx)
if err != nil {
return fmt.Errorf("error interpolating source: %s", err)
}

dst, err := interpolate.Render(destination, &ctx)
if err != nil {
return fmt.Errorf("error interpolating destination: %s", err)
}

ui.Say(fmt.Sprintf("Uploading %s => %s", src, dst))

info, err := os.Stat(src)
if err != nil {
return err
}

if info.IsDir() {
return fmt.Errorf("source should be a file; '%s', however, is a directory", src)
}

f, err := os.Open(src)
if err != nil {
return err
}
defer f.Close()

fi, err := f.Stat()
if err != nil {
return err
}

filedst := dst
if strings.HasSuffix(dst, "/") {
filedst = dst + filepath.Base(src)
}

pf := ui.TrackProgress(filepath.Base(src), 0, info.Size(), f)
defer pf.Close()

// Upload the file
if err = communicator.Upload(filedst, pf, &fi); err != nil {
if strings.Contains(err.Error(), "Error restoring file") {
ui.Error(fmt.Sprintf("Upload failed: %s; this can occur when "+
"your file destination is a folder without a trailing "+
"slash.", err))
}
ui.Error(fmt.Sprintf("Upload failed: %s", err))
return err
}

return nil
}

func Provision(ctx context.Context, interCtx interpolate.Context, ui packersdk.Ui, communicator packersdk.Communicator, homeDir string, sslCertBase64 string, sslCertKeyBase64 string, nginxConfig string, amiConfigCommands []string) error {
sslCert, err := decodeBase64(sslCertBase64)
sslCertSource, err := WriteToFile(sslCert)
sslCertDestination := fmt.Sprintf(filepath.Join(homeDir, "ssl.crt"))
err = UploadFile(interCtx, ui, communicator, sslCertSource, sslCertDestination)
err = fileProvisioner.Provision(interCtx, ui, communicator, sslCertSource, sslCertDestination)
if err != nil {
return fmt.Errorf("error uploading '%s' to '%s': %s", sslCertSource, sslCertDestination, err)
}

sslCertKey, err := decodeBase64(sslCertKeyBase64)
sslCertKeySource, err := WriteToFile(sslCertKey)
sslCertKeyDestination := fmt.Sprintf(filepath.Join(homeDir, "ssl.key"))
err = UploadFile(interCtx, ui, communicator, sslCertKeySource, sslCertKeyDestination)
err = fileProvisioner.Provision(interCtx, ui, communicator, sslCertSource, sslCertDestination)
if err != nil {
return fmt.Errorf("error uploading '%s' to '%s': %s", sslCertKeySource, sslCertKeyDestination, err)
}

if nginxConfig != "" {
nginxSource, err := WriteToFile(nginxConfig)
nginxDst := fmt.Sprintf(filepath.Join(homeDir, "nginx-ssl.conf"))
err = UploadFile(interCtx, ui, communicator, nginxSource, nginxDst)
err = fileProvisioner.Provision(interCtx, ui, communicator, sslCertSource, sslCertDestination)
if err != nil {
return fmt.Errorf("error uploading '%s' to '%s': %s", nginxSource, nginxDst, err)
}
Expand Down

0 comments on commit ef59654

Please sign in to comment.