-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add create etcd cluster and weed cluster function (#2304)
feat: add create etcd cluster and weed cluster function --------- Signed-off-by: sjcsjc123 <1401189096@qq.com>
- Loading branch information
Showing
155 changed files
with
61,172 additions
and
86 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright © 2023 Alibaba Group Holding Ltd. | ||
// | ||
// 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 imagedistributor | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/sealerio/sealer/pkg/define/options" | ||
"github.com/sealerio/sealer/pkg/imageengine" | ||
v1 "github.com/sealerio/sealer/types/api/v1" | ||
"github.com/sealerio/sealer/utils/os/fs" | ||
"github.com/sealerio/sealer/utils/weed" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
type weedMounter struct { | ||
imageEngine imageengine.Interface | ||
weedClient weed.Deployer | ||
} | ||
|
||
func (w *weedMounter) Mount(imageName string, platform v1.Platform, dest string) (string, string, string, error) { | ||
mountDir := filepath.Join(dest, | ||
strings.ReplaceAll(imageName, "/", "_"), | ||
strings.Join([]string{platform.OS, platform.Architecture, platform.Variant}, "_")) | ||
|
||
imageID, err := w.imageEngine.Pull(&options.PullOptions{ | ||
Quiet: false, | ||
PullPolicy: "missing", | ||
Image: imageName, | ||
Platform: platform.ToString(), | ||
}) | ||
if err != nil { | ||
return "", "", "", err | ||
} | ||
|
||
if err := fs.FS.MkdirAll(filepath.Dir(mountDir)); err != nil { | ||
return "", "", "", err | ||
} | ||
|
||
id, err := w.imageEngine.CreateWorkingContainer(&options.BuildRootfsOptions{ | ||
DestDir: mountDir, | ||
ImageNameOrID: imageID, | ||
}) | ||
|
||
if err != nil { | ||
return "", "", "", err | ||
} | ||
|
||
// Upload the mounted files to the WeedFS cluster | ||
err = w.weedClient.UploadFile(context.Background(), mountDir) | ||
if err != nil { | ||
return "", "", "", err | ||
} | ||
|
||
return mountDir, id, imageID, nil | ||
} | ||
|
||
func (w *weedMounter) Umount(dir, containerID string) error { | ||
// Download the files from WeedFS cluster | ||
err := w.weedClient.DownloadFile(context.Background(), dir, dir) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Umount the image and remove the working container | ||
err = w.imageEngine.RemoveContainer(&options.RemoveContainerOptions{ | ||
ContainerNamesOrIDs: []string{containerID}, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Remove the mounted files from the WeedFS cluster | ||
err = w.weedClient.RemoveFile(context.Background(), dir) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Remove the local mount directory | ||
if err := fs.FS.RemoveAll(dir); err != nil { | ||
return fmt.Errorf("failed to remove mount dir %s: %v", dir, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func NewWeedMounter(imageEngine imageengine.Interface, config *weed.Config) Mounter { | ||
deployer := weed.NewDeployer(config) | ||
return &weedMounter{ | ||
imageEngine: imageEngine, | ||
weedClient: deployer, | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright © 2021 Alibaba Group Holding Ltd. | ||
// | ||
// 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 weed | ||
|
||
import ( | ||
"path" | ||
"strconv" | ||
|
||
"github.com/sealerio/sealer/utils/exec" | ||
) | ||
|
||
// checkPort checks if the port is available or can be used. | ||
func checkPort(port int) bool { | ||
// lsof -i:9333 | ||
err := exec.Cmd("lsof", "-i:"+strconv.Itoa(port)) | ||
return err == nil | ||
} | ||
|
||
// checkDir checks if the dir is available or can be used. | ||
//func checkDir(dir string) bool { | ||
// // ls /tmp | ||
// err := exec.Cmd("ls", dir) | ||
// if err != nil { | ||
// return false | ||
// } | ||
// return true | ||
//} | ||
|
||
func checkBinFile(fileName string) bool { | ||
binName := path.Base(fileName) | ||
switch binName { | ||
case "weed": | ||
|
||
case "etcd": | ||
|
||
default: | ||
} | ||
return false | ||
} |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright © 2021 Alibaba Group Holding Ltd. | ||
// | ||
// 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 weed | ||
|
||
//import "testing" | ||
// | ||
//func TestCheckPort(t *testing.T) { | ||
// ok := checkPort(9333) | ||
// t.Log(ok) | ||
//} | ||
// | ||
//func TestCheckDir(t *testing.T) { | ||
// ok := checkDir("/tmp") | ||
// t.Log(ok) | ||
//} |
Oops, something went wrong.