loader.go 6.7 KB
Newer Older
D
dogsheng 已提交
1 2
/*
 * Copyright (c) 2019 Huawei Technologies Co., Ltd.
3 4 5
 * A-Tune is licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
6
 *     http://license.coscl.org.cn/MulanPSL2
D
dogsheng 已提交
7 8 9
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
10
 * See the Mulan PSL v2 for more details.
D
dogsheng 已提交
11 12 13 14 15 16
 * Create: 2019-10-29
 */

package profile

import (
Z
Zhipeng Xie 已提交
17
	"fmt"
18 19 20
	CONF "gitee.com/openeuler/A-Tune/common/config"
	"gitee.com/openeuler/A-Tune/common/log"
	"gitee.com/openeuler/A-Tune/common/sqlstore"
21
	"os"
D
dogsheng 已提交
22
	"path"
23
	"path/filepath"
D
dogsheng 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
	"regexp"
	"strings"

	"github.com/go-ini/ini"
)

func filter(vs []string) []string {
	regex := regexp.MustCompile("^[a-zA-Z0-9_.-]+$")
	vsf := make([]string, 0)

	for _, v := range vs {
		if regex.MatchString(v) {
			vsf = append(vsf, v)
		}
	}

	return vsf
}

/*
Load profile_names slice, and it's include profiles, the profiles slice returned must be in order.
The profile inherit from it's include profile, make sure the include profile behind the main profile,
and not override by the include profile.
*/
Z
Zhipeng Xie 已提交
48 49
func loadProfile(profileNames []string, profiles []Profile,
	processedProfiles []string, included bool) ([]Profile, []string) {
D
dogsheng 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
	for _, name := range profileNames {
		name = strings.Trim(name, " ")

		processedProfiles = append(processedProfiles, name)

		config, err := loadConfigData(name)
		if err != nil {
			fmt.Println("Failure to load_config_data")
			continue
		}
		profile := Create(name, name, config)

		profiles = append(profiles, profile)
		if profile.options != nil {
			if profile.options.HasKey("include") {
				include, _ := profile.options.GetKey("include")
				names := make([]string, 0)
67 68 69
				for _, includeValue := range strings.Split(include.Value(), ",") {
					names = append(names, includeValue)
				}
D
dogsheng 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
				profiles, processedProfiles = loadProfile(names, profiles, processedProfiles, true)
			}
		}
		profile.included = included
	}

	return profiles, processedProfiles
}

func isInclude(section string, profile Profile) (int, bool) {
	var index = 0
	for index, unit := range profile.units {
		if unit.Name() == section {
			return index, true
		}
	}

	return index, false
}

func merge(profiles []Profile) Profile {
	final := profiles[0]

	// Ingnore included profile name
	if final.included {
		final.name = ""
		final.path = ""
	}

	for i, profile := range profiles {
		if i == 0 { // Ignore first element: final
			continue
		}

		// Process Options
		if profile.options != nil {
			for _, key := range profile.options.Keys() {
				if key.Name() == "include" {
					continue
				}

				if final.options == nil {
					final.options, _ = final.config.NewSection("main")
				}
Z
Zhipeng Xie 已提交
114
				_, _ = final.options.NewKey(key.Name(), key.Value())
D
dogsheng 已提交
115 116

				section, _ := final.config.GetSection("main")
Z
Zhipeng Xie 已提交
117
				_, _ = section.NewKey(key.Name(), key.Value())
D
dogsheng 已提交
118 119 120 121 122 123 124 125 126 127 128
			}
		}

		// Process Units
		for _, unit := range profile.units {
			index, include := isInclude(unit.Name(), final)
			if !include {
				final.units = append(final.units, unit)

				section, _ := final.config.NewSection(unit.Name())
				for _, key := range unit.Keys() {
Z
Zhipeng Xie 已提交
129
					_, _ = section.NewKey(key.Name(), key.Value())
D
dogsheng 已提交
130 131 132 133 134 135 136
				}
			} else {
				section, _ := final.config.GetSection(unit.Name())
				for _, key := range unit.Keys() {
					if final.units[index].HasKey(key.Name()) {
						continue
					} else {
Z
Zhipeng Xie 已提交
137 138
						_, _ = final.units[index].NewKey(key.Name(), key.Value())
						_, _ = section.NewKey(key.Name(), key.Value())
D
dogsheng 已提交
139 140 141 142 143 144 145 146 147 148 149
					}
				}
			}
		}

		// Process inputs parameter
		if profile.inputs != nil {
			for _, key := range profile.inputs.Keys() {
				if final.inputs == nil {
					final.inputs, _ = final.config.NewSection("inputs")
				}
Z
Zhipeng Xie 已提交
150
				_, _ = final.inputs.NewKey(key.Name(), key.Value())
D
dogsheng 已提交
151 152

				section, _ := final.config.GetSection("inputs")
Z
Zhipeng Xie 已提交
153
				_, _ = section.NewKey(key.Name(), key.Value())
D
dogsheng 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
			}
		}
		//
		if !profile.included {
			final.name = final.name + " " + profile.name
			final.path = final.path + "," + profile.path
		}
	}

	return final
}

// LoadFromWorkloadType method load the profile content depned the workload type
func LoadFromWorkloadType(workloadType string) (Profile, bool) {
	classProfile := &sqlstore.GetClass{Class: workloadType}
	err := sqlstore.GetClasses(classProfile)
	if err != nil {
H
hanxinke 已提交
171
		log.Errorf("inquery class_profile table failed")
D
dogsheng 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185
		return Profile{}, false
	}
	if len(classProfile.Result) == 0 {
		log.Errorf("%s is not exist in the class_profile table", workloadType)
		return Profile{}, false
	}

	profileType := classProfile.Result[0].ProfileType
	profileNames := strings.Split(profileType, ",")

	pro, exist := Load(profileNames)
	return pro, exist
}

186 187 188 189 190 191 192
// LoadFromProfile method load the profile content depend the profile name
func LoadFromProfile(profiles string) (Profile, bool) {
	profileNames := strings.Split(profiles, ",")
	pro, exist := Load(profileNames)
	return pro, exist
}

D
dogsheng 已提交
193 194 195 196 197 198 199 200 201 202
// Load method load the profile content depned the profile names list
func Load(profileNames []string) (Profile, bool) {
	profileNames = filter(profileNames)
	if len(profileNames) == 0 {
		fmt.Println("No profile or invaild profiles were specified.")
		return Profile{}, false
	}

	profiles := make([]Profile, 0)
	processedProfiles := make([]string, 0)
Z
Zhipeng Xie 已提交
203
	profiles, _ = loadProfile(profileNames, profiles, processedProfiles, false)
D
dogsheng 已提交
204 205 206 207 208 209 210 211 212 213

	if len(profiles) == 0 {
		return Profile{}, false
	}
	finalProfile := merge(profiles)

	defaultConfigFile := path.Join(CONF.DefaultConfPath, "atuned.cnf")
	cfg, _ := ini.Load(defaultConfigFile)
	finalProfile.inputs = cfg.Section("system")

214
	finalProfile.name = strings.Join(profileNames, ",")
D
dogsheng 已提交
215 216 217 218
	return finalProfile, true
}

func loadConfigData(name string) (*ini.File, error) {
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
	var config *ini.File

	err := filepath.Walk(CONF.DefaultProfilePath, func(absPath string, info os.FileInfo, err error) error {
		if !info.IsDir() {
			absFilename := absPath[len(CONF.DefaultProfilePath)+1:]
			filenameOnly := strings.TrimSuffix(strings.ReplaceAll(absFilename, "/", "-"),
				path.Ext(info.Name()))
			if filenameOnly == name {
				config, err = ini.Load(absPath)
				if err != nil {
					return err
				}
				return nil
			}
		}
		return nil
	})
D
dogsheng 已提交
236 237 238 239

	if err != nil {
		return nil, err
	}
240
	if config == nil {
Z
Zhipeng Xie 已提交
241
		return nil, fmt.Errorf("%s profile is not found!", name)
242
	}
D
dogsheng 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255
	dirName := CONF.DefaultScriptPath
	for _, section := range config.Sections() {
		if section.Name() == "script" {
			if section.HasKey("shell") {
				key, _ := section.GetKey("shell")
				scriptPath := path.Join(dirName, strings.Trim(key.Value(), " "))
				config.Section(section.Name()).Key("shell").SetValue(scriptPath)
			}
		}
	}

	return config, nil
}