github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/apply/processor/install.go (about)

     1  // Copyright © 2021 Alibaba Group Holding Ltd.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package processor
    16  
    17  import (
    18  	"github.com/alibaba/sealer/pkg/clusterfile"
    19  	"github.com/alibaba/sealer/pkg/config"
    20  	"github.com/alibaba/sealer/pkg/filesystem"
    21  	"github.com/alibaba/sealer/pkg/guest"
    22  	"github.com/alibaba/sealer/pkg/plugin"
    23  	v2 "github.com/alibaba/sealer/types/api/v2"
    24  	"github.com/alibaba/sealer/utils/platform"
    25  )
    26  
    27  type InstallProcessor struct {
    28  	clusterFile clusterfile.Interface
    29  	Guest       guest.Interface
    30  	Config      config.Interface
    31  	Plugins     plugin.Plugins
    32  }
    33  
    34  func (i *InstallProcessor) GetPipeLine() ([]func(cluster *v2.Cluster) error, error) {
    35  	var todoList []func(cluster *v2.Cluster) error
    36  	todoList = append(todoList,
    37  		i.Process,
    38  		i.RunConfig,
    39  		i.MountRootfs,
    40  		i.GetPhasePluginFunc(plugin.PhasePreGuest),
    41  		i.Install,
    42  		i.GetPhasePluginFunc(plugin.PhasePostInstall),
    43  	)
    44  	return todoList, nil
    45  }
    46  
    47  func (i *InstallProcessor) Process(cluster *v2.Cluster) error {
    48  	i.Config = config.NewConfiguration(cluster)
    49  	i.Plugins = plugin.NewPlugins(cluster, i.clusterFile.GetPlugins())
    50  	return nil
    51  }
    52  
    53  func (i *InstallProcessor) RunConfig(cluster *v2.Cluster) error {
    54  	return i.Config.Dump(i.clusterFile.GetConfigs())
    55  }
    56  
    57  func (i *InstallProcessor) MountRootfs(cluster *v2.Cluster) error {
    58  	hosts := append(cluster.GetMasterIPList(), cluster.GetNodeIPList()...)
    59  	//initFlag : no need to do init cmd like installing docker service and so on.
    60  	fs, err := filesystem.NewFilesystem(platform.DefaultMountCloudImageDir(cluster.Name))
    61  	if err != nil {
    62  		return err
    63  	}
    64  	return fs.MountRootfs(cluster, hosts, false)
    65  }
    66  
    67  func (i *InstallProcessor) Install(cluster *v2.Cluster) error {
    68  	return i.Guest.Apply(cluster)
    69  }
    70  
    71  func (i *InstallProcessor) GetPhasePluginFunc(phase plugin.Phase) func(cluster *v2.Cluster) error {
    72  	return func(cluster *v2.Cluster) error {
    73  		if phase == plugin.PhasePreGuest {
    74  			if err := i.Plugins.Load(); err != nil {
    75  				return err
    76  			}
    77  		}
    78  		return i.Plugins.Run(cluster.GetAllIPList(), phase)
    79  	}
    80  }
    81  
    82  func NewInstallProcessor(clusterFile clusterfile.Interface) (Processor, error) {
    83  	gs, err := guest.NewGuestManager()
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  
    88  	return &InstallProcessor{
    89  		clusterFile: clusterFile,
    90  		Guest:       gs,
    91  	}, nil
    92  }