-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
119 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,98 @@ | ||
package main | ||
|
||
import ( | ||
"log/slog" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/pelletier/go-toml/v2" | ||
"github.com/wweir/contatto/conf" | ||
) | ||
|
||
type InstallCmd struct{} | ||
type InstallCmd struct { | ||
Interactive *InstallInteractiveCmd `cmd:"" default:"1" hidden:"" help:"123"` | ||
Docker *InstallDockerCmd `cmd:"" help:"inject proxy as mirror to dockerd config file"` | ||
Containerd *InstallContainerdCmd `cmd:"" help:"inject proxy as puller in containerd config file"` | ||
} | ||
|
||
func (c *InstallCmd) Run() error { | ||
slog.Info("install") | ||
type InstallInteractiveCmd struct{} | ||
|
||
func (c *InstallInteractiveCmd) Run(config *conf.Config) error { | ||
return nil | ||
} | ||
|
||
type InstallDockerCmd struct { | ||
File string `arg:"" required:"" default:"/etc/docker/daemon.json" help:"dockerd config file, default: /etc/docker/daemon.json"` | ||
} | ||
|
||
func (c *InstallDockerCmd) Run(config *conf.Config) error { | ||
fmt.Printf("inject mirror http://%s into docker config: %s\n", config.Addr, c.File) | ||
|
||
f, err := os.Open(c.File) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
dockerConfig := map[string]any{} | ||
if err := json.NewDecoder(f).Decode(&dockerConfig); err != nil { | ||
return err | ||
} | ||
|
||
if dockerConfig["registry-mirrors"] == nil { | ||
dockerConfig["registry-mirrors"] = []string{} | ||
} | ||
|
||
mirrors := dockerConfig["registry-mirrors"].([]any) | ||
proxyAddr := "http://" + config.Addr | ||
for _, mirror := range mirrors { | ||
if mirror.(string) == proxyAddr { | ||
return nil | ||
} | ||
} | ||
|
||
dockerConfig["registry-mirrors"] = append([]any{proxyAddr}, mirrors...) | ||
|
||
f, err = os.OpenFile(c.File+"_safe", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
en := json.NewEncoder(f) | ||
en.SetIndent("", " ") | ||
if err := en.Encode(dockerConfig); err != nil { | ||
return err | ||
} | ||
|
||
return os.Rename(c.File+"_safe", c.File) | ||
} | ||
|
||
type InstallContainerdCmd struct { | ||
File string `arg:"" required:"" default:"/etc/containerd/config.toml" help:"containerd config file, default: /etc/containerd/config.toml"` | ||
} | ||
|
||
func (c *InstallContainerdCmd) Run(config *conf.Config) error { | ||
fmt.Printf("inject proxy http://%s into containerd config: %s\n", config.Addr, c.File) | ||
|
||
f, err := os.Open(c.File) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
containerdConfig := map[string]any{} | ||
if err := toml.NewDecoder(f).Decode(&containerdConfig); err != nil { | ||
return err | ||
} | ||
|
||
switch containerdConfig["version"].(int64) { | ||
case 2: | ||
return c.installContainerdV2(config, containerdConfig) | ||
default: | ||
return fmt.Errorf("unsupported containerd config version: %d", containerdConfig["version"]) | ||
} | ||
} | ||
|
||
func (c *InstallContainerdCmd) installContainerdV2(config *conf.Config, containerdConfig map[string]any) error { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters