deploy.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. package cmd
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "io/ioutil"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "github.com/fatih/color"
  11. "github.com/porter-dev/porter/cli/cmd/api"
  12. "github.com/porter-dev/porter/cli/cmd/docker"
  13. "github.com/porter-dev/porter/cli/cmd/github"
  14. "github.com/spf13/cobra"
  15. )
  16. var app = ""
  17. // deployCmd represents the "porter deploy" base command when called
  18. // without any subcommands
  19. var deployCmd = &cobra.Command{
  20. Use: "deploy",
  21. Short: "Builds and deploys a specified application given by the --app flag.",
  22. Run: func(cmd *cobra.Command, args []string) {
  23. err := checkLoginAndRun(args, deploy)
  24. if err != nil {
  25. os.Exit(1)
  26. }
  27. },
  28. }
  29. var deployInitCmd = &cobra.Command{
  30. Use: "init",
  31. Short: "Initializes a deployment for a specified application given by the --app flag.",
  32. Run: func(cmd *cobra.Command, args []string) {
  33. err := checkLoginAndRun(args, deployInit)
  34. if err != nil {
  35. os.Exit(1)
  36. }
  37. },
  38. }
  39. var getEnvFileDest = ""
  40. var deployGetEnvCmd = &cobra.Command{
  41. Use: "get-env",
  42. Short: "Gets environment variables for a deployment for a specified application given by the --app flag.",
  43. Long: fmt.Sprintf(`Gets environment variables for a deployment for a specified application given by the --app flag.
  44. By default, env variables are printed via stdout for use in downstream commands, for example:
  45. %s
  46. Output can also be written to a dotenv file via the --file flag, which should specify the destination
  47. path for a .env file. For example:
  48. %s
  49. `,
  50. color.New(color.FgGreen).Sprintf("porter deploy get-env --app <app> | xargs"),
  51. color.New(color.FgGreen).Sprintf("porter deploy get-env --app <app> --file .env"),
  52. ),
  53. Run: func(cmd *cobra.Command, args []string) {
  54. err := checkLoginAndRun(args, deployGetEnv)
  55. if err != nil {
  56. os.Exit(1)
  57. }
  58. },
  59. }
  60. var deployBuildCmd = &cobra.Command{
  61. Use: "build",
  62. Short: "TBD",
  63. Run: func(cmd *cobra.Command, args []string) {
  64. err := checkLoginAndRun(args, deployBuild)
  65. if err != nil {
  66. os.Exit(1)
  67. }
  68. },
  69. }
  70. func init() {
  71. rootCmd.AddCommand(deployCmd)
  72. deployCmd.PersistentFlags().StringVar(
  73. &app,
  74. "app",
  75. "",
  76. "Application in the Porter dashboard",
  77. )
  78. deployCmd.AddCommand(deployInitCmd)
  79. deployCmd.AddCommand(deployGetEnvCmd)
  80. deployGetEnvCmd.PersistentFlags().StringVar(
  81. &getEnvFileDest,
  82. "file",
  83. "",
  84. "file destination for .env files",
  85. )
  86. deployCmd.AddCommand(deployBuildCmd)
  87. }
  88. func deploy(resp *api.AuthCheckResponse, client *api.Client, args []string) error {
  89. color.New(color.FgGreen).Println("Deploying app:", app)
  90. return deployInit(resp, client, args)
  91. }
  92. var release *api.GetReleaseResponse = nil
  93. // deployInit first reads the release given by the --app or the --job flag. It then
  94. // configures docker with the registries linked to the project.
  95. func deployInit(resp *api.AuthCheckResponse, client *api.Client, args []string) error {
  96. pID := config.Project
  97. cID := config.Cluster
  98. var err error
  99. release, err = client.GetRelease(context.TODO(), pID, cID, namespace, app)
  100. if err != nil {
  101. return err
  102. }
  103. return dockerConfig(resp, client, args)
  104. }
  105. // deployGetEnv retrieves the env from a release and outputs it to either a file
  106. // or stdout depending on getEnvFileDest
  107. func deployGetEnv(resp *api.AuthCheckResponse, client *api.Client, args []string) error {
  108. if release == nil {
  109. err := deployInit(resp, client, args)
  110. if err != nil {
  111. return err
  112. }
  113. }
  114. prefix, err := deploySetEnv(client)
  115. if err != nil {
  116. return err
  117. }
  118. // join lines together
  119. lines := make([]string, 0)
  120. // use os.Environ to get output already formatted as KEY=value
  121. for _, line := range os.Environ() {
  122. // filter for PORTER_<RELEASE> and strip prefix
  123. if strings.Contains(line, prefix+"_") {
  124. lines = append(lines, strings.Split(line, prefix+"_")[1])
  125. }
  126. }
  127. output := strings.Join(lines, "\n")
  128. // case on output type
  129. if getEnvFileDest != "" {
  130. ioutil.WriteFile(getEnvFileDest, []byte(output), 0700)
  131. } else {
  132. fmt.Println(output)
  133. }
  134. return nil
  135. }
  136. func deploySetEnv(client *api.Client) (prefix string, err error) {
  137. prefix = fmt.Sprintf("PORTER_%s", strings.Replace(
  138. strings.ToUpper(app), "-", "_", -1,
  139. ))
  140. envVars, err := getEnvFromRelease()
  141. if err != nil {
  142. return prefix, err
  143. }
  144. // iterate through env and set the environment variables for the process
  145. // these are prefixed with PORTER_<RELEASE> to avoid collisions
  146. for key, val := range envVars {
  147. prefixedKey := fmt.Sprintf("%s_%s", prefix, key)
  148. err := os.Setenv(prefixedKey, val)
  149. if err != nil {
  150. return prefix, err
  151. }
  152. }
  153. return prefix, nil
  154. }
  155. func getEnvFromRelease() (map[string]string, error) {
  156. envConfig, err := getNestedMap(release.Config, "container", "env", "normal")
  157. // if the field is not found, set envConfig to an empty map; this release has no env set
  158. if e := (&NestedMapFieldNotFoundError{}); errors.As(err, &e) {
  159. envConfig = make(map[string]interface{})
  160. } else if err != nil {
  161. return nil, fmt.Errorf("could not get environment variables from release: %s", err.Error())
  162. }
  163. mapEnvConfig := make(map[string]string)
  164. for key, val := range envConfig {
  165. valStr, ok := val.(string)
  166. if !ok {
  167. return nil, fmt.Errorf("could not cast environment variables to object")
  168. }
  169. mapEnvConfig[key] = valStr
  170. }
  171. return mapEnvConfig, nil
  172. }
  173. type NestedMapFieldNotFoundError struct {
  174. Field string
  175. }
  176. func (e *NestedMapFieldNotFoundError) Error() string {
  177. return fmt.Sprintf("could not find field %s in configuration", e.Field)
  178. }
  179. func getNestedMap(obj map[string]interface{}, fields ...string) (map[string]interface{}, error) {
  180. var res map[string]interface{}
  181. curr := obj
  182. for _, field := range fields {
  183. objField, ok := curr[field]
  184. if !ok {
  185. return nil, &NestedMapFieldNotFoundError{field}
  186. }
  187. res, ok = objField.(map[string]interface{})
  188. if !ok {
  189. return nil, fmt.Errorf("%s is not a nested object", field)
  190. }
  191. curr = res
  192. }
  193. return res, nil
  194. }
  195. func deployBuild(resp *api.AuthCheckResponse, client *api.Client, args []string) error {
  196. if release == nil {
  197. err := deployInit(resp, client, args)
  198. if err != nil {
  199. return err
  200. }
  201. }
  202. zipResp, err := client.GetRepoZIPDownloadURL(
  203. context.Background(),
  204. config.Project,
  205. release.GitActionConfig,
  206. )
  207. if err != nil {
  208. return err
  209. }
  210. // download the repository from remote source into a temp directory
  211. dst, err := downloadRepoToDir(zipResp.URLString)
  212. if err != nil {
  213. return err
  214. }
  215. agent, err := docker.NewAgentFromEnv()
  216. if err != nil {
  217. return err
  218. }
  219. err = pullCurrentReleaseImage(agent)
  220. if err != nil {
  221. return err
  222. }
  223. // case on Dockerfile path
  224. if release.GitActionConfig.DockerfilePath != "" {
  225. return agent.BuildLocal(
  226. release.GitActionConfig.DockerfilePath,
  227. release.GitActionConfig.ImageRepoURI,
  228. dst,
  229. )
  230. }
  231. return nil
  232. }
  233. func pullCurrentReleaseImage(agent *docker.Agent) error {
  234. // pull the currently deployed image to use cache, if possible
  235. imageConfig, err := getNestedMap(release.Config, "image")
  236. if err != nil {
  237. return fmt.Errorf("could not get image config from release: %s", err.Error())
  238. }
  239. tagInterface, ok := imageConfig["tag"]
  240. if !ok {
  241. return fmt.Errorf("tag field does not exist for image")
  242. }
  243. tagStr, ok := tagInterface.(string)
  244. if !ok {
  245. return fmt.Errorf("could not cast image.tag field to string")
  246. }
  247. return agent.PullImage(fmt.Sprintf("%s:%s", release.GitActionConfig.ImageRepoURI, tagStr))
  248. }
  249. func downloadRepoToDir(downloadURL string) (string, error) {
  250. dstDir := filepath.Join(home, ".porter")
  251. downloader := &github.ZIPDownloader{
  252. ZipFolderDest: dstDir,
  253. AssetFolderDest: dstDir,
  254. ZipName: fmt.Sprintf("%s.zip", strings.Replace(release.GitActionConfig.GitRepo, "/", "-", 1)),
  255. RemoveAfterDownload: true,
  256. }
  257. err := downloader.DownloadToFile(downloadURL)
  258. if err != nil {
  259. return "", fmt.Errorf("Error downloading to file: %s", err.Error())
  260. }
  261. err = downloader.UnzipToDir()
  262. if err != nil {
  263. return "", fmt.Errorf("Error unzipping to directory: %s", err.Error())
  264. }
  265. var res string
  266. dstFiles, err := ioutil.ReadDir(dstDir)
  267. for _, info := range dstFiles {
  268. if info.Mode().IsDir() && strings.Contains(info.Name(), strings.Replace(release.GitActionConfig.GitRepo, "/", "-", 1)) {
  269. res = filepath.Join(dstDir, info.Name())
  270. }
  271. }
  272. if res == "" {
  273. return "", fmt.Errorf("unzipped file not found on host")
  274. }
  275. return res, nil
  276. }