apply.go 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. package cmd
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/url"
  8. "os"
  9. "path/filepath"
  10. "strconv"
  11. "strings"
  12. "github.com/cli/cli/git"
  13. "github.com/fatih/color"
  14. "github.com/mitchellh/mapstructure"
  15. api "github.com/porter-dev/porter/api/client"
  16. "github.com/porter-dev/porter/api/types"
  17. "github.com/porter-dev/porter/cli/cmd/config"
  18. "github.com/porter-dev/porter/cli/cmd/deploy"
  19. "github.com/porter-dev/porter/cli/cmd/deploy/wait"
  20. "github.com/porter-dev/porter/cli/cmd/preview"
  21. "github.com/porter-dev/porter/internal/templater/utils"
  22. "github.com/porter-dev/switchboard/pkg/drivers"
  23. "github.com/porter-dev/switchboard/pkg/models"
  24. "github.com/porter-dev/switchboard/pkg/parser"
  25. switchboardTypes "github.com/porter-dev/switchboard/pkg/types"
  26. switchboardWorker "github.com/porter-dev/switchboard/pkg/worker"
  27. "github.com/rs/zerolog"
  28. "github.com/spf13/cobra"
  29. )
  30. // applyCmd represents the "porter apply" base command when called
  31. // with a porter.yaml file as an argument
  32. var applyCmd = &cobra.Command{
  33. Use: "apply",
  34. Short: "Applies a configuration to an application",
  35. Long: fmt.Sprintf(`
  36. %s
  37. Applies a configuration to an application by either creating a new one or updating an existing
  38. one. For example:
  39. %s
  40. This command will apply the configuration contained in porter.yaml to the requested project and
  41. cluster either provided inside the porter.yaml file or through environment variables. Note that
  42. environment variables will always take precendence over values specified in the porter.yaml file.
  43. By default, this command expects to be run from a local git repository.
  44. The following are the environment variables that can be used to set certain values while
  45. applying a configuration:
  46. PORTER_CLUSTER Cluster ID that contains the project
  47. PORTER_PROJECT Project ID that contains the application
  48. PORTER_NAMESPACE The Kubernetes namespace that the application belongs to
  49. PORTER_SOURCE_NAME Name of the source Helm chart
  50. PORTER_SOURCE_REPO The URL of the Helm charts registry
  51. PORTER_SOURCE_VERSION The version of the Helm chart to use
  52. PORTER_TAG The Docker image tag to use (like the git commit hash)
  53. `,
  54. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter apply\":"),
  55. color.New(color.FgGreen, color.Bold).Sprintf("porter apply -f porter.yaml"),
  56. ),
  57. Run: func(cmd *cobra.Command, args []string) {
  58. err := checkLoginAndRun(args, apply)
  59. if err != nil {
  60. os.Exit(1)
  61. }
  62. },
  63. }
  64. var porterYAML string
  65. func init() {
  66. rootCmd.AddCommand(applyCmd)
  67. applyCmd.Flags().StringVarP(&porterYAML, "file", "f", "", "path to porter.yaml")
  68. applyCmd.MarkFlagRequired("file")
  69. }
  70. func apply(_ *types.GetAuthenticatedUserResponse, client *api.Client, _ []string) error {
  71. fileBytes, err := ioutil.ReadFile(porterYAML)
  72. if err != nil {
  73. return fmt.Errorf("error reading porter.yaml: %w", err)
  74. }
  75. resGroup, err := parser.ParseRawBytes(fileBytes)
  76. if err != nil {
  77. return fmt.Errorf("error parsing porter.yaml: %w", err)
  78. }
  79. basePath, err := os.Getwd()
  80. if err != nil {
  81. return fmt.Errorf("error getting working directory: %w", err)
  82. }
  83. worker := switchboardWorker.NewWorker()
  84. worker.RegisterDriver("deploy", NewDeployDriver)
  85. worker.RegisterDriver("build-image", preview.NewBuildDriver)
  86. worker.RegisterDriver("push-image", preview.NewPushDriver)
  87. worker.RegisterDriver("update-config", preview.NewUpdateConfigDriver)
  88. worker.RegisterDriver("random-string", preview.NewRandomStringDriver)
  89. worker.RegisterDriver("env-group", preview.NewEnvGroupDriver)
  90. worker.RegisterDriver("os-env", preview.NewOSEnvDriver)
  91. worker.SetDefaultDriver("deploy")
  92. if hasDeploymentHookEnvVars() {
  93. deplNamespace := os.Getenv("PORTER_NAMESPACE")
  94. if deplNamespace == "" {
  95. return fmt.Errorf("namespace must be set by PORTER_NAMESPACE")
  96. }
  97. deploymentHook, err := NewDeploymentHook(client, resGroup, deplNamespace)
  98. if err != nil {
  99. return fmt.Errorf("error creating deployment hook: %w", err)
  100. }
  101. worker.RegisterHook("deployment", deploymentHook)
  102. }
  103. cloneEnvGroupHook := NewCloneEnvGroupHook(client, resGroup)
  104. worker.RegisterHook("cloneenvgroup", cloneEnvGroupHook)
  105. return worker.Apply(resGroup, &switchboardTypes.ApplyOpts{
  106. BasePath: basePath,
  107. })
  108. }
  109. func hasDeploymentHookEnvVars() bool {
  110. if ghIDStr := os.Getenv("PORTER_GIT_INSTALLATION_ID"); ghIDStr == "" {
  111. return false
  112. }
  113. if prIDStr := os.Getenv("PORTER_PULL_REQUEST_ID"); prIDStr == "" {
  114. return false
  115. }
  116. if branchFrom := os.Getenv("PORTER_BRANCH_FROM"); branchFrom == "" {
  117. return false
  118. }
  119. if branchInto := os.Getenv("PORTER_BRANCH_INTO"); branchInto == "" {
  120. return false
  121. }
  122. if actionIDStr := os.Getenv("PORTER_ACTION_ID"); actionIDStr == "" {
  123. return false
  124. }
  125. if repoName := os.Getenv("PORTER_REPO_NAME"); repoName == "" {
  126. return false
  127. }
  128. if repoOwner := os.Getenv("PORTER_REPO_OWNER"); repoOwner == "" {
  129. return false
  130. }
  131. if prName := os.Getenv("PORTER_PR_NAME"); prName == "" {
  132. return false
  133. }
  134. return true
  135. }
  136. type ApplicationConfig struct {
  137. WaitForJob bool
  138. // If set to true, this does not run an update, it only creates the initial application and job,
  139. // skipping subsequent updates
  140. OnlyCreate bool
  141. Build struct {
  142. UseCache bool `mapstructure:"use_cache"`
  143. Method string
  144. Context string
  145. Dockerfile string
  146. Image string
  147. Builder string
  148. Buildpacks []string
  149. Env map[string]string
  150. }
  151. EnvGroups []types.EnvGroupMeta `mapstructure:"env_groups"`
  152. Values map[string]interface{}
  153. }
  154. type DeployDriver struct {
  155. source *preview.Source
  156. target *preview.Target
  157. output map[string]interface{}
  158. lookupTable *map[string]drivers.Driver
  159. logger *zerolog.Logger
  160. }
  161. func NewDeployDriver(resource *models.Resource, opts *drivers.SharedDriverOpts) (drivers.Driver, error) {
  162. driver := &DeployDriver{
  163. lookupTable: opts.DriverLookupTable,
  164. logger: opts.Logger,
  165. output: make(map[string]interface{}),
  166. }
  167. source, err := preview.GetSource(resource.Name, resource.Source)
  168. if err != nil {
  169. return nil, err
  170. }
  171. driver.source = source
  172. target, err := preview.GetTarget(resource.Name, resource.Target)
  173. if err != nil {
  174. return nil, err
  175. }
  176. driver.target = target
  177. return driver, nil
  178. }
  179. func (d *DeployDriver) ShouldApply(_ *models.Resource) bool {
  180. return true
  181. }
  182. func (d *DeployDriver) Apply(resource *models.Resource) (*models.Resource, error) {
  183. client := config.GetAPIClient()
  184. name := resource.Name
  185. if name == "" {
  186. return nil, fmt.Errorf("empty resource name")
  187. }
  188. _, err := client.GetRelease(
  189. context.Background(),
  190. d.target.Project,
  191. d.target.Cluster,
  192. d.target.Namespace,
  193. resource.Name,
  194. )
  195. shouldCreate := err != nil
  196. if err != nil {
  197. color.New(color.FgYellow).Printf("Could not read release %s/%s (%s): attempting creation\n", d.target.Namespace, resource.Name, err.Error())
  198. }
  199. if d.source.IsApplication {
  200. return d.applyApplication(resource, client, shouldCreate)
  201. }
  202. return d.applyAddon(resource, client, shouldCreate)
  203. }
  204. // Simple apply for addons
  205. func (d *DeployDriver) applyAddon(resource *models.Resource, client *api.Client, shouldCreate bool) (*models.Resource, error) {
  206. addonConfig, err := d.getAddonConfig(resource)
  207. if err != nil {
  208. return nil, fmt.Errorf("error getting addon config for resource %s: %w", resource.Name, err)
  209. }
  210. if shouldCreate {
  211. err := client.DeployAddon(
  212. context.Background(),
  213. d.target.Project,
  214. d.target.Cluster,
  215. d.target.Namespace,
  216. &types.CreateAddonRequest{
  217. CreateReleaseBaseRequest: &types.CreateReleaseBaseRequest{
  218. RepoURL: d.source.Repo,
  219. TemplateName: d.source.Name,
  220. TemplateVersion: d.source.Version,
  221. Values: addonConfig,
  222. Name: resource.Name,
  223. },
  224. },
  225. )
  226. if err != nil {
  227. return nil, fmt.Errorf("error creating addon from resource %s: %w", resource.Name, err)
  228. }
  229. } else {
  230. bytes, err := json.Marshal(addonConfig)
  231. if err != nil {
  232. return nil, fmt.Errorf("error marshalling addon config from resource %s: %w", resource.Name, err)
  233. }
  234. err = client.UpgradeRelease(
  235. context.Background(),
  236. d.target.Project,
  237. d.target.Cluster,
  238. d.target.Namespace,
  239. resource.Name,
  240. &types.UpgradeReleaseRequest{
  241. Values: string(bytes),
  242. },
  243. )
  244. if err != nil {
  245. return nil, fmt.Errorf("error updating addon from resource %s: %w", resource.Name, err)
  246. }
  247. }
  248. if err = d.assignOutput(resource, client); err != nil {
  249. return nil, err
  250. }
  251. return resource, nil
  252. }
  253. func (d *DeployDriver) applyApplication(resource *models.Resource, client *api.Client, shouldCreate bool) (*models.Resource, error) {
  254. if resource == nil {
  255. return nil, fmt.Errorf("nil resource")
  256. }
  257. appConfig, err := d.getApplicationConfig(resource)
  258. if err != nil {
  259. return nil, err
  260. }
  261. method := appConfig.Build.Method
  262. if method != "pack" && method != "docker" && method != "registry" {
  263. return nil, fmt.Errorf("for resource %s, config.build.method should either be \"docker\", \"pack\" or \"registry\"",
  264. resource.Name)
  265. }
  266. fullPath, err := filepath.Abs(appConfig.Build.Context)
  267. if err != nil {
  268. return nil, fmt.Errorf("for resource %s, error getting absolute path for config.build.context: %w", resource.Name,
  269. err)
  270. }
  271. tag := os.Getenv("PORTER_TAG")
  272. if tag == "" {
  273. color.New(color.FgYellow).Printf("for resource %s, since PORTER_TAG is not set, the Docker image tag will default to"+
  274. " the git repo SHA", resource.Name)
  275. commit, err := git.LastCommit()
  276. if err != nil {
  277. return nil, fmt.Errorf("for resource %s, error getting last git commit: %w", resource.Name, err)
  278. }
  279. tag = commit.Sha[:7]
  280. color.New(color.FgYellow).Printf("for resource %s, using tag %s\n", resource.Name, tag)
  281. }
  282. // if the method is registry and a tag is defined, we use the provided tag
  283. if appConfig.Build.Method == "registry" {
  284. imageSpl := strings.Split(appConfig.Build.Image, ":")
  285. if len(imageSpl) == 2 {
  286. tag = imageSpl[1]
  287. }
  288. if tag == "" {
  289. tag = "latest"
  290. }
  291. }
  292. sharedOpts := &deploy.SharedOpts{
  293. ProjectID: d.target.Project,
  294. ClusterID: d.target.Cluster,
  295. Namespace: d.target.Namespace,
  296. LocalPath: fullPath,
  297. LocalDockerfile: appConfig.Build.Dockerfile,
  298. OverrideTag: tag,
  299. Method: deploy.DeployBuildType(method),
  300. EnvGroups: appConfig.EnvGroups,
  301. UseCache: appConfig.Build.UseCache,
  302. }
  303. if appConfig.Build.UseCache {
  304. // set the docker config so that pack caching can use the repo credentials
  305. err := config.SetDockerConfig(client)
  306. if err != nil {
  307. return nil, err
  308. }
  309. }
  310. if shouldCreate {
  311. resource, err = d.createApplication(resource, client, sharedOpts, appConfig)
  312. if err != nil {
  313. return nil, fmt.Errorf("error creating app from resource %s: %w", resource.Name, err)
  314. }
  315. } else if !appConfig.OnlyCreate {
  316. resource, err = d.updateApplication(resource, client, sharedOpts, appConfig)
  317. if err != nil {
  318. return nil, fmt.Errorf("error updating application from resource %s: %w", resource.Name, err)
  319. }
  320. } else {
  321. color.New(color.FgYellow).Printf("Skipping creation for resource %s as onlyCreate is set to true\n", resource.Name)
  322. }
  323. if err = d.assignOutput(resource, client); err != nil {
  324. return nil, err
  325. }
  326. if d.source.Name == "job" && appConfig.WaitForJob && (shouldCreate || !appConfig.OnlyCreate) {
  327. color.New(color.FgYellow).Printf("Waiting for job '%s' to finish\n", resource.Name)
  328. err = wait.WaitForJob(client, &wait.WaitOpts{
  329. ProjectID: d.target.Project,
  330. ClusterID: d.target.Cluster,
  331. Namespace: d.target.Namespace,
  332. Name: resource.Name,
  333. })
  334. if err != nil && appConfig.OnlyCreate {
  335. deleteJobErr := client.DeleteRelease(
  336. context.Background(),
  337. d.target.Project,
  338. d.target.Cluster,
  339. d.target.Namespace,
  340. resource.Name,
  341. )
  342. if deleteJobErr != nil {
  343. return nil, fmt.Errorf("error deleting job %s with waitForJob and onlyCreate set to true: %w",
  344. resource.Name, deleteJobErr)
  345. }
  346. } else if err != nil {
  347. return nil, fmt.Errorf("error waiting for job %s: %w", resource.Name, err)
  348. }
  349. }
  350. return resource, err
  351. }
  352. func (d *DeployDriver) createApplication(resource *models.Resource, client *api.Client, sharedOpts *deploy.SharedOpts, appConf *ApplicationConfig) (*models.Resource, error) {
  353. // create new release
  354. color.New(color.FgGreen).Printf("Creating %s release: %s\n", d.source.Name, resource.Name)
  355. regList, err := client.ListRegistries(context.Background(), d.target.Project)
  356. if err != nil {
  357. return nil, fmt.Errorf("for resource %s, error listing registries: %w", resource.Name, err)
  358. }
  359. var registryURL string
  360. if len(*regList) == 0 {
  361. return nil, fmt.Errorf("no registry found")
  362. } else {
  363. registryURL = (*regList)[0].URL
  364. }
  365. color.New(color.FgBlue).Printf("for resource %s, using registry %s\n", resource.Name, registryURL)
  366. // attempt to get repo suffix from environment variables
  367. var repoSuffix string
  368. if repoName := os.Getenv("PORTER_REPO_NAME"); repoName != "" {
  369. if repoOwner := os.Getenv("PORTER_REPO_OWNER"); repoOwner != "" {
  370. repoSuffix = strings.ToLower(strings.ReplaceAll(fmt.Sprintf("%s-%s", repoOwner, repoName), "_", "-"))
  371. }
  372. }
  373. createAgent := &deploy.CreateAgent{
  374. Client: client,
  375. CreateOpts: &deploy.CreateOpts{
  376. SharedOpts: sharedOpts,
  377. Kind: d.source.Name,
  378. ReleaseName: resource.Name,
  379. RegistryURL: registryURL,
  380. RepoSuffix: repoSuffix,
  381. },
  382. }
  383. var buildConfig *types.BuildConfig
  384. if appConf.Build.Builder != "" {
  385. buildConfig = &types.BuildConfig{
  386. Builder: appConf.Build.Builder,
  387. Buildpacks: appConf.Build.Buildpacks,
  388. }
  389. }
  390. var subdomain string
  391. if appConf.Build.Method == "registry" {
  392. subdomain, err = createAgent.CreateFromRegistry(appConf.Build.Image, appConf.Values)
  393. } else {
  394. // if useCache is set, create the image repository first
  395. if appConf.Build.UseCache {
  396. regID, imageURL, err := createAgent.GetImageRepoURL(resource.Name, sharedOpts.Namespace)
  397. if err != nil {
  398. return nil, err
  399. }
  400. err = client.CreateRepository(
  401. context.Background(),
  402. sharedOpts.ProjectID,
  403. regID,
  404. &types.CreateRegistryRepositoryRequest{
  405. ImageRepoURI: imageURL,
  406. },
  407. )
  408. if err != nil {
  409. return nil, err
  410. }
  411. }
  412. subdomain, err = createAgent.CreateFromDocker(appConf.Values, sharedOpts.OverrideTag, buildConfig)
  413. }
  414. if err != nil {
  415. return nil, err
  416. }
  417. return resource, handleSubdomainCreate(subdomain, err)
  418. }
  419. func (d *DeployDriver) updateApplication(resource *models.Resource, client *api.Client, sharedOpts *deploy.SharedOpts, appConf *ApplicationConfig) (*models.Resource, error) {
  420. color.New(color.FgGreen).Println("Updating existing release:", resource.Name)
  421. if len(appConf.Build.Env) > 0 {
  422. sharedOpts.AdditionalEnv = appConf.Build.Env
  423. }
  424. updateAgent, err := deploy.NewDeployAgent(client, resource.Name, &deploy.DeployOpts{
  425. SharedOpts: sharedOpts,
  426. Local: appConf.Build.Method != "registry",
  427. })
  428. if err != nil {
  429. return nil, err
  430. }
  431. // if the build method is registry, we do not trigger a build
  432. if appConf.Build.Method != "registry" {
  433. buildEnv, err := updateAgent.GetBuildEnv(&deploy.GetBuildEnvOpts{
  434. UseNewConfig: true,
  435. NewConfig: appConf.Values,
  436. })
  437. if err != nil {
  438. return nil, err
  439. }
  440. err = updateAgent.SetBuildEnv(buildEnv)
  441. if err != nil {
  442. return nil, err
  443. }
  444. var buildConfig *types.BuildConfig
  445. if appConf.Build.Builder != "" {
  446. buildConfig = &types.BuildConfig{
  447. Builder: appConf.Build.Builder,
  448. Buildpacks: appConf.Build.Buildpacks,
  449. }
  450. }
  451. err = updateAgent.Build(buildConfig)
  452. if err != nil {
  453. return nil, err
  454. }
  455. if !appConf.Build.UseCache {
  456. err = updateAgent.Push()
  457. if err != nil {
  458. return nil, err
  459. }
  460. }
  461. }
  462. err = updateAgent.UpdateImageAndValues(appConf.Values)
  463. if err != nil {
  464. return nil, err
  465. }
  466. return resource, nil
  467. }
  468. func (d *DeployDriver) assignOutput(resource *models.Resource, client *api.Client) error {
  469. release, err := client.GetRelease(
  470. context.Background(),
  471. d.target.Project,
  472. d.target.Cluster,
  473. d.target.Namespace,
  474. resource.Name,
  475. )
  476. if err != nil {
  477. return err
  478. }
  479. d.output = utils.CoalesceValues(d.source.SourceValues, release.Config)
  480. return nil
  481. }
  482. func (d *DeployDriver) Output() (map[string]interface{}, error) {
  483. return d.output, nil
  484. }
  485. func (d *DeployDriver) getApplicationConfig(resource *models.Resource) (*ApplicationConfig, error) {
  486. populatedConf, err := drivers.ConstructConfig(&drivers.ConstructConfigOpts{
  487. RawConf: resource.Config,
  488. LookupTable: *d.lookupTable,
  489. Dependencies: resource.Dependencies,
  490. })
  491. if err != nil {
  492. return nil, err
  493. }
  494. appConf := &ApplicationConfig{}
  495. err = mapstructure.Decode(populatedConf, appConf)
  496. if err != nil {
  497. return nil, err
  498. }
  499. if _, ok := resource.Config["waitForJob"]; !ok && d.source.Name == "job" {
  500. // default to true and wait for the job to finish
  501. appConf.WaitForJob = true
  502. }
  503. return appConf, nil
  504. }
  505. func (d *DeployDriver) getAddonConfig(resource *models.Resource) (map[string]interface{}, error) {
  506. return drivers.ConstructConfig(&drivers.ConstructConfigOpts{
  507. RawConf: resource.Config,
  508. LookupTable: *d.lookupTable,
  509. Dependencies: resource.Dependencies,
  510. })
  511. }
  512. type DeploymentHook struct {
  513. client *api.Client
  514. resourceGroup *switchboardTypes.ResourceGroup
  515. gitInstallationID, projectID, clusterID, prID, actionID, envID uint
  516. branchFrom, branchInto, namespace, repoName, repoOwner, prName, commitSHA string
  517. }
  518. func NewDeploymentHook(client *api.Client, resourceGroup *switchboardTypes.ResourceGroup, namespace string) (*DeploymentHook, error) {
  519. res := &DeploymentHook{
  520. client: client,
  521. resourceGroup: resourceGroup,
  522. namespace: namespace,
  523. }
  524. ghIDStr := os.Getenv("PORTER_GIT_INSTALLATION_ID")
  525. ghID, err := strconv.Atoi(ghIDStr)
  526. if err != nil {
  527. return nil, err
  528. }
  529. res.gitInstallationID = uint(ghID)
  530. prIDStr := os.Getenv("PORTER_PULL_REQUEST_ID")
  531. prID, err := strconv.Atoi(prIDStr)
  532. if err != nil {
  533. return nil, err
  534. }
  535. res.prID = uint(prID)
  536. res.projectID = cliConf.Project
  537. if res.projectID == 0 {
  538. return nil, fmt.Errorf("project id must be set")
  539. }
  540. res.clusterID = cliConf.Cluster
  541. if res.clusterID == 0 {
  542. return nil, fmt.Errorf("cluster id must be set")
  543. }
  544. branchFrom := os.Getenv("PORTER_BRANCH_FROM")
  545. res.branchFrom = branchFrom
  546. branchInto := os.Getenv("PORTER_BRANCH_INTO")
  547. res.branchInto = branchInto
  548. actionIDStr := os.Getenv("PORTER_ACTION_ID")
  549. actionID, err := strconv.Atoi(actionIDStr)
  550. if err != nil {
  551. return nil, err
  552. }
  553. res.actionID = uint(actionID)
  554. repoName := os.Getenv("PORTER_REPO_NAME")
  555. res.repoName = repoName
  556. repoOwner := os.Getenv("PORTER_REPO_OWNER")
  557. res.repoOwner = repoOwner
  558. prName := os.Getenv("PORTER_PR_NAME")
  559. res.prName = prName
  560. commit, err := git.LastCommit()
  561. if err != nil {
  562. return nil, fmt.Errorf(err.Error())
  563. }
  564. res.commitSHA = commit.Sha[:7]
  565. return res, nil
  566. }
  567. func (t *DeploymentHook) PreApply() error {
  568. envList, err := t.client.ListEnvironments(
  569. context.Background(), t.projectID, t.clusterID,
  570. )
  571. if err != nil {
  572. return err
  573. }
  574. envs := *envList
  575. for _, env := range envs {
  576. if env.GitRepoOwner == t.repoOwner && env.GitRepoName == t.repoName && env.GitInstallationID == t.gitInstallationID {
  577. t.envID = env.ID
  578. break
  579. }
  580. }
  581. if t.envID == 0 {
  582. return fmt.Errorf("could not find environment for deployment")
  583. }
  584. // attempt to read the deployment -- if it doesn't exist, create it
  585. _, err = t.client.GetDeployment(
  586. context.Background(),
  587. t.projectID, t.clusterID, t.envID,
  588. &types.GetDeploymentRequest{
  589. Namespace: t.namespace,
  590. },
  591. )
  592. if err != nil && strings.Contains(err.Error(), "not found") {
  593. // in this case, create the deployment
  594. _, err = t.client.CreateDeployment(
  595. context.Background(),
  596. t.projectID, t.gitInstallationID, t.clusterID,
  597. t.repoOwner, t.repoName,
  598. &types.CreateDeploymentRequest{
  599. Namespace: t.namespace,
  600. PullRequestID: t.prID,
  601. CreateGHDeploymentRequest: &types.CreateGHDeploymentRequest{
  602. ActionID: t.actionID,
  603. },
  604. GitHubMetadata: &types.GitHubMetadata{
  605. PRName: t.prName,
  606. RepoName: t.repoName,
  607. RepoOwner: t.repoOwner,
  608. CommitSHA: t.commitSHA,
  609. PRBranchFrom: t.branchFrom,
  610. PRBranchInto: t.branchInto,
  611. },
  612. },
  613. )
  614. } else if err == nil {
  615. _, err = t.client.UpdateDeployment(
  616. context.Background(),
  617. t.projectID, t.gitInstallationID, t.clusterID,
  618. t.repoOwner, t.repoName,
  619. &types.UpdateDeploymentRequest{
  620. Namespace: t.namespace,
  621. CreateGHDeploymentRequest: &types.CreateGHDeploymentRequest{
  622. ActionID: t.actionID,
  623. },
  624. PRBranchFrom: t.branchFrom,
  625. CommitSHA: t.commitSHA,
  626. },
  627. )
  628. }
  629. return err
  630. }
  631. func (t *DeploymentHook) DataQueries() map[string]interface{} {
  632. res := make(map[string]interface{})
  633. // use the resource group to find all web applications that can have an exposed subdomain
  634. // that we can query for
  635. for _, resource := range t.resourceGroup.Resources {
  636. isWeb := false
  637. if sourceNameInter, exists := resource.Source["name"]; exists {
  638. if sourceName, ok := sourceNameInter.(string); ok {
  639. if sourceName == "web" {
  640. isWeb = true
  641. }
  642. }
  643. }
  644. if isWeb {
  645. // determine if we should query for porter_hosts or just hosts
  646. isCustomDomain := false
  647. ingressMap, err := deploy.GetNestedMap(resource.Config, "values", "ingress")
  648. if err == nil {
  649. enabledVal, enabledExists := ingressMap["enabled"]
  650. customDomVal, customDomExists := ingressMap["custom_domain"]
  651. if enabledExists && customDomExists {
  652. enabled, eOK := enabledVal.(bool)
  653. customDomain, cOK := customDomVal.(bool)
  654. if eOK && cOK && enabled {
  655. if customDomain {
  656. // return the first custom domain when one exists
  657. hostsArr, hostsExists := ingressMap["hosts"]
  658. if hostsExists {
  659. hostsArrVal, hostsArrOk := hostsArr.([]interface{})
  660. if hostsArrOk && len(hostsArrVal) > 0 {
  661. if _, ok := hostsArrVal[0].(string); ok {
  662. res[resource.Name] = fmt.Sprintf("{ .%s.ingress.hosts[0] }", resource.Name)
  663. isCustomDomain = true
  664. }
  665. }
  666. }
  667. }
  668. }
  669. }
  670. }
  671. if !isCustomDomain {
  672. res[resource.Name] = fmt.Sprintf("{ .%s.ingress.porter_hosts[0] }", resource.Name)
  673. }
  674. }
  675. }
  676. return res
  677. }
  678. func (t *DeploymentHook) PostApply(populatedData map[string]interface{}) error {
  679. subdomains := make([]string, 0)
  680. for _, data := range populatedData {
  681. domain, ok := data.(string)
  682. if !ok {
  683. continue
  684. }
  685. if _, err := url.Parse("https://" + domain); err == nil {
  686. subdomains = append(subdomains, "https://"+domain)
  687. }
  688. }
  689. req := &types.FinalizeDeploymentRequest{
  690. Namespace: t.namespace,
  691. Subdomain: strings.Join(subdomains, ", "),
  692. }
  693. for _, res := range t.resourceGroup.Resources {
  694. releaseType := getReleaseType(res)
  695. releaseName := getReleaseName(res)
  696. if releaseType != "" && releaseName != "" {
  697. req.SuccessfulResources = append(req.SuccessfulResources, &types.SuccessfullyDeployedResource{
  698. ReleaseName: releaseName,
  699. ReleaseType: releaseType,
  700. })
  701. }
  702. }
  703. // finalize the deployment
  704. _, err := t.client.FinalizeDeployment(
  705. context.Background(),
  706. t.projectID, t.gitInstallationID, t.clusterID,
  707. t.repoOwner, t.repoName, req,
  708. )
  709. return err
  710. }
  711. func (t *DeploymentHook) OnError(err error) {
  712. // if the deployment exists, throw an error for that deployment
  713. _, getDeplErr := t.client.GetDeployment(
  714. context.Background(),
  715. t.projectID, t.clusterID, t.envID,
  716. &types.GetDeploymentRequest{
  717. Namespace: t.namespace,
  718. },
  719. )
  720. if getDeplErr == nil {
  721. _, err = t.client.UpdateDeploymentStatus(
  722. context.Background(),
  723. t.projectID, t.gitInstallationID, t.clusterID,
  724. t.repoOwner, t.repoName,
  725. &types.UpdateDeploymentStatusRequest{
  726. Namespace: t.namespace,
  727. CreateGHDeploymentRequest: &types.CreateGHDeploymentRequest{
  728. ActionID: t.actionID,
  729. },
  730. PRBranchFrom: t.branchFrom,
  731. Status: string(types.DeploymentStatusFailed),
  732. },
  733. )
  734. }
  735. }
  736. func (t *DeploymentHook) OnConsolidatedErrors(allErrors map[string]error) {
  737. // if the deployment exists, throw an error for that deployment
  738. _, getDeplErr := t.client.GetDeployment(
  739. context.Background(),
  740. t.projectID, t.clusterID, t.envID,
  741. &types.GetDeploymentRequest{
  742. Namespace: t.namespace,
  743. },
  744. )
  745. if getDeplErr == nil {
  746. req := &types.FinalizeDeploymentWithErrorsRequest{
  747. Namespace: t.namespace,
  748. Errors: make(map[string]string),
  749. }
  750. for _, res := range t.resourceGroup.Resources {
  751. if _, ok := allErrors[res.Name]; !ok {
  752. req.SuccessfulResources = append(req.SuccessfulResources, &types.SuccessfullyDeployedResource{
  753. ReleaseName: getReleaseName(res),
  754. ReleaseType: getReleaseType(res),
  755. })
  756. }
  757. }
  758. for res, err := range allErrors {
  759. req.Errors[res] = err.Error()
  760. }
  761. // FIXME: handle the error
  762. t.client.FinalizeDeploymentWithErrors(
  763. context.Background(),
  764. t.projectID, t.gitInstallationID, t.clusterID,
  765. t.repoOwner, t.repoName,
  766. req,
  767. )
  768. }
  769. }
  770. type CloneEnvGroupHook struct {
  771. client *api.Client
  772. resGroup *switchboardTypes.ResourceGroup
  773. }
  774. func NewCloneEnvGroupHook(client *api.Client, resourceGroup *switchboardTypes.ResourceGroup) *CloneEnvGroupHook {
  775. return &CloneEnvGroupHook{
  776. client: client,
  777. resGroup: resourceGroup,
  778. }
  779. }
  780. func (t *CloneEnvGroupHook) PreApply() error {
  781. for _, res := range t.resGroup.Resources {
  782. if res.Driver == "env-group" {
  783. continue
  784. }
  785. appConf := &ApplicationConfig{}
  786. err := mapstructure.Decode(res.Config, &appConf)
  787. if err != nil {
  788. continue
  789. }
  790. if appConf != nil && len(appConf.EnvGroups) > 0 {
  791. target, err := preview.GetTarget(res.Name, res.Target)
  792. if err != nil {
  793. return err
  794. }
  795. for _, group := range appConf.EnvGroups {
  796. if group.Name == "" {
  797. return fmt.Errorf("env group name cannot be empty")
  798. }
  799. _, err := t.client.GetEnvGroup(
  800. context.Background(),
  801. target.Project,
  802. target.Cluster,
  803. target.Namespace,
  804. &types.GetEnvGroupRequest{
  805. Name: group.Name,
  806. Version: group.Version,
  807. },
  808. )
  809. if err != nil && err.Error() == "env group not found" {
  810. if group.Namespace == "" {
  811. return fmt.Errorf("env group namespace cannot be empty")
  812. }
  813. color.New(color.FgBlue, color.Bold).
  814. Printf("Env group '%s' does not exist in the target namespace '%s'\n", group.Name, target.Namespace)
  815. color.New(color.FgBlue, color.Bold).
  816. Printf("Cloning env group '%s' from namespace '%s' to target namespace '%s'\n",
  817. group.Name, group.Namespace, target.Namespace)
  818. _, err = t.client.CloneEnvGroup(
  819. context.Background(), target.Project, target.Cluster, group.Namespace,
  820. &types.CloneEnvGroupRequest{
  821. Name: group.Name,
  822. Namespace: target.Namespace,
  823. },
  824. )
  825. if err != nil {
  826. return err
  827. }
  828. } else if err != nil {
  829. return err
  830. }
  831. }
  832. }
  833. }
  834. return nil
  835. }
  836. func (t *CloneEnvGroupHook) DataQueries() map[string]interface{} {
  837. return nil
  838. }
  839. func (t *CloneEnvGroupHook) PostApply(map[string]interface{}) error {
  840. return nil
  841. }
  842. func (t *CloneEnvGroupHook) OnError(error) {}
  843. func (t *CloneEnvGroupHook) OnConsolidatedErrors(map[string]error) {}
  844. func getReleaseName(res *switchboardTypes.Resource) string {
  845. // can ignore the error because this method is called once
  846. // GetTarget has alrealy been called and validated previously
  847. target, _ := preview.GetTarget(res.Name, res.Target)
  848. if target.AppName != "" {
  849. return target.AppName
  850. }
  851. return res.Name
  852. }
  853. func getReleaseType(res *switchboardTypes.Resource) string {
  854. // can ignore the error because this method is called once
  855. // GetSource has alrealy been called and validated previously
  856. source, _ := preview.GetSource(res.Name, res.Source)
  857. if source != nil && source.Name != "" {
  858. return source.Name
  859. }
  860. return ""
  861. }