diff --git a/SimpleInstall/Linstall.go b/SimpleInstall/Linstall.go new file mode 100644 index 0000000..8e83d0a --- /dev/null +++ b/SimpleInstall/Linstall.go @@ -0,0 +1,198 @@ +package main + +import ( + "archive/tar" + "bufio" + "bytes" + "compress/gzip" + "fmt" + "io" + "log" + "net/http" + "os" + "os/exec" + "path/filepath" +) + +// public command to do something, for example:to execute a command +func publicCommand() { + +} +func osCommand() { + + cmd := exec.Command("mkdir", "-p", "/etc/docker") + var stdout, stderr bytes.Buffer + cmd.Stdout = &stdout // 标准输出 + cmd.Stderr = &stderr // 标准错误 + err := cmd.Run() + outStr, errStr := string(stdout.Bytes()), string(stderr.Bytes()) + fmt.Printf("out:\n%s\nerr:\n%s\n", outStr, errStr) + if err != nil { + log.Fatalf("cmd.Run() failed with %s\n", err) + } +} + +func creatService() { + var dockerService = ` +[Unit] +Description=Docker Application Container Engine +Documentation=http://docs.docker.io + +[Service] +Environment="PATH=/usr/local/bin:/bin:/sbin:/usr/bin:/usr/sbin" +ExecStart=/usr/local/bin/dockerd +ExecStartPost=/sbin/iptables -I FORWARD -s 0.0.0.0/0 -j ACCEPT +ExecReload=/bin/kill -s HUP $MAINPID +Restart=on-failure +RestartSec=5 +LimitNOFILE=infinity +LimitNPROC=infinity +LimitCORE=infinity +Delegate=yes +KillMode=process + +[Install] +WantedBy=multi-user.target +` + + filePath := "/Users/yun/dev/code/go/devops/SimpleInstall/docker.service" + //filePath := "/usr/lib/systemd/system/docker.service" + file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666) + if err != nil { + fmt.Println("文件打开失败", err) + } + //及时关闭file句柄 + defer file.Close() + //写入文件时,使用带缓存的 *Writer + write := bufio.NewWriter(file) + for i := 0; i < 5; i++ { + write.WriteString(dockerService) + } + //Flush将缓存的文件真正写入到文件中 + write.Flush() +} + +// download install source code or executable file +func publicDownloadFile() { + imgUrl := "https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/static/stable/x86_64/docker-23.0.1.tgz" + // Get the data + resp, err := http.Get(imgUrl) + if err != nil { + panic(err) + } + defer resp.Body.Close() + // 创建一个文件用于保存 + out, err := os.Create("docker-23.0.1.tgz") + if err != nil { + panic(err) + } + defer out.Close() + + // 然后将响应流和文件流对接起来 + _, err = io.Copy(out, resp.Body) + if err != nil { + panic(err) + } +} + +// create system service +func createSystemlServices() { + +} + +func UnTar(dst, src string) (err error) { + // 打开准备解压的 tar 包 + fr, err := os.Open(src) + if err != nil { + return + } + defer fr.Close() + + // 将打开的文件先解压 + gr, err := gzip.NewReader(fr) + if err != nil { + return + } + defer gr.Close() + + // 通过 gr 创建 tar.Reader + tr := tar.NewReader(gr) + + // 现在已经获得了 tar.Reader 结构了,只需要循环里面的数据写入文件就可以了 + for { + hdr, err := tr.Next() + + switch { + case err == io.EOF: + return nil + case err != nil: + return err + case hdr == nil: + continue + } + + // 处理下保存路径,将要保存的目录加上 header 中的 Name + // 这个变量保存的有可能是目录,有可能是文件,所以就叫 FileDir 了…… + dstFileDir := filepath.Join(dst, hdr.Name) + + // 根据 header 的 Typeflag 字段,判断文件的类型 + switch hdr.Typeflag { + case tar.TypeDir: // 如果是目录时候,创建目录 + // 判断下目录是否存在,不存在就创建 + if b := ExistDir(dstFileDir); !b { + // 使用 MkdirAll 不使用 Mkdir ,就类似 Linux 终端下的 mkdir -p, + // 可以递归创建每一级目录 + if err := os.MkdirAll(dstFileDir, 0775); err != nil { + return err + } + } + case tar.TypeReg: // 如果是文件就写入到磁盘 + // 创建一个可以读写的文件,权限就使用 header 中记录的权限 + // 因为操作系统的 FileMode 是 int32 类型的,hdr 中的是 int64,所以转换下 + file, err := os.OpenFile(dstFileDir, os.O_CREATE|os.O_RDWR, os.FileMode(hdr.Mode)) + if err != nil { + return err + } + n, err := io.Copy(file, tr) + if err != nil { + return err + } + // 将解压结果输出显示 + fmt.Printf("成功解压: %s , 共处理了 %d 个字符\n", dstFileDir, n) + + // 不要忘记关闭打开的文件,因为它是在 for 循环中,不能使用 defer + // 如果想使用 defer 就放在一个单独的函数中 + file.Close() + } + } + + return nil +} + +// 判断目录是否存在 +func ExistDir(dirname string) bool { + fi, err := os.Stat(dirname) + return (err == nil || os.IsExist(err)) && fi.IsDir() +} + +// https://learnku.com/articles/23433/golang-learning-notes-four-archivetar-package-compression-and-decompression + +func initSystem() { + + fmt.Println("env config change ok!") + fmt.Println("kernel config change ok!") + fmt.Println("network config change ok!") +} +func installDocker() { + + fmt.Println("install docker ok!") +} +func main() { + creatService() + //publicDownloadFile() + + //UnTar("", "/Users/yun/dev/code/go/devops/SimpleInstall/docker-23.0.1.tgz") + //creatService() + //installDocker() + +} diff --git a/data.json b/data.json new file mode 100644 index 0000000..11e31db --- /dev/null +++ b/data.json @@ -0,0 +1,135 @@ +{ + "data": { + "timeStamp": 1723899671, + "fetchTimeStamp": 1723899020, + "updateTimeStamp": 1723898460, + "provinceName": "江苏", + "cityName": "南京", + "countyName": "浦口", + "linkSeven": "http://www.weather.com.cn/weather/101190107.shtml#7d", + "currentWeather": "晴", + "currentWeatherIcon": "a0", + "currentTemp": "31", + "currentAqiValue": "46", + "currentAqiLevel": "优", + "weatherArr": [ + { + "date": "08月17日", + "time": "周六", + "dateInfo": { + "date": "8月17日", + "lunar": "七月十四", + "timeStamp": 1723824000, + "festival": "", + "weatherSourceUrl": "" + }, + "temp": "26 ~ 35", + "wind": "东北风3-4级", + "condition": "多云", + "imgs": [ + "a1", + "a1" + ], + "link": "http://www.weather.com.cn/weather/101190107.shtml#7d", + "pm25": "46", + "pm25url": "//www.baidu.com/s?wd=南京空气质量指数\u0026amp;tn=baidutop10\u0026amp;rsv_idx=2" + }, + { + "date": "08月18日", + "time": "周日", + "dateInfo": { + "date": "8月18日", + "lunar": "七月十五", + "timeStamp": 1723910400, + "festival": "", + "weatherSourceUrl": "" + }, + "temp": "27 ~ 35", + "wind": "东风3-4级", + "condition": "多云", + "imgs": [ + "a1", + "a1" + ], + "link": "http://www.weather.com.cn/weather/101190107.shtml#7d", + "pm25": "", + "pm25url": "//www.baidu.com/s?wd=南京空气质量指数\u0026amp;tn=baidutop10\u0026amp;rsv_idx=2" + }, + { + "date": "08月19日", + "time": "周一", + "dateInfo": { + "date": "8月19日", + "lunar": "七月十六", + "timeStamp": 1723996800, + "festival": "", + "weatherSourceUrl": "" + }, + "temp": "26 ~ 35", + "wind": "东风3-4级", + "condition": "雷阵雨转中到大雨", + "imgs": [ + "a4", + "a1" + ], + "link": "http://www.weather.com.cn/weather/101190107.shtml#7d", + "pm25": "", + "pm25url": "//www.baidu.com/s?wd=南京空气质量指数\u0026amp;tn=baidutop10\u0026amp;rsv_idx=2" + }, + { + "date": "08月20日", + "time": "周二", + "dateInfo": { + "date": "8月20日", + "lunar": "七月十七", + "timeStamp": 1724083200, + "festival": "", + "weatherSourceUrl": "" + }, + "temp": "25 ~ 31", + "wind": "南风3-4级", + "condition": "中到大雨转多云", + "imgs": [ + "a1", + "a1" + ], + "link": "http://www.weather.com.cn/weather/101190107.shtml#7d", + "pm25": "", + "pm25url": "//www.baidu.com/s?wd=南京空气质量指数\u0026amp;tn=baidutop10\u0026amp;rsv_idx=2" + }, + { + "date": "08月21日", + "time": "周三", + "dateInfo": { + "date": "8月21日", + "lunar": "七月十八", + "timeStamp": 1724169600, + "festival": "", + "weatherSourceUrl": "" + }, + "temp": "25 ~ 34", + "wind": "西风\u003c3级", + "condition": "阴转多云", + "imgs": [ + "a2", + "a1" + ], + "link": "http://www.weather.com.cn/weather/101190107.shtml#7d", + "pm25": "", + "pm25url": "//www.baidu.com/s?wd=南京空气质量指数\u0026amp;tn=baidutop10\u0026amp;rsv_idx=2" + } + ], + "weatherType": "aladdin", + "pslink": "//www.baidu.com/s?tn=baidutop10\u0026rsv_idx=2\u0026wd=南京天气预报", + "pollution": "0", + "alarm": [ + { + "type": "高温", + "level": "黄色", + "color": "fcc419" + } + ] + }, + "errno": 0, + "msg": "ok" +} \ No newline at end of file diff --git a/getjson.go b/getjson.go new file mode 100644 index 0000000..6134c18 --- /dev/null +++ b/getjson.go @@ -0,0 +1,105 @@ +package main + +import ( + "encoding/json" + "log" + "os" + + "github.com/tealeg/xlsx" +) + +type WeatherData struct { + Data struct { + TimeStamp int64 `json:"timeStamp"` + FetchTimeStamp int64 `json:"fetchTimeStamp"` + UpdateTimeStamp int64 `json:"updateTimeStamp"` + ProvinceName string + CityName string + CountyName string + LinkSeven string + CurrentWeather string + CurrentWeatherIcon string + CurrentTemp string + CurrentAqiValue string + CurrentAqiLevel string + WeatherArr []struct { + Date string + Time string + DateInfo struct { + Date string + Lunar string + TimeStamp int64 + Festival string + WeatherSourceUrl string + } + Temp string + Wind string + Condition string + Imgs []string + Link string + Pm25 string + Pm25Url string + } + WeatherType string + Pslink string + Pollution string + Alarm []struct { + Type string + Level string + Color string + } + } `json:"data"` + Errno int `json:"errno"` + Msg string `json:"msg"` +} + +func main() { + // 打开 JSON 文件 + jsonFile, err := os.Open("data.json") + if err != nil { + log.Fatalf("Failed to open JSON file: %v", err) + } + defer jsonFile.Close() + + // 解析 JSON 文件 + var weatherData WeatherData + err = json.NewDecoder(jsonFile).Decode(&weatherData) + if err != nil { + log.Fatalf("Failed to decode JSON file: %v", err) + } + + // 创建 Excel 文件 + file := xlsx.NewFile() + sheet, err := file.AddSheet("Weather Data") + if err != nil { + log.Fatalf("Failed to add sheet: %v", err) + } + + // 添加表头 + headerRow := sheet.AddRow() + headerRow.AddCell().SetValue("Date") + headerRow.AddCell().SetValue("Time") + headerRow.AddCell().SetValue("Temperature") + headerRow.AddCell().SetValue("Wind") + headerRow.AddCell().SetValue("Condition") + headerRow.AddCell().SetValue("PM2.5") + + // 添加数据 + for _, data := range weatherData.Data.WeatherArr { + row := sheet.AddRow() + row.AddCell().SetValue(data.Date) + row.AddCell().SetValue(data.Time) + row.AddCell().SetValue(data.Temp) + row.AddCell().SetValue(data.Wind) + row.AddCell().SetValue(data.Condition) + row.AddCell().SetValue(data.Pm25) + } + + // 保存文件 + err = file.Save("weather_data1.xlsx") + if err != nil { + log.Fatalf("Failed to save the Excel file: %v", err) + } + + log.Println("Excel file created successfully.") +} diff --git a/t1.go b/t1.go new file mode 100644 index 0000000..7403d14 --- /dev/null +++ b/t1.go @@ -0,0 +1,84 @@ +package main + +import ( + "encoding/json" + "fmt" + "github.com/360EntSecGroup-Skylar/excelize" + "io/ioutil" + "os" +) + +type VM struct { + ID int `json:"id"` + Name string `json:"name"` + Status string `json:"status"` + IP string `json:"ip"` + CPU int `json:"cpu"` + Memory int `json:"memory"` + Disk int `json:"disk"` + OS string `json:"os"` + CreateTime string `json:"create_time"` + UpdateTime string `json:"update_time"` +} + +type Response struct { + Code int `json:"code"` + Msg string `json:"msg"` + Data []VM `json:"data"` +} + +func main() { + // 读取 JSON 文件 + jsonBytes, err := ioutil.ReadFile("vm.json") + if err != nil { + fmt.Println("Error reading JSON file:", err) + os.Exit(1) + } + + // 解析 JSON 数据 + var response Response + err = json.Unmarshal(jsonBytes, &response) + if err != nil { + fmt.Println("Error unmarshaling JSON:", err) + os.Exit(1) + } + + // 创建新的 Excel 文件 + f := excelize.NewFile() + + // 设置表头 + f.SetCellValue("Sheet1", "A1", "ID") + f.SetCellValue("Sheet1", "B1", "Name") + f.SetCellValue("Sheet1", "C1", "Status") + f.SetCellValue("Sheet1", "D1", "IP") + f.SetCellValue("Sheet1", "E1", "CPU") + f.SetCellValue("Sheet1", "F1", "Memory") + f.SetCellValue("Sheet1", "G1", "Disk") + f.SetCellValue("Sheet1", "H1", "OS") + f.SetCellValue("Sheet1", "I1", "Create Time") + f.SetCellValue("Sheet1", "J1", "Update Time") + + // 循环写入数据 + for i, vm := range response.Data { + row := i + 2 // Start from row 2 + f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), vm.ID) + f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), vm.Name) + f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), vm.Status) + f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), vm.IP) + f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), vm.CPU) + f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), vm.Memory) + f.SetCellValue("Sheet1", fmt.Sprintf("G%d", row), vm.Disk) + f.SetCellValue("Sheet1", fmt.Sprintf("H%d", row), vm.OS) + f.SetCellValue("Sheet1", fmt.Sprintf("I%d", row), vm.CreateTime) + f.SetCellValue("Sheet1", fmt.Sprintf("J%d", row), vm.UpdateTime) + } + + // 保存 Excel 文件 + err = f.SaveAs("virtual_machines.xlsx") + if err != nil { + fmt.Println("Failed to save Excel file:", err) + os.Exit(1) + } + + fmt.Println("Excel file saved successfully.") +} diff --git a/vm.json b/vm.json new file mode 100644 index 0000000..a430d44 --- /dev/null +++ b/vm.json @@ -0,0 +1,55 @@ +{ + "code": 0, + "msg": "success", + "data": [ + { + "id": 1, + "name": "vm1", + "status": "running", + "ip": "192.168.1.1", + "cpu": 1, + "memory": 1024, + "disk": 10, + "os": "centos7", + "create_time": "2020-01-01 00:00:00", + "update_time": "2020-01-01 00:00:00" + }, + { + "id": 2, + "name": "vm2", + "status": "running", + "ip": "192.168.1.2", + "cpu": 2, + "memory": 2048, + "disk": 20, + "os": "centos8", + "create_time": "2020-01-01 00:00:00", + "update_time": "2020-01-01 00:00:00" + }, + { + "id": 3, + "name": "vm3", + "status": "running", + "ip": "192.168.1.3", + "cpu": 4, + "memory": 4096, + "disk": 40, + "os": "centos7", + "create_time": "2020-01-01 00:00:00", + "update_time": "2020-01-0100:00:00" + }, + { + "id": 4, + "name": "vm4", + "status": "running", + "ip": "192.168.1.4", + "cpu": 8, + "memory": 8192, + "disk": 80, + "os": "centos8", + "create_time": "2020-01-01 00:00:00", + "update_time": "2020-01-01 00:00:00" + } + ] + +} \ No newline at end of file