Skip to content

Commit dcc639e

Browse files
committed
chore(local): adopt new core launch logic
1 parent 3c59531 commit dcc639e

File tree

6 files changed

+183
-184
lines changed

6 files changed

+183
-184
lines changed

pkg/cmd/local/deploy.go

Lines changed: 108 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"path"
77
"path/filepath"
8+
"strconv"
89
"strings"
910
"time"
1011

@@ -35,6 +36,8 @@ type DeployOptions struct {
3536
Interactive bool
3637
Force bool
3738
Upgrade bool
39+
Latest bool
40+
Build bool
3841
checkForUpdate func(ExecDep, string, string, string) (*releaseInfo, error)
3942
isDeployed func(ExecDep, string) error
4043
}
@@ -74,8 +77,11 @@ func NewDeployCmd(f *cmdutil.Factory, runF func(*DeployOptions) error) *cobra.Co
7477
}
7578

7679
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")
7883
cmd.MarkFlagsMutuallyExclusive("force", "upgrade")
84+
cmd.MarkFlagsMutuallyExclusive("upgrade", "latest")
7985

8086
return cmd
8187
}
@@ -99,49 +105,82 @@ func runDeploy(opts *DeployOptions) error {
99105
}
100106

101107
// 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")
108119
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)
111122
}
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)
114125
}
115126
} 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)
117128
}
118129
}
119130
}
120131

121132
if opts.Force {
122133
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
135140
}
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
138144
}
139145
}
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+
}
140149
}
141-
} else if opts.Upgrade {
150+
151+
} else if opts.Upgrade && !opts.Latest {
142152
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")
145184
_, err = os.Stat(projDirPath)
146185
if !os.IsNotExist(err) {
147186
if opts.OS != nil {
@@ -153,97 +192,68 @@ func runDeploy(opts *DeployOptions) error {
153192
return err
154193
}
155194
}
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)
166197
}
167198
}
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-
}
189199

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)
195204
}
196-
} else {
197-
return fmt.Errorf("ERROR: cannot read %s, %w", projDirPath, err)
198205
}
206+
} else {
207+
return fmt.Errorf("ERROR: cannot read %s, %w", projDirPath, err)
208+
}
199209

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)
208215
}
216+
} else {
217+
return fmt.Errorf("ERROR: cannot find latest release version of Instill Core, %w:\n%s", err, latestVersion)
209218
}
210219
} else {
211220
p(opts.IO, "No upgrade available")
212221
return nil
213222
}
214223

215224
} 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
221228
}
222229
}
223230

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)
237240
}
241+
}
238242

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 {
239249
if currentVersion, err := execCmd(opts.Exec, "bash", "-c", "git name-rev --tags --name-only $(git rev-parse HEAD)"); err == nil {
240250
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)
245252
} 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)
247257
}
248258
}
249259

pkg/cmd/local/local.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import (
1717
"github.com/instill-ai/cli/pkg/cmdutil"
1818
)
1919

20-
var projs = [3]string{"core", "vdp", "model"}
21-
2220
// ExecDep is an interface for executing commands
2321
type ExecDep interface {
2422
Command(name string, arg ...string) *exec.Cmd
@@ -64,22 +62,22 @@ func New(f *cmdutil.Factory) *cobra.Command {
6462

6563
// check for update
6664
if cmd.Flags().Lookup("upgrade") == nil || (cmd.Flags().Lookup("upgrade") != nil && !cmd.Flags().Lookup("upgrade").Changed) {
67-
for _, proj := range projs {
68-
projDirPath := filepath.Join(LocalInstancePath, proj)
69-
_, err = os.Stat(projDirPath)
70-
if !os.IsNotExist(err) {
71-
if err = os.Chdir(projDirPath); err != nil {
72-
return err
73-
}
74-
if currentVersion, err := execCmd(nil, "bash", "-c", "git name-rev --tags --name-only $(git rev-parse HEAD)"); err == nil {
75-
currentVersion = strings.Trim(currentVersion, "\n")
76-
if newRelease, err := checkForUpdate(nil, filepath.Join(config.StateDir(), fmt.Sprintf("%s.yml", proj)), fmt.Sprintf("instill-ai/%s", proj), currentVersion); err != nil {
77-
return fmt.Errorf("ERROR: cannot check for update %s, %w:\n%s", proj, err, currentVersion)
65+
projDirPath := filepath.Join(LocalInstancePath, "core")
66+
_, err = os.Stat(projDirPath)
67+
if !os.IsNotExist(err) {
68+
if err = os.Chdir(projDirPath); err != nil {
69+
return err
70+
}
71+
if currentVersion, err := execCmd(nil, "bash", "-c", "git name-rev --tags --name-only $(git rev-parse HEAD)"); err == nil {
72+
currentVersion = strings.Trim(currentVersion, "\n")
73+
if currentVersion != "undefined" {
74+
if newRelease, err := checkForUpdate(nil, filepath.Join(config.StateDir(), "core.yml"), "instill-ai/core", currentVersion); err != nil {
75+
return fmt.Errorf("ERROR: cannot check for the update Instill Core, %w:\n%s", err, currentVersion)
7876
} else if newRelease != nil {
7977
cmdFactory := factory.New(build.Version)
8078
stderr := cmdFactory.IOStreams.ErrOut
8179
fmt.Fprintf(stderr, "\n%s %s → %s\n",
82-
ansi.Color(fmt.Sprintf("A new release of Instill %s is available:", proj), "yellow"),
80+
ansi.Color("A new release of Instill Core is available:", "yellow"),
8381
ansi.Color(currentVersion, "cyan"),
8482
ansi.Color(newRelease.Version, "cyan"))
8583
fmt.Fprintf(stderr, "%s\n\n",

pkg/cmd/local/start.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,28 +64,26 @@ func runStart(opts *StartOptions) error {
6464
return nil
6565
}
6666

67-
for _, proj := range projs {
68-
projDirPath := filepath.Join(LocalInstancePath, proj)
69-
if err := isProjectDeployed(opts.Exec, proj); err == nil {
70-
if opts.OS != nil {
71-
err = opts.OS.Chdir(projDirPath)
72-
} else {
73-
err = os.Chdir(projDirPath)
74-
}
75-
if err != nil {
76-
return fmt.Errorf("ERROR: cannot open the directory: %w", err)
77-
}
78-
p(opts.IO, fmt.Sprintf("Starting %s...", proj))
79-
out, err := execCmd(opts.Exec, "make", "start")
80-
if err != nil {
81-
return fmt.Errorf("ERROR: when starting, %w", err)
82-
}
83-
if err != nil {
84-
return fmt.Errorf("ERROR: %s when starting, %w\n%s", proj, err, out)
85-
}
67+
projDirPath := filepath.Join(LocalInstancePath, "core")
68+
if err := isProjectDeployed(opts.Exec, "core"); err == nil {
69+
if opts.OS != nil {
70+
err = opts.OS.Chdir(projDirPath)
8671
} else {
87-
return fmt.Errorf("ERROR: %w", err)
72+
err = os.Chdir(projDirPath)
73+
}
74+
if err != nil {
75+
return fmt.Errorf("ERROR: cannot open the directory: %w", err)
76+
}
77+
p(opts.IO, "Starting Instill Core...")
78+
out, err := execCmd(opts.Exec, "make", "start")
79+
if err != nil {
80+
return fmt.Errorf("ERROR: when starting, %w", err)
81+
}
82+
if err != nil {
83+
return fmt.Errorf("ERROR: when starting Instill Core, %w\n%s", err, out)
8884
}
85+
} else {
86+
return fmt.Errorf("ERROR: %w", err)
8987
}
9088

9189
p(opts.IO, "Instill Core started")

0 commit comments

Comments
 (0)