5
5
"os"
6
6
"path"
7
7
"path/filepath"
8
+ "strconv"
8
9
"strings"
9
10
"time"
10
11
@@ -35,6 +36,8 @@ type DeployOptions struct {
35
36
Interactive bool
36
37
Force bool
37
38
Upgrade bool
39
+ Latest bool
40
+ Build bool
38
41
checkForUpdate func (ExecDep , string , string , string ) (* releaseInfo , error )
39
42
isDeployed func (ExecDep , string ) error
40
43
}
@@ -74,8 +77,11 @@ func NewDeployCmd(f *cmdutil.Factory, runF func(*DeployOptions) error) *cobra.Co
74
77
}
75
78
76
79
cmd .Flags ().BoolVarP (& opts .Force , "force" , "f" , false , "Force to deploy a new local Instill Core instance" )
77
- cmd .Flags ().BoolVarP (& opts .Upgrade , "upgrade" , "u" , false , "Upgrade Instill Core instance to the latest version" )
80
+ cmd .Flags ().BoolVarP (& opts .Upgrade , "upgrade" , "u" , false , "Upgrade Instill Core instance to the latest release version" )
81
+ cmd .Flags ().BoolVarP (& opts .Latest , "latest" , "l" , false , "Deploy an Instill Core instance with the latest version (unstable)" )
82
+ cmd .Flags ().BoolVarP (& opts .Build , "build" , "b" , false , "Deploy an Instill Core instance and build the images at the local" )
78
83
cmd .MarkFlagsMutuallyExclusive ("force" , "upgrade" )
84
+ cmd .MarkFlagsMutuallyExclusive ("upgrade" , "latest" )
79
85
80
86
return cmd
81
87
}
@@ -99,49 +105,82 @@ func runDeploy(opts *DeployOptions) error {
99
105
}
100
106
101
107
// download the latest version of the projects, if local repos are not present
102
- for _ , proj := range projs {
103
- projDirPath := filepath .Join (LocalInstancePath , proj )
104
- _ , err = os .Stat (projDirPath )
105
- if os .IsNotExist (err ) {
106
- if latestVersion , err := execCmd (opts .Exec , "bash" , "-c" , fmt .Sprintf ("curl https://api.github.com/repos/instill-ai/%s/releases | jq -r 'map(select(.prerelease)) | first | .tag_name'" , proj )); err == nil {
107
- latestVersion = strings .Trim (latestVersion , "\n " )
108
+ projDirPath := filepath .Join (LocalInstancePath , "core" )
109
+ _ , err = os .Stat (projDirPath )
110
+ if os .IsNotExist (err ) {
111
+ if opts .Latest {
112
+ if out , err := execCmd (opts .Exec , "bash" , "-c" ,
113
+ fmt .Sprintf ("git clone --depth 1 https://github.com/instill-ai/core.git %s" , projDirPath )); err != nil {
114
+ return fmt .Errorf ("ERROR: cannot clone Instill Core repo, %w:\n %s" , err , out )
115
+ }
116
+ } else {
117
+ if latestReleaseVersion , err := execCmd (opts .Exec , "bash" , "-c" , "curl https://api.github.com/repos/instill-ai/core/releases | jq -r 'map(select(.prerelease)) | first | .tag_name'" ); err == nil {
118
+ latestReleaseVersion = strings .Trim (latestReleaseVersion , "\n " )
108
119
if out , err := execCmd (opts .Exec , "bash" , "-c" ,
109
- fmt .Sprintf ("git clone --depth 1 -b %s -c advice.detachedHead=false https://github.com/instill-ai/%s .git %s" , latestVersion , proj , projDirPath )); err != nil {
110
- return fmt .Errorf ("ERROR: cannot clone %s , %w:\n %s" , proj , err , out )
120
+ fmt .Sprintf ("git clone --depth 1 -b %s -c advice.detachedHead=false https://github.com/instill-ai/core .git %s" , latestReleaseVersion , projDirPath )); err != nil {
121
+ return fmt .Errorf ("ERROR: cannot clone core , %w:\n %s" , err , out )
111
122
}
112
- if _ , err := opts .checkForUpdate (opts .Exec , filepath .Join (config .StateDir (), fmt . Sprintf ( "%s .yml", proj )), fmt . Sprintf ( "instill-ai/%s " , proj ), latestVersion ); err != nil {
113
- return fmt .Errorf ("ERROR: cannot check for update %s , %w:\n %s" , proj , err , latestVersion )
123
+ if _ , err := opts .checkForUpdate (opts .Exec , filepath .Join (config .StateDir (), "core .yml"), "instill-ai/core " , latestReleaseVersion ); err != nil {
124
+ return fmt .Errorf ("ERROR: cannot check for the update of Instill Core , %w:\n %s" , err , latestReleaseVersion )
114
125
}
115
126
} else {
116
- return fmt .Errorf ("ERROR: cannot find latest release version of %s , %w:\n %s" , proj , err , latestVersion )
127
+ return fmt .Errorf ("ERROR: cannot find latest release version of Instill Core , %w:\n %s" , err , latestReleaseVersion )
117
128
}
118
129
}
119
130
}
120
131
121
132
if opts .Force {
122
133
p (opts .IO , "Tear down Instill Core instance if existing..." )
123
- for _ , proj := range projs {
124
- projDirPath := filepath .Join (LocalInstancePath , proj )
125
- _ , err = os .Stat (projDirPath )
126
- if ! os .IsNotExist (err ) {
127
- if opts .OS != nil {
128
- if err = opts .OS .Chdir (projDirPath ); err != nil {
129
- return err
130
- }
131
- } else {
132
- if err = os .Chdir (projDirPath ); err != nil {
133
- return err
134
- }
134
+ projDirPath := filepath .Join (LocalInstancePath , "core" )
135
+ _ , err = os .Stat (projDirPath )
136
+ if ! os .IsNotExist (err ) {
137
+ if opts .OS != nil {
138
+ if err = opts .OS .Chdir (projDirPath ); err != nil {
139
+ return err
135
140
}
136
- if out , err := execCmd (opts .Exec , "bash" , "-c" , "make down" ); err != nil {
137
- return fmt .Errorf ("ERROR: cannot force tearing down %s, %w:\n %s" , proj , err , out )
141
+ } else {
142
+ if err = os .Chdir (projDirPath ); err != nil {
143
+ return err
138
144
}
139
145
}
146
+ if out , err := execCmd (opts .Exec , "bash" , "-c" , "make down" ); err != nil {
147
+ return fmt .Errorf ("ERROR: cannot force tearing down Instill Core, %w:\n %s" , err , out )
148
+ }
140
149
}
141
- } else if opts .Upgrade {
150
+
151
+ } else if opts .Upgrade && ! opts .Latest {
142
152
hasNewVersion := false
143
- for _ , proj := range projs {
144
- projDirPath := filepath .Join (LocalInstancePath , proj )
153
+ projDirPath := filepath .Join (LocalInstancePath , "core" )
154
+ _ , err = os .Stat (projDirPath )
155
+ if ! os .IsNotExist (err ) {
156
+ if opts .OS != nil {
157
+ if err = opts .OS .Chdir (projDirPath ); err != nil {
158
+ return err
159
+ }
160
+ } else {
161
+ if err = os .Chdir (projDirPath ); err != nil {
162
+ return err
163
+ }
164
+ }
165
+ if currentVersion , err := execCmd (opts .Exec , "bash" , "-c" , "git name-rev --tags --name-only $(git rev-parse HEAD)" ); err == nil {
166
+ currentVersion = strings .Trim (currentVersion , "\n " )
167
+ if currentVersion == "undefined" {
168
+ currentVersion = "latest"
169
+ }
170
+ if newRelease , err := opts .checkForUpdate (opts .Exec , filepath .Join (config .StateDir (), "core.yml" ), "instill-ai/core" , currentVersion ); err != nil {
171
+ return fmt .Errorf ("ERROR: cannot check for the update of Instill Core, %w:\n %s" , err , currentVersion )
172
+ } else if newRelease != nil {
173
+ p (opts .IO , "Upgrade Instill Core to %s..." , newRelease .Version )
174
+ hasNewVersion = true
175
+ }
176
+ } else {
177
+ return fmt .Errorf ("ERROR: cannot find current release version of Instill Core, %w:\n %s" , err , currentVersion )
178
+ }
179
+ }
180
+
181
+ if hasNewVersion {
182
+ p (opts .IO , "Tear down Instill Core instance if existing..." )
183
+ projDirPath := filepath .Join (LocalInstancePath , "core" )
145
184
_ , err = os .Stat (projDirPath )
146
185
if ! os .IsNotExist (err ) {
147
186
if opts .OS != nil {
@@ -153,97 +192,68 @@ func runDeploy(opts *DeployOptions) error {
153
192
return err
154
193
}
155
194
}
156
- if currentVersion , err := execCmd (opts .Exec , "bash" , "-c" , "git name-rev --tags --name-only $(git rev-parse HEAD)" ); err == nil {
157
- currentVersion = strings .Trim (currentVersion , "\n " )
158
- if newRelease , err := opts .checkForUpdate (opts .Exec , filepath .Join (config .StateDir (), fmt .Sprintf ("%s.yml" , proj )), fmt .Sprintf ("instill-ai/%s" , proj ), currentVersion ); err != nil {
159
- return fmt .Errorf ("ERROR: cannot check for update %s, %w:\n %s" , proj , err , currentVersion )
160
- } else if newRelease != nil {
161
- p (opts .IO , "Upgrade %s to %s..." , proj , newRelease .Version )
162
- hasNewVersion = true
163
- }
164
- } else {
165
- return fmt .Errorf ("ERROR: cannot find current release version of %s, %w:\n %s" , proj , err , currentVersion )
195
+ if out , err := execCmd (opts .Exec , "bash" , "-c" , "make down" ); err != nil {
196
+ return fmt .Errorf ("ERROR: cannot force tearing down Instill Core, %w:\n %s" , err , out )
166
197
}
167
198
}
168
- }
169
-
170
- if hasNewVersion {
171
- p (opts .IO , "Tear down Instill Core instance if existing..." )
172
- for _ , proj := range projs {
173
- projDirPath := filepath .Join (LocalInstancePath , proj )
174
- _ , err = os .Stat (projDirPath )
175
- if ! os .IsNotExist (err ) {
176
- if opts .OS != nil {
177
- if err = opts .OS .Chdir (projDirPath ); err != nil {
178
- return err
179
- }
180
- } else {
181
- if err = os .Chdir (projDirPath ); err != nil {
182
- return err
183
- }
184
- }
185
- if out , err := execCmd (opts .Exec , "bash" , "-c" , "make down" ); err != nil {
186
- return fmt .Errorf ("ERROR: cannot force tearing down %s, %w:\n %s" , proj , err , out )
187
- }
188
- }
189
199
190
- if dir , err := os .ReadDir (projDirPath ); err == nil {
191
- for _ , d := range dir {
192
- if os .RemoveAll (path .Join ([]string {projDirPath , d .Name ()}... )); err != nil {
193
- return fmt .Errorf ("ERROR: cannot remove %s, %w" , projDirPath , err )
194
- }
200
+ if dir , err := os .ReadDir (projDirPath ); err == nil {
201
+ for _ , d := range dir {
202
+ if os .RemoveAll (path .Join ([]string {projDirPath , d .Name ()}... )); err != nil {
203
+ return fmt .Errorf ("ERROR: cannot remove %s, %w" , projDirPath , err )
195
204
}
196
- } else {
197
- return fmt .Errorf ("ERROR: cannot read %s, %w" , projDirPath , err )
198
205
}
206
+ } else {
207
+ return fmt .Errorf ("ERROR: cannot read %s, %w" , projDirPath , err )
208
+ }
199
209
200
- if latestVersion , err := execCmd (opts .Exec , "bash" , "-c" , fmt .Sprintf ("curl https://api.github.com/repos/instill-ai/%s/releases | jq -r 'map(select(.prerelease)) | first | .tag_name'" , proj )); err == nil {
201
- latestVersion = strings .Trim (latestVersion , "\n " )
202
- if out , err := execCmd (opts .Exec , "bash" , "-c" ,
203
- fmt .Sprintf ("git clone --depth 1 -b %s -c advice.detachedHead=false https://github.com/instill-ai/%s.git %s" , latestVersion , proj , projDirPath )); err != nil {
204
- return fmt .Errorf ("ERROR: cannot clone %s, %w:\n %s" , proj , err , out )
205
- }
206
- } else {
207
- return fmt .Errorf ("ERROR: cannot find latest release version of %s, %w:\n %s" , proj , err , latestVersion )
210
+ if latestVersion , err := execCmd (opts .Exec , "bash" , "-c" , fmt .Sprintf ("curl https://api.github.com/repos/instill-ai/%s/releases | jq -r 'map(select(.prerelease)) | first | .tag_name'" , "core" )); err == nil {
211
+ latestVersion = strings .Trim (latestVersion , "\n " )
212
+ if out , err := execCmd (opts .Exec , "bash" , "-c" ,
213
+ fmt .Sprintf ("git clone --depth 1 -b %s -c advice.detachedHead=false https://github.com/instill-ai/core.git %s" , latestVersion , projDirPath )); err != nil {
214
+ return fmt .Errorf ("ERROR: cannot clone Instill Core, %w:\n %s" , err , out )
208
215
}
216
+ } else {
217
+ return fmt .Errorf ("ERROR: cannot find latest release version of Instill Core, %w:\n %s" , err , latestVersion )
209
218
}
210
219
} else {
211
220
p (opts .IO , "No upgrade available" )
212
221
return nil
213
222
}
214
223
215
224
} else {
216
- for _ , proj := range projs {
217
- if err := opts .isDeployed (opts .Exec , proj ); err == nil {
218
- p (opts .IO , "A local Instill Core deployment detected" )
219
- return nil
220
- }
225
+ if err := opts .isDeployed (opts .Exec , "core" ); err == nil {
226
+ p (opts .IO , "A local Instill Core deployment detected" )
227
+ return nil
221
228
}
222
229
}
223
230
224
- p (opts .IO , "Launch Instill Core..." )
225
- for _ , proj := range projs {
226
- projDirPath := filepath .Join (LocalInstancePath , proj )
227
- _ , err = os .Stat (projDirPath )
228
- if ! os .IsNotExist (err ) {
229
- if opts .OS != nil {
230
- err = opts .OS .Chdir (projDirPath )
231
- } else {
232
- err = os .Chdir (projDirPath )
233
- }
234
- if err != nil {
235
- return fmt .Errorf ("ERROR: cannot open the directory: %w" , err )
236
- }
231
+ _ , err = os .Stat (projDirPath )
232
+ if ! os .IsNotExist (err ) {
233
+ if opts .OS != nil {
234
+ err = opts .OS .Chdir (projDirPath )
235
+ } else {
236
+ err = os .Chdir (projDirPath )
237
+ }
238
+ if err != nil {
239
+ return fmt .Errorf ("ERROR: cannot open the directory: %w" , err )
237
240
}
241
+ }
238
242
243
+ if opts .Latest {
244
+ p (opts .IO , "Spin up latest Instill Core..." )
245
+ if out , err := execCmd (opts .Exec , "bash" , "-c" , fmt .Sprintf ("make latest BUILD=%s" , strconv .FormatBool (opts .Build ))); err != nil {
246
+ return fmt .Errorf ("ERROR: Instill Core spin-up failed, %w\n %s" , err , out )
247
+ }
248
+ } else {
239
249
if currentVersion , err := execCmd (opts .Exec , "bash" , "-c" , "git name-rev --tags --name-only $(git rev-parse HEAD)" ); err == nil {
240
250
currentVersion = strings .Trim (currentVersion , "\n " )
241
- p (opts .IO , "Spin up %s %s..." , proj , currentVersion )
242
- if out , err := execCmd (opts .Exec , "bash" , "-c" , "make all" ); err != nil {
243
- return fmt .Errorf ("ERROR: %s spin-up failed, %w\n %s" , proj , err , out )
244
- }
251
+ p (opts .IO , "Spin up Instill Core %s..." , currentVersion )
245
252
} else {
246
- return fmt .Errorf ("ERROR: cannot get current tag %s, %w:\n %s" , proj , err , currentVersion )
253
+ return fmt .Errorf ("ERROR: cannot get the current tag of Instill Core repo, %w:\n %s" , err , currentVersion )
254
+ }
255
+ if out , err := execCmd (opts .Exec , "bash" , "-c" , fmt .Sprintf ("make all BUILD=%s" , strconv .FormatBool (opts .Build ))); err != nil {
256
+ return fmt .Errorf ("ERROR: Instill Core spin-up failed, %w\n %s" , err , out )
247
257
}
248
258
}
249
259
0 commit comments