|
| 1 | +package create |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/devstream-io/devstream/internal/pkg/configmanager" |
| 8 | + "github.com/devstream-io/devstream/internal/pkg/create/param" |
| 9 | + "github.com/devstream-io/devstream/internal/pkg/plugin/argocdapp" |
| 10 | + general "github.com/devstream-io/devstream/internal/pkg/plugin/githubactions" |
| 11 | + "github.com/devstream-io/devstream/internal/pkg/plugin/reposcaffolding" |
| 12 | + "github.com/devstream-io/devstream/pkg/util/cli" |
| 13 | +) |
| 14 | + |
| 15 | +func Create() error { |
| 16 | + params, err := param.GetParams() |
| 17 | + if err != nil { |
| 18 | + return err |
| 19 | + } |
| 20 | + fmt.Printf("Start create app %s's stream...\n", params.GitHubRepo) |
| 21 | + return create(params) |
| 22 | +} |
| 23 | + |
| 24 | +// create will do following three things: |
| 25 | +// 1. create repo by repoScaffolding |
| 26 | +// 2. config GitHub actions for this repo |
| 27 | +// 3. create Argo CD application for this repo |
| 28 | +func create(params *param.Param) error { |
| 29 | + if err := createRepo(params); err != nil { |
| 30 | + return err |
| 31 | + } |
| 32 | + |
| 33 | + if err := createApp(params); err != nil { |
| 34 | + return err |
| 35 | + } |
| 36 | + |
| 37 | + // finalMessage is used to help user to vist this app |
| 38 | + finalMessage := `You can now connect to you app with: |
| 39 | +
|
| 40 | + kubectl port-forward service/%s 8080:8080 -n default |
| 41 | +
|
| 42 | +Then you can visit this app by http://127.0.0.1:8080 in your browser. |
| 43 | +
|
| 44 | +Happy Hacking! 😊 |
| 45 | +` |
| 46 | + fmt.Printf(finalMessage, params.GitHubRepo) |
| 47 | + return nil |
| 48 | +} |
| 49 | + |
| 50 | +func createRepo(params *param.Param) error { |
| 51 | + repoOptions := configmanager.RawOptions{ |
| 52 | + "owner": params.GithubUsername, |
| 53 | + "name": params.GitHubRepo, |
| 54 | + "scmType": "github", |
| 55 | + "token": params.GithubToken, |
| 56 | + } |
| 57 | + |
| 58 | + // 1.create repo |
| 59 | + status := cli.StatusForPlugin() |
| 60 | + repoScaffoldingOptions := configmanager.RawOptions{ |
| 61 | + "destinationRepo": repoOptions, |
| 62 | + "sourceRepo": configmanager.RawOptions{ |
| 63 | + "url": params.RepoScaffoldingURL, |
| 64 | + }, |
| 65 | + } |
| 66 | + status.Start("Creating repo from scaffolding 🖼") |
| 67 | + _, err := reposcaffolding.Create(repoScaffoldingOptions) |
| 68 | + status.End(err) |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + |
| 73 | + // 2.config ci |
| 74 | + ciOptions := configmanager.RawOptions{ |
| 75 | + "scm": repoOptions, |
| 76 | + "pipeline": configmanager.RawOptions{ |
| 77 | + "language": configmanager.RawOptions{ |
| 78 | + "name": params.Language, |
| 79 | + "framework": params.Framework, |
| 80 | + }, |
| 81 | + "imageRepo": configmanager.RawOptions{ |
| 82 | + "user": params.DockerhubUsername, |
| 83 | + "password": params.DockerhubToken, |
| 84 | + }, |
| 85 | + }, |
| 86 | + } |
| 87 | + status.Start("Writing github action configuration ✍️ ") |
| 88 | + _, err = general.Create(ciOptions) |
| 89 | + status.End(err) |
| 90 | + status.Start("Waiting for github action finished 🐎") |
| 91 | + |
| 92 | + // 3.wait repo ci finished |
| 93 | + waitCIFinished() |
| 94 | + status.End(err) |
| 95 | + return err |
| 96 | +} |
| 97 | + |
| 98 | +func createApp(params *param.Param) error { |
| 99 | + status := cli.StatusForPlugin() |
| 100 | + argocdAppOption := configmanager.RawOptions{ |
| 101 | + "app": configmanager.RawOptions{ |
| 102 | + "name": params.GitHubRepo, |
| 103 | + "namespace": "argocd", |
| 104 | + }, |
| 105 | + "destination": configmanager.RawOptions{ |
| 106 | + "server": "https://kubernetes.default.svc", |
| 107 | + "namespace": "default", |
| 108 | + }, |
| 109 | + "source": configmanager.RawOptions{ |
| 110 | + "valuefile": "values.yaml", |
| 111 | + "path": fmt.Sprintf("helm/%s", params.GitHubRepo), |
| 112 | + "repoURL": fmt.Sprintf("https://github.com/%s/%s", params.GithubUsername, params.GitHubRepo), |
| 113 | + "repoBranch": "main", |
| 114 | + "token": params.GithubToken, |
| 115 | + }, |
| 116 | + "imageRepo": configmanager.RawOptions{ |
| 117 | + "user": params.DockerhubUsername, |
| 118 | + }, |
| 119 | + } |
| 120 | + status.Start("Creating argocd app 🕹️") |
| 121 | + _, err := argocdapp.Create(argocdAppOption) |
| 122 | + status.End(err) |
| 123 | + status.Start("Waiting for app to running 🚀") |
| 124 | + // wait argocd app status to running |
| 125 | + waitAppUp() |
| 126 | + status.End(nil) |
| 127 | + return err |
| 128 | +} |
| 129 | + |
| 130 | +// TODO(steinliber): add logic to wait for ci finished |
| 131 | +func waitCIFinished() { |
| 132 | + time.Sleep(70 * time.Second) // current github actions takes 62 seconds for finished |
| 133 | +} |
| 134 | + |
| 135 | +// TODO(steinliber): add logic to wait for pod start running |
| 136 | +func waitAppUp() { |
| 137 | + time.Sleep(30 * time.Second) |
| 138 | +} |
0 commit comments