Skip to content

Commit

Permalink
Support specify address for rotate server
Browse files Browse the repository at this point in the history
  • Loading branch information
lucklove committed Dec 8, 2020
1 parent ed6dcc8 commit f1cfa79
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
5 changes: 4 additions & 1 deletion cmd/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ func newMirrorModifyCmd() *cobra.Command {

// the `mirror rotate` sub command
func newMirrorRotateCmd() *cobra.Command {
addr := "0.0.0.0:8080"

cmd := &cobra.Command{
Use: "rotate",
Short: "Rotate root.json",
Expand All @@ -287,14 +289,15 @@ func newMirrorRotateCmd() *cobra.Command {
return err
}

manifest, err := rotate.Serve(":8080", root)
manifest, err := rotate.Serve(addr, root)
if err != nil {
return err
}

return environment.GlobalEnv().V1Repository().Mirror().Rotate(manifest)
},
}
cmd.Flags().StringVarP(&addr, "addr", "", addr, "listen address of rotate server")

return cmd
}
Expand Down
17 changes: 15 additions & 2 deletions server/rotate/rotate_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package rotate
import (
"context"
"fmt"
"net"
"net/http"
"strings"

cjson "github.com/gibson042/canonicaljson-go"
"github.com/gorilla/mux"
Expand All @@ -19,8 +21,19 @@ type statusRender struct {
}

func newStatusRender(manifest *v1manifest.Manifest, addr string) *statusRender {
ss := strings.Split(addr, ":")
if strings.Trim(ss[0], " ") == "" || strings.Trim(ss[0], " ") == "0.0.0.0" {
addrs, _ := net.InterfaceAddrs()
for _, addr := range addrs {
if ip, ok := addr.(*net.IPNet); ok && !ip.IP.IsLoopback() && ip.IP.To4() != nil {
ss[0] = ip.IP.To4().String()
break
}
}
}

status := &statusRender{
mbar: progress.NewMultiBar(fmt.Sprintf("Waiting all administrators to sign %s", addr)),
mbar: progress.NewMultiBar(fmt.Sprintf("Waiting all administrators to sign http://%s/rotate/root.json", strings.Join(ss, ":"))),
bars: make(map[string]*progress.MultiBarItem),
}
root := manifest.Signed.(*v1manifest.Root)
Expand Down Expand Up @@ -75,7 +88,7 @@ func Serve(addr string, root *v1manifest.Root) (*v1manifest.Manifest, error) {
}()

manifest := &v1manifest.Manifest{Signed: root}
status := newStatusRender(manifest, fmt.Sprintf("%s/rotate/root.json", addr))
status := newStatusRender(manifest, addr)
defer status.stop()

SIGLOOP:
Expand Down

0 comments on commit f1cfa79

Please sign in to comment.