/
airflow.go
182 lines (149 loc) · 4.18 KB
/
airflow.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package cmd
import (
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/astronomerio/astro-cli/airflow"
"github.com/astronomerio/astro-cli/config"
"github.com/astronomerio/astro-cli/utils"
"github.com/iancoleman/strcase"
"github.com/spf13/cobra"
)
var (
projectRoot string
projectName string
airflowRootCmd = &cobra.Command{
Use: "airflow",
Short: "Manage airflow projects and deployments",
Long: "Manage airflow projects and deployments",
}
airflowInitCmd = &cobra.Command{
Use: "init",
Short: "Scaffold a new airflow project",
Long: "Scaffold a new airflow project",
RunE: airflowInit,
}
airflowCreateCmd = &cobra.Command{
Use: "create",
Short: "Create a new airflow deployment",
Long: "Create a new airflow deployment",
RunE: airflowCreate,
}
airflowDeployCmd = &cobra.Command{
Use: "deploy",
Short: "Deploy an airflow project",
Long: "Deploy an airflow project to a given deployment",
Args: cobra.ExactArgs(1),
PreRun: ensureProjectDir,
RunE: airflowDeploy,
}
airflowStartCmd = &cobra.Command{
Use: "start",
Short: "Start a development airflow cluster",
Long: "Start a development airflow cluster",
PreRun: ensureProjectDir,
RunE: airflowStart,
}
airflowKillCmd = &cobra.Command{
Use: "kill",
Short: "Kill a development airflow cluster",
Long: "Kill a development airflow cluster",
PreRun: ensureProjectDir,
RunE: airflowKill,
}
airflowStopCmd = &cobra.Command{
Use: "stop",
Short: "Stop a development airflow cluster",
Long: "Stop a development airflow cluster",
PreRun: ensureProjectDir,
RunE: airflowStop,
}
airflowPSCmd = &cobra.Command{
Use: "ps",
Short: "List airflow containers",
Long: "List airflow containers",
PreRun: ensureProjectDir,
RunE: airflowPS,
}
)
func init() {
// Set up project root
projectRoot, _ = config.ProjectRoot()
// Airflow root
RootCmd.AddCommand(airflowRootCmd)
// Airflow init
airflowInitCmd.Flags().StringVarP(&projectName, "name", "n", "", "Name of airflow project")
airflowRootCmd.AddCommand(airflowInitCmd)
// Airflow create
airflowRootCmd.AddCommand(airflowCreateCmd)
// Airflow deploy
airflowRootCmd.AddCommand(airflowDeployCmd)
// Airflow start
airflowRootCmd.AddCommand(airflowStartCmd)
// Airflow kill
airflowRootCmd.AddCommand(airflowKillCmd)
// Airflow stop
airflowRootCmd.AddCommand(airflowStopCmd)
// Airflow PS
airflowRootCmd.AddCommand(airflowPSCmd)
}
func ensureProjectDir(cmd *cobra.Command, args []string) {
if !(len(projectRoot) > 0) {
fmt.Println("Error: Not in an astronomer project directory")
os.Exit(1)
}
}
// Use project name for image name
func airflowInit(cmd *cobra.Command, args []string) error {
// Grab working directory
path := utils.GetWorkingDir()
// Validate project name
if len(projectName) != 0 {
projectNameValid := regexp.
MustCompile(`^[A-Za-z0-9]([A-Za-z0-9_-]*[A-Za-z0-9])?$`).
MatchString
if !projectNameValid(projectName) {
return errors.New("Project name is invalid")
}
} else {
projectDirectory := filepath.Base(path)
projectName = strings.Replace(strcase.ToSnake(projectDirectory), "_", "-", -1)
}
exists := config.ProjectConfigExists()
if !exists {
config.CreateProjectConfig(path)
}
config.CFG.ProjectName.SetProjectString(projectName)
airflow.Init(path)
if exists {
fmt.Printf("Reinitialized existing astronomer project in %s\n", path)
} else {
fmt.Printf("Initialized empty astronomer project in %s\n", path)
}
return nil
}
func airflowCreate(cmd *cobra.Command, args []string) error {
return airflow.Create()
}
func airflowDeploy(cmd *cobra.Command, args []string) error {
return airflow.Deploy(projectRoot, args[0])
}
// Start an airflow cluster
func airflowStart(cmd *cobra.Command, args []string) error {
return airflow.Start(projectRoot)
}
// Kill an airflow cluster
func airflowKill(cmd *cobra.Command, args []string) error {
return airflow.Kill(projectRoot)
}
// Stop an airflow cluster
func airflowStop(cmd *cobra.Command, args []string) error {
return airflow.Stop(projectRoot)
}
// List containers of an airflow cluster
func airflowPS(cmd *cobra.Command, args []string) error {
return airflow.PS(projectRoot)
}