deploy.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. package cmd
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/fatih/color"
  6. "github.com/porter-dev/porter/cli/cmd/api"
  7. "github.com/porter-dev/porter/cli/cmd/deploy"
  8. "github.com/spf13/cobra"
  9. )
  10. // updateCmd represents the "porter update" base command when called
  11. // without any subcommands
  12. var updateCmd = &cobra.Command{
  13. Use: "update",
  14. Short: "Builds and updates a specified application given by the --app flag.",
  15. Long: fmt.Sprintf(`
  16. %s
  17. Builds and updates a specified application given by the --app flag. For example:
  18. %s
  19. This command will automatically build from a local path. The path can be configured via the
  20. --path flag. You can also overwrite the tag using the --tag flag. For example, to build from the
  21. local directory ~/path-to-dir with the tag "testing":
  22. %s
  23. If the application has a remote Git repository source configured, you can specify that the remote
  24. Git repository should be used to build the new image by specifying "--source github". Porter will use
  25. the latest commit from the remote repo and branch to update an application, and will use the latest
  26. commit as the image tag.
  27. %s
  28. To add new configuration or update existing configuration, you can pass a values.yaml file in via the
  29. --values flag. For example;
  30. %s
  31. If your application is set up to use a Dockerfile by default, you can use a buildpack via the flag
  32. "--method pack". Conversely, if your application is set up to use a buildpack by default, you can
  33. use a Dockerfile by passing the flag "--method docker". You can specify the relative path to a Dockerfile
  34. in your remote Git repository. For example, if a Dockerfile is found at ./docker/prod.Dockerfile, you can
  35. specify it as follows:
  36. %s
  37. `,
  38. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter update\":"),
  39. color.New(color.FgGreen, color.Bold).Sprintf("porter update --app example-app"),
  40. color.New(color.FgGreen, color.Bold).Sprintf("porter update --app example-app --path ~/path-to-dir --tag testing"),
  41. color.New(color.FgGreen, color.Bold).Sprintf("porter update --app remote-git-app --source github"),
  42. color.New(color.FgGreen, color.Bold).Sprintf("porter update --app example-app --values my-values.yaml"),
  43. color.New(color.FgGreen, color.Bold).Sprintf("porter update --app example-app --method docker --dockerfile ./docker/prod.Dockerfile"),
  44. ),
  45. Run: func(cmd *cobra.Command, args []string) {
  46. err := checkLoginAndRun(args, updateFull)
  47. if err != nil {
  48. os.Exit(1)
  49. }
  50. },
  51. }
  52. var updateGetEnvCmd = &cobra.Command{
  53. Use: "get-env",
  54. Short: "Gets environment variables for a deployment for a specified application given by the --app flag.",
  55. Long: fmt.Sprintf(`
  56. %s
  57. Gets environment variables for a deployment for a specified application given by the --app
  58. flag. By default, env variables are printed via stdout for use in downstream commands:
  59. %s
  60. Output can also be written to a file via the --file flag, which should specify the
  61. destination path for a .env file. For example:
  62. %s
  63. `,
  64. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter update get-env\":"),
  65. color.New(color.FgGreen, color.Bold).Sprintf("porter update get-env --app example-app | xargs"),
  66. color.New(color.FgGreen, color.Bold).Sprintf("porter update get-env --app example-app --file .env"),
  67. ),
  68. Run: func(cmd *cobra.Command, args []string) {
  69. err := checkLoginAndRun(args, updateGetEnv)
  70. if err != nil {
  71. os.Exit(1)
  72. }
  73. },
  74. }
  75. var updateBuildCmd = &cobra.Command{
  76. Use: "build",
  77. Short: "Builds a new version of the application specified by the --app flag.",
  78. Long: fmt.Sprintf(`
  79. %s
  80. Builds a new version of the application specified by the --app flag. Depending on the
  81. configured settings, this command may work automatically or will require a specified
  82. --method flag.
  83. If you have configured the Dockerfile path and/or a build context for this application,
  84. this command will by default use those settings, so you just need to specify the --app
  85. flag:
  86. %s
  87. If you have not linked the build-time requirements for this application, the command will
  88. use a local build. By default, the cloud-native buildpacks builder will automatically be run
  89. from the current directory. If you would like to change the build method, you can do so by
  90. using the --method flag, for example:
  91. %s
  92. When using "--method docker", you can specify the path to the Dockerfile using the
  93. --dockerfile flag. This will also override the Dockerfile path that you may have linked
  94. for the application:
  95. %s
  96. `,
  97. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter update build\":"),
  98. color.New(color.FgGreen, color.Bold).Sprintf("porter update build --app example-app"),
  99. color.New(color.FgGreen, color.Bold).Sprintf("porter update build --app example-app --method docker"),
  100. color.New(color.FgGreen, color.Bold).Sprintf("porter update build --app example-app --method docker --dockerfile ./prod.Dockerfile"),
  101. ),
  102. Run: func(cmd *cobra.Command, args []string) {
  103. err := checkLoginAndRun(args, updateBuild)
  104. if err != nil {
  105. os.Exit(1)
  106. }
  107. },
  108. }
  109. var updatePushCmd = &cobra.Command{
  110. Use: "push",
  111. Short: "Pushes a new image for an application specified by the --app flag.",
  112. Long: fmt.Sprintf(`
  113. %s
  114. Pushes a new image for an application specified by the --app flag. This command uses
  115. the image repository saved in the application config by default. For example, if an
  116. application "nginx" was created from the image repo "gcr.io/snowflake-123456/nginx",
  117. the following command would push the image "gcr.io/snowflake-123456/nginx:new-tag":
  118. %s
  119. This command will not use your pre-saved authentication set up via "docker login," so if you
  120. are using an image registry that was created outside of Porter, make sure that you have
  121. linked it via "porter connect".
  122. `,
  123. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter update push\":"),
  124. color.New(color.FgGreen, color.Bold).Sprintf("porter update push --app nginx --tag new-tag"),
  125. ),
  126. Run: func(cmd *cobra.Command, args []string) {
  127. err := checkLoginAndRun(args, updatePush)
  128. if err != nil {
  129. os.Exit(1)
  130. }
  131. },
  132. }
  133. var updateConfigCmd = &cobra.Command{
  134. Use: "config",
  135. Short: "Updates the configuration for an application specified by the --app flag.",
  136. Long: fmt.Sprintf(`
  137. %s
  138. Updates the configuration for an application specified by the --app flag, using the configuration
  139. given by the --values flag. This will trigger a new deployment for the application with
  140. new configuration set. Note that this will merge your existing configuration with configuration
  141. specified in the --values file. For example:
  142. %s
  143. You can update the configuration with only a new tag with the --tag flag, which will only update
  144. the image that the application uses if no --values file is specified:
  145. %s
  146. `,
  147. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter update config\":"),
  148. color.New(color.FgGreen, color.Bold).Sprintf("porter update config --app example-app --values my-values.yaml"),
  149. color.New(color.FgGreen, color.Bold).Sprintf("porter update config --app example-app --tag custom-tag"),
  150. ),
  151. Run: func(cmd *cobra.Command, args []string) {
  152. err := checkLoginAndRun(args, updateUpgrade)
  153. if err != nil {
  154. os.Exit(1)
  155. }
  156. },
  157. }
  158. var app string
  159. var getEnvFileDest string
  160. var localPath string
  161. var tag string
  162. var dockerfile string
  163. var method string
  164. var stream bool
  165. func init() {
  166. rootCmd.AddCommand(updateCmd)
  167. updateCmd.PersistentFlags().StringVar(
  168. &app,
  169. "app",
  170. "",
  171. "Application in the Porter dashboard",
  172. )
  173. updateCmd.MarkPersistentFlagRequired("app")
  174. updateCmd.PersistentFlags().StringVar(
  175. &namespace,
  176. "namespace",
  177. "default",
  178. "Namespace of the application",
  179. )
  180. updateCmd.PersistentFlags().StringVar(
  181. &source,
  182. "source",
  183. "local",
  184. "the type of source (\"local\" or \"github\")",
  185. )
  186. updateCmd.PersistentFlags().StringVarP(
  187. &localPath,
  188. "path",
  189. "p",
  190. ".",
  191. "If local build, the path to the build directory. If remote build, the relative path from the repository root to the build directory.",
  192. )
  193. updateCmd.PersistentFlags().StringVarP(
  194. &tag,
  195. "tag",
  196. "t",
  197. "",
  198. "the specified tag to use, if not \"latest\"",
  199. )
  200. updateCmd.PersistentFlags().StringVarP(
  201. &values,
  202. "values",
  203. "v",
  204. "",
  205. "Filepath to a values.yaml file",
  206. )
  207. updateCmd.PersistentFlags().StringVar(
  208. &dockerfile,
  209. "dockerfile",
  210. "",
  211. "the path to the dockerfile",
  212. )
  213. updateCmd.PersistentFlags().StringVar(
  214. &method,
  215. "method",
  216. "",
  217. "the build method to use (\"docker\" or \"pack\")",
  218. )
  219. updateCmd.PersistentFlags().BoolVar(
  220. &stream,
  221. "stream",
  222. false,
  223. "stream update logs to porter dashboard",
  224. )
  225. updateCmd.AddCommand(updateGetEnvCmd)
  226. updateGetEnvCmd.PersistentFlags().StringVar(
  227. &getEnvFileDest,
  228. "file",
  229. "",
  230. "file destination for .env files",
  231. )
  232. updateCmd.AddCommand(updateBuildCmd)
  233. updateCmd.AddCommand(updatePushCmd)
  234. updateCmd.AddCommand(updateConfigCmd)
  235. }
  236. func updateFull(resp *api.AuthCheckResponse, client *api.Client, args []string) error {
  237. color.New(color.FgGreen).Println("Deploying app:", app)
  238. updateAgent, err := updateGetAgent(client)
  239. if err != nil {
  240. return err
  241. }
  242. err = updateBuildWithAgent(updateAgent)
  243. if err != nil {
  244. return err
  245. }
  246. err = updatePushWithAgent(updateAgent)
  247. if err != nil {
  248. return err
  249. }
  250. err = updateUpgradeWithAgent(updateAgent)
  251. if err != nil {
  252. return err
  253. }
  254. return nil
  255. }
  256. func updateGetEnv(resp *api.AuthCheckResponse, client *api.Client, args []string) error {
  257. updateAgent, err := updateGetAgent(client)
  258. if err != nil {
  259. return err
  260. }
  261. buildEnv, err := updateAgent.GetBuildEnv()
  262. if err != nil {
  263. return err
  264. }
  265. // set the environment variables in the process
  266. err = updateAgent.SetBuildEnv(buildEnv)
  267. if err != nil {
  268. return err
  269. }
  270. // write the environment variables to either a file or stdout (stdout by default)
  271. return updateAgent.WriteBuildEnv(getEnvFileDest)
  272. }
  273. func updateBuild(resp *api.AuthCheckResponse, client *api.Client, args []string) error {
  274. updateAgent, err := updateGetAgent(client)
  275. if err != nil {
  276. return err
  277. }
  278. return updateBuildWithAgent(updateAgent)
  279. }
  280. func updatePush(resp *api.AuthCheckResponse, client *api.Client, args []string) error {
  281. updateAgent, err := updateGetAgent(client)
  282. if err != nil {
  283. return err
  284. }
  285. return updatePushWithAgent(updateAgent)
  286. }
  287. func updateUpgrade(resp *api.AuthCheckResponse, client *api.Client, args []string) error {
  288. updateAgent, err := updateGetAgent(client)
  289. if err != nil {
  290. return err
  291. }
  292. return updateUpgradeWithAgent(updateAgent)
  293. }
  294. // HELPER METHODS
  295. func updateGetAgent(client *api.Client) (*deploy.DeployAgent, error) {
  296. var buildMethod deploy.DeployBuildType
  297. if method != "" {
  298. buildMethod = deploy.DeployBuildType(method)
  299. }
  300. // initialize the update agent
  301. return deploy.NewDeployAgent(client, app, &deploy.DeployOpts{
  302. SharedOpts: &deploy.SharedOpts{
  303. ProjectID: config.Project,
  304. ClusterID: config.Cluster,
  305. Namespace: namespace,
  306. LocalPath: localPath,
  307. LocalDockerfile: dockerfile,
  308. OverrideTag: tag,
  309. Method: buildMethod,
  310. },
  311. Local: source != "github",
  312. })
  313. }
  314. func updateBuildWithAgent(updateAgent *deploy.DeployAgent) error {
  315. // build the deployment
  316. color.New(color.FgGreen).Println("Building docker image for", app)
  317. if stream {
  318. updateAgent.StreamEvent(api.Event{
  319. ID: "build",
  320. Name: "Build",
  321. Index: 100,
  322. Status: api.EventStatusInProgress,
  323. Info: "",
  324. })
  325. }
  326. buildEnv, err := updateAgent.GetBuildEnv()
  327. if err != nil {
  328. if stream {
  329. // another concern: is it safe to ignore the error here?
  330. updateAgent.StreamEvent(api.Event{
  331. ID: "build",
  332. Name: "Build",
  333. Index: 110,
  334. Status: api.EventStatusFailed,
  335. Info: err.Error(),
  336. })
  337. }
  338. return err
  339. }
  340. // set the environment variables in the process
  341. err = updateAgent.SetBuildEnv(buildEnv)
  342. if err != nil {
  343. if stream {
  344. updateAgent.StreamEvent(api.Event{
  345. ID: "build",
  346. Name: "Build",
  347. Index: 120,
  348. Status: api.EventStatusFailed,
  349. Info: err.Error(),
  350. })
  351. }
  352. return err
  353. }
  354. if err := updateAgent.Build(); err != nil {
  355. if stream {
  356. updateAgent.StreamEvent(api.Event{
  357. ID: "build",
  358. Name: "Build",
  359. Index: 130,
  360. Status: api.EventStatusFailed,
  361. Info: err.Error(),
  362. })
  363. }
  364. return err
  365. }
  366. if stream {
  367. updateAgent.StreamEvent(api.Event{
  368. ID: "build",
  369. Name: "Build",
  370. Index: 140,
  371. Status: api.EventStatusSuccess,
  372. Info: "",
  373. })
  374. }
  375. return nil
  376. }
  377. func updatePushWithAgent(updateAgent *deploy.DeployAgent) error {
  378. // push the deployment
  379. color.New(color.FgGreen).Println("Pushing new image for", app)
  380. if stream {
  381. updateAgent.StreamEvent(api.Event{
  382. ID: "push",
  383. Name: "Push",
  384. Index: 200,
  385. Status: api.EventStatusInProgress,
  386. Info: "",
  387. })
  388. }
  389. if err := updateAgent.Push(); err != nil {
  390. if stream {
  391. updateAgent.StreamEvent(api.Event{
  392. ID: "push",
  393. Name: "Push",
  394. Index: 210,
  395. Status: api.EventStatusFailed,
  396. Info: err.Error(),
  397. })
  398. }
  399. return err
  400. }
  401. if stream {
  402. updateAgent.StreamEvent(api.Event{
  403. ID: "push",
  404. Name: "Push",
  405. Index: 220,
  406. Status: api.EventStatusSuccess,
  407. Info: "",
  408. })
  409. }
  410. return nil
  411. }
  412. func updateUpgradeWithAgent(updateAgent *deploy.DeployAgent) error {
  413. // push the deployment
  414. color.New(color.FgGreen).Println("Calling webhook for", app)
  415. if stream {
  416. updateAgent.StreamEvent(api.Event{
  417. ID: "upgrade",
  418. Name: "Upgrade",
  419. Index: 300,
  420. Status: api.EventStatusInProgress,
  421. Info: "",
  422. })
  423. }
  424. // read the values if necessary
  425. valuesObj, err := readValuesFile()
  426. if err != nil {
  427. if stream {
  428. updateAgent.StreamEvent(api.Event{
  429. ID: "upgrade",
  430. Name: "Upgrade",
  431. Index: 310,
  432. Status: api.EventStatusFailed,
  433. Info: err.Error(),
  434. })
  435. }
  436. return err
  437. }
  438. err = updateAgent.UpdateImageAndValues(valuesObj)
  439. if err != nil {
  440. if stream {
  441. updateAgent.StreamEvent(api.Event{
  442. ID: "upgrade",
  443. Name: "Upgrade",
  444. Index: 320,
  445. Status: api.EventStatusFailed,
  446. Info: err.Error(),
  447. })
  448. }
  449. return err
  450. }
  451. if stream {
  452. updateAgent.StreamEvent(api.Event{
  453. ID: "upgrade",
  454. Name: "Upgrade",
  455. Index: 330,
  456. Status: api.EventStatusSuccess,
  457. Info: err.Error(),
  458. })
  459. }
  460. color.New(color.FgGreen).Println("Successfully updated", app)
  461. return nil
  462. }