-
Notifications
You must be signed in to change notification settings - Fork 0
/
beamer.go
106 lines (98 loc) · 2.88 KB
/
beamer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
package beamer
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
)
var (
errBeamerDirNotFound = errors.New("could not find .beamer directory. \n\tRun `beamer init` to create it")
errRepoAbsolutePathNotSet = errors.New("please set absolute path in `.beamer/.config/` to GCP Template repo")
errTemplateDirNotFoundInConfig = errors.New("please set the template directory in .beamer/config. \n\ttemplateDir=/absolute/path/to/gcp/template ")
)
func Init() {
if beamerDirIsExist() {
fmt.Println("Found `.beamer` directory, skipping...")
os.Exit(0)
}
// create .beamer directory
fmt.Println("Could not find `.beamer` directory, attempting to create..")
err := os.Mkdir(".beamer", os.ModePerm)
if err != nil {
panic(err)
}
tempConfig := "templateDir=<PATH/TO/REPO>/DataflowTemplates/src/main/java/com/google/cloud/teleport/templates/"
err = ioutil.WriteFile(".beamer/config", []byte(tempConfig), 0644)
if err != nil {
panic(err)
}
// ignore .beamer directory
f, err := os.OpenFile(".gitignore", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
defer f.Close()
if _, err := f.WriteString("\n.beamer/"); err != nil {
panic(err)
}
fmt.Println("Created, done!")
}
func Gen(templateName string) {
if !beamerDirIsExist() {
fmt.Println(errBeamerDirNotFound)
os.Exit(64)
}
data, err := ioutil.ReadFile(".beamer/config")
if err != nil {
panic(err)
}
txt := string(data)
if strings.Contains(txt, "<PATH/TO/REPO>") {
fmt.Println(errRepoAbsolutePathNotSet)
os.Exit(64)
}
if !strings.Contains(txt, "templateDir") {
fmt.Println(errTemplateDirNotFoundInConfig)
os.Exit(64)
}
config := strings.Split(txt, "=")
filePath := fmt.Sprintf("%s%s.java", config[1], templateName)
options := ExtractOptionsFromFile(filePath)
options.WriteToFile(fmt.Sprintf("%s.json", templateName))
fmt.Printf("Job config template generated for `%s` migration.\n", templateName)
}
func Run(templateName string, skipValidation bool) {
data, err := ioutil.ReadFile(fmt.Sprintf(".beamer/%s.json", templateName))
if err != nil {
panic(err)
}
var config JobConfig
json.Unmarshal(data, &config)
config.Validate(skipValidation)
gcloudExecPath, err := exec.LookPath("gcloud")
if err != nil {
panic(err)
}
cmdGCloud := &exec.Cmd{
Path: gcloudExecPath,
Args: []string{
gcloudExecPath, "dataflow", "jobs", "run", config.JobName,
fmt.Sprintf("--gcs-location=%v", config.GCSLocation),
fmt.Sprintf("--project=%v", config.Project),
fmt.Sprintf("--region=%v", config.Region),
fmt.Sprintf("--service-account-email=%v", config.ServiceAccountEmail),
fmt.Sprintf("--staging-location=%v", config.StagingLocation),
fmt.Sprintf("--parameters %v", config.ParamString()),
},
Stdout: os.Stdout,
Stderr: os.Stderr,
}
fmt.Println(cmdGCloud.String())
}
func beamerDirIsExist() bool {
_, err := os.Stat(".beamer/")
return err == nil
}