Skip to content

Commit

Permalink
Add explicit pull command for e2e to use that pulls from manifest
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Broadhurst <peter.broadhurst@kaleido.io>
  • Loading branch information
peterbroadhurst committed Oct 15, 2021
1 parent d1140f3 commit 4dfe87b
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 19 deletions.
83 changes: 83 additions & 0 deletions cmd/pull.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright © 2021 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"errors"
"fmt"
"time"

"github.com/briandowns/spinner"
"github.com/hyperledger/firefly-cli/internal/log"
"github.com/hyperledger/firefly-cli/internal/stacks"
"github.com/spf13/cobra"
)

var pullCmd = &cobra.Command{
Use: "pull <stack_name>",
Short: "Pull a stack",
Long: `Pull a stack
Pull the images for a stack .
`,
RunE: func(cmd *cobra.Command, args []string) error {
var spin *spinner.Spinner
if fancyFeatures && !verbose {
spin = spinner.New(spinner.CharSets[11], 100*time.Millisecond)
spin.FinalMSG = "done"
logger = &log.SpinnerLogger{
Spinner: spin,
}
}

stackManager := stacks.NewStackManager(logger)
if len(args) == 0 {
return errors.New("no stack specified")
}
stackName := args[0]

if err := stackManager.LoadStack(stackName); err != nil {
return err
}

if runBefore, err := stackManager.StackHasRunBefore(); err != nil {
return err
} else if !runBefore {
fmt.Println("this will take a few seconds longer since this is the first time you're running this stack...")
}

if spin != nil {
spin.Start()
}
if err := stackManager.PullStack(verbose); err != nil {
return err
}
if spin != nil {
spin.Stop()
}
fmt.Print("\n\n")
for _, member := range stackManager.Stack.Members {
fmt.Printf("Web UI for member '%v': http://127.0.0.1:%v/ui\n", member.ID, member.ExposedFireflyPort)
}
fmt.Printf("\nTo see logs for your stack run:\n\n%s logs %s\n\n", rootCmd.Use, stackName)
return nil
},
}

func init() {
rootCmd.AddCommand(pullCmd)
}
3 changes: 1 addition & 2 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ This command will start a stack and run it in the background.
if spin != nil {
spin.Start()
}
if err := stackManager.StartStack(fancyFeatures, verbose, &startOptions); err != nil {
if err := stackManager.StartStack(verbose, &startOptions); err != nil {
return err
}
if spin != nil {
Expand All @@ -81,7 +81,6 @@ This command will start a stack and run it in the background.
}

func init() {
startCmd.Flags().BoolVarP(&startOptions.NoPull, "no-pull", "n", false, "Do not pull latest images when starting")
startCmd.Flags().BoolVarP(&startOptions.NoRollback, "no-rollback", "b", false, "Do not automatically rollback changes if first time setup fails")

rootCmd.AddCommand(startCmd)
Expand Down
1 change: 1 addition & 0 deletions internal/core/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func ReadManifestFile(p string) (*types.VersionManifest, error) {
if manifest.FireFly == nil {
manifest.FireFly = &types.ManifestEntry{
Image: "hyperledger/firefly",
Local: true,
}
}
return manifest, err
Expand Down
7 changes: 0 additions & 7 deletions internal/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ func RunDockerCommand(workingDir string, showCommand bool, pipeStdout bool, comm
return runCommand(dockerCmd, showCommand, pipeStdout, command...)
}

func RunDockerComposeCommandWithRetry(workingDir string, showCommand bool, pipeStdout bool, command ...string) (err error) {
for i := 0; err != nil && i < 3; i++ {
err = RunDockerComposeCommand(workingDir, showCommand, pipeStdout, command...)
}
return err
}

func RunDockerComposeCommand(workingDir string, showCommand bool, pipeStdout bool, command ...string) error {
dockerCmd := exec.Command("docker-compose", command...)
dockerCmd.Dir = workingDir
Expand Down
30 changes: 20 additions & 10 deletions internal/stacks/stack_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ type StackManager struct {
}

type StartOptions struct {
NoPull bool
NoRollback bool
}

Expand Down Expand Up @@ -354,7 +353,7 @@ func createMember(id string, index int, options *InitOptions, external bool) *ty
}
}

func (s *StackManager) StartStack(fancyFeatures bool, verbose bool, options *StartOptions) error {
func (s *StackManager) StartStack(verbose bool, options *StartOptions) error {
fmt.Printf("starting FireFly stack '%s'... ", s.Stack.Name)
// Check to make sure all of our ports are available
if err := s.checkPortsAvailable(); err != nil {
Expand Down Expand Up @@ -391,6 +390,24 @@ func (s *StackManager) StartStack(fancyFeatures bool, verbose bool, options *Sta
}
}

func (s *StackManager) PullStack(verbose bool) error {
workingDir := filepath.Join(constants.StacksDir, s.Stack.Name)
for _, entry := range s.Stack.VersionManifest.Entries() {
if entry.Local {
continue
}
fullImage := fmt.Sprintf("%s@sha256:%s", entry.Image, entry.SHA)
if entry.SHA == "" {
fullImage = fmt.Sprintf("%s:%s", entry.Image, entry.Tag)
}
s.Log.Info(fmt.Sprintf("pulling '%s", fullImage))
if err := docker.RunDockerCommand(workingDir, verbose, verbose, "pull", fullImage); err != nil {
return err
}
}
return nil
}

func (s *StackManager) removeVolumes(verbose bool) {
var volumes []string
for _, service := range s.blockchainProvider.GetDockerServiceDefinitions() {
Expand Down Expand Up @@ -537,13 +554,6 @@ func (s *StackManager) runFirstTimeSetup(verbose bool, options *StartOptions) er
}
}

if !options.NoPull {
s.Log.Info("pulling latest versions")
if err := docker.RunDockerComposeCommandWithRetry(workingDir, verbose, verbose, "pull"); err != nil {
return err
}
}

if err := s.runStartupSequence(workingDir, verbose, true); err != nil {
return err
}
Expand Down Expand Up @@ -620,7 +630,7 @@ func (s *StackManager) UpgradeStack(verbose bool) error {
if err := docker.RunDockerComposeCommand(workingDir, verbose, verbose, "down"); err != nil {
return err
}
return docker.RunDockerComposeCommandWithRetry(workingDir, verbose, verbose, "pull")
return docker.RunDockerComposeCommand(workingDir, verbose, verbose, "pull")
}

func (s *StackManager) PrintStackInfo(verbose bool) error {
Expand Down
14 changes: 14 additions & 0 deletions pkg/types/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,22 @@ type VersionManifest struct {
Tokens *ManifestEntry `json:"tokens-erc1155"`
}

func (m *VersionManifest) Entries() []*ManifestEntry {
if m == nil {
return []*ManifestEntry{}
}
return []*ManifestEntry{
m.FireFly,
m.Ethconnect,
m.Fabconnect,
m.DataExchange,
m.Tokens,
}
}

type ManifestEntry struct {
Image string `json:"image,omitempty"`
Local bool `json:"local,omitempty"`
Tag string `json:"tag,omitempty"`
SHA string `json:"sha,omitempty"`
}
Expand Down

0 comments on commit 4dfe87b

Please sign in to comment.