Lemoe před 4 roky
revize
db7aeff888
6 změnil soubory, kde provedl 463 přidání a 0 odebrání
  1. 34 0
      .github/build/friendly-filenames.json
  2. 87 0
      .github/workflows/release.yml
  3. 17 0
      .gitignore
  4. 5 0
      go.mod
  5. 3 0
      go.sum
  6. 317 0
      main.go

+ 34 - 0
.github/build/friendly-filenames.json

@@ -0,0 +1,34 @@
+{
+  "android-arm64": { "friendlyName": "android-arm64-v8a" },
+  "darwin-amd64": { "friendlyName": "macos-64" },
+  "darwin-arm64": { "friendlyName": "macos-arm64-v8a" },
+  "dragonfly-amd64": { "friendlyName": "dragonfly-64" },
+  "freebsd-386": { "friendlyName": "freebsd-32" },
+  "freebsd-amd64": { "friendlyName": "freebsd-64" },
+  "freebsd-arm64": { "friendlyName": "freebsd-arm64-v8a" },
+  "freebsd-arm7": { "friendlyName": "freebsd-arm32-v7a" },
+  "linux-386": { "friendlyName": "linux-32" },
+  "linux-amd64": { "friendlyName": "linux-64" },
+  "linux-arm5": { "friendlyName": "linux-arm32-v5" },
+  "linux-arm64": { "friendlyName": "linux-arm64-v8a" },
+  "linux-arm6": { "friendlyName": "linux-arm32-v6" },
+  "linux-arm7": { "friendlyName": "linux-arm32-v7a" },
+  "linux-mips64le": { "friendlyName": "linux-mips64le" },
+  "linux-mips64": { "friendlyName": "linux-mips64" },
+  "linux-mipslesoftfloat": { "friendlyName": "linux-mips32le-softfloat" },
+  "linux-mipsle": { "friendlyName": "linux-mips32le" },
+  "linux-mipssoftfloat": { "friendlyName": "linux-mips32-softfloat" },
+  "linux-mips": { "friendlyName": "linux-mips32" },
+  "linux-ppc64le": { "friendlyName": "linux-ppc64le" },
+  "linux-ppc64": { "friendlyName": "linux-ppc64" },
+  "linux-riscv64": { "friendlyName": "linux-riscv64" },
+  "linux-s390x": { "friendlyName": "linux-s390x" },
+  "openbsd-386": { "friendlyName": "openbsd-32" },
+  "openbsd-amd64": { "friendlyName": "openbsd-64" },
+  "openbsd-arm64": { "friendlyName": "openbsd-arm64-v8a" },
+  "openbsd-arm7": { "friendlyName": "openbsd-arm32-v7a" },
+  "windows-386": { "friendlyName": "windows-32" },
+  "windows-amd64": { "friendlyName": "windows-64" },
+  "windows-arm64": { "friendlyName": "windows-arm64-v8a" },
+  "windows-arm7": { "friendlyName": "windows-arm32-v7a" }
+}

+ 87 - 0
.github/workflows/release.yml

@@ -0,0 +1,87 @@
+name: Release
+
+on:
+  workflow_dispatch:
+  release:
+    types: [published]
+  push:
+    #branches:
+    #  - main
+    tags:
+      - '*'
+
+jobs:
+  build:
+    strategy:
+      matrix:
+        # Include amd64 on Linux and drawin.
+        goos: [linux, darwin]
+        goarch: [amd64, 386]
+        exclude:
+          # Exclude i386 on darwin.
+          - goarch: 386
+            goos: darwin
+        include:
+          - goos: darwin
+            goarch: arm64
+          - goos: linux
+            goarch: arm64
+      fail-fast: false
+
+    runs-on: ubuntu-latest
+    env:
+      GOOS: ${{ matrix.goos }}
+      GOARCH: ${{ matrix.goarch }}
+      GOARM: ${{ matrix.goarm }}
+      CGO_ENABLED: 0
+
+    steps:
+      - name: Checkout
+        uses: actions/checkout@v2
+
+      - name: Setup Go
+        uses: actions/setup-go@v2
+        with:
+          go-version: 1.17
+    
+      - name: Get project dependencies
+        run: go mod download
+
+      - name: Build
+        run: |
+          mkdir -p build_assets
+          go build -v -o build_assets/oci-help -trimpath -ldflags "-s -w -buildid=" ./...
+
+      - name: Show workflow information
+        id: get_filename
+        run: |
+          export _NAME=$(jq ".[\"$GOOS-$GOARCH$GOARM\"].friendlyName" -r < .github/build/friendly-filenames.json)
+          echo "GOOS: $GOOS, GOARCH: $GOARCH, GOARM: $GOARM, RELEASE_NAME: $_NAME"
+          echo "::set-output name=ASSET_NAME::$_NAME"
+          echo "ASSET_NAME=$_NAME" >> $GITHUB_ENV
+
+      - name: Create ZIP archive
+        run: |
+          pushd build_assets || exit 1
+          zip -9vr ../oci-help-$ASSET_NAME.zip .
+          popd || exit 1
+          FILE=./oci-help-$ASSET_NAME.zip
+          DGST=$FILE.dgst
+          for METHOD in {"md5","sha1","sha256","sha512"}
+          do
+            openssl dgst -$METHOD $FILE | sed 's/([^)]*)//g' >>$DGST
+          done
+
+      - name: Upload ZIP file to Artifacts
+        uses: actions/upload-artifact@v2
+        with:
+          name: oci-help-${{ steps.get_filename.outputs.ASSET_NAME }}.zip
+          path: oci-help-${{ steps.get_filename.outputs.ASSET_NAME }}.zip
+
+      - name: Upload Release
+        uses: softprops/action-gh-release@v1
+        if: startsWith(github.ref, 'refs/tags/')
+        with:
+          files: ./oci-help-${{ steps.get_filename.outputs.ASSET_NAME }}.zip*
+          #draft: true
+          #prerelease: true

+ 17 - 0
.gitignore

@@ -0,0 +1,17 @@
+# Binaries for programs and plugins
+*.exe
+*.exe~
+*.dll
+*.so
+*.dylib
+
+# Test binary, built with `go test -c`
+*.test
+
+# Output of the go coverage tool, specifically when used with LiteIDE
+*.out
+
+# Dependency directories (remove the comment below to include it)
+# vendor/
+
+.DS_Store

+ 5 - 0
go.mod

@@ -0,0 +1,5 @@
+module oci-help
+
+go 1.17
+
+require gopkg.in/ini.v1 v1.63.2

+ 3 - 0
go.sum

@@ -0,0 +1,3 @@
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/ini.v1 v1.63.2 h1:tGK/CyBg7SMzb60vP1M03vNZ3VDu3wGQJwn7Sxi9r3c=
+gopkg.in/ini.v1 v1.63.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=

+ 317 - 0
main.go

@@ -0,0 +1,317 @@
+package main
+
+import (
+	"bufio"
+	"encoding/json"
+	"fmt"
+	"math/rand"
+	"net/http"
+	"net/url"
+	"os"
+	"os/exec"
+	"os/signal"
+	"strings"
+	"syscall"
+	"time"
+
+	"gopkg.in/ini.v1"
+)
+
+var configFile = "./oci-help.ini"
+var cfg *ini.File
+var defSec *ini.Section
+var tg_token string
+var tg_chatId string
+var name string
+
+type Result struct {
+	Status  json.Number `json:"status"`
+	Message string      `json:"message"`
+	Data    interface{} `json:"data"`
+}
+
+type Node struct {
+	Name     string `ini:"name"`
+	ADId     string `ini:"ad_id"`
+	ImageId  string `ini:"image_id"`
+	SubnetId string `ini:"subnet_id"`
+	KeyPub   string `ini:"key_pub"`
+	Tenancy  string `ini:"tenancy_id"`
+	Shape    string `ini:"shape"`
+	CPU      string `ini:"cpu_num"`
+	RAM      string `ini:"ram_num"`
+	HD       string `ini:"hd_num"`
+	MinTime  int    `ini:"min_time"`
+	MaxTime  int    `ini:"max_time"`
+}
+
+func main() {
+	_, Error := os.Stat(configFile)
+	if os.IsNotExist(Error) {
+		os.Create(configFile)
+	}
+	cfg, _ = ini.Load(configFile)
+	defSec = cfg.Section("")
+	tg_token = defSec.Key("token").Value()
+	tg_chatId = defSec.Key("chat_id").Value()
+	rand.Seed(time.Now().UnixNano())
+	setCloseHandler()
+	mainMenu()
+}
+
+func mainMenu() {
+	cmdClear()
+	fmt.Printf("\n\033[1;32;40m%s\033[0m\n\n", "欢迎使用甲骨文新建实例脚本")
+	fmt.Printf("\033[1;36;40m%s\033[0m %s\n", "1.", "查看新建记录")
+	fmt.Printf("\033[1;36;40m%s\033[0m %s\n", "2.", "开始新建实例")
+	fmt.Printf("\033[1;36;40m%s\033[0m %s\n", "3.", "Telegram bot 消息提醒")
+	fmt.Println("")
+	fmt.Print("请选择[1-3]: ")
+	var num int
+	fmt.Scanln(&num)
+	switch num {
+	case 1:
+		loadNode()
+	case 2:
+		addNode()
+	case 3:
+		setTelegramBot()
+	}
+}
+
+func loadNode() {
+	cmdClear()
+	sections := cfg.Sections()
+	fmt.Printf("\n\033[1;32;40m%s\033[0m\n\n", "新建实例历史记录")
+	for i := 1; i < len(sections); i++ {
+		fmt.Printf("\033[1;36;40m%d.\033[0m %s\n", i, sections[i].Name())
+	}
+	fmt.Printf("\n\033[1;36;40m%d.\033[0m %s\n", 0, "返回主菜单")
+	var num int
+	fmt.Print("\n请输入序号, 开始新建实例: ")
+	fmt.Scanln(&num)
+	if num <= 0 || num >= len(sections) {
+		mainMenu()
+		return
+	}
+	section := sections[num]
+	node := new(Node)
+	err := section.MapTo(node)
+	if err != nil {
+		fmt.Println("MapTo failed: ", err)
+		return
+	}
+	launchInstance(node)
+}
+
+func addNode() {
+	cmdClear()
+	var (
+		name       string
+		ad_id      string
+		image_id   string
+		subnet_id  string
+		key_pub    string
+		tenancy_id string
+		shape      string
+		cpu_num    string
+		ram_num    string
+		hd_num     string
+		min_time   string
+		max_time   string
+	)
+	fmt.Printf("\n\033[1;32;40m%s\033[0m\n\n", "开始新建实例, 请按要求输入以下参数")
+	fmt.Print("请随便输入一个名称(不能有空格): ")
+	fmt.Scanln(&name)
+	fmt.Print("请输入[availabilityDomain|availability_domain]: ")
+	fmt.Scanln(&ad_id)
+	fmt.Print("请输入[imageId|source_id]: ")
+	fmt.Scanln(&image_id)
+	fmt.Print("请输入[subnetId|subnet_id]: ")
+	fmt.Scanln(&subnet_id)
+	fmt.Print("请输入[ssh_authorized_keys]: ")
+	reader := bufio.NewReader(os.Stdin)
+	key_pub, _ = reader.ReadString('\n')
+	key_pub = strings.TrimSuffix(key_pub, "\n")
+	fmt.Print("请输入[compartmentId|compartment_id]: ")
+	fmt.Scanln(&tenancy_id)
+	fmt.Print("请输入[shape]: ")
+	fmt.Scanln(&shape)
+	fmt.Print("请输入CPU个数: ")
+	fmt.Scanln(&cpu_num)
+	fmt.Print("请输入内存大小(GB): ")
+	fmt.Scanln(&ram_num)
+	fmt.Print("请输入引导卷大小(GB): ")
+	fmt.Scanln(&hd_num)
+	fmt.Print("请输入最小间隔时间(秒): ")
+	fmt.Scanln(&min_time)
+	fmt.Print("请输入最大间隔时间(秒): ")
+	fmt.Scanln(&max_time)
+
+	section := cfg.Section(name)
+	section.Key("name").SetValue(name)
+	section.NewKey("ad_id", ad_id)
+	section.NewKey("image_id", image_id)
+	section.NewKey("subnet_id", subnet_id)
+	section.NewKey("key_pub", key_pub)
+	section.NewKey("tenancy_id", tenancy_id)
+	section.NewKey("shape", shape)
+	section.NewKey("cpu_num", cpu_num)
+	section.NewKey("ram_num", ram_num)
+	section.NewKey("hd_num", hd_num)
+	section.Key("min_time").SetValue(min_time)
+	section.Key("max_time").SetValue(max_time)
+	cfg.SaveTo(configFile)
+
+	node := new(Node)
+	err := section.MapTo(node)
+	if err != nil {
+		fmt.Println("MapTo failed: ", err)
+		return
+	}
+	launchInstance(node)
+}
+
+func setTelegramBot() {
+	cmdClear()
+	fmt.Printf("\n\033[1;32;40m%s\033[0m\n\n", "Telegram bot 消息提醒配置")
+	fmt.Println("Telegram Bot Token:", tg_token)
+	fmt.Println("Telegram User ID:", tg_chatId)
+	fmt.Printf("\n\033[1;36;40m%s\033[0m %s\n", "1.", "设置token和用户id")
+	fmt.Printf("\n\033[1;36;40m%s\033[0m %s\n", "0.", "返回主菜单")
+	fmt.Print("\n请选择[0-1]: ")
+	var num int
+	fmt.Scanln(&num)
+	switch num {
+	case 1:
+		fmt.Print("请输入 Telegram Bot Token: ")
+		fmt.Scanln(&tg_token)
+		fmt.Print("请输入 Telegram User ID: ")
+		fmt.Scanln(&tg_chatId)
+		defSec.Key("token").SetValue(tg_token)
+		defSec.Key("chat_id").SetValue(tg_chatId)
+		cfg.SaveTo(configFile)
+		fmt.Println("设置成功")
+		mainMenu()
+	default:
+		mainMenu()
+	}
+}
+
+func sendMessage(name, text string) {
+	tg_url := "https://api.telegram.org/bot" + tg_token + "/sendMessage"
+	urlValues := url.Values{
+		"parse_mode": {"Markdown"},
+		"chat_id":    {tg_chatId},
+		"text":       {"*实例: *" + name + "\n" + "*状态: *" + text},
+	}
+	cli := http.Client{Timeout: 10 * time.Second}
+	_, err := cli.PostForm(tg_url, urlValues)
+	if err != nil {
+		printYellow("Telegram 消息提醒发送失败" + err.Error())
+	}
+}
+
+func launchInstance(node *Node) {
+	name = node.Name
+	text := "开始创建实例"
+	printGreen(text)
+	sendMessage(node.Name, text)
+	cmd := "oci"
+	args := []string{
+		"compute", "instance", "launch",
+		"--availability-domain", node.ADId,
+		"--image-id", node.ImageId,
+		"--subnet-id", node.SubnetId,
+		"--metadata", `{"ssh_authorized_keys": "` + node.KeyPub + `"}`,
+		"--compartment-id", node.Tenancy,
+		"--shape", node.Shape,
+		"--shape-config", `{"ocpus":` + node.CPU + `,"memory_in_gbs":` + node.RAM + `}`,
+		"--boot-volume-size-in-gbs", node.HD,
+		"--assign-public-ip", "true",
+		"--is-pv-encryption-in-transit-enabled", "true",
+	}
+
+	for {
+		out, _ := exec.Command(cmd, args...).CombinedOutput()
+		ret := string(out)
+		pos := strings.Index(ret, "{")
+		if pos != -1 {
+			ret = ret[pos:]
+		}
+
+		var result Result
+		err := json.Unmarshal([]byte(ret), &result)
+		if err != nil {
+			text = "出现异常, " + ret
+			printRed(text)
+			sendMessage(node.Name, text)
+			return
+		}
+
+		switch result.Status {
+		case "500", "429":
+			printNone(result.Message)
+		default:
+			if result.Data != nil {
+				text = "实例创建成功"
+				printGreen(text)
+				sendMessage(node.Name, text)
+				return
+			}
+			text = "出现异常, " + result.Message
+			printRed(text)
+			sendMessage(node.Name, text)
+			return
+		}
+		random := random(node.MinTime, node.MaxTime)
+		time.Sleep(time.Duration(random) * time.Second)
+	}
+}
+
+func random(min, max int) int {
+	if min == 0 || max == 0 {
+		return 1
+	}
+	if min >= max {
+		return max
+	}
+	return rand.Intn(max-min) + min
+}
+
+func setCloseHandler() {
+	c := make(chan os.Signal)
+	signal.Notify(c, os.Interrupt, syscall.SIGTERM)
+	go func() {
+		<-c
+		fmt.Printf("\033[1;33;40m%s\033[0m\n", "已停止")
+		if name != "" {
+			sendMessage(name, "已停止")
+		}
+		os.Exit(0)
+	}()
+
+}
+
+func cmdClear() {
+	cmd := exec.Command("clear")
+	cmd.Stdout = os.Stdout
+	cmd.Run()
+}
+
+func printRed(str string) {
+	fmt.Print(time.Now().Format("[2006-01-02 15:04:05] "))
+	fmt.Printf("\033[1;31;40m%s\033[0m\n", str)
+}
+func printGreen(str string) {
+	fmt.Print(time.Now().Format("[2006-01-02 15:04:05] "))
+	fmt.Printf("\033[1;32;40m%s\033[0m\n", str)
+}
+func printYellow(str string) {
+	fmt.Print(time.Now().Format("[2006-01-02 15:04:05] "))
+	fmt.Printf("\033[1;33;40m%s\033[0m\n", str)
+}
+func printNone(str string) {
+	fmt.Print(time.Now().Format("[2006-01-02 15:04:05] "))
+	fmt.Printf("%s\n", str)
+}