Skip to content

Commit

Permalink
Remove picard build-agent cache
Browse files Browse the repository at this point in the history
Remove the cache entirely.
  • Loading branch information
marcomorain committed Mar 21, 2022
1 parent 263902a commit bf781e7
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 118 deletions.
5 changes: 2 additions & 3 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"time"

"github.com/CircleCI-Public/circleci-cli/local"
"github.com/CircleCI-Public/circleci-cli/settings"
"github.com/CircleCI-Public/circleci-cli/update"
"github.com/CircleCI-Public/circleci-cli/version"
Expand Down Expand Up @@ -74,15 +73,15 @@ func newUpdateCommand(config *settings.Config) *cobra.Command {
update.AddCommand(&cobra.Command{
Use: "build-agent",
Hidden: true,
Short: "Update the build agent to the latest version",
Short: "This command has no effect, and is kept for backwards compatiblity",
PersistentPreRun: func(_ *cobra.Command, _ []string) {
opts.cfg.SkipUpdateCheck = true
},
PreRun: func(cmd *cobra.Command, args []string) {
opts.args = args
},
RunE: func(_ *cobra.Command, _ []string) error {
return local.UpdateBuildAgent()
return nil
},
})

Expand Down
90 changes: 3 additions & 87 deletions local/local.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package local

import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"regexp"
"syscall"

Expand All @@ -23,22 +21,6 @@ var picardRepo = "circleci/picard"

const DefaultConfigPath = ".circleci/config.yml"

type buildAgentSettings struct {
LatestSha256 string
}

func UpdateBuildAgent() error {
latestSha256, err := findLatestPicardSha()

if err != nil {
return err
}

fmt.Printf("Latest build agent is version %s\n", latestSha256)

return nil
}

func Execute(flags *pflag.FlagSet, cfg *settings.Config) error {
processedArgs, configPath := buildAgentArguments(flags)
orgSlug, _ := flags.GetString("org-slug")
Expand Down Expand Up @@ -149,25 +131,13 @@ func buildAgentArguments(flags *pflag.FlagSet) ([]string, string) {

func picardImage(output io.Writer) (string, error) {

sha, err := loadCurrentBuildAgentSha()
fmt.Fprintf(output, "Fetching latest build environment...\n")
sha, err := findLatestPicardSha()

if err != nil {
fmt.Printf("Failed to load build agent settings: %s\n", err)
return "", err
}

if sha == "" {

fmt.Println("Downloading latest CircleCI build agent...")

var err error

sha, err = findLatestPicardSha()

if err != nil {
return "", err
}

}
_, _ = fmt.Fprintf(output, "Docker image digest: %s\n", sha)
return fmt.Sprintf("%s@%s", picardRepo, sha), nil
}
Expand Down Expand Up @@ -210,63 +180,9 @@ func findLatestPicardSha() (string, error) {
return "", fmt.Errorf("failed to parse sha256 from docker pull output")
}

// This function still lives in cmd/build.go
err = storeBuildAgentSha(latest)

if err != nil {
return "", err
}

return latest, nil
}

func buildAgentSettingsPath() string {
return path.Join(settings.SettingsPath(), "build_agent_settings.json")
}

func storeBuildAgentSha(sha256 string) error {
agentSettings := buildAgentSettings{
LatestSha256: sha256,
}

settingsJSON, err := json.Marshal(agentSettings)

if err != nil {
return errors.Wrap(err, "Failed to serialize build agent settings")
}

if err = os.MkdirAll(settings.SettingsPath(), 0700); err != nil {
return errors.Wrap(err, "Could not create settings directory")
}

err = ioutil.WriteFile(buildAgentSettingsPath(), settingsJSON, 0644)

return errors.Wrap(err, "Failed to write build agent settings file")
}

func loadCurrentBuildAgentSha() (string, error) {

if _, err := os.Stat(buildAgentSettingsPath()); os.IsNotExist(err) {
// Settings file does not exist.
return "", nil
}

file, err := os.Open(buildAgentSettingsPath())
if err != nil {
return "", errors.Wrap(err, "Could not open build settings config")
}
defer file.Close()

var settings buildAgentSettings

if err := json.NewDecoder(file).Decode(&settings); err != nil {

return "", errors.Wrap(err, "Could not parse build settings config")
}

return settings.LatestSha256, nil
}

// Write data to a temp file, and return the path to that file.
func writeStringToTempFile(data string) (string, error) {
// It's important to specify `/tmp` here as the location of the temp file.
Expand Down
28 changes: 0 additions & 28 deletions local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,32 +122,4 @@ var _ = Describe("build", func() {
}))

})

Describe("loading settings", func() {

var (
tempHome string
)

BeforeEach(func() {
var err error
tempHome, err = ioutil.TempDir("", "circleci-cli-test-")

Expect(err).ToNot(HaveOccurred())
Expect(os.Setenv("HOME", tempHome)).To(Succeed())

})

AfterEach(func() {
Expect(os.RemoveAll(tempHome)).To(Succeed())
})

It("can load settings", func() {
Expect(storeBuildAgentSha("deipnosophist")).To(Succeed())
Expect(loadCurrentBuildAgentSha()).To(Equal("deipnosophist"))
image, err := picardImage(ioutil.Discard)
Expect(err).ToNot(HaveOccurred())
Expect(image).To(Equal("circleci/picard@deipnosophist"))
})
})
})

0 comments on commit bf781e7

Please sign in to comment.