123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553 |
- package license
- import (
- "bufio"
- "fmt"
- "io"
- "io/ioutil"
- "os"
- "os/exec"
- "strings"
- "text/template"
- "time"
- )
- func GenerateLicense(pJ ProjectInfo, eI EnvironmentInfo, lI LicenseInfo) []byte {
- app := LicenseApplication{
- ProjectInfo: pJ,
- EnvironmentInfo: eI,
- LicenseInfo: lI,
- FeatureInfo: FeatureInfo{
- HA: true,
- Hint: true,
- FullTextIndex: true,
- FunctionIndex: true,
- BatchMode: true,
- PlSql: true,
- AddNode: false,
- AutoPartition: true,
- ResourceQuota: true,
- NetChannel: 0,
- DataAutoRebalance: true,
- BlackWhiteList: true,
- Mac: true,
- ColPrivilege: true,
- TransmitEncrypt: true,
- StoreEncrypt: true,
- BackupEncrypt: true,
- AuditMode: 0,
- GlobalBinlogNode: 0,
- Dblink: true,
- Json: true,
- Spatial: true,
- MaxParallel: 0,
- StreamLoad: true,
- },
- ServiceCapacity: ServiceCapacity{
- InstanceNum: 0,
- TableRows: 0,
- ConnectNum: 0,
- StoreCapacity: 0,
- MaxNodeNum: 0,
- ListenChannel: 0,
- },
- }
- fileName, _ := GenerateLicenseCofingFile(app)
- ExecLicenseProgram(fileName)
- license := ReadLicenseFile(fileName)
- return license
- }
- func GenerateClusterTmpl() string {
- const tmpl = `
- #用户项目信息
- project_name = {{.ProjectInfo.ProjectName}}
- user_name = {{.ProjectInfo.UserName}}
- user_addr = {{.ProjectInfo.UserAddr}}
- serial_number = {{.ProjectInfo.SerialNumber}}
- #环境信息
- cpu_sn = {{.EnvironmentInfo.CpuSN}}
- baseboard_sn = {{.EnvironmentInfo.BaseboardSN}}
- mac_addr = {{.EnvironmentInfo.MacAddr}}
- disk_id = {{.EnvironmentInfo.DiskID}}
- ip_addr = {{.EnvironmentInfo.IPAddr}}
- #License信息
- gen_date = {{.LicenseInfo.GenDate}}
- expire_date = {{.LicenseInfo.ExpireDate}}
- license_type = {{.LicenseInfo.LicenseType}}
- license_version = {{.LicenseInfo.LicenseVersion}}
- hard_type = {{.LicenseInfo.HardType}}
- #企业版
- ha = true
- hint = true
- full_text_index = true
- function_index = true
- batch_mode = true
- pl_sql = true
- #分布式版
- add_node = true
- auto_partition = true
- resource_quota = true
- net_channel = 0
- data_auto_rebalance = true
- #其他组件
- black_white_list = true
- mac = true
- col_privilege = true
- transmit_encrypt = true
- store_encrypt = true
- backup_encrypt = true
- audit_mode = 0
- global_binlog_node = 0
- dblink = true
- json = true
- spatial = true
- max_parallel = 0
- stream_load = true
- #服务能力
- instance_num = 0
- table_rows = 0
- connect_num = 0
- store_capacity = 0
- max_node_num = 0
- listen_channel = 0
- `
- return tmpl
- }
- func GenerateEnterpriseTmpl() string {
- const tmpl = `
- #用户项目信息
- project_name = {{.ProjectInfo.ProjectName}}
- user_name = {{.ProjectInfo.UserName}}
- user_addr = {{.ProjectInfo.UserAddr}}
- serial_number = {{.ProjectInfo.SerialNumber}}
- #环境信息
- cpu_sn = {{.EnvironmentInfo.CpuSN}}
- baseboard_sn = {{.EnvironmentInfo.BaseboardSN}}
- mac_addr = {{.EnvironmentInfo.MacAddr}}
- disk_id = {{.EnvironmentInfo.DiskID}}
- ip_addr = {{.EnvironmentInfo.IPAddr}}
- #License信息
- gen_date = {{.LicenseInfo.GenDate}}
- expire_date = {{.LicenseInfo.ExpireDate}}
- license_type = {{.LicenseInfo.LicenseType}}
- license_version = {{.LicenseInfo.LicenseVersion}}
- hard_type = {{.LicenseInfo.HardType}}
- #企业版
- ha = true
- hint = true
- full_text_index = true
- function_index = true
- batch_mode = true
- pl_sql = true
- #分布式版
- add_node = false
- auto_partition = true
- resource_quota = true
- net_channel = 0
- data_auto_rebalance = true
- #其他组件
- black_white_list = true
- mac = true
- col_privilege = true
- transmit_encrypt = true
- store_encrypt = true
- backup_encrypt = true
- audit_mode = 0
- global_binlog_node = 0
- dblink = true
- json = true
- spatial = true
- max_parallel = 0
- stream_load = true
- #服务能力
- instance_num = 0
- table_rows = 0
- connect_num = 0
- store_capacity = 0
- max_node_num = 0
- listen_channel = 0
- `
- return tmpl
- }
- func GenerateStandardTmpl() string {
- const tmpl = `
- #用户项目信息
- project_name = {{.ProjectInfo.ProjectName}}
- user_name = {{.ProjectInfo.UserName}}
- user_addr = {{.ProjectInfo.UserAddr}}
- serial_number = {{.ProjectInfo.SerialNumber}}
- #环境信息
- cpu_sn = {{.EnvironmentInfo.CpuSN}}
- baseboard_sn = {{.EnvironmentInfo.BaseboardSN}}
- mac_addr = {{.EnvironmentInfo.MacAddr}}
- disk_id = {{.EnvironmentInfo.DiskID}}
- ip_addr = {{.EnvironmentInfo.IPAddr}}
- #License信息
- gen_date = {{.LicenseInfo.GenDate}}
- expire_date = {{.LicenseInfo.ExpireDate}}
- license_type = {{.LicenseInfo.LicenseType}}
- license_version = {{.LicenseInfo.LicenseVersion}}
- hard_type = {{.LicenseInfo.HardType}}
- #企业版
- ha = true
- hint = true
- full_text_index = true
- function_index = true
- batch_mode = true
- pl_sql = true
- #分布式版
- add_node = false
- auto_partition = true
- resource_quota = true
- net_channel = 0
- data_auto_rebalance = true
- #其他组件
- black_white_list = true
- mac = true
- col_privilege = true
- transmit_encrypt = true
- store_encrypt = true
- backup_encrypt = true
- audit_mode = 0
- global_binlog_node = 0
- dblink = true
- json = true
- spatial = true
- max_parallel = 0
- stream_load = true
- #服务能力
- instance_num = 0
- table_rows = 0
- connect_num = 0
- store_capacity = 0
- max_node_num = 0
- listen_channel = 0
- `
- return tmpl
- }
- func GenerateLiteTmpl() string {
- const tmpl = `
- #用户项目信息
- project_name = {{.ProjectInfo.ProjectName}}
- user_name = {{.ProjectInfo.UserName}}
- user_addr = {{.ProjectInfo.UserAddr}}
- serial_number = {{.ProjectInfo.SerialNumber}}
- #环境信息
- cpu_sn = {{.EnvironmentInfo.CpuSN}}
- baseboard_sn = {{.EnvironmentInfo.BaseboardSN}}
- mac_addr = {{.EnvironmentInfo.MacAddr}}
- disk_id = {{.EnvironmentInfo.DiskID}}
- ip_addr = {{.EnvironmentInfo.IPAddr}}
- #License信息
- gen_date = {{.LicenseInfo.GenDate}}
- expire_date = {{.LicenseInfo.ExpireDate}}
- license_type = {{.LicenseInfo.LicenseType}}
- license_version = {{.LicenseInfo.LicenseVersion}}
- hard_type = {{.LicenseInfo.HardType}}
- #功能列表
- ha = true
- hint = true
- full_text_index = true
- function_index = true
- batch_mode = true
- pl_sql = true
- auto_partition = true
- net_channel = 0
- col_privilege = true
- transmit_encrypt = true
- store_encrypt = true
- backup_encrypt = true
- global_binlog_node = 0
- json = true
- spatial = true
- max_parallel = 0
- stream_load = true
- instance_num = 0
- table_rows = 0
- connect_num = 0
- store_capacity = 0
- max_node_num = 0
- listen_channel = 0
- `
- return tmpl
- }
- func GeneratePersonTmpl() string {
- const tmpl = `
- #用户项目信息
- project_name = {{.ProjectInfo.ProjectName}}
- user_name = {{.ProjectInfo.UserName}}
- user_addr = {{.ProjectInfo.UserAddr}}
- serial_number = {{.ProjectInfo.SerialNumber}}
- #环境信息
- cpu_sn = {{.EnvironmentInfo.CpuSN}}
- baseboard_sn = {{.EnvironmentInfo.BaseboardSN}}
- mac_addr = {{.EnvironmentInfo.MacAddr}}
- disk_id = {{.EnvironmentInfo.DiskID}}
- ip_addr = {{.EnvironmentInfo.IPAddr}}
- #License信息
- gen_date = {{.LicenseInfo.GenDate}}
- expire_date = {{.LicenseInfo.ExpireDate}}
- license_type = {{.LicenseInfo.LicenseType}}
- license_version = {{.LicenseInfo.LicenseVersion}}
- hard_type = {{.LicenseInfo.HardType}}
- #企业版
- ha = true
- hint = true
- full_text_index = true
- function_index = true
- batch_mode = true
- pl_sql = true
- #分布式版
- add_node = false
- auto_partition = true
- resource_quota = true
- net_channel = 0
- data_auto_rebalance = true
- #其他组件
- black_white_list = true
- mac = true
- col_privilege = true
- transmit_encrypt = true
- store_encrypt = true
- backup_encrypt = true
- audit_mode = 0
- global_binlog_node = 0
- dblink = true
- json = true
- spatial = true
- max_parallel = 0
- stream_load = true
- #服务能力
- instance_num = 0
- table_rows = 0
- connect_num = 0
- store_capacity = 0
- max_node_num = 0
- listen_channel = 0
- `
- return tmpl
- }
- func GenerateLicenseCofingFile(app LicenseApplication) (string, error) {
- var tmpl string
- switch app.LicenseInfo.LicenseType {
- case 4:
- tmpl = GenerateClusterTmpl()
- case 3:
- tmpl = GenerateEnterpriseTmpl()
- case 1:
- tmpl = GeneratePersonTmpl()
- case 2:
- tmpl = GenerateStandardTmpl()
- case 6:
- tmpl = GenerateLiteTmpl()
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- t, err := template.New("config").Parse(tmpl)
- if err != nil {
- fmt.Println("Error creating template:", err)
- return "", err
- }
- fileName := fmt.Sprintf("lic_op_%s_%s.cfg", time.Now().Format("20060102150405"), app.UserName)
-
- file, err := os.Create(fileName)
- if err != nil {
- fmt.Println("Error creating file:", err)
- return "", err
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- err = t.Execute(file, app)
- if err != nil {
- fmt.Println("Error executing template:", err)
- }
- fileNameToCRLF := fmt.Sprintf("CRLF_" + fileName)
-
- err = convertLFtoCRLF(fileName, fileNameToCRLF)
- if err != nil {
- fmt.Printf("转换文件时出错: %v\n", err)
- } else {
- fmt.Println("文件转换成功!")
- }
- defer file.Close()
-
-
-
-
-
-
-
-
- return fileNameToCRLF, nil
- }
- func ExecLicenseProgram(fileName string) {
- cmd2 := exec.Command("pwd")
-
- output2, err := cmd2.CombinedOutput()
- if err != nil {
- fmt.Printf("执行命令时出错: %v\n", err)
- return
- }
-
- fmt.Printf("PWD输出: %s\n", output2)
-
- cmd := exec.Command("/home/gtong/license/lic_go/xugu_license/pkg/lic/xugu_linux_x86_64_20240920_license-test", "-gen_license", fileName)
-
- output, err := cmd.CombinedOutput()
- if err != nil {
- fmt.Printf("执行命令时出错: %v\n", err)
- return
- }
-
- fmt.Printf("输出: %s\n", output)
- }
- func ReadLicenseFile(fileName string) []byte {
-
- localFile := "license.dat@" + fileName
- file, err := os.Open(localFile)
- if err != nil {
- fmt.Printf("无法打开文件: %v\n", err)
- return nil
- }
- defer file.Close()
-
- content, err := ioutil.ReadAll(file)
- if err != nil {
- fmt.Printf("无法读取文件: %v\n", err)
- return nil
- }
- return content
- }
- func convertLFtoCRLF(inputFile, outputFile string) error {
-
- inFile, err := os.Open(inputFile)
- if err != nil {
- return fmt.Errorf("无法打开输入文件: %v", err)
- }
- defer inFile.Close()
-
- outFile, err := os.Create(outputFile)
- if err != nil {
- return fmt.Errorf("无法创建输出文件: %v", err)
- }
- defer outFile.Close()
- reader := bufio.NewReader(inFile)
- writer := bufio.NewWriter(outFile)
- for {
- line, err := reader.ReadString('\n')
- if err != nil && err != io.EOF {
- return fmt.Errorf("读取文件时出错: %v", err)
- }
- if err == io.EOF {
- if len(line) > 0 {
- line = strings.TrimRight(line, "\n")
- writer.WriteString(line + "\r\n")
- }
- break
- }
-
- line = strings.TrimRight(line, "\n")
- writer.WriteString(line + "\r\n")
- }
- writer.Flush()
- return nil
- }
|